Another and hopefully, last, YAZ OID DB update
[metaproxy-moved-to-github.git] / src / util.cpp
1 /* $Id: util.cpp,v 1.28 2007-04-16 21:54:52 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     for (cat = 1; ; cat++)
296     {
297         // check virtual host
298         const char *vhost =
299             yaz_oi_get_string_oid(otherInformation,
300                                   yaz_oid_userinfo_proxy,
301                                   cat /* categoryValue */,
302                                   remove_flag /* delete flag */);
303         if (!vhost)
304             break;
305         vhosts.push_back(std::string(vhost));
306     }
307     --cat;
308     return cat;
309 }
310
311 void mp_util::get_vhost_otherinfo(
312     Z_OtherInformation *otherInformation,
313     std::list<std::string> &vhosts)
314 {
315     get_or_remove_vhost_otherinfo(&otherInformation, false, vhosts);
316 }
317
318 int mp_util::remove_vhost_otherinfo(
319     Z_OtherInformation **otherInformation,
320     std::list<std::string> &vhosts)
321 {
322     return get_or_remove_vhost_otherinfo(otherInformation, true, vhosts);
323 }
324
325 void mp_util::set_vhost_otherinfo(
326     Z_OtherInformation **otherInformation, ODR odr,
327     const std::list<std::string> &vhosts)
328 {
329     int cat;
330     std::list<std::string>::const_iterator it = vhosts.begin();
331
332     for (cat = 1; it != vhosts.end() ; cat++, it++)
333     {
334         yaz_oi_set_string_oid(otherInformation, odr,
335                               yaz_oid_userinfo_proxy, cat, it->c_str());
336     }
337 }
338
339 void mp_util::set_vhost_otherinfo(
340     Z_OtherInformation **otherInformation, ODR odr,
341     const std::string vhost, const int cat)
342 {
343     yaz_oi_set_string_oid(otherInformation, odr,
344                           yaz_oid_userinfo_proxy, cat, vhost.c_str());
345 }
346
347 void mp_util::split_zurl(std::string zurl, std::string &host,
348                          std::list<std::string> &db)
349 {
350     const char *zurl_cstr = zurl.c_str();
351     const char *sep = strchr(zurl_cstr, '/');
352     
353     if (sep)
354     {
355         host = std::string(zurl_cstr, sep - zurl_cstr);
356
357         const char *cp1 = sep+1;
358         while(1)
359         {
360             const char *cp2 = strchr(cp1, '+');
361             if (cp2)
362                 db.push_back(std::string(cp1, cp2 - cp1));
363             else
364             {
365                 db.push_back(std::string(cp1));
366                 break;
367             }
368             cp1 = cp2+1;
369         }
370     }
371     else
372     {
373         host = zurl;
374     }
375 }
376
377 bool mp_util::set_databases_from_zurl(
378     ODR odr, std::string zurl,
379     int *db_num, char ***db_strings)
380 {
381     std::string host;
382     std::list<std::string> dblist;
383
384     split_zurl(zurl, host, dblist);
385    
386     if (dblist.size() == 0)
387         return false;
388     *db_num = dblist.size();
389     *db_strings = (char **) odr_malloc(odr, sizeof(char*) * (*db_num));
390
391     std::list<std::string>::const_iterator it = dblist.begin();
392     for (int i = 0; it != dblist.end(); it++, i++)
393         (*db_strings)[i] = odr_strdup(odr, it->c_str());
394     return true;
395 }
396
397 mp::odr::odr(int type)
398 {
399     m_odr = odr_createmem(type);
400 }
401
402 mp::odr::odr()
403 {
404     m_odr = odr_createmem(ODR_ENCODE);
405 }
406
407 mp::odr::~odr()
408 {
409     odr_destroy(m_odr);
410 }
411
412 mp::odr::operator ODR() const
413 {
414     return m_odr;
415 }
416
417 Z_APDU *mp::odr::create_close(const Z_APDU *in_apdu,
418                               int reason, const char *addinfo)
419 {
420     Z_APDU *apdu = create_APDU(Z_APDU_close, in_apdu);
421     
422     *apdu->u.close->closeReason = reason;
423     if (addinfo)
424         apdu->u.close->diagnosticInformation = odr_strdup(m_odr, addinfo);
425     return apdu;
426 }
427
428 Z_APDU *mp::odr::create_APDU(int type, const Z_APDU *in_apdu)
429 {
430     return mp::util::create_APDU(m_odr, type, in_apdu);
431 }
432
433 Z_APDU *mp_util::create_APDU(ODR odr, int type, const Z_APDU *in_apdu)
434 {
435     Z_APDU *out_apdu = zget_APDU(odr, type);
436     transfer_referenceId(odr, in_apdu, out_apdu);
437     return out_apdu;
438 }
439
440 void mp_util::transfer_referenceId(ODR odr, const Z_APDU *src, Z_APDU *dst)
441 {
442     Z_ReferenceId **id_to = mp::util::get_referenceId(dst);
443     *id_to = 0;
444     if (src)
445     {
446         Z_ReferenceId **id_from = mp::util::get_referenceId(src);
447         if (id_from && *id_from && id_to)
448         {
449             *id_to = (Z_ReferenceId*) odr_malloc (odr, sizeof(**id_to));
450             (*id_to)->size = (*id_to)->len = (*id_from)->len;
451             (*id_to)->buf = (unsigned char*) odr_malloc(odr, (*id_to)->len);
452             memcpy((*id_to)->buf, (*id_from)->buf, (*id_to)->len);
453         }
454         else if (id_to)
455             *id_to = 0;
456     }
457 }
458
459 Z_APDU *mp::odr::create_initResponse(const Z_APDU *in_apdu,
460                                      int error, const char *addinfo)
461 {
462     Z_APDU *apdu = create_APDU(Z_APDU_initResponse, in_apdu);
463     if (error)
464     {
465         apdu->u.initResponse->userInformationField =
466             zget_init_diagnostics(m_odr, error, addinfo);
467         *apdu->u.initResponse->result = 0;
468     }
469     return apdu;
470 }
471
472 Z_APDU *mp::odr::create_searchResponse(const Z_APDU *in_apdu,
473                                        int error, const char *addinfo)
474 {
475     Z_APDU *apdu = create_APDU(Z_APDU_searchResponse, in_apdu);
476     if (error)
477     {
478         Z_Records *rec = (Z_Records *) odr_malloc(m_odr, sizeof(Z_Records));
479         *apdu->u.searchResponse->searchStatus = 0;
480         apdu->u.searchResponse->records = rec;
481         rec->which = Z_Records_NSD;
482         rec->u.nonSurrogateDiagnostic =
483             zget_DefaultDiagFormat(m_odr, error, addinfo);
484         
485     }
486     return apdu;
487 }
488
489 Z_APDU *mp::odr::create_presentResponse(const Z_APDU *in_apdu,
490                                         int error, const char *addinfo)
491 {
492     Z_APDU *apdu = create_APDU(Z_APDU_presentResponse, in_apdu);
493     if (error)
494     {
495         Z_Records *rec = (Z_Records *) odr_malloc(m_odr, sizeof(Z_Records));
496         apdu->u.presentResponse->records = rec;
497         
498         rec->which = Z_Records_NSD;
499         rec->u.nonSurrogateDiagnostic =
500             zget_DefaultDiagFormat(m_odr, error, addinfo);
501         *apdu->u.presentResponse->presentStatus = Z_PresentStatus_failure;
502     }
503     return apdu;
504 }
505
506 Z_APDU *mp::odr::create_scanResponse(const Z_APDU *in_apdu,
507                                      int error, const char *addinfo)
508 {
509     Z_APDU *apdu = create_APDU(Z_APDU_scanResponse, in_apdu);
510     Z_ScanResponse *res = apdu->u.scanResponse;
511     res->entries = (Z_ListEntries *) odr_malloc(m_odr, sizeof(*res->entries));
512     res->entries->num_entries = 0;
513     res->entries->entries = 0;
514
515     if (error)
516     {
517         *res->scanStatus = Z_Scan_failure;
518
519         res->entries->num_nonsurrogateDiagnostics = 1;
520         res->entries->nonsurrogateDiagnostics = (Z_DiagRec **)
521             odr_malloc(m_odr, sizeof(Z_DiagRec *));
522         res->entries->nonsurrogateDiagnostics[0] = 
523             zget_DiagRec(m_odr, error, addinfo);
524     }
525     else
526     {
527         res->entries->num_nonsurrogateDiagnostics = 0;
528         res->entries->nonsurrogateDiagnostics = 0;
529     }
530     return apdu;
531 }
532
533 Z_GDU *mp::odr::create_HTTP_Response(mp::Session &session,
534                                      Z_HTTP_Request *hreq, int code)
535 {
536     const char *response_version = "1.0";
537     bool keepalive = false;
538     if (!strcmp(hreq->version, "1.0")) 
539     {
540         const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
541         if (v && !strcmp(v, "Keep-Alive"))
542             keepalive = true;
543         else
544             session.close();
545         response_version = "1.0";
546     }
547     else
548     {
549         const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
550         if (v && !strcmp(v, "close"))
551             session.close();
552         else
553             keepalive = true;
554         response_version = "1.1";
555     }
556
557     Z_GDU *gdu = z_get_HTTP_Response(m_odr, code);
558     Z_HTTP_Response *hres = gdu->u.HTTP_Response;
559     hres->version = odr_strdup(m_odr, response_version);
560     if (keepalive)
561         z_HTTP_header_add(m_odr, &hres->headers, "Connection", "Keep-Alive");
562     
563     return gdu;
564 }
565
566 Z_ReferenceId **mp_util::get_referenceId(const Z_APDU *apdu)
567 {
568     switch (apdu->which)
569     {
570     case  Z_APDU_initRequest:
571         return &apdu->u.initRequest->referenceId; 
572     case  Z_APDU_initResponse:
573         return &apdu->u.initResponse->referenceId;
574     case  Z_APDU_searchRequest:
575         return &apdu->u.searchRequest->referenceId;
576     case  Z_APDU_searchResponse:
577         return &apdu->u.searchResponse->referenceId;
578     case  Z_APDU_presentRequest:
579         return &apdu->u.presentRequest->referenceId;
580     case  Z_APDU_presentResponse:
581         return &apdu->u.presentResponse->referenceId;
582     case  Z_APDU_deleteResultSetRequest:
583         return &apdu->u.deleteResultSetRequest->referenceId;
584     case  Z_APDU_deleteResultSetResponse:
585         return &apdu->u.deleteResultSetResponse->referenceId;
586     case  Z_APDU_accessControlRequest:
587         return &apdu->u.accessControlRequest->referenceId;
588     case  Z_APDU_accessControlResponse:
589         return &apdu->u.accessControlResponse->referenceId;
590     case  Z_APDU_resourceControlRequest:
591         return &apdu->u.resourceControlRequest->referenceId;
592     case  Z_APDU_resourceControlResponse:
593         return &apdu->u.resourceControlResponse->referenceId;
594     case  Z_APDU_triggerResourceControlRequest:
595         return &apdu->u.triggerResourceControlRequest->referenceId;
596     case  Z_APDU_resourceReportRequest:
597         return &apdu->u.resourceReportRequest->referenceId;
598     case  Z_APDU_resourceReportResponse:
599         return &apdu->u.resourceReportResponse->referenceId;
600     case  Z_APDU_scanRequest:
601         return &apdu->u.scanRequest->referenceId;
602     case  Z_APDU_scanResponse:
603         return &apdu->u.scanResponse->referenceId;
604     case  Z_APDU_sortRequest:
605         return &apdu->u.sortRequest->referenceId;
606     case  Z_APDU_sortResponse:
607         return &apdu->u.sortResponse->referenceId;
608     case  Z_APDU_segmentRequest:
609         return &apdu->u.segmentRequest->referenceId;
610     case  Z_APDU_extendedServicesRequest:
611         return &apdu->u.extendedServicesRequest->referenceId;
612     case  Z_APDU_extendedServicesResponse:
613         return &apdu->u.extendedServicesResponse->referenceId;
614     case  Z_APDU_close:
615         return &apdu->u.close->referenceId;
616     }
617     return 0;
618 }
619
620
621 /*
622  * Local variables:
623  * c-basic-offset: 4
624  * indent-tabs-mode: nil
625  * c-file-style: "stroustrup"
626  * End:
627  * vim: shiftwidth=4 tabstop=8 expandtab
628  */