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