Retain old mp::util::piggyback (used by CF)
[metaproxy-moved-to-github.git] / src / util.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 <metaproxy/util.hpp>
21
22 #include <yaz/odr.h>
23 #include <yaz/pquery.h>
24 #include <yaz/otherinfo.h>
25 #include <yaz/querytowrbuf.h>
26 #include <yaz/oid_db.h>
27
28 #include <iostream>
29
30 namespace mp = metaproxy_1;
31
32 // Doxygen doesn't like mp::util, so we use this instead
33 namespace mp_util = metaproxy_1::util;
34
35 const char * 
36 mp_util::record_composition_to_esn(Z_RecordComposition *comp)
37 {
38     if (comp && comp->which == Z_RecordComp_complex)
39     {
40         if (comp->u.complex->generic 
41             && comp->u.complex->generic->elementSpec
42             && (comp->u.complex->generic->elementSpec->which == 
43                 Z_ElementSpec_elementSetName))
44             return comp->u.complex->generic->elementSpec->u.elementSetName;
45     }
46     else if (comp && comp->which == Z_RecordComp_simple &&
47              comp->u.simple->which == Z_ElementSetNames_generic)
48         return comp->u.simple->u.generic;
49     return 0;
50 }
51
52
53
54 std::string mp_util::http_header_value(const Z_HTTP_Header* header, 
55                                        const std::string name)
56 {
57     while (header && header->name
58            && std::string(header->name) !=  name)
59         header = header->next;
60     
61     if (header && header->name && std::string(header->name) == name
62         && header->value)
63         return std::string(header->value);
64     
65     return std::string();
66 }
67     
68 std::string mp_util::http_headers_debug(const Z_HTTP_Request &http_req)
69 {
70     std::string message("<html>\n<body>\n<h1>"
71                         "Metaproxy SRUtoZ3950 filter"
72                         "</h1>\n");
73     
74     message += "<h3>HTTP Info</h3><br/>\n";
75     message += "<p>\n";
76     message += "<b>Method: </b> " + std::string(http_req.method) + "<br/>\n";
77     message += "<b>Version:</b> " + std::string(http_req.version) + "<br/>\n";
78     message += "<b>Path:   </b> " + std::string(http_req.path) + "<br/>\n";
79
80     message += "<b>Content-Type:</b>"
81         + mp_util::http_header_value(http_req.headers, "Content-Type")
82         + "<br/>\n";
83     message += "<b>Content-Length:</b>"
84         + mp_util::http_header_value(http_req.headers, "Content-Length")
85         + "<br/>\n";
86     message += "</p>\n";    
87     
88     message += "<h3>Headers</h3><br/>\n";
89     message += "<p>\n";    
90     Z_HTTP_Header* header = http_req.headers;
91     while (header){
92         message += "<b>Header: </b> <i>" 
93             + std::string(header->name) + ":</i> "
94             + std::string(header->value) + "<br/>\n";
95         header = header->next;
96     }
97     message += "</p>\n";    
98     message += "</body>\n</html>\n";
99     return message;
100 }
101
102
103 void mp_util::http_response(metaproxy_1::Package &package, 
104                      const std::string &content, 
105                      int http_code)
106 {
107
108     Z_GDU *zgdu_req = package.request().get(); 
109     Z_GDU *zgdu_res = 0; 
110     mp::odr odr;
111     zgdu_res 
112        = odr.create_HTTP_Response(package.session(), 
113                                   zgdu_req->u.HTTP_Request, 
114                                   http_code);
115         
116     zgdu_res->u.HTTP_Response->content_len = content.size();
117     zgdu_res->u.HTTP_Response->content_buf 
118         = (char*) odr_malloc(odr, zgdu_res->u.HTTP_Response->content_len);
119     
120     strncpy(zgdu_res->u.HTTP_Response->content_buf, 
121             content.c_str(),  zgdu_res->u.HTTP_Response->content_len);
122     
123     //z_HTTP_header_add(odr, &hres->headers,
124     //                  "Content-Type", content_type.c_str());
125     package.response() = zgdu_res;
126 }
127
128
129 int mp_util::memcmp2(const void *buf1, int len1,
130                      const void *buf2, int len2)
131 {
132     int d = len1 - len2;
133
134     // compare buffer (common length)
135     int c = memcmp(buf1, buf2, d > 0 ? len2 : len1);
136     if (c > 0)
137         return 1;
138     else if (c < 0)
139         return -1;
140     
141     // compare (remaining bytes)
142     if (d > 0)
143         return 1;
144     else if (d < 0)
145         return -1;
146     return 0;
147 }
148
149
150 std::string mp_util::database_name_normalize(const std::string &s)
151 {
152     std::string r = s;
153     size_t i;
154     for (i = 0; i < r.length(); i++)
155     {
156         int ch = r[i];
157         if (ch >= 'A' && ch <= 'Z')
158             r[i] = ch + 'a' - 'A';
159     }
160     return r;
161
162 }
163
164 void mp_util::piggyback_sr(Z_SearchRequest *sreq,
165                            Odr_int result_set_size,
166                            Odr_int &number_to_present,
167                            const char **element_set_name)
168 {
169     Z_ElementSetNames *esn;
170     const char *smallSetElementSetNames = 0;
171     const char *mediumSetElementSetNames = 0;
172
173     esn = sreq->smallSetElementSetNames;
174     if (esn && esn->which == Z_ElementSetNames_generic)
175         smallSetElementSetNames = esn->u.generic;
176
177     esn = sreq->mediumSetElementSetNames;
178     if (esn && esn->which == Z_ElementSetNames_generic)
179         mediumSetElementSetNames = esn->u.generic;
180
181     piggyback(*sreq->smallSetUpperBound,
182               *sreq->largeSetLowerBound,
183               *sreq->mediumSetPresentNumber,
184               smallSetElementSetNames,
185               mediumSetElementSetNames,
186               result_set_size,
187               number_to_present,
188               element_set_name);
189 }
190
191 void mp_util::piggyback(int smallSetUpperBound,
192                         int largeSetLowerBound,
193                         int mediumSetPresentNumber,
194                         int result_set_size,
195                         int &number_to_present)
196 {
197     Odr_int tmp = number_to_present;
198     piggyback(smallSetUpperBound, largeSetLowerBound, mediumSetPresentNumber,
199               0, 0, result_set_size, tmp, 0);
200     number_to_present = tmp;
201 }
202
203 void mp_util::piggyback(Odr_int smallSetUpperBound,
204                         Odr_int largeSetLowerBound,
205                         Odr_int mediumSetPresentNumber,
206                         const char *smallSetElementSetNames,
207                         const char *mediumSetElementSetNames,
208                         Odr_int result_set_size,
209                         Odr_int &number_to_present,
210                         const char **element_set_name)
211 {
212     // deal with piggyback
213
214     if (result_set_size < smallSetUpperBound)
215     {
216         // small set . Return all records in set
217         number_to_present = result_set_size;
218         if (element_set_name && smallSetElementSetNames)
219             *element_set_name = smallSetElementSetNames;
220             
221     }
222     else if (result_set_size > largeSetLowerBound)
223     {
224         // large set . Return no records
225         number_to_present = 0;
226         if (element_set_name)
227             *element_set_name = 0;
228     }
229     else
230     {
231         // medium set . Return mediumSetPresentNumber records
232         number_to_present = mediumSetPresentNumber;
233         if (number_to_present > result_set_size)
234             number_to_present = result_set_size;
235         if (element_set_name && mediumSetElementSetNames)
236             *element_set_name = mediumSetElementSetNames;
237     }
238 }
239
240 bool mp_util::pqf(ODR odr, Z_APDU *apdu, const std::string &q)
241 {
242     YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
243     
244     Z_RPNQuery *rpn = yaz_pqf_parse(pqf_parser, odr, q.c_str());
245     if (!rpn)
246     {
247         yaz_pqf_destroy(pqf_parser);
248         return false;
249     }
250     yaz_pqf_destroy(pqf_parser);
251     Z_Query *query = (Z_Query *) odr_malloc(odr, sizeof(Z_Query));
252     query->which = Z_Query_type_1;
253     query->u.type_1 = rpn;
254     
255     apdu->u.searchRequest->query = query;
256     return true;
257 }
258
259
260 std::string mp_util::zQueryToString(Z_Query *query)
261 {
262     std::string query_str = "";
263
264     if (query && query->which == Z_Query_type_1){
265         Z_RPNQuery *rpn = query->u.type_1;
266         
267         if (rpn){
268             
269             // allocate wrbuf (strings in YAZ!)
270             WRBUF w = wrbuf_alloc();
271             
272             // put query in w
273             yaz_rpnquery_to_wrbuf(w, rpn);
274             
275             // from w to std::string
276             query_str = std::string(wrbuf_buf(w), wrbuf_len(w));
277             
278             // destroy wrbuf
279             wrbuf_destroy(w);
280         }
281     }
282
283 #if 0
284     if (query && query->which == Z_Query_type_1){
285         
286         // allocate wrbuf (strings in YAZ!)
287         WRBUF w = wrbuf_alloc();
288         
289         // put query in w
290         yaz_query_to_wrbuf(w, query);
291         
292         // from w to std::string
293         query_str = std::string(wrbuf_buf(w), wrbuf_len(w));
294         
295         // destroy wrbuf
296         wrbuf_free(w, 1);
297     }    
298 #endif
299     return query_str;
300 }
301
302 void mp_util::get_default_diag(Z_DefaultDiagFormat *r,
303                                          int &error_code, std::string &addinfo)
304 {
305     error_code = *r->condition;
306     switch (r->which)
307     {
308     case Z_DefaultDiagFormat_v2Addinfo:
309         addinfo = std::string(r->u.v2Addinfo);
310         break;
311     case Z_DefaultDiagFormat_v3Addinfo:
312         addinfo = r->u.v3Addinfo;
313         break;
314     }
315 }
316
317 void mp_util::get_init_diagnostics(
318     Z_InitResponse *initrs, int &error_code, std::string &addinfo)
319 {
320     Z_External *uif = initrs->userInformationField;
321     
322     if (uif && uif->which == Z_External_userInfo1)
323     {
324         Z_OtherInformation *ui = uif->u.userInfo1;
325         int i;
326         for (i = 0; i < ui->num_elements; i++)
327         {
328             Z_OtherInformationUnit *unit = ui->list[i];
329             if (unit->which == Z_OtherInfo_externallyDefinedInfo &&
330                 unit->information.externallyDefinedInfo &&
331                 unit->information.externallyDefinedInfo->which ==
332                 Z_External_diag1) 
333             {
334                 Z_DiagnosticFormat *diag = 
335                     unit->information.externallyDefinedInfo->u.diag1;
336
337                 if (diag->num > 0)
338                 {
339                     Z_DiagnosticFormat_s *ds = diag->elements[0];
340                     if (ds->which == Z_DiagnosticFormat_s_defaultDiagRec)
341                         mp::util::get_default_diag(ds->u.defaultDiagRec,
342                                                     error_code, addinfo);
343                 }
344             } 
345         }
346     }
347 }
348
349 int mp_util::get_or_remove_vhost_otherinfo(
350     Z_OtherInformation **otherInformation,
351     bool remove_flag,
352     std::list<std::string> &vhosts)
353 {
354     int cat;
355     for (cat = 1; ; cat++)
356     {
357         // check virtual host
358         const char *vhost =
359             yaz_oi_get_string_oid(otherInformation,
360                                   yaz_oid_userinfo_proxy,
361                                   cat /* categoryValue */,
362                                   remove_flag /* delete flag */);
363         if (!vhost)
364             break;
365         vhosts.push_back(std::string(vhost));
366     }
367     --cat;
368     return cat;
369 }
370
371 void mp_util::get_vhost_otherinfo(
372     Z_OtherInformation *otherInformation,
373     std::list<std::string> &vhosts)
374 {
375     get_or_remove_vhost_otherinfo(&otherInformation, false, vhosts);
376 }
377
378 int mp_util::remove_vhost_otherinfo(
379     Z_OtherInformation **otherInformation,
380     std::list<std::string> &vhosts)
381 {
382     return get_or_remove_vhost_otherinfo(otherInformation, true, vhosts);
383 }
384
385 void mp_util::set_vhost_otherinfo(
386     Z_OtherInformation **otherInformation, ODR odr,
387     const std::list<std::string> &vhosts)
388 {
389     int cat;
390     std::list<std::string>::const_iterator it = vhosts.begin();
391
392     for (cat = 1; it != vhosts.end() ; cat++, it++)
393     {
394         yaz_oi_set_string_oid(otherInformation, odr,
395                               yaz_oid_userinfo_proxy, cat, it->c_str());
396     }
397 }
398
399 void mp_util::set_vhost_otherinfo(
400     Z_OtherInformation **otherInformation, ODR odr,
401     const std::string vhost, const int cat)
402 {
403     yaz_oi_set_string_oid(otherInformation, odr,
404                           yaz_oid_userinfo_proxy, cat, vhost.c_str());
405 }
406
407 void mp_util::split_zurl(std::string zurl, std::string &host,
408                          std::list<std::string> &db)
409 {
410     const char *zurl_cstr = zurl.c_str();
411     const char *sep = strchr(zurl_cstr, '/');
412     
413     if (sep)
414     {
415         host = std::string(zurl_cstr, sep - zurl_cstr);
416
417         const char *cp1 = sep+1;
418         while(1)
419         {
420             const char *cp2 = strchr(cp1, '+');
421             if (cp2)
422                 db.push_back(std::string(cp1, cp2 - cp1));
423             else
424             {
425                 db.push_back(std::string(cp1));
426                 break;
427             }
428             cp1 = cp2+1;
429         }
430     }
431     else
432     {
433         host = zurl;
434     }
435 }
436
437 bool mp_util::set_databases_from_zurl(
438     ODR odr, std::string zurl,
439     int *db_num, char ***db_strings)
440 {
441     std::string host;
442     std::list<std::string> dblist;
443
444     split_zurl(zurl, host, dblist);
445    
446     if (dblist.size() == 0)
447         return false;
448     *db_num = dblist.size();
449     *db_strings = (char **) odr_malloc(odr, sizeof(char*) * (*db_num));
450
451     std::list<std::string>::const_iterator it = dblist.begin();
452     for (int i = 0; it != dblist.end(); it++, i++)
453         (*db_strings)[i] = odr_strdup(odr, it->c_str());
454     return true;
455 }
456
457 mp::odr::odr(int type)
458 {
459     m_odr = odr_createmem(type);
460 }
461
462 mp::odr::odr()
463 {
464     m_odr = odr_createmem(ODR_ENCODE);
465 }
466
467 mp::odr::~odr()
468 {
469     odr_destroy(m_odr);
470 }
471
472 mp::odr::operator ODR() const
473 {
474     return m_odr;
475 }
476
477 Z_APDU *mp::odr::create_close(const Z_APDU *in_apdu,
478                               int reason, const char *addinfo)
479 {
480     Z_APDU *apdu = create_APDU(Z_APDU_close, in_apdu);
481     
482     *apdu->u.close->closeReason = reason;
483     if (addinfo)
484         apdu->u.close->diagnosticInformation = odr_strdup(m_odr, addinfo);
485     return apdu;
486 }
487
488 Z_APDU *mp::odr::create_APDU(int type, const Z_APDU *in_apdu)
489 {
490     return mp::util::create_APDU(m_odr, type, in_apdu);
491 }
492
493 Z_APDU *mp_util::create_APDU(ODR odr, int type, const Z_APDU *in_apdu)
494 {
495     Z_APDU *out_apdu = zget_APDU(odr, type);
496     transfer_referenceId(odr, in_apdu, out_apdu);
497     return out_apdu;
498 }
499
500 void mp_util::transfer_referenceId(ODR odr, const Z_APDU *src, Z_APDU *dst)
501 {
502     Z_ReferenceId **id_to = mp::util::get_referenceId(dst);
503     *id_to = 0;
504     if (src)
505     {
506         Z_ReferenceId **id_from = mp::util::get_referenceId(src);
507         if (id_from && *id_from && id_to)
508         {
509             *id_to = (Z_ReferenceId*) odr_malloc (odr, sizeof(**id_to));
510             (*id_to)->size = (*id_to)->len = (*id_from)->len;
511             (*id_to)->buf = (unsigned char*) odr_malloc(odr, (*id_to)->len);
512             memcpy((*id_to)->buf, (*id_from)->buf, (*id_to)->len);
513         }
514         else if (id_to)
515             *id_to = 0;
516     }
517 }
518
519 Z_APDU *mp::odr::create_initResponse(const Z_APDU *in_apdu,
520                                      int error, const char *addinfo)
521 {
522     Z_APDU *apdu = create_APDU(Z_APDU_initResponse, in_apdu);
523     if (error)
524     {
525         apdu->u.initResponse->userInformationField =
526             zget_init_diagnostics(m_odr, error, addinfo);
527         *apdu->u.initResponse->result = 0;
528     }
529     apdu->u.initResponse->implementationName =
530         odr_prepend(m_odr, "Metaproxy",
531                     apdu->u.initResponse->implementationName);
532     apdu->u.initResponse->implementationVersion = 
533         odr_prepend(m_odr,
534                     VERSION, apdu->u.initResponse->implementationVersion);
535                    
536     return apdu;
537 }
538
539 Z_APDU *mp::odr::create_searchResponse(const Z_APDU *in_apdu,
540                                        int error, const char *addinfo)
541 {
542     Z_APDU *apdu = create_APDU(Z_APDU_searchResponse, in_apdu);
543     if (error)
544     {
545         Z_Records *rec = (Z_Records *) odr_malloc(m_odr, sizeof(Z_Records));
546         *apdu->u.searchResponse->searchStatus = 0;
547         apdu->u.searchResponse->records = rec;
548         rec->which = Z_Records_NSD;
549         rec->u.nonSurrogateDiagnostic =
550             zget_DefaultDiagFormat(m_odr, error, addinfo);
551         
552     }
553     return apdu;
554 }
555
556 Z_APDU *mp::odr::create_presentResponse(const Z_APDU *in_apdu,
557                                         int error, const char *addinfo)
558 {
559     Z_APDU *apdu = create_APDU(Z_APDU_presentResponse, in_apdu);
560     if (error)
561     {
562         Z_Records *rec = (Z_Records *) odr_malloc(m_odr, sizeof(Z_Records));
563         apdu->u.presentResponse->records = rec;
564         
565         rec->which = Z_Records_NSD;
566         rec->u.nonSurrogateDiagnostic =
567             zget_DefaultDiagFormat(m_odr, error, addinfo);
568         *apdu->u.presentResponse->presentStatus = Z_PresentStatus_failure;
569     }
570     return apdu;
571 }
572
573 Z_APDU *mp::odr::create_scanResponse(const Z_APDU *in_apdu,
574                                      int error, const char *addinfo)
575 {
576     Z_APDU *apdu = create_APDU(Z_APDU_scanResponse, in_apdu);
577     Z_ScanResponse *res = apdu->u.scanResponse;
578     res->entries = (Z_ListEntries *) odr_malloc(m_odr, sizeof(*res->entries));
579     res->entries->num_entries = 0;
580     res->entries->entries = 0;
581
582     if (error)
583     {
584         *res->scanStatus = Z_Scan_failure;
585
586         res->entries->num_nonsurrogateDiagnostics = 1;
587         res->entries->nonsurrogateDiagnostics = (Z_DiagRec **)
588             odr_malloc(m_odr, sizeof(Z_DiagRec *));
589         res->entries->nonsurrogateDiagnostics[0] = 
590             zget_DiagRec(m_odr, error, addinfo);
591     }
592     else
593     {
594         res->entries->num_nonsurrogateDiagnostics = 0;
595         res->entries->nonsurrogateDiagnostics = 0;
596     }
597     return apdu;
598 }
599
600 Z_GDU *mp::odr::create_HTTP_Response(mp::Session &session,
601                                      Z_HTTP_Request *hreq, int code)
602 {
603     const char *response_version = "1.0";
604     bool keepalive = false;
605     if (!strcmp(hreq->version, "1.0")) 
606     {
607         const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
608         if (v && !strcmp(v, "Keep-Alive"))
609             keepalive = true;
610         else
611             session.close();
612         response_version = "1.0";
613     }
614     else
615     {
616         const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
617         if (v && !strcmp(v, "close"))
618             session.close();
619         else
620             keepalive = true;
621         response_version = "1.1";
622     }
623
624     Z_GDU *gdu = z_get_HTTP_Response(m_odr, code);
625     Z_HTTP_Response *hres = gdu->u.HTTP_Response;
626     hres->version = odr_strdup(m_odr, response_version);
627     if (keepalive)
628         z_HTTP_header_add(m_odr, &hres->headers, "Connection", "Keep-Alive");
629     
630     return gdu;
631 }
632
633 Z_ReferenceId **mp_util::get_referenceId(const Z_APDU *apdu)
634 {
635     switch (apdu->which)
636     {
637     case  Z_APDU_initRequest:
638         return &apdu->u.initRequest->referenceId; 
639     case  Z_APDU_initResponse:
640         return &apdu->u.initResponse->referenceId;
641     case  Z_APDU_searchRequest:
642         return &apdu->u.searchRequest->referenceId;
643     case  Z_APDU_searchResponse:
644         return &apdu->u.searchResponse->referenceId;
645     case  Z_APDU_presentRequest:
646         return &apdu->u.presentRequest->referenceId;
647     case  Z_APDU_presentResponse:
648         return &apdu->u.presentResponse->referenceId;
649     case  Z_APDU_deleteResultSetRequest:
650         return &apdu->u.deleteResultSetRequest->referenceId;
651     case  Z_APDU_deleteResultSetResponse:
652         return &apdu->u.deleteResultSetResponse->referenceId;
653     case  Z_APDU_accessControlRequest:
654         return &apdu->u.accessControlRequest->referenceId;
655     case  Z_APDU_accessControlResponse:
656         return &apdu->u.accessControlResponse->referenceId;
657     case  Z_APDU_resourceControlRequest:
658         return &apdu->u.resourceControlRequest->referenceId;
659     case  Z_APDU_resourceControlResponse:
660         return &apdu->u.resourceControlResponse->referenceId;
661     case  Z_APDU_triggerResourceControlRequest:
662         return &apdu->u.triggerResourceControlRequest->referenceId;
663     case  Z_APDU_resourceReportRequest:
664         return &apdu->u.resourceReportRequest->referenceId;
665     case  Z_APDU_resourceReportResponse:
666         return &apdu->u.resourceReportResponse->referenceId;
667     case  Z_APDU_scanRequest:
668         return &apdu->u.scanRequest->referenceId;
669     case  Z_APDU_scanResponse:
670         return &apdu->u.scanResponse->referenceId;
671     case  Z_APDU_sortRequest:
672         return &apdu->u.sortRequest->referenceId;
673     case  Z_APDU_sortResponse:
674         return &apdu->u.sortResponse->referenceId;
675     case  Z_APDU_segmentRequest:
676         return &apdu->u.segmentRequest->referenceId;
677     case  Z_APDU_extendedServicesRequest:
678         return &apdu->u.extendedServicesRequest->referenceId;
679     case  Z_APDU_extendedServicesResponse:
680         return &apdu->u.extendedServicesResponse->referenceId;
681     case  Z_APDU_close:
682         return &apdu->u.close->referenceId;
683     }
684     return 0;
685 }
686
687
688 /*
689  * Local variables:
690  * c-basic-offset: 4
691  * c-file-style: "Stroustrup"
692  * indent-tabs-mode: nil
693  * End:
694  * vim: shiftwidth=4 tabstop=8 expandtab
695  */
696