Happy new year
[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(int smallSetUpperBound,
165                                   int largeSetLowerBound,
166                                   int mediumSetPresentNumber,
167                                   int result_set_size,
168                                   int &number_to_present)
169 {
170     // deal with piggyback
171
172     if (result_set_size < smallSetUpperBound)
173     {
174         // small set . Return all records in set
175         number_to_present = result_set_size;
176     }
177     else if (result_set_size > largeSetLowerBound)
178     {
179         // large set . Return no records
180         number_to_present = 0;
181     }
182     else
183     {
184         // medium set . Return mediumSetPresentNumber records
185         number_to_present = mediumSetPresentNumber;
186         if (number_to_present > result_set_size)
187             number_to_present = result_set_size;
188     }
189 }
190
191
192 bool mp_util::pqf(ODR odr, Z_APDU *apdu, const std::string &q)
193 {
194     YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
195     
196     Z_RPNQuery *rpn = yaz_pqf_parse(pqf_parser, odr, q.c_str());
197     if (!rpn)
198     {
199         yaz_pqf_destroy(pqf_parser);
200         return false;
201     }
202     yaz_pqf_destroy(pqf_parser);
203     Z_Query *query = (Z_Query *) odr_malloc(odr, sizeof(Z_Query));
204     query->which = Z_Query_type_1;
205     query->u.type_1 = rpn;
206     
207     apdu->u.searchRequest->query = query;
208     return true;
209 }
210
211
212 std::string mp_util::zQueryToString(Z_Query *query)
213 {
214     std::string query_str = "";
215
216     if (query && query->which == Z_Query_type_1){
217         Z_RPNQuery *rpn = query->u.type_1;
218         
219         if (rpn){
220             
221             // allocate wrbuf (strings in YAZ!)
222             WRBUF w = wrbuf_alloc();
223             
224             // put query in w
225             yaz_rpnquery_to_wrbuf(w, rpn);
226             
227             // from w to std::string
228             query_str = std::string(wrbuf_buf(w), wrbuf_len(w));
229             
230             // destroy wrbuf
231             wrbuf_destroy(w);
232         }
233     }
234
235 #if 0
236     if (query && query->which == Z_Query_type_1){
237         
238         // allocate wrbuf (strings in YAZ!)
239         WRBUF w = wrbuf_alloc();
240         
241         // put query in w
242         yaz_query_to_wrbuf(w, query);
243         
244         // from w to std::string
245         query_str = std::string(wrbuf_buf(w), wrbuf_len(w));
246         
247         // destroy wrbuf
248         wrbuf_free(w, 1);
249     }    
250 #endif
251     return query_str;
252 }
253
254 void mp_util::get_default_diag(Z_DefaultDiagFormat *r,
255                                          int &error_code, std::string &addinfo)
256 {
257     error_code = *r->condition;
258     switch (r->which)
259     {
260     case Z_DefaultDiagFormat_v2Addinfo:
261         addinfo = std::string(r->u.v2Addinfo);
262         break;
263     case Z_DefaultDiagFormat_v3Addinfo:
264         addinfo = r->u.v3Addinfo;
265         break;
266     }
267 }
268
269 void mp_util::get_init_diagnostics(
270     Z_InitResponse *initrs, int &error_code, std::string &addinfo)
271 {
272     Z_External *uif = initrs->userInformationField;
273     
274     if (uif && uif->which == Z_External_userInfo1)
275     {
276         Z_OtherInformation *ui = uif->u.userInfo1;
277         int i;
278         for (i = 0; i < ui->num_elements; i++)
279         {
280             Z_OtherInformationUnit *unit = ui->list[i];
281             if (unit->which == Z_OtherInfo_externallyDefinedInfo &&
282                 unit->information.externallyDefinedInfo &&
283                 unit->information.externallyDefinedInfo->which ==
284                 Z_External_diag1) 
285             {
286                 Z_DiagnosticFormat *diag = 
287                     unit->information.externallyDefinedInfo->u.diag1;
288
289                 if (diag->num > 0)
290                 {
291                     Z_DiagnosticFormat_s *ds = diag->elements[0];
292                     if (ds->which == Z_DiagnosticFormat_s_defaultDiagRec)
293                         mp::util::get_default_diag(ds->u.defaultDiagRec,
294                                                     error_code, addinfo);
295                 }
296             } 
297         }
298     }
299 }
300
301 int mp_util::get_or_remove_vhost_otherinfo(
302     Z_OtherInformation **otherInformation,
303     bool remove_flag,
304     std::list<std::string> &vhosts)
305 {
306     int cat;
307     for (cat = 1; ; cat++)
308     {
309         // check virtual host
310         const char *vhost =
311             yaz_oi_get_string_oid(otherInformation,
312                                   yaz_oid_userinfo_proxy,
313                                   cat /* categoryValue */,
314                                   remove_flag /* delete flag */);
315         if (!vhost)
316             break;
317         vhosts.push_back(std::string(vhost));
318     }
319     --cat;
320     return cat;
321 }
322
323 void mp_util::get_vhost_otherinfo(
324     Z_OtherInformation *otherInformation,
325     std::list<std::string> &vhosts)
326 {
327     get_or_remove_vhost_otherinfo(&otherInformation, false, vhosts);
328 }
329
330 int mp_util::remove_vhost_otherinfo(
331     Z_OtherInformation **otherInformation,
332     std::list<std::string> &vhosts)
333 {
334     return get_or_remove_vhost_otherinfo(otherInformation, true, vhosts);
335 }
336
337 void mp_util::set_vhost_otherinfo(
338     Z_OtherInformation **otherInformation, ODR odr,
339     const std::list<std::string> &vhosts)
340 {
341     int cat;
342     std::list<std::string>::const_iterator it = vhosts.begin();
343
344     for (cat = 1; it != vhosts.end() ; cat++, it++)
345     {
346         yaz_oi_set_string_oid(otherInformation, odr,
347                               yaz_oid_userinfo_proxy, cat, it->c_str());
348     }
349 }
350
351 void mp_util::set_vhost_otherinfo(
352     Z_OtherInformation **otherInformation, ODR odr,
353     const std::string vhost, const int cat)
354 {
355     yaz_oi_set_string_oid(otherInformation, odr,
356                           yaz_oid_userinfo_proxy, cat, vhost.c_str());
357 }
358
359 void mp_util::split_zurl(std::string zurl, std::string &host,
360                          std::list<std::string> &db)
361 {
362     const char *zurl_cstr = zurl.c_str();
363     const char *sep = strchr(zurl_cstr, '/');
364     
365     if (sep)
366     {
367         host = std::string(zurl_cstr, sep - zurl_cstr);
368
369         const char *cp1 = sep+1;
370         while(1)
371         {
372             const char *cp2 = strchr(cp1, '+');
373             if (cp2)
374                 db.push_back(std::string(cp1, cp2 - cp1));
375             else
376             {
377                 db.push_back(std::string(cp1));
378                 break;
379             }
380             cp1 = cp2+1;
381         }
382     }
383     else
384     {
385         host = zurl;
386     }
387 }
388
389 bool mp_util::set_databases_from_zurl(
390     ODR odr, std::string zurl,
391     int *db_num, char ***db_strings)
392 {
393     std::string host;
394     std::list<std::string> dblist;
395
396     split_zurl(zurl, host, dblist);
397    
398     if (dblist.size() == 0)
399         return false;
400     *db_num = dblist.size();
401     *db_strings = (char **) odr_malloc(odr, sizeof(char*) * (*db_num));
402
403     std::list<std::string>::const_iterator it = dblist.begin();
404     for (int i = 0; it != dblist.end(); it++, i++)
405         (*db_strings)[i] = odr_strdup(odr, it->c_str());
406     return true;
407 }
408
409 mp::odr::odr(int type)
410 {
411     m_odr = odr_createmem(type);
412 }
413
414 mp::odr::odr()
415 {
416     m_odr = odr_createmem(ODR_ENCODE);
417 }
418
419 mp::odr::~odr()
420 {
421     odr_destroy(m_odr);
422 }
423
424 mp::odr::operator ODR() const
425 {
426     return m_odr;
427 }
428
429 Z_APDU *mp::odr::create_close(const Z_APDU *in_apdu,
430                               int reason, const char *addinfo)
431 {
432     Z_APDU *apdu = create_APDU(Z_APDU_close, in_apdu);
433     
434     *apdu->u.close->closeReason = reason;
435     if (addinfo)
436         apdu->u.close->diagnosticInformation = odr_strdup(m_odr, addinfo);
437     return apdu;
438 }
439
440 Z_APDU *mp::odr::create_APDU(int type, const Z_APDU *in_apdu)
441 {
442     return mp::util::create_APDU(m_odr, type, in_apdu);
443 }
444
445 Z_APDU *mp_util::create_APDU(ODR odr, int type, const Z_APDU *in_apdu)
446 {
447     Z_APDU *out_apdu = zget_APDU(odr, type);
448     transfer_referenceId(odr, in_apdu, out_apdu);
449     return out_apdu;
450 }
451
452 void mp_util::transfer_referenceId(ODR odr, const Z_APDU *src, Z_APDU *dst)
453 {
454     Z_ReferenceId **id_to = mp::util::get_referenceId(dst);
455     *id_to = 0;
456     if (src)
457     {
458         Z_ReferenceId **id_from = mp::util::get_referenceId(src);
459         if (id_from && *id_from && id_to)
460         {
461             *id_to = (Z_ReferenceId*) odr_malloc (odr, sizeof(**id_to));
462             (*id_to)->size = (*id_to)->len = (*id_from)->len;
463             (*id_to)->buf = (unsigned char*) odr_malloc(odr, (*id_to)->len);
464             memcpy((*id_to)->buf, (*id_from)->buf, (*id_to)->len);
465         }
466         else if (id_to)
467             *id_to = 0;
468     }
469 }
470
471 Z_APDU *mp::odr::create_initResponse(const Z_APDU *in_apdu,
472                                      int error, const char *addinfo)
473 {
474     Z_APDU *apdu = create_APDU(Z_APDU_initResponse, in_apdu);
475     if (error)
476     {
477         apdu->u.initResponse->userInformationField =
478             zget_init_diagnostics(m_odr, error, addinfo);
479         *apdu->u.initResponse->result = 0;
480     }
481     apdu->u.initResponse->implementationName =
482         odr_prepend(m_odr, "Metaproxy",
483                     apdu->u.initResponse->implementationName);
484     apdu->u.initResponse->implementationVersion = 
485         odr_prepend(m_odr,
486                     VERSION, apdu->u.initResponse->implementationVersion);
487                    
488     return apdu;
489 }
490
491 Z_APDU *mp::odr::create_searchResponse(const Z_APDU *in_apdu,
492                                        int error, const char *addinfo)
493 {
494     Z_APDU *apdu = create_APDU(Z_APDU_searchResponse, in_apdu);
495     if (error)
496     {
497         Z_Records *rec = (Z_Records *) odr_malloc(m_odr, sizeof(Z_Records));
498         *apdu->u.searchResponse->searchStatus = 0;
499         apdu->u.searchResponse->records = rec;
500         rec->which = Z_Records_NSD;
501         rec->u.nonSurrogateDiagnostic =
502             zget_DefaultDiagFormat(m_odr, error, addinfo);
503         
504     }
505     return apdu;
506 }
507
508 Z_APDU *mp::odr::create_presentResponse(const Z_APDU *in_apdu,
509                                         int error, const char *addinfo)
510 {
511     Z_APDU *apdu = create_APDU(Z_APDU_presentResponse, in_apdu);
512     if (error)
513     {
514         Z_Records *rec = (Z_Records *) odr_malloc(m_odr, sizeof(Z_Records));
515         apdu->u.presentResponse->records = rec;
516         
517         rec->which = Z_Records_NSD;
518         rec->u.nonSurrogateDiagnostic =
519             zget_DefaultDiagFormat(m_odr, error, addinfo);
520         *apdu->u.presentResponse->presentStatus = Z_PresentStatus_failure;
521     }
522     return apdu;
523 }
524
525 Z_APDU *mp::odr::create_scanResponse(const Z_APDU *in_apdu,
526                                      int error, const char *addinfo)
527 {
528     Z_APDU *apdu = create_APDU(Z_APDU_scanResponse, in_apdu);
529     Z_ScanResponse *res = apdu->u.scanResponse;
530     res->entries = (Z_ListEntries *) odr_malloc(m_odr, sizeof(*res->entries));
531     res->entries->num_entries = 0;
532     res->entries->entries = 0;
533
534     if (error)
535     {
536         *res->scanStatus = Z_Scan_failure;
537
538         res->entries->num_nonsurrogateDiagnostics = 1;
539         res->entries->nonsurrogateDiagnostics = (Z_DiagRec **)
540             odr_malloc(m_odr, sizeof(Z_DiagRec *));
541         res->entries->nonsurrogateDiagnostics[0] = 
542             zget_DiagRec(m_odr, error, addinfo);
543     }
544     else
545     {
546         res->entries->num_nonsurrogateDiagnostics = 0;
547         res->entries->nonsurrogateDiagnostics = 0;
548     }
549     return apdu;
550 }
551
552 Z_GDU *mp::odr::create_HTTP_Response(mp::Session &session,
553                                      Z_HTTP_Request *hreq, int code)
554 {
555     const char *response_version = "1.0";
556     bool keepalive = false;
557     if (!strcmp(hreq->version, "1.0")) 
558     {
559         const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
560         if (v && !strcmp(v, "Keep-Alive"))
561             keepalive = true;
562         else
563             session.close();
564         response_version = "1.0";
565     }
566     else
567     {
568         const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
569         if (v && !strcmp(v, "close"))
570             session.close();
571         else
572             keepalive = true;
573         response_version = "1.1";
574     }
575
576     Z_GDU *gdu = z_get_HTTP_Response(m_odr, code);
577     Z_HTTP_Response *hres = gdu->u.HTTP_Response;
578     hres->version = odr_strdup(m_odr, response_version);
579     if (keepalive)
580         z_HTTP_header_add(m_odr, &hres->headers, "Connection", "Keep-Alive");
581     
582     return gdu;
583 }
584
585 Z_ReferenceId **mp_util::get_referenceId(const Z_APDU *apdu)
586 {
587     switch (apdu->which)
588     {
589     case  Z_APDU_initRequest:
590         return &apdu->u.initRequest->referenceId; 
591     case  Z_APDU_initResponse:
592         return &apdu->u.initResponse->referenceId;
593     case  Z_APDU_searchRequest:
594         return &apdu->u.searchRequest->referenceId;
595     case  Z_APDU_searchResponse:
596         return &apdu->u.searchResponse->referenceId;
597     case  Z_APDU_presentRequest:
598         return &apdu->u.presentRequest->referenceId;
599     case  Z_APDU_presentResponse:
600         return &apdu->u.presentResponse->referenceId;
601     case  Z_APDU_deleteResultSetRequest:
602         return &apdu->u.deleteResultSetRequest->referenceId;
603     case  Z_APDU_deleteResultSetResponse:
604         return &apdu->u.deleteResultSetResponse->referenceId;
605     case  Z_APDU_accessControlRequest:
606         return &apdu->u.accessControlRequest->referenceId;
607     case  Z_APDU_accessControlResponse:
608         return &apdu->u.accessControlResponse->referenceId;
609     case  Z_APDU_resourceControlRequest:
610         return &apdu->u.resourceControlRequest->referenceId;
611     case  Z_APDU_resourceControlResponse:
612         return &apdu->u.resourceControlResponse->referenceId;
613     case  Z_APDU_triggerResourceControlRequest:
614         return &apdu->u.triggerResourceControlRequest->referenceId;
615     case  Z_APDU_resourceReportRequest:
616         return &apdu->u.resourceReportRequest->referenceId;
617     case  Z_APDU_resourceReportResponse:
618         return &apdu->u.resourceReportResponse->referenceId;
619     case  Z_APDU_scanRequest:
620         return &apdu->u.scanRequest->referenceId;
621     case  Z_APDU_scanResponse:
622         return &apdu->u.scanResponse->referenceId;
623     case  Z_APDU_sortRequest:
624         return &apdu->u.sortRequest->referenceId;
625     case  Z_APDU_sortResponse:
626         return &apdu->u.sortResponse->referenceId;
627     case  Z_APDU_segmentRequest:
628         return &apdu->u.segmentRequest->referenceId;
629     case  Z_APDU_extendedServicesRequest:
630         return &apdu->u.extendedServicesRequest->referenceId;
631     case  Z_APDU_extendedServicesResponse:
632         return &apdu->u.extendedServicesResponse->referenceId;
633     case  Z_APDU_close:
634         return &apdu->u.close->referenceId;
635     }
636     return 0;
637 }
638
639
640 /*
641  * Local variables:
642  * c-basic-offset: 4
643  * c-file-style: "Stroustrup"
644  * indent-tabs-mode: nil
645  * End:
646  * vim: shiftwidth=4 tabstop=8 expandtab
647  */
648