dc7d574418d81878e8f39ad5158b77043ba47a96
[metaproxy-moved-to-github.git] / src / filter_zoom.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2011 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 #include "filter_zoom.hpp"
21 #include <yaz/zoom.h>
22 #include <yaz/yaz-version.h>
23 #include <yaz/tpath.h>
24 #include <yaz/srw.h>
25 #include <metaproxy/package.hpp>
26 #include <metaproxy/util.hpp>
27 #include <metaproxy/xmlutil.hpp>
28 #include "torus.hpp"
29
30 #include <libxslt/xsltutils.h>
31 #include <libxslt/transform.h>
32
33 #include <boost/thread/mutex.hpp>
34 #include <boost/thread/condition.hpp>
35 #include <yaz/ccl_xml.h>
36 #include <yaz/ccl.h>
37 #include <yaz/rpn2cql.h>
38 #include <yaz/rpn2solr.h>
39 #include <yaz/pquery.h>
40 #include <yaz/cql.h>
41 #include <yaz/oid_db.h>
42 #include <yaz/diagbib1.h>
43 #include <yaz/log.h>
44 #include <yaz/zgdu.h>
45 #include <yaz/querytowrbuf.h>
46
47 namespace mp = metaproxy_1;
48 namespace yf = mp::filter;
49
50 namespace metaproxy_1 {
51     namespace filter {
52         struct Zoom::Searchable : boost::noncopyable {
53             std::string authentication;
54             std::string cfAuth;
55             std::string cfProxy;
56             std::string cfSubDb;
57             std::string udb;
58             std::string target;
59             std::string query_encoding;
60             std::string sru;
61             std::string request_syntax;
62             std::string element_set;
63             std::string record_encoding;
64             std::string transform_xsl_fname;
65             std::string urlRecipe;
66             std::string contentConnector;
67             bool use_turbomarc;
68             bool piggyback;
69             CCL_bibset ccl_bibset;
70             Searchable(CCL_bibset base);
71             ~Searchable();
72         };
73         class Zoom::Backend : boost::noncopyable {
74             friend class Impl;
75             friend class Frontend;
76             std::string zurl;
77             ZOOM_connection m_connection;
78             ZOOM_resultset m_resultset;
79             std::string m_frontend_database;
80             SearchablePtr sptr;
81             xsltStylesheetPtr xsp;
82             std::string content_session_id;
83         public:
84             Backend(SearchablePtr sptr);
85             ~Backend();
86             void connect(std::string zurl, int *error, char **addinfo,
87                          ODR odr);
88             void search_pqf(const char *pqf, Odr_int *hits,
89                             int *error, char **addinfo, ODR odr);
90             void search_cql(const char *cql, Odr_int *hits,
91                             int *error, char **addinfo, ODR odr);
92             void present(Odr_int start, Odr_int number, ZOOM_record *recs,
93                          int *error, char **addinfo, ODR odr);
94             void set_option(const char *name, const char *value);
95             const char *get_option(const char *name);
96             void get_zoom_error(int *error, char **addinfo, ODR odr);
97         };
98         class Zoom::Frontend : boost::noncopyable {
99             friend class Impl;
100             Impl *m_p;
101             bool m_is_virtual;
102             bool m_in_use;
103             yazpp_1::GDU m_init_gdu;
104             BackendPtr m_backend;
105             void handle_package(mp::Package &package);
106             void handle_search(mp::Package &package);
107             void handle_present(mp::Package &package);
108             BackendPtr get_backend_from_databases(std::string &database,
109                                                   int *error,
110                                                   char **addinfo,
111                                                   ODR odr);
112             Z_Records *get_records(Odr_int start,
113                                    Odr_int number_to_present,
114                                    int *error,
115                                    char **addinfo,
116                                    Odr_int *number_of_records_returned,
117                                    ODR odr, BackendPtr b,
118                                    Odr_oid *preferredRecordSyntax,
119                                    const char *element_set_name);
120         public:
121             Frontend(Impl *impl);
122             ~Frontend();
123         };
124         class Zoom::Impl {
125             friend class Frontend;
126         public:
127             Impl();
128             ~Impl();
129             void process(metaproxy_1::Package & package);
130             void configure(const xmlNode * ptr, bool test_only,
131                            const char *path);
132         private:
133             void configure_local_records(const xmlNode * ptr, bool test_only);
134             FrontendPtr get_frontend(mp::Package &package);
135             void release_frontend(mp::Package &package);
136             SearchablePtr parse_torus_record(const xmlNode *ptr);
137             struct cql_node *convert_cql_fields(struct cql_node *cn, ODR odr);
138             std::map<mp::Session, FrontendPtr> m_clients;            
139             boost::mutex m_mutex;
140             boost::condition m_cond_session_ready;
141             std::string torus_url;
142             std::map<std::string,std::string> fieldmap;
143             std::string xsldir;
144             std::string file_path;
145             std::string content_proxy_server;
146             std::string content_tmp_file;
147             CCL_bibset bibset;
148             std::string element_transform;
149             std::string element_raw;
150             std::map<std::string,SearchablePtr> s_map;
151         };
152     }
153 }
154
155 // define Pimpl wrapper forwarding to Impl
156  
157 yf::Zoom::Zoom() : m_p(new Impl)
158 {
159 }
160
161 yf::Zoom::~Zoom()
162 {  // must have a destructor because of boost::scoped_ptr
163 }
164
165 void yf::Zoom::configure(const xmlNode *xmlnode, bool test_only,
166                          const char *path)
167 {
168     m_p->configure(xmlnode, test_only, path);
169 }
170
171 void yf::Zoom::process(mp::Package &package) const
172 {
173     m_p->process(package);
174 }
175
176
177 // define Implementation stuff
178
179 yf::Zoom::Backend::Backend(SearchablePtr ptr) : sptr(ptr)
180 {
181     m_connection = ZOOM_connection_create(0);
182     m_resultset = 0;
183     xsp = 0;
184 }
185
186 yf::Zoom::Backend::~Backend()
187 {
188     if (xsp)
189         xsltFreeStylesheet(xsp);
190     ZOOM_connection_destroy(m_connection);
191     ZOOM_resultset_destroy(m_resultset);
192 }
193
194
195 void yf::Zoom::Backend::get_zoom_error(int *error, char **addinfo,
196                                        ODR odr)
197 {
198     const char *msg = 0;
199     const char *zoom_addinfo = 0;
200     *error = ZOOM_connection_error(m_connection, &msg, &zoom_addinfo);
201     if (*error)
202     {
203         if (*error >= ZOOM_ERROR_CONNECT)
204         {
205             // turn ZOOM diagnostic into a Bib-1 2: with addinfo=zoom err msg
206             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
207             *addinfo = (char *) odr_malloc(
208                 odr, 20 + strlen(msg) + 
209                 (zoom_addinfo ? strlen(zoom_addinfo) : 0));
210             strcpy(*addinfo, msg);
211             if (zoom_addinfo)
212             {
213                 strcat(*addinfo, ": ");
214                 strcat(*addinfo, zoom_addinfo);
215                 strcat(*addinfo, " ");
216             }
217         }
218         else
219         {
220             *addinfo = (char *) odr_malloc(
221                 odr, 20 + (zoom_addinfo ? strlen(zoom_addinfo) : 0));
222             **addinfo = '\0';
223             if (zoom_addinfo && *zoom_addinfo)
224             {
225                 strcpy(*addinfo, zoom_addinfo);
226                 strcat(*addinfo, " ");
227             }
228             strcat(*addinfo, "(backend)");
229         }
230     }
231 }
232
233 void yf::Zoom::Backend::connect(std::string zurl,
234                                 int *error, char **addinfo,
235                                 ODR odr)
236 {
237     ZOOM_connection_connect(m_connection, zurl.c_str(), 0);
238     get_zoom_error(error, addinfo, odr);
239 }
240
241 void yf::Zoom::Backend::search_pqf(const char *pqf, Odr_int *hits,
242                                    int *error, char **addinfo, ODR odr)
243 {
244     m_resultset = ZOOM_connection_search_pqf(m_connection, pqf);
245     get_zoom_error(error, addinfo, odr);
246     if (*error == 0)
247         *hits = ZOOM_resultset_size(m_resultset);
248     else
249         *hits = 0;
250 }
251
252 void yf::Zoom::Backend::search_cql(const char *cql, Odr_int *hits,
253                                    int *error, char **addinfo, ODR odr)
254 {
255     ZOOM_query q = ZOOM_query_create();
256
257     ZOOM_query_cql(q, cql);
258
259     m_resultset = ZOOM_connection_search(m_connection, q);
260     ZOOM_query_destroy(q);
261     get_zoom_error(error, addinfo, odr);
262     if (*error == 0)
263         *hits = ZOOM_resultset_size(m_resultset);
264     else
265         *hits = 0;
266 }
267
268 void yf::Zoom::Backend::present(Odr_int start, Odr_int number,
269                                 ZOOM_record *recs,
270                                 int *error, char **addinfo, ODR odr)
271 {
272     ZOOM_resultset_records(m_resultset, recs, start, number);
273     get_zoom_error(error, addinfo, odr);
274 }
275
276 void yf::Zoom::Backend::set_option(const char *name, const char *value)
277 {
278     ZOOM_connection_option_set(m_connection, name, value);
279     if (m_resultset)
280         ZOOM_resultset_option_set(m_resultset, name, value);
281 }
282
283 const char *yf::Zoom::Backend::get_option(const char *name)
284 {
285     return ZOOM_connection_option_get(m_connection, name);
286 }
287
288 yf::Zoom::Searchable::Searchable(CCL_bibset base)
289 {
290     piggyback = true;
291     use_turbomarc = true;
292     ccl_bibset = ccl_qual_dup(base);
293 }
294
295 yf::Zoom::Searchable::~Searchable()
296 {
297     ccl_qual_rm(&ccl_bibset);
298 }
299
300 yf::Zoom::Frontend::Frontend(Impl *impl) : 
301     m_p(impl), m_is_virtual(false), m_in_use(true)
302 {
303 }
304
305 yf::Zoom::Frontend::~Frontend()
306 {
307 }
308
309 yf::Zoom::FrontendPtr yf::Zoom::Impl::get_frontend(mp::Package &package)
310 {
311     boost::mutex::scoped_lock lock(m_mutex);
312
313     std::map<mp::Session,yf::Zoom::FrontendPtr>::iterator it;
314     
315     while(true)
316     {
317         it = m_clients.find(package.session());
318         if (it == m_clients.end())
319             break;
320         
321         if (!it->second->m_in_use)
322         {
323             it->second->m_in_use = true;
324             return it->second;
325         }
326         m_cond_session_ready.wait(lock);
327     }
328     FrontendPtr f(new Frontend(this));
329     m_clients[package.session()] = f;
330     f->m_in_use = true;
331     return f;
332 }
333
334 void yf::Zoom::Impl::release_frontend(mp::Package &package)
335 {
336     boost::mutex::scoped_lock lock(m_mutex);
337     std::map<mp::Session,yf::Zoom::FrontendPtr>::iterator it;
338     
339     it = m_clients.find(package.session());
340     if (it != m_clients.end())
341     {
342         if (package.session().is_closed())
343         {
344             m_clients.erase(it);
345         }
346         else
347         {
348             it->second->m_in_use = false;
349         }
350         m_cond_session_ready.notify_all();
351     }
352 }
353
354 yf::Zoom::Impl::Impl() : element_transform("pz2") , element_raw("raw")
355 {
356     bibset = ccl_qual_mk();
357 }
358
359 yf::Zoom::Impl::~Impl()
360
361     ccl_qual_rm(&bibset);
362 }
363
364 yf::Zoom::SearchablePtr yf::Zoom::Impl::parse_torus_record(const xmlNode *ptr)
365 {
366     Zoom::SearchablePtr s(new Searchable(bibset));
367     
368     for (ptr = ptr->children; ptr; ptr = ptr->next)
369     {
370         if (ptr->type != XML_ELEMENT_NODE)
371             continue;
372         if (!strcmp((const char *) ptr->name, "layer"))
373             ptr = ptr->children;
374         else if (!strcmp((const char *) ptr->name,
375                          "authentication"))
376         {
377             s->authentication = mp::xml::get_text(ptr);
378         }
379         else if (!strcmp((const char *) ptr->name,
380                          "cfAuth"))
381         {
382             s->cfAuth = mp::xml::get_text(ptr);
383         } 
384         else if (!strcmp((const char *) ptr->name,
385                          "cfProxy"))
386         {
387             s->cfProxy = mp::xml::get_text(ptr);
388         }  
389         else if (!strcmp((const char *) ptr->name,
390                          "cfSubDb"))
391         {
392             s->cfSubDb = mp::xml::get_text(ptr);
393         }  
394         else if (!strcmp((const char *) ptr->name,
395                          "contentConnector"))
396         {
397             s->contentConnector = mp::xml::get_text(ptr);
398         }  
399         else if (!strcmp((const char *) ptr->name, "udb"))
400         {
401             s->udb = mp::xml::get_text(ptr);
402         }
403         else if (!strcmp((const char *) ptr->name, "zurl"))
404         {
405             s->target = mp::xml::get_text(ptr);
406         }
407         else if (!strcmp((const char *) ptr->name, "sru"))
408         {
409             s->sru = mp::xml::get_text(ptr);
410         }
411         else if (!strcmp((const char *) ptr->name,
412                          "queryEncoding"))
413         {
414             s->query_encoding = mp::xml::get_text(ptr);
415         }
416         else if (!strcmp((const char *) ptr->name,
417                          "piggyback"))
418         {
419             s->piggyback = mp::xml::get_bool(ptr, true);
420         }
421         else if (!strcmp((const char *) ptr->name,
422                          "requestSyntax"))
423         {
424             s->request_syntax = mp::xml::get_text(ptr);
425         }
426         else if (!strcmp((const char *) ptr->name,
427                          "elementSet"))
428         {
429             s->element_set = mp::xml::get_text(ptr);
430         }
431         else if (!strcmp((const char *) ptr->name,
432                          "recordEncoding"))
433         {
434             s->record_encoding = mp::xml::get_text(ptr);
435         }
436         else if (!strcmp((const char *) ptr->name,
437                          "transform"))
438         {
439             s->transform_xsl_fname = mp::xml::get_text(ptr);
440         }
441         else if (!strcmp((const char *) ptr->name,
442                          "urlRecipe"))
443         {
444             s->urlRecipe = mp::xml::get_text(ptr);
445         }
446         else if (!strcmp((const char *) ptr->name,
447                          "useTurboMarc"))
448         {
449             ; // useTurboMarc is ignored
450         }
451         else if (!strncmp((const char *) ptr->name,
452                           "cclmap_", 7))
453         {
454             std::string value = mp::xml::get_text(ptr);
455             ccl_qual_fitem(s->ccl_bibset, value.c_str(),
456                            (const char *) ptr->name + 7);
457         }
458     }
459     return s;
460 }
461
462 void yf::Zoom::Impl::configure_local_records(const xmlNode *ptr, bool test_only)
463 {
464     while (ptr && ptr->type != XML_ELEMENT_NODE)
465         ptr = ptr->next;
466     
467     if (ptr)
468     {
469         if (!strcmp((const char *) ptr->name, "records"))
470         {
471             for (ptr = ptr->children; ptr; ptr = ptr->next)
472             {
473                 if (ptr->type != XML_ELEMENT_NODE)
474                     continue;
475                 if (!strcmp((const char *) ptr->name, "record"))
476                 {
477                     SearchablePtr s = parse_torus_record(ptr);
478                     if (s)
479                     {
480                         std::string udb = s->udb;
481                         if (udb.length())
482                             s_map[s->udb] = s;
483                         else
484                         {
485                             throw mp::filter::FilterException
486                                 ("No udb for local torus record");
487                         }
488                     }
489                 }
490                 else
491                 {
492                     throw mp::filter::FilterException
493                         ("Bad element " 
494                          + std::string((const char *) ptr->name)
495                          + " in zoom filter inside element "
496                          "<torus><records>");
497                 }
498             }
499         }
500         else
501         {
502             throw mp::filter::FilterException
503                 ("Bad element " 
504                  + std::string((const char *) ptr->name)
505                  + " in zoom filter inside element <torus>");
506         }
507     }
508 }
509
510 void yf::Zoom::Impl::configure(const xmlNode *ptr, bool test_only,
511                                const char *path)
512 {
513     content_tmp_file = "/tmp/mp_content_proxy.";
514     if (path && *path)
515     {
516         file_path = path;
517     }
518     for (ptr = ptr->children; ptr; ptr = ptr->next)
519     {
520         if (ptr->type != XML_ELEMENT_NODE)
521             continue;
522         else if (!strcmp((const char *) ptr->name, "torus"))
523         {
524             const struct _xmlAttr *attr;
525             for (attr = ptr->properties; attr; attr = attr->next)
526             {
527                 if (!strcmp((const char *) attr->name, "url"))
528                     torus_url = mp::xml::get_text(attr->children);
529                 else if (!strcmp((const char *) attr->name, "xsldir"))
530                     xsldir = mp::xml::get_text(attr->children);
531                 else if (!strcmp((const char *) attr->name, "element_transform"))
532                     element_transform = mp::xml::get_text(attr->children);
533                 else if (!strcmp((const char *) attr->name, "element_raw"))
534                     element_raw = mp::xml::get_text(attr->children);
535                 else
536                     throw mp::filter::FilterException(
537                         "Bad attribute " + std::string((const char *)
538                                                        attr->name));
539             }
540             configure_local_records(ptr->children, test_only);
541         }
542         else if (!strcmp((const char *) ptr->name, "cclmap"))
543         {
544             const char *addinfo = 0;
545             ccl_xml_config(bibset, ptr, &addinfo);
546         }
547         else if (!strcmp((const char *) ptr->name, "fieldmap"))
548         {
549             const struct _xmlAttr *attr;
550             std::string ccl_field;
551             std::string cql_field;
552             for (attr = ptr->properties; attr; attr = attr->next)
553             {
554                 if (!strcmp((const char *) attr->name, "ccl"))
555                     ccl_field = mp::xml::get_text(attr->children);
556                 else if (!strcmp((const char *) attr->name, "cql"))
557                     cql_field = mp::xml::get_text(attr->children);
558                 else
559                     throw mp::filter::FilterException(
560                         "Bad attribute " + std::string((const char *)
561                                                        attr->name));
562             }
563             if (cql_field.length())
564                 fieldmap[cql_field] = ccl_field;
565         }
566         else if (!strcmp((const char *) ptr->name, "contentProxy"))
567         {
568             const struct _xmlAttr *attr;
569             for (attr = ptr->properties; attr; attr = attr->next)
570             {
571                 if (!strcmp((const char *) attr->name, "server"))
572                     content_proxy_server = mp::xml::get_text(attr->children);
573                 else if (!strcmp((const char *) attr->name, "tmp_file"))
574                     content_tmp_file = mp::xml::get_text(attr->children);
575                 else
576                     throw mp::filter::FilterException(
577                         "Bad attribute " + std::string((const char *)
578                                                        attr->name));
579             }
580         }
581         else
582         {
583             throw mp::filter::FilterException
584                 ("Bad element " 
585                  + std::string((const char *) ptr->name)
586                  + " in zoom filter");
587         }
588     }
589 }
590
591 yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases(
592     std::string &database, int *error, char **addinfo, ODR odr)
593 {
594     std::list<BackendPtr>::const_iterator map_it;
595     if (m_backend && m_backend->m_frontend_database == database)
596         return m_backend;
597
598     const char *sru_proxy = 0;
599     std::string db_args;
600     std::string torus_db;
601     size_t db_arg_pos = database.find(',');
602     if (db_arg_pos != std::string::npos)
603     {
604         torus_db = database.substr(0, db_arg_pos);
605         db_args = database.substr(db_arg_pos + 1);
606     }
607     else
608         torus_db = database;
609  
610     SearchablePtr sptr;
611
612     std::map<std::string,SearchablePtr>::iterator it;
613     it = m_p->s_map.find(torus_db);
614     if (it != m_p->s_map.end())
615         sptr = it->second;
616     else
617     {
618         xmlDoc *doc = mp::get_searchable(m_p->torus_url, torus_db);
619         if (!doc)
620         {
621             *error = YAZ_BIB1_DATABASE_DOES_NOT_EXIST;
622             *addinfo = odr_strdup(odr, database.c_str());
623             BackendPtr b;
624             return b;
625         }
626         const xmlNode *ptr = xmlDocGetRootElement(doc);
627         if (ptr)
628         {   // presumably ptr is a records element node
629             // parse first record in document
630             for (ptr = ptr->children; ptr; ptr = ptr->next)
631             {
632                 if (ptr->type == XML_ELEMENT_NODE
633                     && !strcmp((const char *) ptr->name, "record"))
634                 {
635                     sptr = m_p->parse_torus_record(ptr);
636                     break;
637                 }
638             }
639         }
640         xmlFreeDoc(doc);
641     }
642
643     if (!sptr)
644     {
645         *error = YAZ_BIB1_DATABASE_DOES_NOT_EXIST;
646         *addinfo = odr_strdup(odr, database.c_str());
647         BackendPtr b;
648         return b;
649     }
650         
651     xsltStylesheetPtr xsp = 0;
652     if (sptr->transform_xsl_fname.length())
653     {
654         const char *path = 0;
655
656         if (m_p->xsldir.length())
657             path = m_p->xsldir.c_str();
658         else
659             path = m_p->file_path.c_str();
660         std::string fname;
661
662         char fullpath[1024];
663         char *cp = yaz_filepath_resolve(sptr->transform_xsl_fname.c_str(),
664                                         path, 0, fullpath);
665         if (cp)
666             fname.assign(cp);
667         else
668         {
669             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
670             *addinfo = (char *)
671                 odr_malloc(odr, 40 + sptr->transform_xsl_fname.length());
672             sprintf(*addinfo, "File could not be read: %s", 
673                     sptr->transform_xsl_fname.c_str());
674             BackendPtr b;
675             return b;
676         }
677         xmlDoc *xsp_doc = xmlParseFile(fname.c_str());
678         if (!xsp_doc)
679         {
680             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
681             *addinfo = (char *) odr_malloc(odr, 40 + fname.length());
682             sprintf(*addinfo, "xmlParseFile failed. File: %s", fname.c_str());
683             BackendPtr b;
684             return b;
685         }
686         xsp = xsltParseStylesheetDoc(xsp_doc);
687         if (!xsp)
688         {
689             *error = YAZ_BIB1_DATABASE_DOES_NOT_EXIST;
690             *addinfo = odr_strdup(odr, "xsltParseStylesheetDoc failed");
691             BackendPtr b;
692             xmlFreeDoc(xsp_doc);
693             return b;
694         }
695     }
696
697     m_backend.reset();
698
699     BackendPtr b(new Backend(sptr));
700
701     b->xsp = xsp;
702     b->m_frontend_database = database;
703     std::string authentication = sptr->authentication;
704         
705     b->set_option("timeout", "40");
706
707     if (sptr->query_encoding.length())
708         b->set_option("rpnCharset", sptr->query_encoding.c_str());
709
710     if (sptr->cfAuth.length())
711     {
712         // A CF target
713         b->set_option("user", sptr->cfAuth.c_str());
714         if (db_args.length() == 0)
715         {
716             if (authentication.length())
717             {
718                 // no database (auth) args specified already.. and the
719                 // Torus authentication has it.. Generate the args that CF
720                 // understands..
721                 size_t found = authentication.find('/');
722                 if (found != std::string::npos)
723                 {
724                     db_args += "user=" + mp::util::uri_encode(authentication.substr(0, found))
725                         + "&password=" + mp::util::uri_encode(authentication.substr(found+1));
726                 }
727                 else
728                     db_args += "user=" + mp::util::uri_encode(authentication);
729             }
730             if (sptr->cfProxy.length())
731             {
732                 if (db_args.length())
733                     db_args += "&";
734                 db_args += "proxy=" + mp::util::uri_encode(sptr->cfProxy);
735             }
736         }
737         if (sptr->cfSubDb.length())
738         {
739             if (db_args.length())
740                 db_args += "&";
741             db_args += "subdatabase=" + mp::util::uri_encode(sptr->cfSubDb);
742         }
743     }
744     else
745     {
746         // A non-CF target
747         if (db_args.length())
748         {
749             // user has specified backend authentication
750             const char *param_user = 0;
751             const char *param_password = 0;
752             char **names;
753             char **values;
754             int i;
755             int no_parms = yaz_uri_to_array(db_args.c_str(),
756                                             odr, &names, &values);
757             for (i = 0; i < no_parms; i++)
758             {
759                 const char *name = names[i];
760                 const char *value = values[i];
761                 if (!strcmp(name, "user"))
762                     param_user = value;
763                 else if (!strcmp(name, "password"))
764                     param_password = value;
765                 else if (!strcmp(name, "proxy"))
766                     sru_proxy = value;
767                 else
768                 {
769                     BackendPtr notfound;
770                     char *msg = (char*) odr_malloc(odr, strlen(name) + 30);
771                     *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
772                     sprintf(msg, "Bad database argument: %s", name);
773                     *addinfo = msg;
774                     return notfound;
775                 }
776             }
777             if (param_user && param_password)
778             {
779                 char *auth = (char*) odr_malloc(
780                     odr, strlen(param_user) + strlen(param_password) + 2);
781                 strcpy(auth, param_user);
782                 strcat(auth, "/");
783                 strcat(auth, param_password);
784                 b->set_option("user", auth);
785             }
786             db_args.clear(); // no arguments to be passed (non-CF)
787         }
788         else
789         {
790             // use authentication from Torus, if given
791             if (authentication.length())
792                 b->set_option("user", authentication.c_str());
793         }
794     }
795
796     if (sru_proxy)
797         b->set_option("proxy", sru_proxy);
798     
799     if (b->sptr->contentConnector.length())
800     {
801         int fd;
802         
803         char *fname = (char *) xmalloc(m_p->content_tmp_file.length() + 8);
804         strcpy(fname, m_p->content_tmp_file.c_str());
805         strcat(fname, "XXXXXX");
806         fd = mkstemp(fname);
807         
808         if (fd == -1)
809         {
810             yaz_log(YLOG_WARN|YLOG_ERRNO, "create %s", fname);
811             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
812             *addinfo = (char *)  odr_malloc(odr, 40 + strlen(fname));
813             sprintf(*addinfo, "Could not create %s", fname);
814
815             xfree(fname);
816             BackendPtr backend_null;
817             return backend_null;
818         }
819         b->content_session_id.assign(fname + (strlen(fname) - 6));
820         char buf[1024];
821         sprintf(buf, "#content_proxy\n");
822         write(fd, buf, strlen(buf));
823         close(fd);
824         yaz_log(YLOG_LOG, "file %s created\n", fname);
825         xfree(fname);
826     }
827     
828
829     std::string url;
830     if (sptr->sru.length())
831     {
832         url = "http://" + sptr->target;
833         b->set_option("sru", sptr->sru.c_str());
834     }
835     else
836     {
837         url = sptr->target;
838     }
839     if (db_args.length())
840         url += "," + db_args;
841     yaz_log(YLOG_LOG, "url=%s", url.c_str());
842     b->connect(url, error, addinfo, odr);
843     if (*error == 0)
844     {
845         m_backend = b;
846     }
847     return b;
848 }
849
850 Z_Records *yf::Zoom::Frontend::get_records(Odr_int start,
851                                            Odr_int number_to_present,
852                                            int *error,
853                                            char **addinfo,
854                                            Odr_int *number_of_records_returned,
855                                            ODR odr,
856                                            BackendPtr b,
857                                            Odr_oid *preferredRecordSyntax,
858                                            const char *element_set_name)
859 {
860     *number_of_records_returned = 0;
861     Z_Records *records = 0;
862     bool enable_pz2_retrieval = false; // whether target profile is used
863     bool enable_pz2_transform = false; // whether XSLT is used as well
864     bool assume_marc8_charset = false;
865
866     if (start < 0 || number_to_present <= 0)
867         return records;
868     
869     if (number_to_present > 10000)
870         number_to_present = 10000;
871     
872     ZOOM_record *recs = (ZOOM_record *)
873         odr_malloc(odr, number_to_present * sizeof(*recs));
874
875     char oid_name_str[OID_STR_MAX];
876     const char *syntax_name = 0;
877     
878     if (preferredRecordSyntax &&
879         !oid_oidcmp(preferredRecordSyntax, yaz_oid_recsyn_xml)
880         && element_set_name)
881     {
882         if (!strcmp(element_set_name, m_p->element_transform.c_str()))
883         {
884             enable_pz2_retrieval = true;
885             enable_pz2_transform = true;
886         }
887         else if (!strcmp(element_set_name, m_p->element_raw.c_str()))
888         {
889             enable_pz2_retrieval = true;
890         }
891     }
892     
893     if (enable_pz2_retrieval)
894     {
895         if (b->sptr->request_syntax.length())
896         {
897             syntax_name = b->sptr->request_syntax.c_str();
898             const Odr_oid *syntax_oid = 
899                 yaz_string_to_oid(yaz_oid_std(), CLASS_RECSYN, syntax_name);
900             if (!oid_oidcmp(syntax_oid, yaz_oid_recsyn_usmarc)
901                 || !oid_oidcmp(syntax_oid, yaz_oid_recsyn_opac))
902                 assume_marc8_charset = true;
903         }
904     }
905     else if (preferredRecordSyntax)
906         syntax_name =
907             yaz_oid_to_string_buf(preferredRecordSyntax, 0, oid_name_str);
908
909     b->set_option("preferredRecordSyntax", syntax_name);
910
911     if (enable_pz2_retrieval)
912     {
913         element_set_name = 0;
914         if (b->sptr->element_set.length())
915             element_set_name = b->sptr->element_set.c_str();
916     }
917
918     b->set_option("elementSetName", element_set_name);
919
920     b->present(start, number_to_present, recs, error, addinfo, odr);
921
922     Odr_int i = 0;
923     if (!*error)
924     {
925         for (i = 0; i < number_to_present; i++)
926             if (!recs[i])
927                 break;
928     }
929     if (i > 0)
930     {  // only return records if no error and at least one record
931         char *odr_database = odr_strdup(odr,
932                                         b->m_frontend_database.c_str());
933         Z_NamePlusRecordList *npl = (Z_NamePlusRecordList *)
934             odr_malloc(odr, sizeof(*npl));
935         *number_of_records_returned = i;
936         npl->num_records = i;
937         npl->records = (Z_NamePlusRecord **)
938             odr_malloc(odr, i * sizeof(*npl->records));
939         for (i = 0; i < number_to_present; i++)
940         {
941             Z_NamePlusRecord *npr = 0;
942             const char *addinfo;
943             int sur_error = ZOOM_record_error(recs[i], 0 /* msg */,
944                                               &addinfo, 0 /* diagset */);
945                 
946             if (sur_error)
947             {
948                 npr = zget_surrogateDiagRec(odr, odr_database, sur_error,
949                                             addinfo);
950             }
951             else if (enable_pz2_retrieval)
952             {
953                 char rec_type_str[100];
954                 const char *record_encoding = 0;
955
956                 if (b->sptr->record_encoding.length())
957                     record_encoding = b->sptr->record_encoding.c_str();
958                 else if (assume_marc8_charset)
959                     record_encoding = "marc8";
960
961                 strcpy(rec_type_str, b->sptr->use_turbomarc ? "txml" : "xml");
962                 if (record_encoding)
963                 {
964                     strcat(rec_type_str, "; charset=");
965                     strcat(rec_type_str, record_encoding);
966                 }
967                 
968                 int rec_len;
969                 xmlChar *xmlrec_buf = 0;
970                 const char *rec_buf = ZOOM_record_get(recs[i], rec_type_str,
971                                                       &rec_len);
972                 if (rec_buf && b->xsp && enable_pz2_transform)
973                 {
974                     xmlDoc *rec_doc = xmlParseMemory(rec_buf, rec_len);
975                     if (rec_doc)
976                     { 
977                         xmlDoc *rec_res;
978                         rec_res = xsltApplyStylesheet(b->xsp, rec_doc, 0);
979
980                         if (rec_res)
981                             xsltSaveResultToString(&xmlrec_buf, &rec_len,
982                                                    rec_res, b->xsp);
983                         rec_buf = (const char *) xmlrec_buf;
984                         xmlFreeDoc(rec_doc);
985                         xmlFreeDoc(rec_res);
986                     }
987                 }
988
989                 if (rec_buf)
990                 {
991                     xmlDoc *doc = xmlParseMemory(rec_buf, rec_len);
992                     std::string res = 
993                         mp::xml::url_recipe_handle(doc, b->sptr->urlRecipe);
994                     if (res.length() && b->content_session_id.length())
995                     {
996                         size_t off = res.find_first_of("://");
997                         if (off != std::string::npos)
998                         {
999                             char tmp[1024];
1000                             long id = 12345;
1001                             sprintf(tmp, "%s.%s/",
1002                                     b->content_session_id.c_str(),
1003                                     m_p->content_proxy_server.c_str());
1004                             res.insert(off + 3, tmp);
1005                         }
1006                     }
1007                     if (res.length())
1008                     {
1009                         xmlNode *ptr = xmlDocGetRootElement(doc);
1010                         while (ptr && ptr->type != XML_ELEMENT_NODE)
1011                             ptr = ptr->next;
1012                         xmlNode *c = 
1013                             xmlNewChild(ptr, 0, BAD_CAST "generated-url", 0);
1014                         xmlNode * t = xmlNewText(BAD_CAST res.c_str());
1015                         xmlAddChild(c, t);
1016
1017                         if (xmlrec_buf)
1018                             xmlFree(xmlrec_buf);
1019
1020                         xmlDocDumpMemory(doc, &xmlrec_buf, &rec_len);
1021                         rec_buf = (const char *) xmlrec_buf;
1022                     }
1023                     xmlFreeDoc(doc);
1024                 }
1025                 if (rec_buf)
1026                 {
1027                     npr = (Z_NamePlusRecord *) odr_malloc(odr, sizeof(*npr));
1028                     npr->databaseName = odr_database;
1029                     npr->which = Z_NamePlusRecord_databaseRecord;
1030                     npr->u.databaseRecord =
1031                         z_ext_record_xml(odr, rec_buf, rec_len);
1032                 }
1033                 else
1034                 {
1035                     npr = zget_surrogateDiagRec(
1036                         odr, odr_database, 
1037                         YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
1038                         rec_type_str);
1039                 }
1040                 if (xmlrec_buf)
1041                     xmlFree(xmlrec_buf);
1042             }
1043             else
1044             {
1045                 Z_External *ext =
1046                     (Z_External *) ZOOM_record_get(recs[i], "ext", 0);
1047                 if (ext)
1048                 {
1049                     npr = (Z_NamePlusRecord *) odr_malloc(odr, sizeof(*npr));
1050                     npr->databaseName = odr_database;
1051                     npr->which = Z_NamePlusRecord_databaseRecord;
1052                     npr->u.databaseRecord = ext;
1053                 }
1054                 else
1055                 {
1056                     npr = zget_surrogateDiagRec(
1057                         odr, odr_database, 
1058                         YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
1059                         "ZOOM_record, type ext");
1060                 }
1061             }
1062             npl->records[i] = npr;
1063         }
1064         records = (Z_Records*) odr_malloc(odr, sizeof(*records));
1065         records->which = Z_Records_DBOSD;
1066         records->u.databaseOrSurDiagnostics = npl;
1067     }
1068     return records;
1069 }
1070     
1071 struct cql_node *yf::Zoom::Impl::convert_cql_fields(struct cql_node *cn,
1072                                                     ODR odr)
1073 {
1074     struct cql_node *r = 0;
1075     if (!cn)
1076         return 0;
1077     switch (cn->which)
1078     {
1079     case CQL_NODE_ST:
1080         if (cn->u.st.index)
1081         {
1082             std::map<std::string,std::string>::const_iterator it;
1083             it = fieldmap.find(cn->u.st.index);
1084             if (it == fieldmap.end())
1085                 return cn;
1086             if (it->second.length())
1087                 cn->u.st.index = odr_strdup(odr, it->second.c_str());
1088             else
1089                 cn->u.st.index = 0;
1090         }
1091         break;
1092     case CQL_NODE_BOOL:
1093         r = convert_cql_fields(cn->u.boolean.left, odr);
1094         if (!r)
1095             r = convert_cql_fields(cn->u.boolean.right, odr);
1096         break;
1097     case CQL_NODE_SORT:
1098         r = convert_cql_fields(cn->u.sort.search, odr);
1099         break;
1100     }
1101     return r;
1102 }
1103
1104 void yf::Zoom::Frontend::handle_search(mp::Package &package)
1105 {
1106     Z_GDU *gdu = package.request().get();
1107     Z_APDU *apdu_req = gdu->u.z3950;
1108     Z_APDU *apdu_res = 0;
1109     mp::odr odr;
1110     Z_SearchRequest *sr = apdu_req->u.searchRequest;
1111     if (sr->num_databaseNames != 1)
1112     {
1113         apdu_res = odr.create_searchResponse(
1114             apdu_req, YAZ_BIB1_TOO_MANY_DATABASES_SPECIFIED, 0);
1115         package.response() = apdu_res;
1116         return;
1117     }
1118
1119     int error = 0;
1120     char *addinfo = 0;
1121     std::string db(sr->databaseNames[0]);
1122     BackendPtr b = get_backend_from_databases(db, &error, &addinfo, odr);
1123     if (error)
1124     {
1125         apdu_res = 
1126             odr.create_searchResponse(apdu_req, error, addinfo);
1127         package.response() = apdu_res;
1128         return;
1129     }
1130
1131     b->set_option("setname", "default");
1132
1133     Odr_int hits = 0;
1134     Z_Query *query = sr->query;
1135     WRBUF ccl_wrbuf = 0;
1136     WRBUF pqf_wrbuf = 0;
1137
1138     if (query->which == Z_Query_type_1 || query->which == Z_Query_type_101)
1139     {
1140         // RPN
1141         pqf_wrbuf = wrbuf_alloc();
1142         yaz_rpnquery_to_wrbuf(pqf_wrbuf, query->u.type_1);
1143     }
1144     else if (query->which == Z_Query_type_2)
1145     {
1146         // CCL
1147         ccl_wrbuf = wrbuf_alloc();
1148         wrbuf_write(ccl_wrbuf, (const char *) query->u.type_2->buf,
1149                     query->u.type_2->len);
1150     }
1151     else if (query->which == Z_Query_type_104 &&
1152              query->u.type_104->which == Z_External_CQL)
1153     {
1154         // CQL
1155         const char *cql = query->u.type_104->u.cql;
1156         CQL_parser cp = cql_parser_create();
1157         int r = cql_parser_string(cp, cql);
1158         if (r)
1159         {
1160             cql_parser_destroy(cp);
1161             apdu_res = 
1162                 odr.create_searchResponse(apdu_req, 
1163                                           YAZ_BIB1_MALFORMED_QUERY,
1164                                           "CQL syntax error");
1165             package.response() = apdu_res;
1166             return;
1167         }
1168         struct cql_node *cn = cql_parser_result(cp);
1169         struct cql_node *cn_error = m_p->convert_cql_fields(cn, odr);
1170         if (cn_error)
1171         {
1172             // hopefully we are getting a ptr to a index+relation+term node
1173             addinfo = 0;
1174             if (cn_error->which == CQL_NODE_ST)
1175                 addinfo = cn_error->u.st.index;
1176
1177             apdu_res = 
1178                 odr.create_searchResponse(apdu_req, 
1179                                           YAZ_BIB1_UNSUPP_USE_ATTRIBUTE,
1180                                           addinfo);
1181             package.response() = apdu_res;
1182             return;
1183         }
1184         char ccl_buf[1024];
1185
1186         r = cql_to_ccl_buf(cn, ccl_buf, sizeof(ccl_buf));
1187         if (r == 0)
1188         {
1189             ccl_wrbuf = wrbuf_alloc();
1190             wrbuf_puts(ccl_wrbuf, ccl_buf);
1191         }
1192         cql_parser_destroy(cp);
1193         if (r)
1194         {
1195             apdu_res = 
1196                 odr.create_searchResponse(apdu_req, 
1197                                           YAZ_BIB1_MALFORMED_QUERY,
1198                                           "CQL to CCL conversion error");
1199             package.response() = apdu_res;
1200             return;
1201         }
1202     }
1203     else
1204     {
1205         apdu_res = 
1206             odr.create_searchResponse(apdu_req, YAZ_BIB1_QUERY_TYPE_UNSUPP, 0);
1207         package.response() = apdu_res;
1208         return;
1209     }
1210
1211     if (ccl_wrbuf)
1212     {
1213         // CCL to PQF
1214         assert(pqf_wrbuf == 0);
1215         int cerror, cpos;
1216         struct ccl_rpn_node *cn;
1217         yaz_log(YLOG_LOG, "CCL: %s", wrbuf_cstr(ccl_wrbuf));
1218         cn = ccl_find_str(b->sptr->ccl_bibset, wrbuf_cstr(ccl_wrbuf),
1219                           &cerror, &cpos);
1220         wrbuf_destroy(ccl_wrbuf);
1221         if (!cn)
1222         {
1223             char *addinfo = odr_strdup(odr, ccl_err_msg(cerror));
1224             int z3950_diag = YAZ_BIB1_MALFORMED_QUERY;
1225
1226             switch (cerror)
1227             {
1228             case CCL_ERR_UNKNOWN_QUAL:
1229                 z3950_diag = YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
1230                 break;
1231             case CCL_ERR_TRUNC_NOT_LEFT: 
1232             case CCL_ERR_TRUNC_NOT_RIGHT:
1233             case CCL_ERR_TRUNC_NOT_BOTH:
1234                 z3950_diag = YAZ_BIB1_UNSUPP_TRUNCATION_ATTRIBUTE;
1235                 break;
1236             }
1237             apdu_res = 
1238                 odr.create_searchResponse(apdu_req, z3950_diag, addinfo);
1239             package.response() = apdu_res;
1240             return;
1241         }
1242         pqf_wrbuf = wrbuf_alloc();
1243         ccl_pquery(pqf_wrbuf, cn);
1244         ccl_rpn_delete(cn);
1245     }
1246     
1247     assert(pqf_wrbuf);
1248     if (b->get_option("sru"))
1249     {
1250         int status = 0;
1251         Z_RPNQuery *zquery;
1252         zquery = p_query_rpn(odr, wrbuf_cstr(pqf_wrbuf));
1253         WRBUF wrb = wrbuf_alloc();
1254             
1255         if (!strcmp(b->get_option("sru"), "solr"))
1256         {
1257             solr_transform_t cqlt = solr_transform_create();
1258             
1259             status = solr_transform_rpn2solr_wrbuf(cqlt, wrb, zquery);
1260             
1261             solr_transform_close(cqlt);
1262         }
1263         else
1264         {
1265             cql_transform_t cqlt = cql_transform_create();
1266             
1267             status = cql_transform_rpn2cql_wrbuf(cqlt, wrb, zquery);
1268             
1269             cql_transform_close(cqlt);
1270         }
1271         if (status == 0)
1272         {
1273             yaz_log(YLOG_LOG, "search CQL: %s", wrbuf_cstr(wrb));
1274             b->search_cql(wrbuf_cstr(wrb), &hits, &error, &addinfo, odr);
1275         }
1276         
1277         wrbuf_destroy(wrb);
1278         wrbuf_destroy(pqf_wrbuf);
1279         if (status)
1280         {
1281             apdu_res = 
1282                 odr.create_searchResponse(apdu_req, YAZ_BIB1_MALFORMED_QUERY,
1283                                           "can not convert from RPN to CQL/SOLR");
1284             package.response() = apdu_res;
1285             return;
1286         }
1287     }
1288     else
1289     {
1290         yaz_log(YLOG_LOG, "search PQF: %s", wrbuf_cstr(pqf_wrbuf));
1291         b->search_pqf(wrbuf_cstr(pqf_wrbuf), &hits, &error, &addinfo, odr);
1292         wrbuf_destroy(pqf_wrbuf);
1293     }
1294     
1295     
1296     const char *element_set_name = 0;
1297     Odr_int number_to_present = 0;
1298     if (!error)
1299         mp::util::piggyback_sr(sr, hits, number_to_present, &element_set_name);
1300     
1301     Odr_int number_of_records_returned = 0;
1302     Z_Records *records = get_records(
1303         0, number_to_present, &error, &addinfo,
1304         &number_of_records_returned, odr, b, sr->preferredRecordSyntax,
1305         element_set_name);
1306     apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
1307     if (records)
1308     {
1309         apdu_res->u.searchResponse->records = records;
1310         apdu_res->u.searchResponse->numberOfRecordsReturned =
1311             odr_intdup(odr, number_of_records_returned);
1312     }
1313     apdu_res->u.searchResponse->resultCount = odr_intdup(odr, hits);
1314     package.response() = apdu_res;
1315 }
1316
1317 void yf::Zoom::Frontend::handle_present(mp::Package &package)
1318 {
1319     Z_GDU *gdu = package.request().get();
1320     Z_APDU *apdu_req = gdu->u.z3950;
1321     Z_APDU *apdu_res = 0;
1322     Z_PresentRequest *pr = apdu_req->u.presentRequest;
1323
1324     mp::odr odr;
1325     if (!m_backend)
1326     {
1327         package.response() = odr.create_presentResponse(
1328             apdu_req, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST, 0);
1329         return;
1330     }
1331     const char *element_set_name = 0;
1332     Z_RecordComposition *comp = pr->recordComposition;
1333     if (comp && comp->which != Z_RecordComp_simple)
1334     {
1335         package.response() = odr.create_presentResponse(
1336             apdu_req, 
1337             YAZ_BIB1_PRESENT_COMP_SPEC_PARAMETER_UNSUPP, 0);
1338         return;
1339     }
1340     if (comp && comp->u.simple->which == Z_ElementSetNames_generic)
1341         element_set_name = comp->u.simple->u.generic;
1342     Odr_int number_of_records_returned = 0;
1343     int error = 0;
1344     char *addinfo = 0;
1345     Z_Records *records = get_records(
1346         *pr->resultSetStartPoint - 1, *pr->numberOfRecordsRequested,
1347         &error, &addinfo, &number_of_records_returned, odr, m_backend,
1348         pr->preferredRecordSyntax, element_set_name);
1349
1350     apdu_res = odr.create_presentResponse(apdu_req, error, addinfo);
1351     if (records)
1352     {
1353         apdu_res->u.presentResponse->records = records;
1354         apdu_res->u.presentResponse->numberOfRecordsReturned =
1355             odr_intdup(odr, number_of_records_returned);
1356     }
1357     package.response() = apdu_res;
1358 }
1359
1360 void yf::Zoom::Frontend::handle_package(mp::Package &package)
1361 {
1362     Z_GDU *gdu = package.request().get();
1363     if (!gdu)
1364         ;
1365     else if (gdu->which == Z_GDU_Z3950)
1366     {
1367         Z_APDU *apdu_req = gdu->u.z3950;
1368         if (apdu_req->which == Z_APDU_initRequest)
1369         {
1370             mp::odr odr;
1371             package.response() = odr.create_close(
1372                 apdu_req,
1373                 Z_Close_protocolError,
1374                 "double init");
1375         }
1376         else if (apdu_req->which == Z_APDU_searchRequest)
1377         {
1378             handle_search(package);
1379         }
1380         else if (apdu_req->which == Z_APDU_presentRequest)
1381         {
1382             handle_present(package);
1383         }
1384         else
1385         {
1386             mp::odr odr;
1387             package.response() = odr.create_close(
1388                 apdu_req,
1389                 Z_Close_protocolError,
1390                 "zoom filter cannot handle this APDU");
1391             package.session().close();
1392         }
1393     }
1394     else
1395     {
1396         package.session().close();
1397     }
1398 }
1399
1400 void yf::Zoom::Impl::process(mp::Package &package)
1401 {
1402     FrontendPtr f = get_frontend(package);
1403     Z_GDU *gdu = package.request().get();
1404
1405     if (f->m_is_virtual)
1406     {
1407         f->handle_package(package);
1408     }
1409     else if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
1410              Z_APDU_initRequest)
1411     {
1412         Z_InitRequest *req = gdu->u.z3950->u.initRequest;
1413         f->m_init_gdu = gdu;
1414         
1415         mp::odr odr;
1416         Z_APDU *apdu = odr.create_initResponse(gdu->u.z3950, 0, 0);
1417         Z_InitResponse *resp = apdu->u.initResponse;
1418         
1419         int i;
1420         static const int masks[] = {
1421             Z_Options_search,
1422             Z_Options_present,
1423             -1 
1424         };
1425         for (i = 0; masks[i] != -1; i++)
1426             if (ODR_MASK_GET(req->options, masks[i]))
1427                 ODR_MASK_SET(resp->options, masks[i]);
1428         
1429         static const int versions[] = {
1430             Z_ProtocolVersion_1,
1431             Z_ProtocolVersion_2,
1432             Z_ProtocolVersion_3,
1433             -1
1434         };
1435         for (i = 0; versions[i] != -1; i++)
1436             if (ODR_MASK_GET(req->protocolVersion, versions[i]))
1437                 ODR_MASK_SET(resp->protocolVersion, versions[i]);
1438             else
1439                 break;
1440         
1441         *resp->preferredMessageSize = *req->preferredMessageSize;
1442         *resp->maximumRecordSize = *req->maximumRecordSize;
1443         
1444         package.response() = apdu;
1445         f->m_is_virtual = true;
1446     }
1447     else
1448         package.move();
1449
1450     release_frontend(package);
1451 }
1452
1453
1454 static mp::filter::Base* filter_creator()
1455 {
1456     return new mp::filter::Zoom;
1457 }
1458
1459 extern "C" {
1460     struct metaproxy_1_filter_struct metaproxy_1_filter_zoom = {
1461         0,
1462         "zoom",
1463         filter_creator
1464     };
1465 }
1466
1467
1468 /*
1469  * Local variables:
1470  * c-basic-offset: 4
1471  * c-file-style: "Stroustrup"
1472  * indent-tabs-mode: nil
1473  * End:
1474  * vim: shiftwidth=4 tabstop=8 expandtab
1475  */
1476