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