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