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