zoom: properly handle proxy failover for re-used backends
[metaproxy-moved-to-github.git] / src / filter_zoom.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2012 Index Data
3
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include "config.hpp"
20
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include "filter_zoom.hpp"
24 #include <metaproxy/package.hpp>
25 #include <metaproxy/util.hpp>
26 #include <metaproxy/xmlutil.hpp>
27 #include <yaz/comstack.h>
28 #include <yaz/poll.h>
29 #include "torus.hpp"
30
31 #include <libxslt/xsltutils.h>
32 #include <libxslt/transform.h>
33
34 #include <boost/thread/mutex.hpp>
35 #include <boost/thread/condition.hpp>
36
37 #include <yaz/yaz-version.h>
38 #include <yaz/tpath.h>
39 #include <yaz/srw.h>
40 #include <yaz/ccl_xml.h>
41 #include <yaz/ccl.h>
42 #include <yaz/rpn2cql.h>
43 #include <yaz/rpn2solr.h>
44 #include <yaz/pquery.h>
45 #include <yaz/cql.h>
46 #include <yaz/oid_db.h>
47 #include <yaz/diagbib1.h>
48 #include <yaz/log.h>
49 #include <yaz/zgdu.h>
50 #include <yaz/querytowrbuf.h>
51 #include <yaz/sortspec.h>
52 #include <yaz/tokenizer.h>
53 #include <yaz/zoom.h>
54
55 namespace mp = metaproxy_1;
56 namespace yf = mp::filter;
57
58 namespace metaproxy_1 {
59     namespace filter {
60         class Zoom::Searchable : boost::noncopyable {
61           public:
62             std::string authentication;
63             std::string cfAuth;
64             std::string cfProxy;
65             std::string cfSubDB;
66             std::string udb;
67             std::string target;
68             std::string query_encoding;
69             std::string sru;
70             std::string sru_version;
71             std::string request_syntax;
72             std::string element_set;
73             std::string record_encoding;
74             std::string transform_xsl_fname;
75             std::string transform_xsl_content;
76             std::string urlRecipe;
77             std::string contentConnector;
78             std::string sortStrategy;
79             bool use_turbomarc;
80             bool piggyback;
81             CCL_bibset ccl_bibset;
82             std::map<std::string, std::string> sortmap;
83             Searchable(CCL_bibset base);
84             ~Searchable();
85         };
86         class Zoom::Backend : boost::noncopyable {
87             friend class Impl;
88             friend class Frontend;
89             std::string zurl;
90             mp::wrbuf m_apdu_wrbuf;
91             ZOOM_connection m_connection;
92             ZOOM_resultset m_resultset;
93             std::string m_frontend_database;
94             SearchablePtr sptr;
95             xsltStylesheetPtr xsp;
96             std::string cproxy_host;
97             bool enable_cproxy;
98             bool enable_explain;
99             xmlDoc *explain_doc;
100             std::string m_proxy;
101         public:
102             Backend();
103             ~Backend();
104             void connect(std::string zurl, int *error, char **addinfo,
105                          ODR odr);
106             void search(ZOOM_query q, Odr_int *hits,
107                         int *error, char **addinfo, ODR odr);
108             void present(Odr_int start, Odr_int number, ZOOM_record *recs,
109                          int *error, char **addinfo, ODR odr);
110             void set_option(const char *name, const char *value);
111             void set_option(const char *name, std::string value);
112             const char *get_option(const char *name);
113             void get_zoom_error(int *error, char **addinfo, ODR odr);
114         };
115         class Zoom::Frontend : boost::noncopyable {
116             friend class Impl;
117             Impl *m_p;
118             bool m_is_virtual;
119             bool m_in_use;
120             yazpp_1::GDU m_init_gdu;
121             BackendPtr m_backend;
122             void handle_package(mp::Package &package);
123             void handle_search(mp::Package &package);
124
125             BackendPtr explain_search(mp::Package &package,
126                                       std::string &database,
127                                       int *error,
128                                       char **addinfo,
129                                       mp::odr &odr,
130                                       std::string &torus_db,
131                                       std::string &realm);
132             void handle_present(mp::Package &package);
133             BackendPtr get_backend_from_databases(mp::Package &package,
134                                                   std::string &database,
135                                                   int *error,
136                                                   char **addinfo,
137                                                   mp::odr &odr,
138                                                   int *proxy_step);
139
140             bool create_content_session(mp::Package &package,
141                                         BackendPtr b,
142                                         int *error,
143                                         char **addinfo,
144                                         ODR odr,
145                                         std::string authentication,
146                                         std::string proxy,
147                                         std::string realm);
148             
149             void prepare_elements(BackendPtr b,
150                                   Odr_oid *preferredRecordSyntax,
151                                   const char *element_set_name,
152                                   bool &enable_pz2_retrieval,
153                                   bool &enable_pz2_transform,
154                                   bool &enable_record_transform,
155                                   bool &assume_marc8_charset);
156
157             Z_Records *get_records(Package &package,
158                                    Odr_int start,
159                                    Odr_int number_to_present,
160                                    int *error,
161                                    char **addinfo,
162                                    Odr_int *number_of_records_returned,
163                                    ODR odr, BackendPtr b,
164                                    Odr_oid *preferredRecordSyntax,
165                                    const char *element_set_name);
166             Z_Records *get_explain_records(Package &package,
167                                            Odr_int start,
168                                            Odr_int number_to_present,
169                                            int *error,
170                                            char **addinfo,
171                                            Odr_int *number_of_records_returned,
172                                            ODR odr, BackendPtr b,
173                                            Odr_oid *preferredRecordSyntax,
174                                            const char *element_set_name);
175
176             void log_diagnostic(mp::Package &package,
177                                 int error, const char *addinfo);
178         public:
179             Frontend(Impl *impl);
180             ~Frontend();
181         };
182         class Zoom::Impl {
183             friend class Frontend;
184         public:
185             Impl();
186             ~Impl();
187             void process(metaproxy_1::Package & package);
188             void configure(const xmlNode * ptr, bool test_only,
189                            const char *path);
190         private:
191             void configure_local_records(const xmlNode * ptr, bool test_only);
192             bool check_proxy(const char *proxy);
193             FrontendPtr get_frontend(mp::Package &package);
194             void release_frontend(mp::Package &package);
195             SearchablePtr parse_torus_record(const xmlNode *ptr);
196             struct cql_node *convert_cql_fields(struct cql_node *cn, ODR odr);
197             std::map<mp::Session, FrontendPtr> m_clients;            
198             boost::mutex m_mutex;
199             boost::condition m_cond_session_ready;
200             std::string torus_searchable_url;
201             std::string torus_content_url;
202             std::string default_realm;
203             std::map<std::string,std::string> fieldmap;
204             std::string xsldir;
205             std::string file_path;
206             std::string content_proxy_server;
207             std::string content_tmp_file;
208             std::string content_config_file;
209             bool apdu_log;
210             CCL_bibset bibset;
211             std::string element_transform;
212             std::string element_raw;
213             std::string proxy;
214             xsltStylesheetPtr explain_xsp;
215             xsltStylesheetPtr record_xsp;
216             std::map<std::string,SearchablePtr> s_map;
217             std::string zoom_timeout;
218             int proxy_timeout;
219         };
220     }
221 }
222
223
224 static xmlNode *xml_node_search(xmlNode *ptr, int *num, int m)
225 {
226     while (ptr)
227     {
228         if (ptr->type == XML_ELEMENT_NODE &&
229             !strcmp((const char *) ptr->name, "recordData"))
230         {
231             (*num)++;
232             if (m == *num)
233                 return ptr;
234         }
235         else  // else: we don't want to find nested nodes
236         {   
237             xmlNode *ret_node = xml_node_search(ptr->children, num, m);
238             if (ret_node)
239                 return ret_node;
240         }
241         ptr = ptr->next;
242     }
243     return 0;
244 }
245
246 // define Pimpl wrapper forwarding to Impl
247  
248 yf::Zoom::Zoom() : m_p(new Impl)
249 {
250 }
251
252 yf::Zoom::~Zoom()
253 {  // must have a destructor because of boost::scoped_ptr
254 }
255
256 void yf::Zoom::configure(const xmlNode *xmlnode, bool test_only,
257                          const char *path)
258 {
259     m_p->configure(xmlnode, test_only, path);
260 }
261
262 void yf::Zoom::process(mp::Package &package) const
263 {
264     m_p->process(package);
265 }
266
267
268 // define Implementation stuff
269
270 yf::Zoom::Backend::Backend()
271 {
272     m_connection = ZOOM_connection_create(0);
273     ZOOM_connection_save_apdu_wrbuf(m_connection, m_apdu_wrbuf);
274     m_resultset = 0;
275     xsp = 0;
276     enable_cproxy = true;
277     enable_explain = false;
278     explain_doc = 0;
279 }
280
281 yf::Zoom::Backend::~Backend()
282 {
283     if (xsp)
284         xsltFreeStylesheet(xsp);
285     if (explain_doc)
286         xmlFreeDoc(explain_doc);
287     ZOOM_connection_destroy(m_connection);
288     ZOOM_resultset_destroy(m_resultset);
289 }
290
291
292 void yf::Zoom::Backend::get_zoom_error(int *error, char **addinfo,
293                                        ODR odr)
294 {
295     const char *msg = 0;
296     const char *zoom_addinfo = 0;
297     const char *dset = 0;
298     int error0 = ZOOM_connection_error_x(m_connection, &msg,
299                                          &zoom_addinfo, &dset);
300     if (error0)
301     {
302         if (!dset)
303             dset = "Unknown";
304         
305         if (!strcmp(dset, "info:srw/diagnostic/1"))
306             *error = yaz_diag_srw_to_bib1(error0);
307         else if (!strcmp(dset, "Bib-1"))
308             *error = error0;
309         else if (!strcmp(dset, "ZOOM"))
310         {
311             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;                
312             if (error0 == ZOOM_ERROR_INIT)
313                 *error = YAZ_BIB1_INIT_AC_AUTHENTICATION_SYSTEM_ERROR;
314             else if (error0 == ZOOM_ERROR_DECODE)
315             {
316                 if (zoom_addinfo)
317                 {
318                     if (strstr(zoom_addinfo, "Authentication") ||
319                         strstr(zoom_addinfo, "authentication"))
320                         *error = YAZ_BIB1_INIT_AC_AUTHENTICATION_SYSTEM_ERROR;
321                 }
322             }
323         }
324         else
325             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
326         
327         *addinfo = (char *) odr_malloc(
328             odr, 30 + strlen(dset) + strlen(msg) +
329             (zoom_addinfo ? strlen(zoom_addinfo) : 0));
330         **addinfo = '\0';
331         if (zoom_addinfo && *zoom_addinfo)
332         {
333             strcpy(*addinfo, zoom_addinfo);
334             strcat(*addinfo, " ");
335         }
336         sprintf(*addinfo + strlen(*addinfo), "(%s %d %s)", dset, error0, msg);
337     }
338 }
339
340 void yf::Zoom::Backend::connect(std::string zurl,
341                                 int *error, char **addinfo,
342                                 ODR odr)
343 {
344     ZOOM_connection_connect(m_connection, zurl.length() ? zurl.c_str() : 0, 0);
345     get_zoom_error(error, addinfo, odr);
346 }
347
348 void yf::Zoom::Backend::search(ZOOM_query q, Odr_int *hits,
349                                int *error, char **addinfo, ODR odr)
350 {
351     ZOOM_resultset_destroy(m_resultset);
352     m_resultset = ZOOM_connection_search(m_connection, q);
353     get_zoom_error(error, addinfo, odr);
354     if (*error == 0)
355         *hits = ZOOM_resultset_size(m_resultset);
356     else
357         *hits = 0;
358 }
359
360 void yf::Zoom::Backend::present(Odr_int start, Odr_int number,
361                                 ZOOM_record *recs,
362                                 int *error, char **addinfo, ODR odr)
363 {
364     ZOOM_resultset_records(m_resultset, recs, start, number);
365     get_zoom_error(error, addinfo, odr);
366 }
367
368 void yf::Zoom::Backend::set_option(const char *name, const char *value)
369 {
370     ZOOM_connection_option_set(m_connection, name, value);
371     if (m_resultset)
372         ZOOM_resultset_option_set(m_resultset, name, value);
373 }
374
375 void yf::Zoom::Backend::set_option(const char *name, std::string value)
376 {
377     set_option(name, value.c_str());
378 }
379
380 const char *yf::Zoom::Backend::get_option(const char *name)
381 {
382     return ZOOM_connection_option_get(m_connection, name);
383 }
384
385 yf::Zoom::Searchable::Searchable(CCL_bibset base)
386 {
387     piggyback = true;
388     use_turbomarc = true;
389     sortStrategy = "embed";
390     ccl_bibset = ccl_qual_dup(base);
391 }
392
393 yf::Zoom::Searchable::~Searchable()
394 {
395     ccl_qual_rm(&ccl_bibset);
396 }
397
398 yf::Zoom::Frontend::Frontend(Impl *impl) : 
399     m_p(impl), m_is_virtual(false), m_in_use(true)
400 {
401 }
402
403 yf::Zoom::Frontend::~Frontend()
404 {
405 }
406
407 yf::Zoom::FrontendPtr yf::Zoom::Impl::get_frontend(mp::Package &package)
408 {
409     boost::mutex::scoped_lock lock(m_mutex);
410
411     std::map<mp::Session,yf::Zoom::FrontendPtr>::iterator it;
412     
413     while(true)
414     {
415         it = m_clients.find(package.session());
416         if (it == m_clients.end())
417             break;
418         
419         if (!it->second->m_in_use)
420         {
421             it->second->m_in_use = true;
422             return it->second;
423         }
424         m_cond_session_ready.wait(lock);
425     }
426     FrontendPtr f(new Frontend(this));
427     m_clients[package.session()] = f;
428     f->m_in_use = true;
429     return f;
430 }
431
432 void yf::Zoom::Impl::release_frontend(mp::Package &package)
433 {
434     boost::mutex::scoped_lock lock(m_mutex);
435     std::map<mp::Session,yf::Zoom::FrontendPtr>::iterator it;
436     
437     it = m_clients.find(package.session());
438     if (it != m_clients.end())
439     {
440         if (package.session().is_closed())
441         {
442             m_clients.erase(it);
443         }
444         else
445         {
446             it->second->m_in_use = false;
447         }
448         m_cond_session_ready.notify_all();
449     }
450 }
451
452 yf::Zoom::Impl::Impl() :
453     apdu_log(false), element_transform("pz2") , element_raw("raw"),
454     zoom_timeout("40"), proxy_timeout(1)
455 {
456     bibset = ccl_qual_mk();
457
458     explain_xsp = 0;
459     record_xsp = 0;
460     srand((unsigned int) time(0));
461 }
462
463 yf::Zoom::Impl::~Impl()
464 {
465     if (explain_xsp)
466         xsltFreeStylesheet(explain_xsp);
467     ccl_qual_rm(&bibset);
468 }
469
470 yf::Zoom::SearchablePtr yf::Zoom::Impl::parse_torus_record(const xmlNode *ptr)
471 {
472     Zoom::SearchablePtr s(new Searchable(bibset));
473     
474     for (ptr = ptr->children; ptr; ptr = ptr->next)
475     {
476         if (ptr->type != XML_ELEMENT_NODE)
477             continue;
478         if (!strcmp((const char *) ptr->name, "layer"))
479             ptr = ptr->children;
480         else if (!strcmp((const char *) ptr->name,
481                          "authentication"))
482         {
483             s->authentication = mp::xml::get_text(ptr);
484         }
485         else if (!strcmp((const char *) ptr->name,
486                          "cfAuth"))
487         {
488             s->cfAuth = mp::xml::get_text(ptr);
489         } 
490         else if (!strcmp((const char *) ptr->name,
491                          "cfProxy"))
492         {
493             s->cfProxy = mp::xml::get_text(ptr);
494         }  
495         else if (!strcmp((const char *) ptr->name,
496                          "cfSubDB"))
497         {
498             s->cfSubDB = mp::xml::get_text(ptr);
499         }  
500         else if (!strcmp((const char *) ptr->name,
501                          "contentConnector"))
502         {
503             s->contentConnector = mp::xml::get_text(ptr);
504         }  
505         else if (!strcmp((const char *) ptr->name, "udb"))
506         {
507             s->udb = mp::xml::get_text(ptr);
508         }
509         else if (!strcmp((const char *) ptr->name, "zurl"))
510         {
511             s->target = mp::xml::get_text(ptr);
512         }
513         else if (!strcmp((const char *) ptr->name, "sru"))
514         {
515             s->sru = mp::xml::get_text(ptr);
516         }
517         else if (!strcmp((const char *) ptr->name, "SRUVersion") ||
518                  !strcmp((const char *) ptr->name, "sruVersion"))
519         {
520             s->sru_version = mp::xml::get_text(ptr);
521         }
522         else if (!strcmp((const char *) ptr->name,
523                          "queryEncoding"))
524         {
525             s->query_encoding = mp::xml::get_text(ptr);
526         }
527         else if (!strcmp((const char *) ptr->name,
528                          "piggyback"))
529         {
530             s->piggyback = mp::xml::get_bool(ptr, true);
531         }
532         else if (!strcmp((const char *) ptr->name,
533                          "requestSyntax"))
534         {
535             s->request_syntax = mp::xml::get_text(ptr);
536         }
537         else if (!strcmp((const char *) ptr->name,
538                          "elementSet"))
539         {
540             s->element_set = mp::xml::get_text(ptr);
541         }
542         else if (!strcmp((const char *) ptr->name,
543                          "recordEncoding"))
544         {
545             s->record_encoding = mp::xml::get_text(ptr);
546         }
547         else if (!strcmp((const char *) ptr->name,
548                          "transform"))
549         {
550             s->transform_xsl_fname = mp::xml::get_text(ptr);
551         }
552         else if (!strcmp((const char *) ptr->name,
553                          "literalTransform"))
554         {
555             s->transform_xsl_content = mp::xml::get_text(ptr);
556         }
557         else if (!strcmp((const char *) ptr->name,
558                          "urlRecipe"))
559         {
560             s->urlRecipe = mp::xml::get_text(ptr);
561         }
562         else if (!strcmp((const char *) ptr->name,
563                          "useTurboMarc"))
564         {
565             ; // useTurboMarc is ignored
566         }
567         else if (!strncmp((const char *) ptr->name,
568                           "cclmap_", 7))
569         {
570             std::string value = mp::xml::get_text(ptr);
571             if (value.length() > 0)
572             {
573                 ccl_qual_fitem(s->ccl_bibset, value.c_str(),
574                                (const char *) ptr->name + 7);
575             }
576         }
577         else if (!strncmp((const char *) ptr->name,
578                           "sortmap_", 8))
579         {
580             std::string value = mp::xml::get_text(ptr);
581             s->sortmap[(const char *) ptr->name + 8] = value;
582         }
583         else if (!strcmp((const char *) ptr->name,
584                           "sortStrategy"))
585         {
586             s->sortStrategy = mp::xml::get_text(ptr);
587         }
588     }
589     return s;
590 }
591
592 void yf::Zoom::Impl::configure_local_records(const xmlNode *ptr, bool test_only)
593 {
594     while (ptr && ptr->type != XML_ELEMENT_NODE)
595         ptr = ptr->next;
596     
597     if (ptr)
598     {
599         if (!strcmp((const char *) ptr->name, "records"))
600         {
601             for (ptr = ptr->children; ptr; ptr = ptr->next)
602             {
603                 if (ptr->type != XML_ELEMENT_NODE)
604                     continue;
605                 if (!strcmp((const char *) ptr->name, "record"))
606                 {
607                     SearchablePtr s = parse_torus_record(ptr);
608                     if (s)
609                     {
610                         std::string udb = s->udb;
611                         if (udb.length())
612                             s_map[s->udb] = s;
613                         else
614                         {
615                             throw mp::filter::FilterException
616                                 ("No udb for local torus record");
617                         }
618                     }
619                 }
620                 else
621                 {
622                     throw mp::filter::FilterException
623                         ("Bad element " 
624                          + std::string((const char *) ptr->name)
625                          + " in zoom filter inside element "
626                          "<torus><records>");
627                 }
628             }
629         }
630         else
631         {
632             throw mp::filter::FilterException
633                 ("Bad element " 
634                  + std::string((const char *) ptr->name)
635                  + " in zoom filter inside element <torus>");
636         }
637     }
638 }
639
640 void yf::Zoom::Impl::configure(const xmlNode *ptr, bool test_only,
641                                const char *path)
642 {
643     std::string explain_xslt_fname;
644     std::string record_xslt_fname;
645
646     if (path && *path)
647     {
648         file_path = path;
649     }
650     for (ptr = ptr->children; ptr; ptr = ptr->next)
651     {
652         if (ptr->type != XML_ELEMENT_NODE)
653             continue;
654         else if (!strcmp((const char *) ptr->name, "torus"))
655         {
656             const struct _xmlAttr *attr;
657             for (attr = ptr->properties; attr; attr = attr->next)
658             {
659                 if (!strcmp((const char *) attr->name, "url"))
660                     torus_searchable_url = mp::xml::get_text(attr->children);
661                 else if (!strcmp((const char *) attr->name, "content_url"))
662                     torus_content_url = mp::xml::get_text(attr->children);
663                 else if (!strcmp((const char *) attr->name, "realm"))
664                     default_realm = mp::xml::get_text(attr->children);
665                 else if (!strcmp((const char *) attr->name, "xsldir"))
666                     xsldir = mp::xml::get_text(attr->children);
667                 else if (!strcmp((const char *) attr->name, "element_transform"))
668                     element_transform = mp::xml::get_text(attr->children);
669                 else if (!strcmp((const char *) attr->name, "element_raw"))
670                     element_raw = mp::xml::get_text(attr->children);
671                 else if (!strcmp((const char *) attr->name, "proxy"))
672                     proxy = mp::xml::get_text(attr->children);
673                 else if (!strcmp((const char *) attr->name, "explain_xsl"))
674                     explain_xslt_fname = mp::xml::get_text(attr->children);
675                 else if (!strcmp((const char *) attr->name, "record_xsl"))
676                     record_xslt_fname = mp::xml::get_text(attr->children);
677                 else
678                     throw mp::filter::FilterException(
679                         "Bad attribute " + std::string((const char *)
680                                                        attr->name));
681             }
682             // If content_url is not given, use value of searchable, to
683             // ensure backwards compatibility
684             if (!torus_content_url.length())
685                 torus_content_url = torus_searchable_url;
686             configure_local_records(ptr->children, test_only);
687         }
688         else if (!strcmp((const char *) ptr->name, "cclmap"))
689         {
690             const char *addinfo = 0;
691             ccl_xml_config(bibset, ptr, &addinfo);
692         }
693         else if (!strcmp((const char *) ptr->name, "fieldmap"))
694         {
695             const struct _xmlAttr *attr;
696             std::string ccl_field;
697             std::string cql_field;
698             for (attr = ptr->properties; attr; attr = attr->next)
699             {
700                 if (!strcmp((const char *) attr->name, "ccl"))
701                     ccl_field = mp::xml::get_text(attr->children);
702                 else if (!strcmp((const char *) attr->name, "cql"))
703                     cql_field = mp::xml::get_text(attr->children);
704                 else
705                     throw mp::filter::FilterException(
706                         "Bad attribute " + std::string((const char *)
707                                                        attr->name));
708             }
709             if (cql_field.length())
710                 fieldmap[cql_field] = ccl_field;
711         }
712         else if (!strcmp((const char *) ptr->name, "contentProxy"))
713         {
714             const struct _xmlAttr *attr;
715             for (attr = ptr->properties; attr; attr = attr->next)
716             {
717                 if (!strcmp((const char *) attr->name, "server"))
718                 {
719                     yaz_log(YLOG_WARN,
720                             "contentProxy's server attribute is deprecated");
721                     yaz_log(YLOG_LOG, 
722                             "Specify config_file instead. For example:");
723                     yaz_log(YLOG_LOG, 
724                             " content_file=\"/etc/cf-proxy/cproxy.cfg\"");
725                     content_proxy_server = mp::xml::get_text(attr->children);
726                 }
727                 else if (!strcmp((const char *) attr->name, "tmp_file"))
728                     content_tmp_file = mp::xml::get_text(attr->children);
729                 else if (!strcmp((const char *) attr->name, "config_file"))
730                     content_config_file = mp::xml::get_text(attr->children);
731                 else
732                     throw mp::filter::FilterException(
733                         "Bad attribute " + std::string((const char *)
734                                                        attr->name));
735             }
736         }
737         else if (!strcmp((const char *) ptr->name, "log"))
738         { 
739             const struct _xmlAttr *attr;
740             for (attr = ptr->properties; attr; attr = attr->next)
741             {
742                 if (!strcmp((const char *) attr->name, "apdu"))
743                     apdu_log = mp::xml::get_bool(attr->children, false);
744                 else
745                     throw mp::filter::FilterException(
746                         "Bad attribute " + std::string((const char *)
747                                                        attr->name));
748             }
749         }
750         else if (!strcmp((const char *) ptr->name, "zoom"))
751         {
752             const struct _xmlAttr *attr;
753             for (attr = ptr->properties; attr; attr = attr->next)
754             {
755                 if (!strcmp((const char *) attr->name, "timeout"))
756                     zoom_timeout = mp::xml::get_text(attr->children);
757                 else if (!strcmp((const char *) attr->name, "proxy_timeout"))
758                     proxy_timeout = mp::xml::get_int(attr->children, 1);
759                 else
760                     throw mp::filter::FilterException(
761                         "Bad attribute " + std::string((const char *)
762                                                        attr->name));
763             }
764         }
765         else
766         {
767             throw mp::filter::FilterException
768                 ("Bad element " 
769                  + std::string((const char *) ptr->name)
770                  + " in zoom filter");
771         }
772     }
773
774     if (explain_xslt_fname.length())
775     {
776         const char *path = 0;
777         
778         if (xsldir.length())
779             path = xsldir.c_str();
780         else
781             path = file_path.c_str();
782         
783         char fullpath[1024];
784         char *cp = yaz_filepath_resolve(explain_xslt_fname.c_str(),
785                                         path, 0, fullpath);
786         if (!cp)
787         {
788             throw mp::filter::FilterException
789                 ("Cannot read XSLT " + explain_xslt_fname);
790         }
791
792         xmlDoc *xsp_doc = xmlParseFile(cp);
793         if (!xsp_doc)
794         {
795             throw mp::filter::FilterException
796                 ("Cannot parse XSLT " + explain_xslt_fname);
797         }
798
799         explain_xsp = xsltParseStylesheetDoc(xsp_doc);
800         if (!explain_xsp)
801         {
802             xmlFreeDoc(xsp_doc);
803             throw mp::filter::FilterException
804                 ("Cannot parse XSLT " + explain_xslt_fname);
805             
806         }
807     }
808
809     if (record_xslt_fname.length())
810     {
811         const char *path = 0;
812         
813         if (xsldir.length())
814             path = xsldir.c_str();
815         else
816             path = file_path.c_str();
817         
818         char fullpath[1024];
819         char *cp = yaz_filepath_resolve(record_xslt_fname.c_str(),
820                                         path, 0, fullpath);
821         if (!cp)
822         {
823             throw mp::filter::FilterException
824                 ("Cannot read XSLT " + record_xslt_fname);
825         }
826
827         xmlDoc *xsp_doc = xmlParseFile(cp);
828         if (!xsp_doc)
829         {
830             throw mp::filter::FilterException
831                 ("Cannot parse XSLT " + record_xslt_fname);
832         }
833
834         record_xsp = xsltParseStylesheetDoc(xsp_doc);
835         if (!record_xsp)
836         {
837             xmlFreeDoc(xsp_doc);
838             throw mp::filter::FilterException
839                 ("Cannot parse XSLT " + record_xslt_fname);
840             
841         }
842     }
843 }
844
845 bool yf::Zoom::Frontend::create_content_session(mp::Package &package,
846                                                 BackendPtr b,
847                                                 int *error, char **addinfo,
848                                                 ODR odr,
849                                                 std::string authentication,
850                                                 std::string proxy,
851                                                 std::string realm)
852 {
853     if (b->sptr->contentConnector.length())
854     {
855         std::string proxyhostname;
856         std::string tmp_file;
857         bool legacy_format = false;
858
859         if (m_p->content_proxy_server.length())
860         {
861             proxyhostname = m_p->content_proxy_server;
862             legacy_format = true;
863         }
864             
865         if (m_p->content_tmp_file.length())
866             tmp_file = m_p->content_tmp_file;
867
868         if (m_p->content_config_file.length())
869         {
870             FILE *inf = fopen(m_p->content_config_file.c_str(), "r");
871             if (inf)
872             {
873                 char buf[1024];
874                 while (fgets(buf, sizeof(buf)-1, inf))
875                 {
876                     char *cp;
877                     cp = strchr(buf, '#');
878                     if (cp)
879                         *cp = '\0';
880                     cp = strchr(buf, '\n');
881                     if (cp)
882                         *cp = '\0';
883                     cp = strchr(buf, ':');
884                     if (cp)
885                     {
886                         char *cp1 = cp;
887                         while (cp1 != buf && cp1[-1] == ' ')
888                             cp1--;
889                         *cp1 = '\0';
890                         cp++;
891                         while (*cp == ' ')
892                             cp++;
893                         if (!strcmp(buf, "proxyhostname"))
894                             proxyhostname = cp; 
895                         if (!strcmp(buf, "sessiondir") && *cp)
896                         {
897                             if (cp[strlen(cp)-1] == '/')
898                                 cp[strlen(cp)-1] = '\0';
899                             tmp_file = std::string(cp) + std::string("/cf.XXXXXX.p");
900                         }
901                     }
902                 }
903                 fclose(inf);
904             }
905             else
906             {
907                 package.log("zoom", YLOG_WARN|YLOG_ERRNO,
908                             "unable to open content config %s",
909                             m_p->content_config_file.c_str());
910                 *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
911                 *addinfo = (char *)  odr_malloc(odr, 60 + tmp_file.length());
912                 sprintf(*addinfo, "unable to open content config %s",
913                         m_p->content_config_file.c_str());
914                 return false;
915             }
916         }
917
918         if (proxyhostname.length() == 0)
919         {
920             package.log("zoom", YLOG_WARN, "no proxyhostname");
921             return true;
922         }
923         if (tmp_file.length() == 0)
924         {
925             package.log("zoom", YLOG_WARN, "no tmp_file");
926             return true;
927         }
928
929         char *fname = xstrdup(tmp_file.c_str());
930         char *xx = strstr(fname, "XXXXXX");
931         if (!xx)
932         {
933             package.log("zoom", YLOG_WARN, "bad tmp_file %s", tmp_file.c_str());
934             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
935             *addinfo = (char *)  odr_malloc(odr, 60 + tmp_file.length());
936             sprintf(*addinfo, "bad format of content tmp_file: %s",
937                     tmp_file.c_str());
938             xfree(fname);
939             return false;
940         }
941         char tmp_char = xx[6];
942         sprintf(xx, "%06d", ((unsigned) rand()) % 1000000);
943         if (legacy_format)
944             b->cproxy_host = std::string(xx) + "." + proxyhostname;
945         else
946             b->cproxy_host = proxyhostname + "/" + xx;
947         xx[6] = tmp_char;
948
949         FILE *file = fopen(fname, "w");
950         if (!file)
951         {
952             package.log("zoom", YLOG_WARN|YLOG_ERRNO, "create %s", fname);
953             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
954             *addinfo = (char *)  odr_malloc(odr, 40 + strlen(fname));
955             sprintf(*addinfo, "Could not create %s", fname);
956             xfree(fname);
957             return false;
958         }
959         mp::wrbuf w;
960         wrbuf_puts(w, "#content_proxy\n");
961         wrbuf_printf(w, "connector: %s\n", b->sptr->contentConnector.c_str());
962         if (authentication.length())
963             wrbuf_printf(w, "auth: %s\n", authentication.c_str());
964         if (proxy.length())
965             wrbuf_printf(w, "proxy: %s\n", proxy.c_str());
966         if (realm.length())
967             wrbuf_printf(w, "realm: %s\n", realm.c_str());
968
969         fwrite(w.buf(), 1, w.len(), file);
970         fclose(file);
971         package.log("zoom", YLOG_LOG, "content file: %s", fname);
972         xfree(fname);
973     }
974     return true;
975 }
976
977 yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases(
978     mp::Package &package,
979     std::string &database, int *error, char **addinfo, mp::odr &odr,
980     int *proxy_step)
981 {
982     bool connection_reuse = false;
983     std::string proxy;
984
985     std::list<BackendPtr>::const_iterator map_it;
986     if (m_backend && !m_backend->enable_explain && 
987         m_backend->m_frontend_database == database)
988     {
989         connection_reuse = true;
990         proxy = m_backend->m_proxy;
991     }
992
993     std::string input_args;
994     std::string torus_db;
995     size_t db_arg_pos = database.find(',');
996     if (db_arg_pos != std::string::npos)
997     {
998         torus_db = database.substr(0, db_arg_pos);
999         input_args = database.substr(db_arg_pos + 1);
1000     }
1001     else
1002         torus_db = database;
1003
1004     std::string authentication;
1005     std::string content_authentication;
1006     std::string content_proxy;
1007     std::string realm = m_p->default_realm;
1008
1009     const char *param_user = 0;
1010     const char *param_password = 0;
1011     const char *param_content_user = 0;
1012     const char *param_content_password = 0;
1013     const char *param_nocproxy = 0;
1014     int no_parms = 0;
1015
1016     char **names;
1017     char **values;
1018     int no_out_args = 0;
1019     if (input_args.length())
1020         no_parms = yaz_uri_to_array(input_args.c_str(),
1021                                     odr, &names, &values);
1022     // adding 10 because we'll be adding other URL args
1023     const char **out_names = (const char **)
1024         odr_malloc(odr, (10 + no_parms) * sizeof(*out_names));
1025     const char **out_values = (const char **)
1026         odr_malloc(odr, (10 + no_parms) * sizeof(*out_values));
1027     
1028     // may be changed if it's a content connection
1029     std::string torus_url = m_p->torus_searchable_url;
1030     int i;
1031     for (i = 0; i < no_parms; i++)
1032     {
1033         const char *name = names[i];
1034         const char *value = values[i];
1035         assert(name);
1036         assert(value);
1037         if (!strcmp(name, "user"))
1038             param_user = value;
1039         else if (!strcmp(name, "password"))
1040             param_password = value;
1041         else if (!strcmp(name, "content-user"))
1042             param_content_user = value;
1043         else if (!strcmp(name, "content-password"))
1044             param_content_password = value;
1045         else if (!strcmp(name, "content-proxy"))
1046             content_proxy = value;
1047         else if (!strcmp(name, "nocproxy"))
1048             param_nocproxy = value;
1049         else if (!strcmp(name, "proxy"))
1050         {
1051             char **dstr;
1052             int dnum = 0;
1053             nmem_strsplit(((ODR) odr)->mem, ",", value, &dstr, &dnum);
1054             if (connection_reuse)
1055             {
1056                 // find the step after our current proxy
1057                 int i;
1058                 for (i = 0; i < dnum; i++)
1059                     if (!strcmp(proxy.c_str(), dstr[i]))
1060                         break;
1061                 if (i >= dnum - 1)
1062                     *proxy_step = 0;
1063                 else
1064                     *proxy_step = i + 1;
1065             }
1066             else
1067             {
1068                 // step is known.. Guess our proxy from it
1069                 if (*proxy_step >= dnum)
1070                     *proxy_step = 0;
1071                 else
1072                 {
1073                     proxy = dstr[*proxy_step];
1074                     
1075                     (*proxy_step)++;
1076                     if (*proxy_step == dnum)
1077                         *proxy_step = 0;
1078                 }
1079             }
1080         }
1081         else if (!strcmp(name, "cproxysession"))
1082         {
1083             out_names[no_out_args] = name;
1084             out_values[no_out_args++] = value;
1085             torus_url = m_p->torus_content_url;
1086         }
1087         else if (!strcmp(name, "realm"))
1088             realm = value;
1089         else if (name[0] == 'x' && name[1] == '-')
1090         {
1091             out_names[no_out_args] = name;
1092             out_values[no_out_args++] = value;
1093         }
1094         else
1095         {
1096             BackendPtr notfound;
1097             char *msg = (char*) odr_malloc(odr, strlen(name) + 30);
1098             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1099             sprintf(msg, "Bad database argument: %s", name);
1100             *addinfo = msg;
1101             return notfound;
1102         }
1103     }    
1104     if (proxy.length())
1105         package.log("zoom", YLOG_LOG, "proxy: %s", proxy.c_str());
1106
1107     if (connection_reuse)
1108     {
1109         m_backend->connect("", error, addinfo, odr);
1110         return m_backend;
1111     }
1112
1113     if (param_user)
1114     {
1115         authentication = std::string(param_user);
1116         if (param_password)
1117             authentication += "/" + std::string(param_password);
1118     }
1119     if (param_content_user)
1120     {
1121         content_authentication = std::string(param_content_user);
1122         if (param_content_password)
1123             content_authentication += "/" + std::string(param_content_password);
1124     }
1125
1126     if (torus_db.compare("IR-Explain---1") == 0)
1127         return explain_search(package, database, error, addinfo, odr, torus_db,
1128             realm);
1129     
1130     SearchablePtr sptr;
1131
1132     std::map<std::string,SearchablePtr>::iterator it;
1133     it = m_p->s_map.find(torus_db);
1134     if (it != m_p->s_map.end())
1135         sptr = it->second;
1136     else if (torus_url.length() > 0)
1137     {
1138         std::string torus_query = "udb==" + torus_db;
1139         xmlDoc *doc = mp::get_searchable(package,torus_url, torus_db,
1140                                          torus_query,
1141                                          realm, m_p->proxy);
1142         if (!doc)
1143         {
1144             *error = YAZ_BIB1_DATABASE_DOES_NOT_EXIST;
1145             *addinfo = odr_strdup(odr, torus_db.c_str());
1146             BackendPtr b;
1147             return b;
1148         }
1149         const xmlNode *ptr = xmlDocGetRootElement(doc);
1150         if (ptr)
1151         {   // presumably ptr is a records element node
1152             // parse first record in document
1153             for (ptr = ptr->children; ptr; ptr = ptr->next)
1154             {
1155                 if (ptr->type == XML_ELEMENT_NODE
1156                     && !strcmp((const char *) ptr->name, "record"))
1157                 {
1158                     if (sptr)
1159                     {
1160                         *error = YAZ_BIB1_UNSPECIFIED_ERROR;
1161                         *addinfo = (char*) odr_malloc(odr, 40 + database.length()),
1162                         sprintf(*addinfo, "multiple records for udb=%s",
1163                                  database.c_str());
1164                         xmlFreeDoc(doc);
1165                         BackendPtr b;
1166                         return b;
1167                     }
1168                     sptr = m_p->parse_torus_record(ptr);
1169                 }
1170             }
1171         }
1172         xmlFreeDoc(doc);
1173     }
1174
1175     if (!sptr)
1176     {
1177         *error = YAZ_BIB1_DATABASE_DOES_NOT_EXIST;
1178         *addinfo = odr_strdup(odr, torus_db.c_str());
1179         BackendPtr b;
1180         return b;
1181     }
1182         
1183     xsltStylesheetPtr xsp = 0;
1184     if (sptr->transform_xsl_content.length())
1185     {
1186         xmlDoc *xsp_doc = xmlParseMemory(sptr->transform_xsl_content.c_str(),
1187                                          sptr->transform_xsl_content.length());
1188         if (!xsp_doc)
1189         {
1190             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1191             *addinfo = (char *) odr_malloc(odr, 40);
1192             sprintf(*addinfo, "xmlParseMemory failed");
1193             BackendPtr b;
1194             return b;
1195         }
1196         xsp = xsltParseStylesheetDoc(xsp_doc);
1197         if (!xsp)
1198         {
1199             *error = YAZ_BIB1_DATABASE_DOES_NOT_EXIST;
1200             *addinfo = odr_strdup(odr, "xsltParseStylesheetDoc failed");
1201             BackendPtr b;
1202             xmlFreeDoc(xsp_doc);
1203             return b;
1204         }
1205     }
1206     else if (sptr->transform_xsl_fname.length())
1207     {
1208         const char *path = 0;
1209
1210         if (m_p->xsldir.length())
1211             path = m_p->xsldir.c_str();
1212         else
1213             path = m_p->file_path.c_str();
1214         std::string fname;
1215
1216         char fullpath[1024];
1217         char *cp = yaz_filepath_resolve(sptr->transform_xsl_fname.c_str(),
1218                                         path, 0, fullpath);
1219         if (cp)
1220             fname.assign(cp);
1221         else
1222         {
1223             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1224             *addinfo = (char *)
1225                 odr_malloc(odr, 40 + sptr->transform_xsl_fname.length());
1226             sprintf(*addinfo, "File could not be read: %s", 
1227                     sptr->transform_xsl_fname.c_str());
1228             BackendPtr b;
1229             return b;
1230         }
1231         xmlDoc *xsp_doc = xmlParseFile(fname.c_str());
1232         if (!xsp_doc)
1233         {
1234             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1235             *addinfo = (char *) odr_malloc(odr, 40 + fname.length());
1236             sprintf(*addinfo, "xmlParseFile failed. File: %s", fname.c_str());
1237             BackendPtr b;
1238             return b;
1239         }
1240         xsp = xsltParseStylesheetDoc(xsp_doc);
1241         if (!xsp)
1242         {
1243             *error = YAZ_BIB1_DATABASE_DOES_NOT_EXIST;
1244             *addinfo = odr_strdup(odr, "xsltParseStylesheetDoc failed");
1245             BackendPtr b;
1246             xmlFreeDoc(xsp_doc);
1247             return b;
1248         }
1249     }
1250
1251     m_backend.reset();
1252
1253     BackendPtr b(new Backend);
1254
1255     b->m_proxy = proxy;
1256     b->sptr = sptr;
1257     b->xsp = xsp;
1258     b->m_frontend_database = database;
1259     b->enable_cproxy = param_nocproxy ? false : true;
1260
1261     if (sptr->query_encoding.length())
1262         b->set_option("rpnCharset", sptr->query_encoding);
1263
1264     b->set_option("timeout", m_p->zoom_timeout.c_str());
1265     
1266     if (m_p->apdu_log) 
1267         b->set_option("apdulog", "1");
1268
1269     if (sptr->piggyback && sptr->sru.length())
1270         b->set_option("count", "1"); /* some SRU servers INSIST on getting
1271                                         maximumRecords > 0 */
1272     b->set_option("piggyback", sptr->piggyback ? "1" : "0");
1273
1274     if (authentication.length() == 0)
1275         authentication = sptr->authentication;
1276
1277     if (proxy.length() == 0)
1278         proxy = sptr->cfProxy;
1279     
1280     if (sptr->cfAuth.length())
1281     {
1282         // A CF target
1283         b->set_option("user", sptr->cfAuth);
1284         if (authentication.length())
1285         {
1286             size_t found = authentication.find('/');
1287             if (found != std::string::npos)
1288             {
1289                 out_names[no_out_args] = "user";
1290                 out_values[no_out_args++] =
1291                     odr_strdup(odr, authentication.substr(0, found).c_str());
1292
1293                 out_names[no_out_args] = "password";
1294                 out_values[no_out_args++] =
1295                     odr_strdup(odr, authentication.substr(found+1).c_str());
1296             }
1297             else
1298             {
1299                 out_names[no_out_args] = "user";
1300                 out_values[no_out_args++] =
1301                     odr_strdup(odr, authentication.c_str());
1302             }                
1303         }
1304         if (proxy.length())
1305         {
1306             out_names[no_out_args] = "proxy";
1307             out_values[no_out_args++] = odr_strdup(odr, proxy.c_str());
1308         }
1309         if (sptr->cfSubDB.length())
1310         {
1311             out_names[no_out_args] = "subdatabase";
1312             out_values[no_out_args++] = odr_strdup(odr, sptr->cfSubDB.c_str());
1313         }
1314         if (param_nocproxy)
1315         {
1316             out_names[no_out_args] = "nocproxy";
1317             out_values[no_out_args++] = odr_strdup(odr, param_nocproxy);
1318         }
1319     }
1320     else
1321     {
1322         size_t found = authentication.find('/');
1323         
1324         if (sptr->sru.length() && found != std::string::npos)
1325         {
1326             b->set_option("user", authentication.substr(0, found));
1327             b->set_option("password", authentication.substr(found+1));
1328         }
1329         else
1330             b->set_option("user", authentication);
1331
1332         if (proxy.length())
1333             b->set_option("proxy", proxy);
1334     }
1335     std::string url;
1336     if (sptr->sru.length())
1337     {
1338         url = "http://" + sptr->target;
1339         b->set_option("sru", sptr->sru);
1340
1341         if (sptr->sru_version.length())
1342             b->set_option("sru_version", sptr->sru_version);
1343     }
1344     else
1345     {
1346         url = sptr->target;
1347     }
1348     if (no_out_args)
1349     {
1350         char *x_args = 0;
1351         out_names[no_out_args] = 0; // terminate list
1352         
1353         yaz_array_to_uri(&x_args, odr, (char **) out_names,
1354                          (char **) out_values);
1355         url += "," + std::string(x_args);
1356     }
1357     package.log("zoom", YLOG_LOG, "url: %s", url.c_str());
1358     b->connect(url, error, addinfo, odr);
1359     if (*error == 0 && b->enable_cproxy)
1360         create_content_session(package, b, error, addinfo, odr,
1361                                content_authentication.length() ?
1362                                content_authentication : authentication,
1363                                content_proxy.length() ? content_proxy : proxy,
1364                                realm);
1365     if (*error == 0)
1366         m_backend = b;
1367     return b;
1368 }
1369
1370 void yf::Zoom::Frontend::prepare_elements(BackendPtr b,
1371                                           Odr_oid *preferredRecordSyntax,
1372                                           const char *element_set_name,
1373                                           bool &enable_pz2_retrieval,
1374                                           bool &enable_pz2_transform,
1375                                           bool &enable_record_transform,
1376                                           bool &assume_marc8_charset)
1377 {
1378     char oid_name_str[OID_STR_MAX];
1379     const char *syntax_name = 0;
1380     
1381     if (preferredRecordSyntax &&
1382         !oid_oidcmp(preferredRecordSyntax, yaz_oid_recsyn_xml))
1383     {
1384         if (element_set_name &&
1385             !strcmp(element_set_name, m_p->element_transform.c_str()))
1386         {
1387             enable_pz2_retrieval = true;
1388             enable_pz2_transform = true;
1389         }
1390         else if (element_set_name && 
1391                  !strcmp(element_set_name, m_p->element_raw.c_str()))
1392         {
1393             enable_pz2_retrieval = true;
1394         }
1395         else if (m_p->record_xsp)
1396         {
1397             enable_pz2_retrieval = true;
1398             enable_pz2_transform = true;
1399             enable_record_transform = true;
1400         }
1401     }
1402     
1403     if (enable_pz2_retrieval)
1404     {
1405         std::string configured_request_syntax = b->sptr->request_syntax;
1406         if (configured_request_syntax.length())
1407         {
1408             syntax_name = configured_request_syntax.c_str();
1409             const Odr_oid *syntax_oid = 
1410                 yaz_string_to_oid(yaz_oid_std(), CLASS_RECSYN, syntax_name);
1411             if (!oid_oidcmp(syntax_oid, yaz_oid_recsyn_usmarc)
1412                 || !oid_oidcmp(syntax_oid, yaz_oid_recsyn_opac))
1413                 assume_marc8_charset = true;
1414         }
1415     }
1416     else if (preferredRecordSyntax)
1417         syntax_name =
1418             yaz_oid_to_string_buf(preferredRecordSyntax, 0, oid_name_str);
1419
1420     if (b->sptr->sru.length())
1421         syntax_name = "XML";
1422
1423     b->set_option("preferredRecordSyntax", syntax_name);
1424
1425     if (enable_pz2_retrieval)
1426     {
1427         element_set_name = 0;
1428         if (b->sptr->element_set.length())
1429             element_set_name = b->sptr->element_set.c_str();
1430     }
1431
1432     b->set_option("elementSetName", element_set_name);
1433     if (b->sptr->sru.length() && element_set_name)
1434         b->set_option("schema", element_set_name);
1435 }
1436
1437 Z_Records *yf::Zoom::Frontend::get_explain_records(
1438     Package &package,
1439     Odr_int start,
1440     Odr_int number_to_present,
1441     int *error,
1442     char **addinfo,
1443     Odr_int *number_of_records_returned,
1444     ODR odr,
1445     BackendPtr b,
1446     Odr_oid *preferredRecordSyntax,
1447     const char *element_set_name)
1448 {
1449     Odr_int i;
1450     Z_Records *records = 0;
1451
1452     if (!b->explain_doc)
1453     {
1454         return records;
1455     }
1456     if (number_to_present > 10000)
1457         number_to_present = 10000;
1458
1459     xmlNode *ptr = xmlDocGetRootElement(b->explain_doc);
1460     
1461     Z_NamePlusRecordList *npl = (Z_NamePlusRecordList *)
1462         odr_malloc(odr, sizeof(*npl));
1463     npl->records = (Z_NamePlusRecord **)
1464         odr_malloc(odr, number_to_present * sizeof(*npl->records));
1465     
1466     for (i = 0; i < number_to_present; i++)
1467     {
1468         int num = 0;
1469         xmlNode *res = xml_node_search(ptr, &num, start + i + 1);
1470         if (!res)
1471             break;
1472         xmlBufferPtr xml_buf = xmlBufferCreate();
1473         xmlNode *tmp_node = xmlCopyNode(res->children, 1);
1474         xmlNodeDump(xml_buf, tmp_node->doc, tmp_node, 0, 0);
1475
1476         Z_NamePlusRecord *npr =
1477             (Z_NamePlusRecord *) odr_malloc(odr, sizeof(*npr));
1478         npr->databaseName = odr_strdup(odr, b->m_frontend_database.c_str());
1479         npr->which = Z_NamePlusRecord_databaseRecord;
1480         npr->u.databaseRecord =
1481             z_ext_record_xml(odr,
1482                              (const char *) xml_buf->content, xml_buf->use);
1483         npl->records[i] = npr;
1484         xmlFreeNode(tmp_node);
1485         xmlBufferFree(xml_buf);
1486     }
1487     records = (Z_Records*) odr_malloc(odr, sizeof(*records));
1488     records->which = Z_Records_DBOSD;
1489     records->u.databaseOrSurDiagnostics = npl;
1490
1491     npl->num_records = i;
1492     *number_of_records_returned = i;
1493     return records;
1494 }
1495
1496
1497 Z_Records *yf::Zoom::Frontend::get_records(Package &package,
1498                                            Odr_int start,
1499                                            Odr_int number_to_present,
1500                                            int *error,
1501                                            char **addinfo,
1502                                            Odr_int *number_of_records_returned,
1503                                            ODR odr,
1504                                            BackendPtr b,
1505                                            Odr_oid *preferredRecordSyntax,
1506                                            const char *element_set_name)
1507 {
1508     *number_of_records_returned = 0;
1509     Z_Records *records = 0;
1510     bool enable_pz2_retrieval = false; // whether target profile is used
1511     bool enable_pz2_transform = false; // whether XSLT is used as well
1512     bool assume_marc8_charset = false;
1513     bool enable_record_transform = false;
1514
1515     prepare_elements(b, preferredRecordSyntax,
1516                      element_set_name,
1517                      enable_pz2_retrieval,
1518                      enable_pz2_transform,
1519                      enable_record_transform,
1520                      assume_marc8_charset);
1521
1522     package.log("zoom", YLOG_LOG, "pz2_retrieval: %s . pz2_transform: %s",
1523                 enable_pz2_retrieval ? "yes" : "no",
1524                 enable_pz2_transform ? "yes" : "no");
1525
1526     if (start < 0 || number_to_present <=0)
1527         return records;
1528     
1529     if (number_to_present > 10000)
1530         number_to_present = 10000;
1531
1532     ZOOM_record *recs = (ZOOM_record *)
1533         odr_malloc(odr, (size_t) number_to_present * sizeof(*recs));
1534
1535     b->present(start, number_to_present, recs, error, addinfo, odr);
1536
1537     int i = 0;
1538     if (!*error)
1539     {
1540         for (i = 0; i < number_to_present; i++)
1541             if (!recs[i])
1542                 break;
1543     }
1544     if (i > 0)
1545     {  // only return records if no error and at least one record
1546
1547         const char *xsl_parms[3];
1548         mp::wrbuf cproxy_host;
1549         
1550         if (b->enable_cproxy && b->cproxy_host.length())
1551         {
1552             wrbuf_puts(cproxy_host, "\"");
1553             wrbuf_puts(cproxy_host, b->cproxy_host.c_str());
1554             wrbuf_puts(cproxy_host, "/\"");
1555
1556             xsl_parms[0] = "cproxyhost";
1557             xsl_parms[1] = wrbuf_cstr(cproxy_host);
1558             xsl_parms[2] = 0;
1559         }
1560         else
1561         {
1562             xsl_parms[0] = 0;
1563         }
1564
1565         char *odr_database = odr_strdup(odr,
1566                                         b->m_frontend_database.c_str());
1567         Z_NamePlusRecordList *npl = (Z_NamePlusRecordList *)
1568             odr_malloc(odr, sizeof(*npl));
1569         *number_of_records_returned = i;
1570         npl->num_records = i;
1571         npl->records = (Z_NamePlusRecord **)
1572             odr_malloc(odr, i * sizeof(*npl->records));
1573         for (i = 0; i < number_to_present; i++)
1574         {
1575             Z_NamePlusRecord *npr = 0;
1576             const char *addinfo;
1577
1578             int sur_error = ZOOM_record_error(recs[i], 0 /* msg */,
1579                                               &addinfo, 0 /* diagset */);
1580                 
1581             if (sur_error)
1582             {
1583                 log_diagnostic(package, sur_error, addinfo);
1584                 npr = zget_surrogateDiagRec(odr, odr_database, sur_error,
1585                                             addinfo);
1586             }
1587             else if (enable_pz2_retrieval)
1588             {
1589                 char rec_type_str[100];
1590                 const char *record_encoding = 0;
1591
1592                 if (b->sptr->record_encoding.length())
1593                     record_encoding = b->sptr->record_encoding.c_str();
1594                 else if (assume_marc8_charset)
1595                     record_encoding = "marc8";
1596
1597                 strcpy(rec_type_str, b->sptr->use_turbomarc ? "txml" : "xml");
1598                 if (record_encoding)
1599                 {
1600                     strcat(rec_type_str, "; charset=");
1601                     strcat(rec_type_str, record_encoding);
1602                 }
1603
1604                 package.log("zoom", YLOG_LOG, "Getting record of type %s",
1605                             rec_type_str);
1606                 int rec_len;
1607                 xmlChar *xmlrec_buf = 0;
1608                 const char *rec_buf = ZOOM_record_get(recs[i], rec_type_str,
1609                                                       &rec_len);
1610                 if (!rec_buf && !npr)
1611                 {
1612                     std::string addinfo("ZOOM_record_get failed for type ");
1613
1614                     int error = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1615                     addinfo += rec_type_str;
1616                     log_diagnostic(package, error, addinfo.c_str());
1617                     npr = zget_surrogateDiagRec(odr, odr_database,
1618                                                 error, addinfo.c_str());
1619                 }
1620                 else
1621                 {
1622                     package.log_write(rec_buf, rec_len);
1623                     package.log_write("\r\n", 2);
1624                 }
1625
1626                 if (rec_buf && b->xsp && enable_pz2_transform)
1627                 {
1628                     xmlDoc *rec_doc = xmlParseMemory(rec_buf, rec_len);
1629                     if (!rec_doc)
1630                     {
1631                         const char *addinfo = "xml parse failed for record";
1632                         int error = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1633                         log_diagnostic(package, error, addinfo);
1634                         npr = zget_surrogateDiagRec(
1635                             odr, odr_database, error, addinfo);
1636                     }
1637                     else
1638                     { 
1639                         // first stage XSLT - per target
1640                         xsltStylesheetPtr xsp = b->xsp;
1641                         xmlDoc *rec_res = xsltApplyStylesheet(xsp, rec_doc,
1642                                                               xsl_parms);
1643                         // insert generated-url
1644                         if (rec_res)
1645                         {
1646                             std::string res = 
1647                                 mp::xml::url_recipe_handle(rec_res,
1648                                                            b->sptr->urlRecipe);
1649                             if (res.length())
1650                             {
1651                                 xmlNode *ptr = xmlDocGetRootElement(rec_res);
1652                                 while (ptr && ptr->type != XML_ELEMENT_NODE)
1653                                     ptr = ptr->next;
1654                                 xmlNode *c = 
1655                                     xmlNewChild(ptr, 0, BAD_CAST "metadata", 0);
1656                                 xmlNewProp(c, BAD_CAST "type", BAD_CAST
1657                                            "generated-url");
1658                                 xmlNode * t = xmlNewText(BAD_CAST res.c_str());
1659                                 xmlAddChild(c, t);
1660                             }
1661                         }
1662                         // second stage XSLT - common
1663                         if (rec_res && m_p->record_xsp &&
1664                             enable_record_transform)
1665                         {
1666                             xmlDoc *tmp_doc = rec_res;
1667
1668                             xsp = m_p->record_xsp;
1669                             rec_res = xsltApplyStylesheet(xsp, tmp_doc,
1670                                                           xsl_parms);
1671                             xmlFreeDoc(tmp_doc);
1672                         }
1673                         // get result out of it
1674                         if (rec_res)
1675                         {
1676                             xsltSaveResultToString(&xmlrec_buf, &rec_len,
1677                                                    rec_res, xsp);
1678                             rec_buf = (const char *) xmlrec_buf;
1679                             package.log_write(rec_buf, rec_len);
1680
1681                             xmlFreeDoc(rec_res);
1682                         }
1683                         if (!rec_buf)
1684                         {
1685                             std::string addinfo;
1686                             int error =
1687                                 YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1688
1689                             addinfo = "xslt apply failed for "
1690                                 + b->sptr->transform_xsl_fname;
1691                             log_diagnostic(package, error, addinfo.c_str());
1692                             npr = zget_surrogateDiagRec(
1693                                 odr, odr_database, error, addinfo.c_str());
1694                         }
1695                         xmlFreeDoc(rec_doc);
1696                     }
1697                 }
1698
1699                 if (!npr)
1700                 {
1701                     if (!rec_buf)
1702                         npr = zget_surrogateDiagRec(
1703                             odr, odr_database, 
1704                             YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
1705                             rec_type_str);
1706                     else
1707                     {
1708                         npr = (Z_NamePlusRecord *)
1709                             odr_malloc(odr, sizeof(*npr));
1710                         npr->databaseName = odr_database;
1711                         npr->which = Z_NamePlusRecord_databaseRecord;
1712                         npr->u.databaseRecord =
1713                             z_ext_record_xml(odr, rec_buf, rec_len);
1714                     }
1715                 }
1716                 if (xmlrec_buf)
1717                     xmlFree(xmlrec_buf);
1718             }
1719             else
1720             {
1721                 Z_External *ext =
1722                     (Z_External *) ZOOM_record_get(recs[i], "ext", 0);
1723                 if (ext)
1724                 {
1725                     npr = (Z_NamePlusRecord *) odr_malloc(odr, sizeof(*npr));
1726                     npr->databaseName = odr_database;
1727                     npr->which = Z_NamePlusRecord_databaseRecord;
1728                     npr->u.databaseRecord = ext;
1729                 }
1730                 else
1731                 {
1732                     npr = zget_surrogateDiagRec(
1733                         odr, odr_database, 
1734                         YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
1735                         "ZOOM_record, type ext");
1736                 }
1737             }
1738             npl->records[i] = npr;
1739         }
1740         records = (Z_Records*) odr_malloc(odr, sizeof(*records));
1741         records->which = Z_Records_DBOSD;
1742         records->u.databaseOrSurDiagnostics = npl;
1743     }
1744     return records;
1745 }
1746
1747 struct cql_node *yf::Zoom::Impl::convert_cql_fields(struct cql_node *cn,
1748                                                     ODR odr)
1749 {
1750     struct cql_node *r = 0;
1751     if (!cn)
1752         return 0;
1753     switch (cn->which)
1754     {
1755     case CQL_NODE_ST:
1756         if (cn->u.st.index)
1757         {
1758             std::map<std::string,std::string>::const_iterator it;
1759             it = fieldmap.find(cn->u.st.index);
1760             if (it == fieldmap.end())
1761                 return cn;
1762             if (it->second.length())
1763                 cn->u.st.index = odr_strdup(odr, it->second.c_str());
1764             else
1765                 cn->u.st.index = 0;
1766         }
1767         break;
1768     case CQL_NODE_BOOL:
1769         r = convert_cql_fields(cn->u.boolean.left, odr);
1770         if (!r)
1771             r = convert_cql_fields(cn->u.boolean.right, odr);
1772         break;
1773     case CQL_NODE_SORT:
1774         r = convert_cql_fields(cn->u.sort.search, odr);
1775         break;
1776     }
1777     return r;
1778 }
1779
1780 void yf::Zoom::Frontend::log_diagnostic(mp::Package &package,
1781                                         int error, const char *addinfo)
1782 {
1783     const char *err_msg = yaz_diag_bib1_str(error);
1784     if (addinfo)
1785         package.log("zoom", YLOG_WARN, "Diagnostic %d %s: %s",
1786                     error, err_msg, addinfo);
1787     else
1788         package.log("zoom", YLOG_WARN, "Diagnostic %d %s:",
1789                     error, err_msg);
1790 }
1791
1792 yf::Zoom::BackendPtr yf::Zoom::Frontend::explain_search(mp::Package &package,
1793                                                         std::string &database,
1794                                                         int *error,
1795                                                         char **addinfo,
1796                                                         mp::odr &odr,
1797                                                         std::string &torus_db,
1798                                                         std::string &realm)
1799 {
1800     m_backend.reset();
1801
1802     BackendPtr b(new Backend);
1803
1804     b->m_frontend_database = database;
1805     b->enable_explain = true;
1806    
1807     Z_GDU *gdu = package.request().get();
1808     Z_APDU *apdu_req = gdu->u.z3950;
1809     Z_SearchRequest *sr = apdu_req->u.searchRequest;
1810     Z_Query *query = sr->query;
1811
1812     if (!m_p->explain_xsp)
1813     {
1814         *error = YAZ_BIB1_UNSPECIFIED_ERROR;
1815         *addinfo =
1816             odr_strdup(odr, "IR-Explain---1 unsupported. "
1817                        "Torus explain_xsl not defined");
1818         return m_backend;
1819     }
1820     else if (query->which == Z_Query_type_104 &&
1821         query->u.type_104->which == Z_External_CQL)
1822     {
1823         std::string torus_url = m_p->torus_searchable_url;
1824         std::string torus_query(query->u.type_104->u.cql);
1825         xmlDoc *doc = mp::get_searchable(package, torus_url, "",
1826                                          torus_query,
1827                                          realm, m_p->proxy);
1828         if (m_p->explain_xsp)
1829         {
1830             xmlDoc *rec_res =  xsltApplyStylesheet(m_p->explain_xsp, doc, 0);
1831
1832             xmlFreeDoc(doc);
1833             doc = rec_res;
1834         }
1835         if (!doc)
1836         {
1837             *error = YAZ_BIB1_UNSPECIFIED_ERROR;
1838             *addinfo = odr_strdup(odr, "IR-Explain---1 problem. " 
1839                                   "Could not obtain Torus records for Explain");
1840         }
1841         else
1842         {
1843             xmlNode *ptr = xmlDocGetRootElement(doc);
1844             int hits = 0;
1845             
1846             xml_node_search(ptr, &hits, 0);
1847
1848             Z_APDU *apdu_res = odr.create_searchResponse(apdu_req, 0, 0);
1849             apdu_res->u.searchResponse->resultCount = odr_intdup(odr, hits);
1850             package.response() = apdu_res;
1851             m_backend = b;
1852         }
1853         if (b->explain_doc)
1854             xmlFreeDoc(b->explain_doc);
1855         b->explain_doc = doc;
1856         return m_backend;
1857     }
1858     else
1859     {
1860         *error = YAZ_BIB1_QUERY_TYPE_UNSUPP;
1861         *addinfo = odr_strdup(odr, "IR-Explain---1 only supports CQL");
1862         return m_backend;
1863     }
1864 }
1865
1866 static bool wait_conn(COMSTACK cs, int secs)
1867 {
1868     struct yaz_poll_fd pfd;
1869
1870     yaz_poll_add(pfd.input_mask, yaz_poll_except);
1871     if (cs->io_pending && CS_WANT_WRITE)
1872         yaz_poll_add(pfd.input_mask, yaz_poll_write);
1873     else if (cs->io_pending & CS_WANT_READ)
1874         yaz_poll_add(pfd.input_mask, yaz_poll_read);
1875
1876     pfd.fd = cs_fileno(cs);
1877     pfd.client_data = 0;
1878     
1879     int ret = yaz_poll(&pfd, 1, secs, 0);
1880     return ret > 0;
1881 }
1882
1883 bool yf::Zoom::Impl::check_proxy(const char *proxy)
1884 {
1885     COMSTACK conn = 0;
1886     const char *uri = "http://localhost/";
1887     void *add;
1888     mp::odr odr;
1889     bool outcome = false;
1890     conn = cs_create_host_proxy(uri, 0, &add, proxy);
1891
1892     if (!conn)
1893         return false;
1894
1895     Z_GDU *gdu = z_get_HTTP_Request_uri(odr, uri, 0, 1);
1896     gdu->u.HTTP_Request->method = odr_strdup(odr, "GET");
1897     
1898     if (z_GDU(odr, &gdu, 0, 0))
1899     {
1900         int len;
1901         char *buf = odr_getbuf(odr, &len, 0);
1902         
1903         int ret = cs_connect(conn, add);
1904         if (ret > 0 || (ret == 0 && wait_conn(conn, 1)))
1905         {
1906             while (1)
1907             {
1908                 ret = cs_put(conn, buf, len);
1909                 if (ret != 1)
1910                     break;
1911                 if (!wait_conn(conn, proxy_timeout))
1912                     break;
1913             }
1914         }
1915     }
1916     cs_close(conn);
1917     return outcome;
1918 }
1919
1920 void yf::Zoom::Frontend::handle_search(mp::Package &package)
1921 {
1922     Z_GDU *gdu = package.request().get();
1923     Z_APDU *apdu_req = gdu->u.z3950;
1924     Z_APDU *apdu_res = 0;
1925     mp::odr odr;
1926     Z_SearchRequest *sr = apdu_req->u.searchRequest;
1927     if (sr->num_databaseNames != 1)
1928     {
1929         int error = YAZ_BIB1_TOO_MANY_DATABASES_SPECIFIED;
1930         log_diagnostic(package, error, 0);
1931         apdu_res = odr.create_searchResponse(apdu_req, error, 0);
1932         package.response() = apdu_res;
1933         return;
1934     }
1935     int proxy_step = 0;
1936     int same_retries = 0;
1937     int proxy_retries = 0;
1938
1939 next_proxy:
1940
1941     int error = 0;
1942     char *addinfo = 0;
1943     std::string db(sr->databaseNames[0]);
1944
1945     BackendPtr b = get_backend_from_databases(package, db, &error,
1946                                               &addinfo, odr, &proxy_step);
1947     if (error)
1948     {
1949         if (b && b->m_proxy.length() && !m_p->check_proxy(b->m_proxy.c_str()))
1950         {
1951             log_diagnostic(package, error, addinfo);
1952             package.log("zoom", YLOG_LOG, "proxy %s fails", b->m_proxy.c_str());
1953             m_backend.reset();
1954             if (proxy_step) // there is a failover
1955             {
1956                 proxy_retries++;
1957                 package.log("zoom", YLOG_WARN, "search failed: trying next proxy");
1958                 goto next_proxy;
1959             }
1960             error = YAZ_BIB1_INIT_AC_AUTHENTICATION_SYSTEM_ERROR;
1961             addinfo = odr_strdup(odr, "proxy failure");
1962         }
1963         else if (same_retries == 0 && proxy_retries == 0)
1964         {
1965             log_diagnostic(package, error, addinfo);
1966             same_retries++;
1967             package.log("zoom", YLOG_WARN, "search failed: retry");
1968             m_backend.reset();
1969             proxy_step = 0;
1970             goto next_proxy;
1971         }
1972     }
1973     if (error)
1974     {
1975         log_diagnostic(package, error, addinfo);
1976         apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
1977         package.response() = apdu_res;
1978         return;
1979     }
1980     if (!b || b->enable_explain)
1981         return;
1982
1983     b->set_option("setname", "default");
1984
1985     bool enable_pz2_retrieval = false;
1986     bool enable_pz2_transform = false;
1987     bool enable_record_transform = false;
1988     bool assume_marc8_charset = false;
1989     prepare_elements(b, sr->preferredRecordSyntax, 0 /*element_set_name */,
1990                      enable_pz2_retrieval,
1991                      enable_pz2_transform,
1992                      enable_record_transform,
1993                      assume_marc8_charset);
1994
1995     Odr_int hits = 0;
1996     Z_Query *query = sr->query;
1997     mp::wrbuf ccl_wrbuf;
1998     mp::wrbuf pqf_wrbuf;
1999     std::string sortkeys;
2000
2001     if (query->which == Z_Query_type_1 || query->which == Z_Query_type_101)
2002     {
2003         // RPN
2004         yaz_rpnquery_to_wrbuf(pqf_wrbuf, query->u.type_1);
2005     }
2006     else if (query->which == Z_Query_type_2)
2007     {
2008         // CCL
2009         wrbuf_write(ccl_wrbuf, (const char *) query->u.type_2->buf,
2010                     query->u.type_2->len);
2011     }
2012     else if (query->which == Z_Query_type_104 &&
2013              query->u.type_104->which == Z_External_CQL)
2014     {
2015         // CQL
2016         const char *cql = query->u.type_104->u.cql;
2017         CQL_parser cp = cql_parser_create();
2018         int r = cql_parser_string(cp, cql);
2019         package.log("zoom", YLOG_LOG, "CQL: %s", cql);
2020         if (r)
2021         {
2022             cql_parser_destroy(cp);
2023             error = YAZ_BIB1_MALFORMED_QUERY;
2024             const char *addinfo = "CQL syntax error";
2025             log_diagnostic(package, error, addinfo);
2026             apdu_res = 
2027                 odr.create_searchResponse(apdu_req, error, addinfo);
2028             package.response() = apdu_res;
2029             return;
2030         }
2031         struct cql_node *cn = cql_parser_result(cp);
2032         struct cql_node *cn_error = m_p->convert_cql_fields(cn, odr);
2033         if (cn_error)
2034         {
2035             // hopefully we are getting a ptr to a index+relation+term node
2036             error = YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
2037             addinfo = 0;
2038             if (cn_error->which == CQL_NODE_ST)
2039                 addinfo = cn_error->u.st.index;
2040             
2041             log_diagnostic(package, error, addinfo);
2042             apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
2043             package.response() = apdu_res;
2044             cql_parser_destroy(cp);
2045             return;
2046         }
2047         char ccl_buf[1024];
2048         r = cql_to_ccl_buf(cn, ccl_buf, sizeof(ccl_buf));
2049         if (r)
2050         {
2051             error = YAZ_BIB1_MALFORMED_QUERY;
2052             const char *addinfo = "CQL to CCL conversion error";
2053
2054             log_diagnostic(package, error, addinfo);
2055             apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
2056             package.response() = apdu_res;
2057             cql_parser_destroy(cp);
2058             return;
2059         }
2060
2061         mp::wrbuf sru_sortkeys_wrbuf;
2062         if (cql_sortby_to_sortkeys(cn, wrbuf_vp_puts, sru_sortkeys_wrbuf))
2063         {
2064             error = YAZ_BIB1_ILLEGAL_SORT_RELATION;
2065             const char *addinfo = "CQL to CCL sortby conversion";
2066
2067             log_diagnostic(package, error, addinfo);
2068             apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
2069             package.response() = apdu_res;
2070             cql_parser_destroy(cp);
2071             return;
2072         }
2073         mp::wrbuf sort_spec_wrbuf;
2074         yaz_srw_sortkeys_to_sort_spec(wrbuf_cstr(sru_sortkeys_wrbuf),
2075                                       sort_spec_wrbuf);
2076         wrbuf_puts(ccl_wrbuf, ccl_buf);
2077         
2078         yaz_tok_cfg_t tc = yaz_tok_cfg_create();
2079         yaz_tok_parse_t tp =
2080             yaz_tok_parse_buf(tc, wrbuf_cstr(sort_spec_wrbuf));
2081         yaz_tok_cfg_destroy(tc);
2082         
2083         /* go through sortspec and map fields */
2084         int token = yaz_tok_move(tp);
2085         while (token != YAZ_TOK_EOF)
2086         {
2087             if (token == YAZ_TOK_STRING)
2088             {
2089                 const char *field = yaz_tok_parse_string(tp);
2090                 std::map<std::string,std::string>::iterator it;
2091                 it = b->sptr->sortmap.find(field);
2092                 if (it != b->sptr->sortmap.end())
2093                     sortkeys += it->second;
2094                 else
2095                     sortkeys += field;
2096             }
2097             sortkeys += " ";
2098             token = yaz_tok_move(tp);
2099             if (token == YAZ_TOK_STRING)
2100             {
2101                 sortkeys += yaz_tok_parse_string(tp);
2102             }
2103             if (token != YAZ_TOK_EOF)
2104             {
2105                 sortkeys += " ";
2106                 token = yaz_tok_move(tp);
2107             }
2108         }
2109         yaz_tok_parse_destroy(tp);
2110         cql_parser_destroy(cp);
2111     }
2112     else
2113     {
2114         error = YAZ_BIB1_QUERY_TYPE_UNSUPP;
2115         const char *addinfo = 0;
2116         log_diagnostic(package, error, addinfo);
2117         apdu_res =  odr.create_searchResponse(apdu_req, error, addinfo);
2118         package.response() = apdu_res;
2119         return;
2120     }
2121
2122     if (ccl_wrbuf.len())
2123     {
2124         // CCL to PQF
2125         assert(pqf_wrbuf.len() == 0);
2126         int cerror, cpos;
2127         struct ccl_rpn_node *cn;
2128         package.log("zoom", YLOG_LOG, "CCL: %s", wrbuf_cstr(ccl_wrbuf));
2129         cn = ccl_find_str(b->sptr->ccl_bibset, wrbuf_cstr(ccl_wrbuf),
2130                           &cerror, &cpos);
2131         if (!cn)
2132         {
2133             char *addinfo = odr_strdup_null(odr, ccl_err_msg(cerror));
2134             error = YAZ_BIB1_MALFORMED_QUERY;
2135
2136             switch (cerror)
2137             {
2138             case CCL_ERR_UNKNOWN_QUAL:
2139             case CCL_ERR_TRUNC_NOT_LEFT: 
2140             case CCL_ERR_TRUNC_NOT_RIGHT:
2141             case CCL_ERR_TRUNC_NOT_BOTH:
2142 #ifdef CCL_ERR_TRUNC_NOT_EMBED
2143             case CCL_ERR_TRUNC_NOT_EMBED:
2144 #endif
2145 #ifdef CCL_ERR_TRUNC_NOT_SINGLE
2146             case CCL_ERR_TRUNC_NOT_SINGLE:
2147 #endif
2148                 error = YAZ_BIB1_UNSUPP_SEARCH;
2149                 break;
2150             }
2151             log_diagnostic(package, error, addinfo);
2152             apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
2153             package.response() = apdu_res;
2154             return;
2155         }
2156         ccl_pquery(pqf_wrbuf, cn);
2157         package.log("zoom", YLOG_LOG, "RPN: %s", wrbuf_cstr(pqf_wrbuf));
2158         ccl_rpn_delete(cn);
2159     }
2160     
2161     assert(pqf_wrbuf.len());
2162
2163     ZOOM_query q = ZOOM_query_create();
2164     ZOOM_query_sortby2(q, b->sptr->sortStrategy.c_str(), sortkeys.c_str());
2165
2166     if (b->get_option("sru"))
2167     {
2168         int status = 0;
2169         Z_RPNQuery *zquery;
2170         zquery = p_query_rpn(odr, wrbuf_cstr(pqf_wrbuf));
2171         mp::wrbuf wrb;
2172             
2173         if (!strcmp(b->get_option("sru"), "solr"))
2174         {
2175             solr_transform_t cqlt = solr_transform_create();
2176             
2177             status = solr_transform_rpn2solr_wrbuf(cqlt, wrb, zquery);
2178             
2179             solr_transform_close(cqlt);
2180         }
2181         else
2182         {
2183             cql_transform_t cqlt = cql_transform_create();
2184             
2185             status = cql_transform_rpn2cql_wrbuf(cqlt, wrb, zquery);
2186             
2187             cql_transform_close(cqlt);
2188         }
2189         if (status == 0)
2190         {
2191             ZOOM_query_cql(q, wrbuf_cstr(wrb));
2192             package.log("zoom", YLOG_LOG, "CQL: %s", wrbuf_cstr(wrb));
2193             b->search(q, &hits, &error, &addinfo, odr);
2194         }
2195         ZOOM_query_destroy(q);
2196         
2197         if (status)
2198         {
2199             error = YAZ_BIB1_MALFORMED_QUERY;
2200             const char *addinfo = "can not convert from RPN to CQL/SOLR";
2201             log_diagnostic(package, error, addinfo);
2202             apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
2203             package.response() = apdu_res;
2204             return;
2205         }
2206     }
2207     else
2208     {
2209         ZOOM_query_prefix(q, wrbuf_cstr(pqf_wrbuf));
2210         package.log("zoom", YLOG_LOG, "search PQF: %s", wrbuf_cstr(pqf_wrbuf));
2211         b->search(q, &hits, &error, &addinfo, odr);
2212         ZOOM_query_destroy(q);
2213     }
2214
2215     if (error)
2216     {
2217         if (b->m_proxy.length() && !m_p->check_proxy(b->m_proxy.c_str()))
2218         {
2219             log_diagnostic(package, error, addinfo);
2220             package.log("zoom", YLOG_LOG, "proxy %s fails", b->m_proxy.c_str());
2221             m_backend.reset();
2222             if (proxy_step) // there is a failover
2223             {
2224                 proxy_retries++;
2225                 package.log("zoom", YLOG_WARN, "search failed: trying next proxy");
2226                 goto next_proxy;
2227             }
2228             error = YAZ_BIB1_INIT_AC_AUTHENTICATION_SYSTEM_ERROR;
2229             addinfo = odr_strdup(odr, "proxy failure");
2230         }
2231         else if (same_retries == 0 && proxy_retries == 0)
2232         { 
2233             log_diagnostic(package, error, addinfo);
2234             same_retries++;
2235             package.log("zoom", YLOG_WARN, "search failed: retry");
2236             m_backend.reset();
2237             proxy_step = 0;
2238             goto next_proxy;
2239         }
2240     }
2241
2242     const char *element_set_name = 0;
2243     Odr_int number_to_present = 0;
2244     if (!error)
2245         mp::util::piggyback_sr(sr, hits, number_to_present, &element_set_name);
2246     
2247     Odr_int number_of_records_returned = 0;
2248     Z_Records *records = get_records(
2249         package,
2250         0, number_to_present, &error, &addinfo,
2251         &number_of_records_returned, odr, b, sr->preferredRecordSyntax,
2252         element_set_name);
2253     if (error)
2254         log_diagnostic(package, error, addinfo);
2255     apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
2256     if (records)
2257     {
2258         apdu_res->u.searchResponse->records = records;
2259         apdu_res->u.searchResponse->numberOfRecordsReturned =
2260             odr_intdup(odr, number_of_records_returned);
2261     }
2262     apdu_res->u.searchResponse->resultCount = odr_intdup(odr, hits);
2263     package.response() = apdu_res;
2264 }
2265
2266 void yf::Zoom::Frontend::handle_present(mp::Package &package)
2267 {
2268     Z_GDU *gdu = package.request().get();
2269     Z_APDU *apdu_req = gdu->u.z3950;
2270     Z_APDU *apdu_res = 0;
2271     Z_PresentRequest *pr = apdu_req->u.presentRequest;
2272
2273     mp::odr odr;
2274     if (!m_backend)
2275     {
2276         package.response() = odr.create_presentResponse(
2277             apdu_req, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST, 0);
2278         return;
2279     }
2280     const char *element_set_name = 0;
2281     Z_RecordComposition *comp = pr->recordComposition;
2282     if (comp && comp->which != Z_RecordComp_simple)
2283     {
2284         package.response() = odr.create_presentResponse(
2285             apdu_req, 
2286             YAZ_BIB1_PRESENT_COMP_SPEC_PARAMETER_UNSUPP, 0);
2287         return;
2288     }
2289     if (comp && comp->u.simple->which == Z_ElementSetNames_generic)
2290         element_set_name = comp->u.simple->u.generic;
2291     Odr_int number_of_records_returned = 0;
2292     int error = 0;
2293     char *addinfo = 0;
2294
2295     if (m_backend->enable_explain)
2296     {
2297         Z_Records *records =
2298             get_explain_records(
2299                 package,
2300                 *pr->resultSetStartPoint - 1, *pr->numberOfRecordsRequested,
2301                 &error, &addinfo, &number_of_records_returned, odr, m_backend,
2302                 pr->preferredRecordSyntax, element_set_name);
2303         
2304         apdu_res = odr.create_presentResponse(apdu_req, error, addinfo);
2305         if (records)
2306         {
2307             apdu_res->u.presentResponse->records = records;
2308             apdu_res->u.presentResponse->numberOfRecordsReturned =
2309                 odr_intdup(odr, number_of_records_returned);
2310         }
2311         package.response() = apdu_res;
2312     }
2313     else
2314     {
2315         Z_Records *records =
2316             get_records(package,
2317                         *pr->resultSetStartPoint - 1, *pr->numberOfRecordsRequested,
2318                         &error, &addinfo, &number_of_records_returned, odr, m_backend,
2319                         pr->preferredRecordSyntax, element_set_name);
2320         
2321         apdu_res = odr.create_presentResponse(apdu_req, error, addinfo);
2322         if (records)
2323         {
2324             apdu_res->u.presentResponse->records = records;
2325             apdu_res->u.presentResponse->numberOfRecordsReturned =
2326                 odr_intdup(odr, number_of_records_returned);
2327         }
2328         package.response() = apdu_res;
2329     }
2330 }
2331
2332 void yf::Zoom::Frontend::handle_package(mp::Package &package)
2333 {
2334     Z_GDU *gdu = package.request().get();
2335     if (!gdu)
2336         ;
2337     else if (gdu->which == Z_GDU_Z3950)
2338     {
2339         Z_APDU *apdu_req = gdu->u.z3950;
2340
2341         if (m_backend)
2342             wrbuf_rewind(m_backend->m_apdu_wrbuf);
2343         if (apdu_req->which == Z_APDU_initRequest)
2344         {
2345             mp::odr odr;
2346             package.response() = odr.create_close(
2347                 apdu_req,
2348                 Z_Close_protocolError,
2349                 "double init");
2350         }
2351         else if (apdu_req->which == Z_APDU_searchRequest)
2352         {
2353             handle_search(package);
2354         }
2355         else if (apdu_req->which == Z_APDU_presentRequest)
2356         {
2357             handle_present(package);
2358         }
2359         else
2360         {
2361             mp::odr odr;
2362             package.response() = odr.create_close(
2363                 apdu_req,
2364                 Z_Close_protocolError,
2365                 "zoom filter cannot handle this APDU");
2366             package.session().close();
2367         }
2368         if (m_backend)
2369         {
2370             WRBUF w = m_backend->m_apdu_wrbuf;
2371             package.log_write(wrbuf_buf(w), wrbuf_len(w));
2372         }
2373     }
2374     else
2375     {
2376         package.session().close();
2377     }
2378 }
2379
2380 void yf::Zoom::Impl::process(mp::Package &package)
2381 {
2382     FrontendPtr f = get_frontend(package);
2383     Z_GDU *gdu = package.request().get();
2384
2385     if (f->m_is_virtual)
2386     {
2387         f->handle_package(package);
2388     }
2389     else if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
2390              Z_APDU_initRequest)
2391     {
2392         Z_InitRequest *req = gdu->u.z3950->u.initRequest;
2393         f->m_init_gdu = gdu;
2394         
2395         mp::odr odr;
2396         Z_APDU *apdu = odr.create_initResponse(gdu->u.z3950, 0, 0);
2397         Z_InitResponse *resp = apdu->u.initResponse;
2398         
2399         int i;
2400         static const int masks[] = {
2401             Z_Options_search,
2402             Z_Options_present,
2403             -1 
2404         };
2405         for (i = 0; masks[i] != -1; i++)
2406             if (ODR_MASK_GET(req->options, masks[i]))
2407                 ODR_MASK_SET(resp->options, masks[i]);
2408         
2409         static const int versions[] = {
2410             Z_ProtocolVersion_1,
2411             Z_ProtocolVersion_2,
2412             Z_ProtocolVersion_3,
2413             -1
2414         };
2415         for (i = 0; versions[i] != -1; i++)
2416             if (ODR_MASK_GET(req->protocolVersion, versions[i]))
2417                 ODR_MASK_SET(resp->protocolVersion, versions[i]);
2418             else
2419                 break;
2420         
2421         *resp->preferredMessageSize = *req->preferredMessageSize;
2422         *resp->maximumRecordSize = *req->maximumRecordSize;
2423         
2424         package.response() = apdu;
2425         f->m_is_virtual = true;
2426     }
2427     else
2428         package.move();
2429
2430     release_frontend(package);
2431 }
2432
2433
2434 static mp::filter::Base* filter_creator()
2435 {
2436     return new mp::filter::Zoom;
2437 }
2438
2439 extern "C" {
2440     struct metaproxy_1_filter_struct metaproxy_1_filter_zoom = {
2441         0,
2442         "zoom",
2443         filter_creator
2444     };
2445 }
2446
2447
2448 /*
2449  * Local variables:
2450  * c-basic-offset: 4
2451  * c-file-style: "Stroustrup"
2452  * indent-tabs-mode: nil
2453  * End:
2454  * vim: shiftwidth=4 tabstop=8 expandtab
2455  */
2456