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