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