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