Change case of Solr in msg
[metaproxy-moved-to-github.git] / src / filter_zoom.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2013 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, 70 + tmp_file.length());
948                 sprintf(*addinfo, "zoom: 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, "zoom: 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, 50 + strlen(fname));
991             sprintf(*addinfo, "zoom: 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, "zoom: 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 = odr_strdup(odr, "zoom: xmlParseMemory failed "
1250                                   "for literalTransform XSL");
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 =
1259                 odr_strdup(odr,"zoom: xsltParseStylesheetDoc failed "
1260                            "for literalTransform XSL");
1261             BackendPtr b;
1262             xmlFreeDoc(xsp_doc);
1263             return b;
1264         }
1265     }
1266     else if (sptr->transform_xsl_fname.length())
1267     {
1268         const char *path = 0;
1269
1270         if (m_p->xsldir.length())
1271             path = m_p->xsldir.c_str();
1272         else
1273             path = m_p->file_path.c_str();
1274         std::string fname;
1275
1276         char fullpath[1024];
1277         char *cp = yaz_filepath_resolve(sptr->transform_xsl_fname.c_str(),
1278                                         path, 0, fullpath);
1279         if (cp)
1280             fname.assign(cp);
1281         else
1282         {
1283             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1284             *addinfo = (char *)
1285                 odr_malloc(odr, 40 + sptr->transform_xsl_fname.length());
1286             sprintf(*addinfo, "zoom: could not open file %s",
1287                     sptr->transform_xsl_fname.c_str());
1288             BackendPtr b;
1289             return b;
1290         }
1291         xmlDoc *xsp_doc = xmlParseFile(fname.c_str());
1292         if (!xsp_doc)
1293         {
1294             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1295             *addinfo = (char *) odr_malloc(odr, 50 + fname.length());
1296             sprintf(*addinfo, "zoom: xmlParseFile failed for file %s",
1297                     fname.c_str());
1298             BackendPtr b;
1299             return b;
1300         }
1301         xsp = xsltParseStylesheetDoc(xsp_doc);
1302         if (!xsp)
1303         {
1304             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1305             *addinfo = (char *) odr_malloc(odr, 50 + fname.length());
1306             sprintf(*addinfo, "zoom: xsltParseStylesheetDoc failed "
1307                     "for file %s", fname.c_str());
1308             BackendPtr b;
1309             xmlFreeDoc(xsp_doc);
1310             return b;
1311         }
1312     }
1313
1314     cql_transform_t cqlt = 0;
1315     if (sptr->rpn2cql_fname.length())
1316     {
1317         char fullpath[1024];
1318         char *cp = yaz_filepath_resolve(sptr->rpn2cql_fname.c_str(),
1319                                         m_p->file_path.c_str(), 0, fullpath);
1320         if (cp)
1321             cqlt = cql_transform_open_fname(fullpath);
1322     }
1323     else
1324         cqlt = cql_transform_create();
1325
1326     if (!cqlt)
1327     {
1328         *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1329         *addinfo = odr_strdup(odr, "zoom: missing/invalid cql2rpn file");
1330         BackendPtr b;
1331         xsltFreeStylesheet(xsp);
1332         return b;
1333     }
1334
1335     m_backend.reset();
1336
1337     BackendPtr b(new Backend);
1338
1339     b->cqlt = cqlt;
1340     b->sptr = sptr;
1341     b->xsp = xsp;
1342     b->m_frontend_database = database;
1343     b->enable_cproxy = param_nocproxy ? false : true;
1344
1345     if (sptr->query_encoding.length())
1346         b->set_option("rpnCharset", sptr->query_encoding);
1347
1348     if (sptr->extraArgs.length())
1349         b->set_option("extraArgs", sptr->extraArgs);
1350
1351     b->set_option("timeout", m_p->zoom_timeout.c_str());
1352
1353     if (m_p->apdu_log)
1354         b->set_option("apdulog", "1");
1355
1356     if (sptr->piggyback && sptr->sru.length())
1357         b->set_option("count", "1"); /* some SRU servers INSIST on getting
1358                                         maximumRecords > 0 */
1359     b->set_option("piggyback", sptr->piggyback ? "1" : "0");
1360
1361     if (authentication.length() == 0)
1362         authentication = sptr->authentication;
1363
1364     if (proxy.length() == 0)
1365         proxy = sptr->cfProxy;
1366     b->m_proxy = proxy;
1367
1368     if (sptr->cfAuth.length())
1369     {
1370         // A CF target
1371         b->set_option("user", sptr->cfAuth);
1372         if (authentication.length())
1373         {
1374             size_t found = authentication.find('/');
1375             if (found != std::string::npos)
1376             {
1377                 out_names[no_out_args] = "user";
1378                 out_values[no_out_args++] =
1379                     odr_strdup(odr, authentication.substr(0, found).c_str());
1380
1381                 out_names[no_out_args] = "password";
1382                 out_values[no_out_args++] =
1383                     odr_strdup(odr, authentication.substr(found+1).c_str());
1384             }
1385             else
1386             {
1387                 out_names[no_out_args] = "user";
1388                 out_values[no_out_args++] =
1389                     odr_strdup(odr, authentication.c_str());
1390             }
1391         }
1392         if (proxy.length())
1393         {
1394             out_names[no_out_args] = "proxy";
1395             out_values[no_out_args++] = odr_strdup(odr, proxy.c_str());
1396         }
1397         if (sptr->cfSubDB.length())
1398         {
1399             out_names[no_out_args] = "subdatabase";
1400             out_values[no_out_args++] = odr_strdup(odr, sptr->cfSubDB.c_str());
1401         }
1402         if (param_nocproxy)
1403         {
1404             out_names[no_out_args] = "nocproxy";
1405             out_values[no_out_args++] = odr_strdup(odr, param_nocproxy);
1406         }
1407     }
1408     else
1409     {
1410         size_t found = authentication.find('/');
1411
1412         if (sptr->sru.length() && found != std::string::npos)
1413         {
1414             b->set_option("user", authentication.substr(0, found));
1415             b->set_option("password", authentication.substr(found+1));
1416         }
1417         else
1418             b->set_option("user", authentication);
1419
1420         if (proxy.length())
1421             b->set_option("proxy", proxy);
1422     }
1423     std::string url(sptr->target);
1424     if (sptr->sru.length())
1425     {
1426         b->set_option("sru", sptr->sru);
1427         if (url.find("://") == std::string::npos)
1428             url = "http://" + url;
1429         if (sptr->sru_version.length())
1430             b->set_option("sru_version", sptr->sru_version);
1431     }
1432     if (no_out_args)
1433     {
1434         char *x_args = 0;
1435         out_names[no_out_args] = 0; // terminate list
1436
1437         yaz_array_to_uri(&x_args, odr, (char **) out_names,
1438                          (char **) out_values);
1439         url += "," + std::string(x_args);
1440     }
1441     package.log("zoom", YLOG_LOG, "url: %s", url.c_str());
1442     b->connect(url, error, addinfo, odr);
1443     if (*error == 0 && b->enable_cproxy)
1444         create_content_session(package, b, error, addinfo, odr,
1445                                content_authentication.length() ?
1446                                content_authentication : authentication,
1447                                content_proxy.length() ? content_proxy : proxy,
1448                                realm);
1449     if (*error == 0)
1450         m_backend = b;
1451     return b;
1452 }
1453
1454 void yf::Zoom::Frontend::prepare_elements(BackendPtr b,
1455                                           Odr_oid *preferredRecordSyntax,
1456                                           const char *element_set_name,
1457                                           bool &enable_pz2_retrieval,
1458                                           bool &enable_pz2_transform,
1459                                           bool &enable_record_transform,
1460                                           bool &assume_marc8_charset)
1461 {
1462     char oid_name_str[OID_STR_MAX];
1463     const char *syntax_name = 0;
1464
1465     if (preferredRecordSyntax &&
1466         !oid_oidcmp(preferredRecordSyntax, yaz_oid_recsyn_xml))
1467     {
1468         if (element_set_name &&
1469             !strcmp(element_set_name, m_p->element_transform.c_str()))
1470         {
1471             enable_pz2_retrieval = true;
1472             enable_pz2_transform = true;
1473         }
1474         else if (element_set_name &&
1475                  !strcmp(element_set_name, m_p->element_raw.c_str()))
1476         {
1477             enable_pz2_retrieval = true;
1478         }
1479         else if (m_p->record_xsp)
1480         {
1481             enable_pz2_retrieval = true;
1482             enable_pz2_transform = true;
1483             enable_record_transform = true;
1484         }
1485     }
1486
1487     if (enable_pz2_retrieval)
1488     {
1489         std::string configured_request_syntax = b->sptr->request_syntax;
1490         if (configured_request_syntax.length())
1491         {
1492             syntax_name = configured_request_syntax.c_str();
1493             const Odr_oid *syntax_oid =
1494                 yaz_string_to_oid(yaz_oid_std(), CLASS_RECSYN, syntax_name);
1495             if (!oid_oidcmp(syntax_oid, yaz_oid_recsyn_usmarc)
1496                 || !oid_oidcmp(syntax_oid, yaz_oid_recsyn_opac))
1497                 assume_marc8_charset = true;
1498         }
1499     }
1500     else if (preferredRecordSyntax)
1501         syntax_name =
1502             yaz_oid_to_string_buf(preferredRecordSyntax, 0, oid_name_str);
1503
1504     if (b->sptr->sru.length())
1505         syntax_name = "XML";
1506
1507     b->set_option("preferredRecordSyntax", syntax_name);
1508
1509     if (enable_pz2_retrieval)
1510     {
1511         if (element_set_name && !strcmp(element_set_name,
1512                                         m_p->element_passthru.c_str()))
1513             ;
1514         else
1515         {
1516             element_set_name = 0;
1517             if (b->sptr->element_set.length())
1518                 element_set_name = b->sptr->element_set.c_str();
1519         }
1520     }
1521
1522     b->set_option("elementSetName", element_set_name);
1523     if (b->sptr->sru.length() && element_set_name)
1524         b->set_option("schema", element_set_name);
1525 }
1526
1527 Z_Records *yf::Zoom::Frontend::get_explain_records(
1528     mp::Package &package,
1529     Odr_int start,
1530     Odr_int number_to_present,
1531     int *error,
1532     char **addinfo,
1533     Odr_int *number_of_records_returned,
1534     ODR odr,
1535     BackendPtr b,
1536     Odr_oid *preferredRecordSyntax,
1537     const char *element_set_name)
1538 {
1539     Odr_int i;
1540     Z_Records *records = 0;
1541
1542     if (!b->explain_doc)
1543     {
1544         return records;
1545     }
1546     if (number_to_present > 10000)
1547         number_to_present = 10000;
1548
1549     xmlNode *ptr = xmlDocGetRootElement(b->explain_doc);
1550
1551     Z_NamePlusRecordList *npl = (Z_NamePlusRecordList *)
1552         odr_malloc(odr, sizeof(*npl));
1553     npl->records = (Z_NamePlusRecord **)
1554         odr_malloc(odr, number_to_present * sizeof(*npl->records));
1555
1556     for (i = 0; i < number_to_present; i++)
1557     {
1558         int num = 0;
1559         xmlNode *res = xml_node_search(ptr, &num, start + i + 1);
1560         if (!res)
1561             break;
1562         xmlBufferPtr xml_buf = xmlBufferCreate();
1563         xmlNode *tmp_node = xmlCopyNode(res->children, 1);
1564         xmlNodeDump(xml_buf, tmp_node->doc, tmp_node, 0, 0);
1565
1566         Z_NamePlusRecord *npr =
1567             (Z_NamePlusRecord *) odr_malloc(odr, sizeof(*npr));
1568         npr->databaseName = odr_strdup(odr, b->m_frontend_database.c_str());
1569         npr->which = Z_NamePlusRecord_databaseRecord;
1570         npr->u.databaseRecord =
1571             z_ext_record_xml(odr,
1572                              (const char *) xml_buf->content, xml_buf->use);
1573         npl->records[i] = npr;
1574         xmlFreeNode(tmp_node);
1575         xmlBufferFree(xml_buf);
1576     }
1577     records = (Z_Records*) odr_malloc(odr, sizeof(*records));
1578     records->which = Z_Records_DBOSD;
1579     records->u.databaseOrSurDiagnostics = npl;
1580
1581     npl->num_records = i;
1582     *number_of_records_returned = i;
1583     return records;
1584 }
1585
1586
1587 Z_Records *yf::Zoom::Frontend::get_records(mp::Package &package,
1588                                            Odr_int start,
1589                                            Odr_int number_to_present,
1590                                            int *error,
1591                                            char **addinfo,
1592                                            Odr_int *number_of_records_returned,
1593                                            ODR odr,
1594                                            BackendPtr b,
1595                                            Odr_oid *preferredRecordSyntax,
1596                                            const char *element_set_name)
1597 {
1598     *number_of_records_returned = 0;
1599     Z_Records *records = 0;
1600     bool enable_pz2_retrieval = false; // whether target profile is used
1601     bool enable_pz2_transform = false; // whether XSLT is used as well
1602     bool assume_marc8_charset = false;
1603     bool enable_record_transform = false;
1604
1605     prepare_elements(b, preferredRecordSyntax,
1606                      element_set_name,
1607                      enable_pz2_retrieval,
1608                      enable_pz2_transform,
1609                      enable_record_transform,
1610                      assume_marc8_charset);
1611
1612     package.log("zoom", YLOG_LOG, "pz2_retrieval: %s . pz2_transform: %s",
1613                 enable_pz2_retrieval ? "yes" : "no",
1614                 enable_pz2_transform ? "yes" : "no");
1615
1616     if (start < 0 || number_to_present <=0)
1617         return records;
1618
1619     if (number_to_present > 10000)
1620         number_to_present = 10000;
1621
1622     ZOOM_record *recs = (ZOOM_record *)
1623         odr_malloc(odr, (size_t) number_to_present * sizeof(*recs));
1624
1625     b->present(start, number_to_present, recs, error, addinfo, odr);
1626
1627     int i = 0;
1628     if (!*error)
1629     {
1630         for (i = 0; i < number_to_present; i++)
1631             if (!recs[i])
1632                 break;
1633     }
1634     if (i > 0)
1635     {  // only return records if no error and at least one record
1636
1637         const char *xsl_parms[3];
1638         mp::wrbuf cproxy_host;
1639
1640         if (b->enable_cproxy && b->cproxy_host.length())
1641         {
1642             wrbuf_puts(cproxy_host, "\"");
1643             wrbuf_puts(cproxy_host, b->cproxy_host.c_str());
1644             wrbuf_puts(cproxy_host, "/\"");
1645
1646             xsl_parms[0] = "cproxyhost";
1647             xsl_parms[1] = wrbuf_cstr(cproxy_host);
1648             xsl_parms[2] = 0;
1649         }
1650         else
1651         {
1652             xsl_parms[0] = 0;
1653         }
1654
1655         char *odr_database = odr_strdup(odr,
1656                                         b->m_frontend_database.c_str());
1657         Z_NamePlusRecordList *npl = (Z_NamePlusRecordList *)
1658             odr_malloc(odr, sizeof(*npl));
1659         *number_of_records_returned = i;
1660         npl->num_records = i;
1661         npl->records = (Z_NamePlusRecord **)
1662             odr_malloc(odr, i * sizeof(*npl->records));
1663         for (i = 0; i < number_to_present; i++)
1664         {
1665             Z_NamePlusRecord *npr = 0;
1666             const char *addinfo;
1667
1668             int sur_error = ZOOM_record_error(recs[i], 0 /* msg */,
1669                                               &addinfo, 0 /* diagset */);
1670
1671             if (sur_error)
1672             {
1673                 log_diagnostic(package, sur_error, addinfo);
1674                 npr = zget_surrogateDiagRec(odr, odr_database, sur_error,
1675                                             addinfo);
1676             }
1677             else if (enable_pz2_retrieval)
1678             {
1679                 char rec_type_str[100];
1680                 const char *record_encoding = 0;
1681
1682                 if (b->sptr->record_encoding.length())
1683                     record_encoding = b->sptr->record_encoding.c_str();
1684                 else if (assume_marc8_charset)
1685                     record_encoding = "marc8";
1686
1687                 strcpy(rec_type_str, b->sptr->use_turbomarc ? "txml" : "xml");
1688                 if (record_encoding)
1689                 {
1690                     strcat(rec_type_str, "; charset=");
1691                     strcat(rec_type_str, record_encoding);
1692                 }
1693
1694                 package.log("zoom", YLOG_LOG, "Getting record of type %s",
1695                             rec_type_str);
1696                 int rec_len;
1697                 xmlChar *xmlrec_buf = 0;
1698                 const char *rec_buf = ZOOM_record_get(recs[i], rec_type_str,
1699                                                       &rec_len);
1700                 if (!rec_buf && !npr)
1701                 {
1702                     std::string addinfo("ZOOM_record_get failed for type ");
1703
1704                     int error = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1705                     addinfo += rec_type_str;
1706                     log_diagnostic(package, error, addinfo.c_str());
1707                     npr = zget_surrogateDiagRec(odr, odr_database,
1708                                                 error, addinfo.c_str());
1709                 }
1710                 else
1711                 {
1712                     package.log_write(rec_buf, rec_len);
1713                     package.log_write("\r\n", 2);
1714                 }
1715
1716                 if (rec_buf && b->xsp && enable_pz2_transform)
1717                 {
1718                     xmlDoc *rec_doc = xmlParseMemory(rec_buf, rec_len);
1719                     if (!rec_doc)
1720                     {
1721                         const char *addinfo = "xml parse failed for record";
1722                         int error = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1723                         log_diagnostic(package, error, addinfo);
1724                         npr = zget_surrogateDiagRec(
1725                             odr, odr_database, error, addinfo);
1726                     }
1727                     else
1728                     {
1729                         // first stage XSLT - per target
1730                         xsltStylesheetPtr xsp = b->xsp;
1731                         xmlDoc *rec_res = xsltApplyStylesheet(xsp, rec_doc,
1732                                                               xsl_parms);
1733                         // insert generated-url
1734                         if (rec_res)
1735                         {
1736                             std::string res =
1737                                 mp::xml::url_recipe_handle(rec_res,
1738                                                            b->sptr->urlRecipe);
1739                             if (res.length())
1740                             {
1741                                 xmlNode *ptr = xmlDocGetRootElement(rec_res);
1742                                 while (ptr && ptr->type != XML_ELEMENT_NODE)
1743                                     ptr = ptr->next;
1744                                 xmlNode *c =
1745                                     xmlNewChild(ptr, 0, BAD_CAST "metadata", 0);
1746                                 xmlNewProp(c, BAD_CAST "type", BAD_CAST
1747                                            "generated-url");
1748                                 xmlNode * t = xmlNewText(BAD_CAST res.c_str());
1749                                 xmlAddChild(c, t);
1750                             }
1751                         }
1752                         // second stage XSLT - common
1753                         if (rec_res && m_p->record_xsp &&
1754                             enable_record_transform)
1755                         {
1756                             xmlDoc *tmp_doc = rec_res;
1757
1758                             xsp = m_p->record_xsp;
1759                             rec_res = xsltApplyStylesheet(xsp, tmp_doc,
1760                                                           xsl_parms);
1761                             xmlFreeDoc(tmp_doc);
1762                         }
1763                         // get result out of it
1764                         if (rec_res)
1765                         {
1766                             xsltSaveResultToString(&xmlrec_buf, &rec_len,
1767                                                    rec_res, xsp);
1768                             rec_buf = (const char *) xmlrec_buf;
1769                             package.log_write(rec_buf, rec_len);
1770
1771                             xmlFreeDoc(rec_res);
1772                         }
1773                         if (!rec_buf)
1774                         {
1775                             std::string addinfo;
1776                             int error =
1777                                 YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1778
1779                             addinfo = "xslt apply failed for "
1780                                 + b->sptr->transform_xsl_fname;
1781                             log_diagnostic(package, error, addinfo.c_str());
1782                             npr = zget_surrogateDiagRec(
1783                                 odr, odr_database, error, addinfo.c_str());
1784                         }
1785                         xmlFreeDoc(rec_doc);
1786                     }
1787                 }
1788
1789                 if (!npr)
1790                 {
1791                     if (!rec_buf)
1792                         npr = zget_surrogateDiagRec(
1793                             odr, odr_database,
1794                             YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
1795                             rec_type_str);
1796                     else
1797                     {
1798                         npr = (Z_NamePlusRecord *)
1799                             odr_malloc(odr, sizeof(*npr));
1800                         npr->databaseName = odr_database;
1801                         npr->which = Z_NamePlusRecord_databaseRecord;
1802                         npr->u.databaseRecord =
1803                             z_ext_record_xml(odr, rec_buf, rec_len);
1804                     }
1805                 }
1806                 if (xmlrec_buf)
1807                     xmlFree(xmlrec_buf);
1808             }
1809             else
1810             {
1811                 Z_External *ext =
1812                     (Z_External *) ZOOM_record_get(recs[i], "ext", 0);
1813                 if (ext)
1814                 {
1815                     npr = (Z_NamePlusRecord *) odr_malloc(odr, sizeof(*npr));
1816                     npr->databaseName = odr_database;
1817                     npr->which = Z_NamePlusRecord_databaseRecord;
1818                     npr->u.databaseRecord = ext;
1819                 }
1820                 else
1821                 {
1822                     npr = zget_surrogateDiagRec(
1823                         odr, odr_database,
1824                         YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
1825                         "ZOOM_record, type ext");
1826                 }
1827             }
1828             npl->records[i] = npr;
1829         }
1830         records = (Z_Records*) odr_malloc(odr, sizeof(*records));
1831         records->which = Z_Records_DBOSD;
1832         records->u.databaseOrSurDiagnostics = npl;
1833     }
1834     return records;
1835 }
1836
1837 struct cql_node *yf::Zoom::Impl::convert_cql_fields(struct cql_node *cn,
1838                                                     ODR odr)
1839 {
1840     struct cql_node *r = 0;
1841     if (!cn)
1842         return 0;
1843     switch (cn->which)
1844     {
1845     case CQL_NODE_ST:
1846         if (cn->u.st.index)
1847         {
1848             std::map<std::string,std::string>::const_iterator it;
1849             it = fieldmap.find(cn->u.st.index);
1850             if (it == fieldmap.end())
1851                 return cn;
1852             if (it->second.length())
1853                 cn->u.st.index = odr_strdup(odr, it->second.c_str());
1854             else
1855                 cn->u.st.index = 0;
1856         }
1857         break;
1858     case CQL_NODE_BOOL:
1859         r = convert_cql_fields(cn->u.boolean.left, odr);
1860         if (!r)
1861             r = convert_cql_fields(cn->u.boolean.right, odr);
1862         break;
1863     case CQL_NODE_SORT:
1864         r = convert_cql_fields(cn->u.sort.search, odr);
1865         break;
1866     }
1867     return r;
1868 }
1869
1870 void yf::Zoom::Frontend::log_diagnostic(mp::Package &package,
1871                                         int error, const char *addinfo)
1872 {
1873     const char *err_msg = yaz_diag_bib1_str(error);
1874     if (addinfo)
1875         package.log("zoom", YLOG_WARN, "Diagnostic %d %s: %s",
1876                     error, err_msg, addinfo);
1877     else
1878         package.log("zoom", YLOG_WARN, "Diagnostic %d %s:",
1879                     error, err_msg);
1880 }
1881
1882 yf::Zoom::BackendPtr yf::Zoom::Frontend::explain_search(mp::Package &package,
1883                                                         std::string &database,
1884                                                         int *error,
1885                                                         char **addinfo,
1886                                                         mp::odr &odr,
1887                                                         std::string torus_url,
1888                                                         std::string &torus_db,
1889                                                         std::string &realm)
1890 {
1891     m_backend.reset();
1892
1893     BackendPtr b(new Backend);
1894
1895     b->m_frontend_database = database;
1896     b->enable_explain = true;
1897
1898     Z_GDU *gdu = package.request().get();
1899     Z_APDU *apdu_req = gdu->u.z3950;
1900     Z_SearchRequest *sr = apdu_req->u.searchRequest;
1901     Z_Query *query = sr->query;
1902
1903     if (!m_p->explain_xsp)
1904     {
1905         *error = YAZ_BIB1_UNSPECIFIED_ERROR;
1906         *addinfo =
1907             odr_strdup(odr, "IR-Explain---1 unsupported. "
1908                        "Torus explain_xsl not defined");
1909         return m_backend;
1910     }
1911     else if (query->which == Z_Query_type_104 &&
1912         query->u.type_104->which == Z_External_CQL)
1913     {
1914         std::string torus_query(query->u.type_104->u.cql);
1915         xmlDoc *doc = mp::get_searchable(package, torus_url, "",
1916                                          torus_query,
1917                                          realm, m_p->proxy);
1918         if (m_p->explain_xsp)
1919         {
1920             xmlDoc *rec_res =  xsltApplyStylesheet(m_p->explain_xsp, doc, 0);
1921
1922             xmlFreeDoc(doc);
1923             doc = rec_res;
1924         }
1925         if (!doc)
1926         {
1927             *error = YAZ_BIB1_UNSPECIFIED_ERROR;
1928             *addinfo = odr_strdup(odr, "Torus server unavailable or "
1929                                   "incorrectly configured");
1930         }
1931         else
1932         {
1933             xmlNode *ptr = xmlDocGetRootElement(doc);
1934             int hits = 0;
1935
1936             xml_node_search(ptr, &hits, 0);
1937
1938             Z_APDU *apdu_res = odr.create_searchResponse(apdu_req, 0, 0);
1939             apdu_res->u.searchResponse->resultCount = odr_intdup(odr, hits);
1940             package.response() = apdu_res;
1941             m_backend = b;
1942         }
1943         if (b->explain_doc)
1944             xmlFreeDoc(b->explain_doc);
1945         b->explain_doc = doc;
1946         return m_backend;
1947     }
1948     else
1949     {
1950         *error = YAZ_BIB1_QUERY_TYPE_UNSUPP;
1951         *addinfo = odr_strdup(odr, "IR-Explain---1 only supports CQL");
1952         return m_backend;
1953     }
1954 }
1955
1956 static bool wait_conn(COMSTACK cs, int secs)
1957 {
1958     struct yaz_poll_fd pfd;
1959
1960     yaz_poll_add(pfd.input_mask, yaz_poll_except);
1961     if (cs->io_pending && CS_WANT_WRITE)
1962         yaz_poll_add(pfd.input_mask, yaz_poll_write);
1963     if (cs->io_pending & CS_WANT_READ)
1964         yaz_poll_add(pfd.input_mask, yaz_poll_read);
1965
1966     pfd.fd = cs_fileno(cs);
1967     pfd.client_data = 0;
1968
1969     int ret = yaz_poll(&pfd, 1, secs, 0);
1970     return ret > 0;
1971 }
1972
1973 bool yf::Zoom::Impl::check_proxy(const char *proxy)
1974 {
1975     COMSTACK conn = 0;
1976     const char *uri = "http://localhost/";
1977     void *add;
1978     mp::odr odr;
1979     bool outcome = false;
1980     conn = cs_create_host_proxy(uri, 0, &add, proxy);
1981
1982     if (!conn)
1983         return false;
1984
1985     Z_GDU *gdu = z_get_HTTP_Request_uri(odr, uri, 0, 1);
1986     gdu->u.HTTP_Request->method = odr_strdup(odr, "GET");
1987
1988     if (z_GDU(odr, &gdu, 0, 0))
1989     {
1990         int len;
1991         char *buf = odr_getbuf(odr, &len, 0);
1992
1993         int ret = cs_connect(conn, add);
1994         if (ret > 0 || (ret == 0 && wait_conn(conn, 1)))
1995         {
1996             while (1)
1997             {
1998                 ret = cs_put(conn, buf, len);
1999                 if (ret != 1)
2000                     break;
2001                 if (!wait_conn(conn, proxy_timeout))
2002                     break;
2003             }
2004             if (ret == 0)
2005                 outcome = true;
2006         }
2007     }
2008     cs_close(conn);
2009     return outcome;
2010 }
2011
2012 bool yf::Zoom::Frontend::retry(mp::Package &package,
2013                                mp::odr &odr,
2014                                BackendPtr b,
2015                                int &error, char **addinfo,
2016                                int &proxy_step, int &same_retries,
2017                                int &proxy_retries)
2018 {
2019     if (b && b->m_proxy.length() && !m_p->check_proxy(b->m_proxy.c_str()))
2020     {
2021         log_diagnostic(package, error, *addinfo);
2022         package.log("zoom", YLOG_LOG, "proxy %s fails", b->m_proxy.c_str());
2023         m_backend.reset();
2024         if (proxy_step) // there is a failover
2025         {
2026             proxy_retries++;
2027             package.log("zoom", YLOG_WARN, "search failed: trying next proxy");
2028             return true;
2029         }
2030         error = YAZ_BIB1_PROXY_FAILURE;
2031         *addinfo = odr_strdup(odr, b->m_proxy.c_str());
2032     }
2033     else if (same_retries == 0 && proxy_retries == 0)
2034     {
2035         log_diagnostic(package, error, *addinfo);
2036         same_retries++;
2037         package.log("zoom", YLOG_WARN, "search failed: retry");
2038         m_backend.reset();
2039         proxy_step = 0;
2040         return true;
2041     }
2042     return false;
2043 }
2044
2045 void yf::Zoom::Frontend::handle_search(mp::Package &package)
2046 {
2047     Z_GDU *gdu = package.request().get();
2048     Z_APDU *apdu_req = gdu->u.z3950;
2049     Z_APDU *apdu_res = 0;
2050     mp::odr odr;
2051     Z_SearchRequest *sr = apdu_req->u.searchRequest;
2052     if (sr->num_databaseNames != 1)
2053     {
2054         int error = YAZ_BIB1_TOO_MANY_DATABASES_SPECIFIED;
2055         log_diagnostic(package, error, 0);
2056         apdu_res = odr.create_searchResponse(apdu_req, error, 0);
2057         package.response() = apdu_res;
2058         return;
2059     }
2060     int proxy_step = 0;
2061     int same_retries = 0;
2062     int proxy_retries = 0;
2063
2064 next_proxy:
2065
2066     int error = 0;
2067     char *addinfo = 0;
2068     std::string db(sr->databaseNames[0]);
2069
2070     BackendPtr b = get_backend_from_databases(package, db, &error,
2071                                               &addinfo, odr, &proxy_step);
2072     if (error)
2073     {
2074         if (retry(package, odr, b, error, &addinfo, proxy_step,
2075                   same_retries, proxy_retries))
2076             goto next_proxy;
2077     }
2078     if (error)
2079     {
2080         log_diagnostic(package, error, addinfo);
2081         apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
2082         package.response() = apdu_res;
2083         return;
2084     }
2085     if (!b || b->enable_explain)
2086         return;
2087
2088     b->set_option("setname", "default");
2089
2090     bool enable_pz2_retrieval = false;
2091     bool enable_pz2_transform = false;
2092     bool enable_record_transform = false;
2093     bool assume_marc8_charset = false;
2094     prepare_elements(b, sr->preferredRecordSyntax, 0 /*element_set_name */,
2095                      enable_pz2_retrieval,
2096                      enable_pz2_transform,
2097                      enable_record_transform,
2098                      assume_marc8_charset);
2099
2100     Odr_int hits = 0;
2101     Z_Query *query = sr->query;
2102     mp::wrbuf ccl_wrbuf;
2103     mp::wrbuf pqf_wrbuf;
2104     std::string sortkeys;
2105
2106     if (query->which == Z_Query_type_1 || query->which == Z_Query_type_101)
2107     {
2108         // RPN
2109         yaz_rpnquery_to_wrbuf(pqf_wrbuf, query->u.type_1);
2110     }
2111     else if (query->which == Z_Query_type_2)
2112     {
2113         // CCL
2114         wrbuf_write(ccl_wrbuf, (const char *) query->u.type_2->buf,
2115                     query->u.type_2->len);
2116     }
2117     else if (query->which == Z_Query_type_104 &&
2118              query->u.type_104->which == Z_External_CQL)
2119     {
2120         // CQL
2121         const char *cql = query->u.type_104->u.cql;
2122         CQL_parser cp = cql_parser_create();
2123         int r = cql_parser_string(cp, cql);
2124         package.log("zoom", YLOG_LOG, "CQL: %s", cql);
2125         if (r)
2126         {
2127             cql_parser_destroy(cp);
2128             error = YAZ_BIB1_MALFORMED_QUERY;
2129             const char *addinfo = "CQL syntax error";
2130             log_diagnostic(package, error, addinfo);
2131             apdu_res =
2132                 odr.create_searchResponse(apdu_req, error, addinfo);
2133             package.response() = apdu_res;
2134             return;
2135         }
2136         struct cql_node *cn = cql_parser_result(cp);
2137         struct cql_node *cn_error = m_p->convert_cql_fields(cn, odr);
2138         if (cn_error)
2139         {
2140             // hopefully we are getting a ptr to a index+relation+term node
2141             error = YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
2142             addinfo = 0;
2143             if (cn_error->which == CQL_NODE_ST)
2144                 addinfo = cn_error->u.st.index;
2145
2146             log_diagnostic(package, error, addinfo);
2147             apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
2148             package.response() = apdu_res;
2149             cql_parser_destroy(cp);
2150             return;
2151         }
2152         r = cql_to_ccl(cn, wrbuf_vp_puts,  ccl_wrbuf);
2153         if (r)
2154         {
2155             error = YAZ_BIB1_MALFORMED_QUERY;
2156             const char *addinfo = "CQL to CCL conversion error";
2157
2158             log_diagnostic(package, error, addinfo);
2159             apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
2160             package.response() = apdu_res;
2161             cql_parser_destroy(cp);
2162             return;
2163         }
2164
2165         mp::wrbuf sru_sortkeys_wrbuf;
2166         if (cql_sortby_to_sortkeys(cn, wrbuf_vp_puts, sru_sortkeys_wrbuf))
2167         {
2168             error = YAZ_BIB1_ILLEGAL_SORT_RELATION;
2169             const char *addinfo = "CQL to CCL sortby conversion";
2170
2171             log_diagnostic(package, error, addinfo);
2172             apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
2173             package.response() = apdu_res;
2174             cql_parser_destroy(cp);
2175             return;
2176         }
2177         mp::wrbuf sort_spec_wrbuf;
2178         yaz_srw_sortkeys_to_sort_spec(wrbuf_cstr(sru_sortkeys_wrbuf),
2179                                       sort_spec_wrbuf);
2180         yaz_tok_cfg_t tc = yaz_tok_cfg_create();
2181         yaz_tok_parse_t tp =
2182             yaz_tok_parse_buf(tc, wrbuf_cstr(sort_spec_wrbuf));
2183         yaz_tok_cfg_destroy(tc);
2184
2185         /* go through sortspec and map fields */
2186         int token = yaz_tok_move(tp);
2187         while (token != YAZ_TOK_EOF)
2188         {
2189             if (token == YAZ_TOK_STRING)
2190             {
2191                 const char *field = yaz_tok_parse_string(tp);
2192                 std::map<std::string,std::string>::iterator it;
2193                 it = b->sptr->sortmap.find(field);
2194                 if (it != b->sptr->sortmap.end())
2195                     sortkeys += it->second;
2196                 else
2197                     sortkeys += field;
2198             }
2199             sortkeys += " ";
2200             token = yaz_tok_move(tp);
2201             if (token == YAZ_TOK_STRING)
2202             {
2203                 sortkeys += yaz_tok_parse_string(tp);
2204             }
2205             if (token != YAZ_TOK_EOF)
2206             {
2207                 sortkeys += " ";
2208                 token = yaz_tok_move(tp);
2209             }
2210         }
2211         yaz_tok_parse_destroy(tp);
2212         cql_parser_destroy(cp);
2213     }
2214     else
2215     {
2216         error = YAZ_BIB1_QUERY_TYPE_UNSUPP;
2217         const char *addinfo = 0;
2218         log_diagnostic(package, error, addinfo);
2219         apdu_res =  odr.create_searchResponse(apdu_req, error, addinfo);
2220         package.response() = apdu_res;
2221         return;
2222     }
2223
2224     if (ccl_wrbuf.len())
2225     {
2226         // CCL to PQF
2227         assert(pqf_wrbuf.len() == 0);
2228         int cerror, cpos;
2229         struct ccl_rpn_node *cn;
2230         package.log("zoom", YLOG_LOG, "CCL: %s", wrbuf_cstr(ccl_wrbuf));
2231         cn = ccl_find_str(b->sptr->ccl_bibset, wrbuf_cstr(ccl_wrbuf),
2232                           &cerror, &cpos);
2233         if (!cn)
2234         {
2235             char *addinfo = odr_strdup_null(odr, ccl_err_msg(cerror));
2236             error = YAZ_BIB1_MALFORMED_QUERY;
2237
2238             switch (cerror)
2239             {
2240             case CCL_ERR_UNKNOWN_QUAL:
2241             case CCL_ERR_TRUNC_NOT_LEFT:
2242             case CCL_ERR_TRUNC_NOT_RIGHT:
2243             case CCL_ERR_TRUNC_NOT_BOTH:
2244 #ifdef CCL_ERR_TRUNC_NOT_EMBED
2245             case CCL_ERR_TRUNC_NOT_EMBED:
2246 #endif
2247 #ifdef CCL_ERR_TRUNC_NOT_SINGLE
2248             case CCL_ERR_TRUNC_NOT_SINGLE:
2249 #endif
2250                 error = YAZ_BIB1_UNSUPP_SEARCH;
2251                 break;
2252             }
2253             log_diagnostic(package, error, addinfo);
2254             apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
2255             package.response() = apdu_res;
2256             return;
2257         }
2258         ccl_pquery(pqf_wrbuf, cn);
2259         package.log("zoom", YLOG_LOG, "RPN: %s", wrbuf_cstr(pqf_wrbuf));
2260         ccl_rpn_delete(cn);
2261     }
2262
2263     assert(pqf_wrbuf.len());
2264
2265     ZOOM_query q = ZOOM_query_create();
2266     ZOOM_query_sortby2(q, b->sptr->sortStrategy.c_str(), sortkeys.c_str());
2267
2268     if (b->get_option("sru"))
2269     {
2270         int status = 0;
2271         Z_RPNQuery *zquery;
2272         zquery = p_query_rpn(odr, wrbuf_cstr(pqf_wrbuf));
2273         mp::wrbuf wrb;
2274
2275         if (!strcmp(b->get_option("sru"), "solr"))
2276         {
2277             solr_transform_t cqlt = solr_transform_create();
2278
2279             status = solr_transform_rpn2solr_wrbuf(cqlt, wrb, zquery);
2280
2281             solr_transform_close(cqlt);
2282         }
2283         else
2284         {
2285             status = cql_transform_rpn2cql_wrbuf(b->cqlt, wrb, zquery);
2286         }
2287         if (status == 0)
2288         {
2289             ZOOM_query_cql(q, wrbuf_cstr(wrb));
2290             package.log("zoom", YLOG_LOG, "CQL: %s", wrbuf_cstr(wrb));
2291             b->search(q, &hits, &error, &addinfo, odr);
2292         }
2293         ZOOM_query_destroy(q);
2294
2295         if (status)
2296         {
2297             error = YAZ_BIB1_MALFORMED_QUERY;
2298             const char *addinfo = "can not convert from RPN to CQL/Solr";
2299             log_diagnostic(package, error, addinfo);
2300             apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
2301             package.response() = apdu_res;
2302             return;
2303         }
2304     }
2305     else
2306     {
2307         ZOOM_query_prefix(q, wrbuf_cstr(pqf_wrbuf));
2308         package.log("zoom", YLOG_LOG, "search PQF: %s", wrbuf_cstr(pqf_wrbuf));
2309         b->search(q, &hits, &error, &addinfo, odr);
2310         ZOOM_query_destroy(q);
2311     }
2312
2313     if (error)
2314     {
2315         if (retry(package, odr, b, error, &addinfo, proxy_step,
2316                   same_retries, proxy_retries))
2317             goto next_proxy;
2318     }
2319
2320     const char *element_set_name = 0;
2321     Odr_int number_to_present = 0;
2322     if (!error)
2323         mp::util::piggyback_sr(sr, hits, number_to_present, &element_set_name);
2324
2325     Odr_int number_of_records_returned = 0;
2326     Z_Records *records = get_records(
2327         package,
2328         0, number_to_present, &error, &addinfo,
2329         &number_of_records_returned, odr, b, sr->preferredRecordSyntax,
2330         element_set_name);
2331     if (error)
2332         log_diagnostic(package, error, addinfo);
2333     apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
2334     if (records)
2335     {
2336         apdu_res->u.searchResponse->records = records;
2337         apdu_res->u.searchResponse->numberOfRecordsReturned =
2338             odr_intdup(odr, number_of_records_returned);
2339     }
2340     apdu_res->u.searchResponse->resultCount = odr_intdup(odr, hits);
2341     package.response() = apdu_res;
2342 }
2343
2344 void yf::Zoom::Frontend::handle_present(mp::Package &package)
2345 {
2346     Z_GDU *gdu = package.request().get();
2347     Z_APDU *apdu_req = gdu->u.z3950;
2348     Z_APDU *apdu_res = 0;
2349     Z_PresentRequest *pr = apdu_req->u.presentRequest;
2350
2351     mp::odr odr;
2352     if (!m_backend)
2353     {
2354         package.response() = odr.create_presentResponse(
2355             apdu_req, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST, 0);
2356         return;
2357     }
2358     const char *element_set_name = 0;
2359     Z_RecordComposition *comp = pr->recordComposition;
2360     if (comp && comp->which != Z_RecordComp_simple)
2361     {
2362         package.response() = odr.create_presentResponse(
2363             apdu_req,
2364             YAZ_BIB1_PRESENT_COMP_SPEC_PARAMETER_UNSUPP, 0);
2365         return;
2366     }
2367     if (comp && comp->u.simple->which == Z_ElementSetNames_generic)
2368         element_set_name = comp->u.simple->u.generic;
2369     Odr_int number_of_records_returned = 0;
2370     int error = 0;
2371     char *addinfo = 0;
2372
2373     if (m_backend->enable_explain)
2374     {
2375         Z_Records *records =
2376             get_explain_records(
2377                 package,
2378                 *pr->resultSetStartPoint - 1, *pr->numberOfRecordsRequested,
2379                 &error, &addinfo, &number_of_records_returned, odr, m_backend,
2380                 pr->preferredRecordSyntax, element_set_name);
2381
2382         apdu_res = odr.create_presentResponse(apdu_req, error, addinfo);
2383         if (records)
2384         {
2385             apdu_res->u.presentResponse->records = records;
2386             apdu_res->u.presentResponse->numberOfRecordsReturned =
2387                 odr_intdup(odr, number_of_records_returned);
2388         }
2389         package.response() = apdu_res;
2390     }
2391     else
2392     {
2393         Z_Records *records =
2394             get_records(package,
2395                         *pr->resultSetStartPoint - 1, *pr->numberOfRecordsRequested,
2396                         &error, &addinfo, &number_of_records_returned, odr, m_backend,
2397                         pr->preferredRecordSyntax, element_set_name);
2398
2399         apdu_res = odr.create_presentResponse(apdu_req, error, addinfo);
2400         if (records)
2401         {
2402             apdu_res->u.presentResponse->records = records;
2403             apdu_res->u.presentResponse->numberOfRecordsReturned =
2404                 odr_intdup(odr, number_of_records_returned);
2405         }
2406         package.response() = apdu_res;
2407     }
2408 }
2409
2410 void yf::Zoom::Frontend::handle_package(mp::Package &package)
2411 {
2412     Z_GDU *gdu = package.request().get();
2413     if (!gdu)
2414         ;
2415     else if (gdu->which == Z_GDU_Z3950)
2416     {
2417         Z_APDU *apdu_req = gdu->u.z3950;
2418
2419         if (m_backend)
2420             wrbuf_rewind(m_backend->m_apdu_wrbuf);
2421         if (apdu_req->which == Z_APDU_initRequest)
2422         {
2423             mp::odr odr;
2424             package.response() = odr.create_close(
2425                 apdu_req,
2426                 Z_Close_protocolError,
2427                 "double init");
2428         }
2429         else if (apdu_req->which == Z_APDU_searchRequest)
2430         {
2431             handle_search(package);
2432         }
2433         else if (apdu_req->which == Z_APDU_presentRequest)
2434         {
2435             handle_present(package);
2436         }
2437         else
2438         {
2439             mp::odr odr;
2440             package.response() = odr.create_close(
2441                 apdu_req,
2442                 Z_Close_protocolError,
2443                 "zoom filter cannot handle this APDU");
2444             package.session().close();
2445         }
2446         if (m_backend)
2447         {
2448             WRBUF w = m_backend->m_apdu_wrbuf;
2449             package.log_write(wrbuf_buf(w), wrbuf_len(w));
2450         }
2451     }
2452     else
2453     {
2454         package.session().close();
2455     }
2456 }
2457
2458 std::string escape_cql_term(std::string inp)
2459 {
2460     std::string res;
2461     size_t l = inp.length();
2462     size_t i;
2463     for (i = 0; i < l; i++)
2464     {
2465         if (strchr("*?^\"", inp[i]))
2466             res += "\\";
2467         res += inp[i];
2468     }
2469     return res;
2470 }
2471
2472 void yf::Zoom::Frontend::auth(mp::Package &package, Z_InitRequest *req,
2473                               int *error, char **addinfo, ODR odr)
2474 {
2475     if (m_p->torus_auth_url.length() == 0)
2476         return;
2477
2478     std::string user;
2479     std::string password;
2480     if (req->idAuthentication)
2481     {
2482         Z_IdAuthentication *auth = req->idAuthentication;
2483         switch (auth->which)
2484         {
2485         case Z_IdAuthentication_open:
2486             if (auth->u.open)
2487             {
2488                 const char *cp = strchr(auth->u.open, '/');
2489                 if (cp)
2490                 {
2491                     user.assign(auth->u.open, cp - auth->u.open);
2492                     password.assign(cp + 1);
2493                 }
2494             }
2495             break;
2496         case Z_IdAuthentication_idPass:
2497             if (auth->u.idPass->userId)
2498                 user.assign(auth->u.idPass->userId);
2499             if (auth->u.idPass->password)
2500                 password.assign(auth->u.idPass->password);
2501             break;
2502         }
2503     }
2504
2505     std::string ip = package.origin().get_address();
2506     yaz_log(YLOG_LOG, "IP=%s", ip.c_str());
2507
2508     std::string torus_query;
2509     int failure_code;
2510
2511     if (user.length() && password.length())
2512     {
2513         torus_query = "userName==\"" + escape_cql_term(user) +
2514             "\" and password==\"" + escape_cql_term(password) + "\"";
2515         failure_code = YAZ_BIB1_INIT_AC_BAD_USERID_AND_OR_PASSWORD;
2516     }
2517     else
2518     {
2519         const char *ip_cstr = ip.c_str();
2520         const char *cp = strchr(ip_cstr, ':');
2521         if (cp)
2522             ip_cstr = cp + 1;
2523
2524         torus_query = "ip encloses/net.ipaddress \"";
2525         torus_query += escape_cql_term(std::string(ip_cstr));
2526         torus_query += "\"";
2527         failure_code = YAZ_BIB1_INIT_AC_BLOCKED_NETWORK_ADDRESS;
2528     }
2529
2530     std::string dummy_db;
2531     std::string dummy_realm;
2532     xmlDoc *doc = mp::get_searchable(package, m_p->torus_auth_url, dummy_db,
2533                                      torus_query, dummy_realm, m_p->proxy);
2534     if (!doc)
2535     {
2536         // something fundamental broken in lookup.
2537         *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
2538         *addinfo = odr_strdup(odr, "zoom: torus server unavailable or "
2539                               "incorrectly configured.");
2540         return;
2541     }
2542     const xmlNode *ptr = xmlDocGetRootElement(doc);
2543     if (ptr && ptr->type == XML_ELEMENT_NODE)
2544     {
2545         if (strcmp((const char *) ptr->name, "records") == 0)
2546         {
2547             ptr = ptr->children;
2548             while (ptr && ptr->type != XML_ELEMENT_NODE)
2549                 ptr = ptr->next;
2550         }
2551         if (ptr && strcmp((const char *) ptr->name, "record") == 0)
2552         {
2553             ptr = ptr->children;
2554             while (ptr && ptr->type != XML_ELEMENT_NODE)
2555                 ptr = ptr->next;
2556         }
2557         if (ptr && strcmp((const char *) ptr->name, "layer") == 0)
2558         {
2559             ptr = ptr->children;
2560             while (ptr && ptr->type != XML_ELEMENT_NODE)
2561                 ptr = ptr->next;
2562         }
2563         while (ptr)
2564         {
2565             if (ptr && ptr->type == XML_ELEMENT_NODE &&
2566                 !strcmp((const char *) ptr->name, "identityId"))
2567                 break;
2568             ptr = ptr->next;
2569         }
2570     }
2571     if (!ptr)
2572     {
2573         *error = failure_code;
2574         return;
2575     }
2576     session_realm = mp::xml::get_text(ptr);
2577 }
2578
2579 void yf::Zoom::Impl::process(mp::Package &package)
2580 {
2581     FrontendPtr f = get_frontend(package);
2582     Z_GDU *gdu = package.request().get();
2583
2584     if (f->m_is_virtual)
2585     {
2586         f->handle_package(package);
2587     }
2588     else if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
2589              Z_APDU_initRequest)
2590     {
2591         Z_InitRequest *req = gdu->u.z3950->u.initRequest;
2592         f->m_init_gdu = gdu;
2593
2594         mp::odr odr;
2595         Z_APDU *apdu = odr.create_initResponse(gdu->u.z3950, 0, 0);
2596         Z_InitResponse *resp = apdu->u.initResponse;
2597
2598         int i;
2599         static const int masks[] = {
2600             Z_Options_search,
2601             Z_Options_present,
2602             -1
2603         };
2604         for (i = 0; masks[i] != -1; i++)
2605             if (ODR_MASK_GET(req->options, masks[i]))
2606                 ODR_MASK_SET(resp->options, masks[i]);
2607
2608         static const int versions[] = {
2609             Z_ProtocolVersion_1,
2610             Z_ProtocolVersion_2,
2611             Z_ProtocolVersion_3,
2612             -1
2613         };
2614         for (i = 0; versions[i] != -1; i++)
2615             if (ODR_MASK_GET(req->protocolVersion, versions[i]))
2616                 ODR_MASK_SET(resp->protocolVersion, versions[i]);
2617             else
2618                 break;
2619
2620         *resp->preferredMessageSize = *req->preferredMessageSize;
2621         *resp->maximumRecordSize = *req->maximumRecordSize;
2622
2623         int error = 0;
2624         char *addinfo = 0;
2625         f->auth(package, req, &error, &addinfo, odr);
2626         if (error)
2627         {
2628             resp->userInformationField =
2629                 zget_init_diagnostics(odr, error, addinfo);
2630             *resp->result = 0;
2631             package.session().close();
2632         }
2633         else
2634             f->m_is_virtual = true;
2635         package.response() = apdu;
2636     }
2637     else
2638         package.move();
2639
2640     release_frontend(package);
2641 }
2642
2643
2644 static mp::filter::Base* filter_creator()
2645 {
2646     return new mp::filter::Zoom;
2647 }
2648
2649 extern "C" {
2650     struct metaproxy_1_filter_struct metaproxy_1_filter_zoom = {
2651         0,
2652         "zoom",
2653         filter_creator
2654     };
2655 }
2656
2657
2658 /*
2659  * Local variables:
2660  * c-basic-offset: 4
2661  * c-file-style: "Stroustrup"
2662  * indent-tabs-mode: nil
2663  * End:
2664  * vim: shiftwidth=4 tabstop=8 expandtab
2665  */
2666