added regex core functionality for filter_query_rewrite
[metaproxy-moved-to-github.git] / src / util.cpp
1 /* $Id: util.cpp,v 1.13 2006-01-20 22:38:12 marc Exp $
2    Copyright (c) 2005, Index Data.
3
4 %LICENSE%
5  */
6
7 #include "config.hpp"
8
9 #include <yaz/odr.h>
10 #include <yaz/pquery.h>
11 #include <yaz/otherinfo.h>
12 #include <yaz/querytowrbuf.h> // for yaz_query_to_wrbuf()
13 #include "util.hpp"
14
15 //#include <iostream>
16
17 void yp2::util::piggyback(int smallSetUpperBound,
18                           int largeSetLowerBound,
19                           int mediumSetPresentNumber,
20                           int result_set_size,
21                           int &number_to_present)
22 {
23     // deal with piggyback
24
25     if (result_set_size < smallSetUpperBound)
26     {
27         // small set . Return all records in set
28         number_to_present = result_set_size;
29     }
30     else if (result_set_size > largeSetLowerBound)
31     {
32         // large set . Return no records
33         number_to_present = 0;
34     }
35     else
36     {
37         // medium set . Return mediumSetPresentNumber records
38         number_to_present = mediumSetPresentNumber;
39         if (number_to_present > result_set_size)
40             number_to_present = result_set_size;
41     }
42 }
43
44
45 bool yp2::util::pqf(ODR odr, Z_APDU *apdu, const std::string &q) {
46     YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
47     
48     Z_RPNQuery *rpn = yaz_pqf_parse(pqf_parser, odr, q.c_str());
49     if (!rpn)
50     {
51         yaz_pqf_destroy(pqf_parser);
52         return false;
53     }
54     yaz_pqf_destroy(pqf_parser);
55     Z_Query *query = (Z_Query *) odr_malloc(odr, sizeof(Z_Query));
56     query->which = Z_Query_type_1;
57     query->u.type_1 = rpn;
58     
59     apdu->u.searchRequest->query = query;
60     return true;
61 }
62
63
64 std::string yp2::util::zQueryToString(Z_Query *query)
65 {
66     std::string query_str = "";
67
68     if (query && query->which == Z_Query_type_1){
69         Z_RPNQuery *rpn = query->u.type_1;
70         
71         if (rpn){
72             
73             // allocate wrbuf (strings in YAZ!)
74             WRBUF w = wrbuf_alloc();
75             
76             // put query in w
77             yaz_rpnquery_to_wrbuf(w, rpn);
78             
79             // from w to std::string
80             query_str = std::string(wrbuf_buf(w), wrbuf_len(w));
81             
82             // destroy wrbuf
83             wrbuf_free(w, 1);
84         }
85     }
86
87 #if 0
88     if (query && query->which == Z_Query_type_1){
89         
90         // allocate wrbuf (strings in YAZ!)
91         WRBUF w = wrbuf_alloc();
92         
93         // put query in w
94         yaz_query_to_wrbuf(w, query);
95         
96         // from w to std::string
97         query_str = std::string(wrbuf_buf(w), wrbuf_len(w));
98         
99         // destroy wrbuf
100         wrbuf_free(w, 1);
101     }    
102 #endif
103     return query_str;
104 }
105
106 void yp2::util::get_default_diag(Z_DefaultDiagFormat *r,
107                                  int &error_code, std::string &addinfo)
108 {
109     error_code = *r->condition;
110     switch (r->which)
111     {
112     case Z_DefaultDiagFormat_v2Addinfo:
113         addinfo = std::string(r->u.v2Addinfo);
114         break;
115     case Z_DefaultDiagFormat_v3Addinfo:
116         addinfo = r->u.v3Addinfo;
117         break;
118     }
119 }
120
121 void yp2::util::get_init_diagnostics(Z_InitResponse *initrs,
122                                      int &error_code, std::string &addinfo)
123 {
124     Z_External *uif = initrs->userInformationField;
125     
126     if (uif && uif->which == Z_External_userInfo1)
127     {
128         Z_OtherInformation *ui = uif->u.userInfo1;
129         int i;
130         for (i = 0; i < ui->num_elements; i++)
131         {
132             Z_OtherInformationUnit *unit = ui->list[i];
133             if (unit->which == Z_OtherInfo_externallyDefinedInfo &&
134                 unit->information.externallyDefinedInfo &&
135                 unit->information.externallyDefinedInfo->which ==
136                 Z_External_diag1) 
137             {
138                 Z_DiagnosticFormat *diag = 
139                     unit->information.externallyDefinedInfo->u.diag1;
140
141                 if (diag->num > 0)
142                 {
143                     Z_DiagnosticFormat_s *ds = diag->elements[0];
144                     if (ds->which == Z_DiagnosticFormat_s_defaultDiagRec)
145                         yp2::util::get_default_diag(ds->u.defaultDiagRec,
146                                                     error_code, addinfo);
147                 }
148             } 
149         }
150     }
151 }
152
153 int yp2::util::get_vhost_otherinfo(Z_OtherInformation **otherInformation,
154                                    bool remove_flag,
155                                    std::list<std::string> &vhosts)
156 {
157     int cat;
158     for (cat = 1; ; cat++)
159     {
160         // check virtual host
161         const char *vhost =
162             yaz_oi_get_string_oidval(otherInformation,
163                                      VAL_PROXY, 
164                                      cat /* categoryValue */,
165                                      remove_flag /* delete flag */);
166         if (!vhost)
167             break;
168         vhosts.push_back(std::string(vhost));
169     }
170     --cat;
171     return cat;
172 }
173
174 void yp2::util::set_vhost_otherinfo(Z_OtherInformation **otherInformation,
175                                     ODR odr,
176                                     const std::list<std::string> &vhosts)
177 {
178     int cat;
179     std::list<std::string>::const_iterator it = vhosts.begin();
180     for (cat = 1; it != vhosts.end() ; cat++, it++)
181     {
182         yaz_oi_set_string_oidval(otherInformation, odr,
183                                  VAL_PROXY, cat, it->c_str());
184     }
185 }
186
187 void yp2::util::split_zurl(std::string zurl, std::string &host,
188                            std::list<std::string> &db)
189 {
190     const char *zurl_cstr = zurl.c_str();
191     const char *sep = strchr(zurl_cstr, '/');
192     
193     if (sep)
194     {
195         host = std::string(zurl_cstr, sep - zurl_cstr);
196
197         const char *cp1 = sep+1;
198         while(1)
199         {
200             const char *cp2 = strchr(cp1, '+');
201             if (cp2)
202                 db.push_back(std::string(cp1, cp2 - cp1));
203             else
204             {
205                 db.push_back(std::string(cp1));
206                 break;
207             }
208             cp1 = cp2+1;
209         }
210     }
211     else
212     {
213         host = zurl;
214     }
215 }
216
217 bool yp2::util::set_databases_from_zurl(ODR odr, std::string zurl,
218                                         int *db_num, char ***db_strings)
219 {
220     std::string host;
221     std::list<std::string> dblist;
222
223     split_zurl(zurl, host, dblist);
224    
225     if (dblist.size() == 0)
226         return false;
227     *db_num = dblist.size();
228     *db_strings = (char **) odr_malloc(odr, sizeof(char*) * (*db_num));
229
230     std::list<std::string>::const_iterator it = dblist.begin();
231     for (int i = 0; it != dblist.end(); it++, i++)
232         (*db_strings)[i] = odr_strdup(odr, it->c_str());
233     return true;
234 }
235
236 yp2::odr::odr(int type)
237 {
238     m_odr = odr_createmem(type);
239 }
240
241 yp2::odr::odr()
242 {
243     m_odr = odr_createmem(ODR_ENCODE);
244 }
245
246 yp2::odr::~odr()
247 {
248     odr_destroy(m_odr);
249 }
250
251 yp2::odr::operator ODR() const
252 {
253     return m_odr;
254 }
255
256 Z_APDU *yp2::odr::create_close(Z_APDU *in_apdu,
257                                int reason, const char *addinfo)
258 {
259     Z_APDU *apdu = create_APDU(Z_APDU_close, in_apdu);
260     
261     *apdu->u.close->closeReason = reason;
262     if (addinfo)
263         apdu->u.close->diagnosticInformation = odr_strdup(m_odr, addinfo);
264     return apdu;
265 }
266
267 Z_APDU *yp2::odr::create_APDU(int type, Z_APDU *in_apdu)
268 {
269     return yp2::util::create_APDU(m_odr, type, in_apdu);
270 }
271
272 Z_APDU *yp2::util::create_APDU(ODR odr, int type, Z_APDU *in_apdu)
273 {
274     Z_APDU *out_apdu = zget_APDU(odr, type);
275
276     Z_ReferenceId **id_to = yp2::util::get_referenceId(out_apdu);
277     *id_to = 0;
278     if (in_apdu)
279     {
280         Z_ReferenceId **id_from = yp2::util::get_referenceId(in_apdu);
281         if (id_from && *id_from && id_to)
282         {
283             *id_to = (Z_ReferenceId*) odr_malloc (odr, sizeof(**id_to));
284             (*id_to)->size = (*id_to)->len = (*id_from)->len;
285             (*id_to)->buf = (unsigned char*) odr_malloc(odr, (*id_to)->len);
286             memcpy((*id_to)->buf, (*id_from)->buf, (*id_to)->len);
287         }
288         else if (id_to)
289             *id_to = 0;
290     }
291     return out_apdu;
292 }
293
294 Z_APDU *yp2::odr::create_initResponse(Z_APDU *in_apdu,
295                                       int error, const char *addinfo)
296 {
297     Z_APDU *apdu = create_APDU(Z_APDU_initResponse, in_apdu);
298     if (error)
299     {
300         apdu->u.initResponse->userInformationField =
301             zget_init_diagnostics(m_odr, error, addinfo);
302         *apdu->u.initResponse->result = 0;
303     }
304     return apdu;
305 }
306
307 Z_APDU *yp2::odr::create_searchResponse(Z_APDU *in_apdu,
308                                         int error, const char *addinfo)
309 {
310     Z_APDU *apdu = create_APDU(Z_APDU_searchResponse, in_apdu);
311     if (error)
312     {
313         Z_Records *rec = (Z_Records *) odr_malloc(m_odr, sizeof(Z_Records));
314         *apdu->u.searchResponse->searchStatus = 0;
315         apdu->u.searchResponse->records = rec;
316         rec->which = Z_Records_NSD;
317         rec->u.nonSurrogateDiagnostic =
318             zget_DefaultDiagFormat(m_odr, error, addinfo);
319         
320     }
321     return apdu;
322 }
323
324 Z_APDU *yp2::odr::create_presentResponse(Z_APDU *in_apdu,
325                                          int error, const char *addinfo)
326 {
327     Z_APDU *apdu = create_APDU(Z_APDU_presentResponse, in_apdu);
328     if (error)
329     {
330         Z_Records *rec = (Z_Records *) odr_malloc(m_odr, sizeof(Z_Records));
331         apdu->u.presentResponse->records = rec;
332         
333         rec->which = Z_Records_NSD;
334         rec->u.nonSurrogateDiagnostic =
335             zget_DefaultDiagFormat(m_odr, error, addinfo);
336         *apdu->u.presentResponse->presentStatus = Z_PresentStatus_failure;
337     }
338     return apdu;
339 }
340
341 Z_APDU *yp2::odr::create_scanResponse(Z_APDU *in_apdu,
342                                       int error, const char *addinfo)
343 {
344     Z_APDU *apdu = create_APDU(Z_APDU_scanResponse, in_apdu);
345     Z_ScanResponse *res = apdu->u.scanResponse;
346     res->entries = (Z_ListEntries *) odr_malloc(m_odr, sizeof(*res->entries));
347     res->entries->num_entries = 0;
348     res->entries->entries = 0;
349
350     if (error)
351     {
352         *res->scanStatus = Z_Scan_failure;
353
354         res->entries->num_nonsurrogateDiagnostics = 1;
355         res->entries->nonsurrogateDiagnostics = (Z_DiagRec **)
356             odr_malloc(m_odr, sizeof(Z_DiagRec *));
357         res->entries->nonsurrogateDiagnostics[0] = 
358             zget_DiagRec(m_odr, error, addinfo);
359     }
360     else
361     {
362         res->entries->num_nonsurrogateDiagnostics = 0;
363         res->entries->nonsurrogateDiagnostics = 0;
364     }
365     return apdu;
366 }
367
368 Z_GDU *yp2::odr::create_HTTP_Response(yp2::Session &session,
369                                       Z_HTTP_Request *hreq, int code)
370 {
371     const char *response_version = "1.0";
372     bool keepalive = false;
373     if (!strcmp(hreq->version, "1.0")) 
374     {
375         const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
376         if (v && !strcmp(v, "Keep-Alive"))
377             keepalive = true;
378         else
379             session.close();
380         response_version = "1.0";
381     }
382     else
383     {
384         const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
385         if (v && !strcmp(v, "close"))
386             session.close();
387         else
388             keepalive = true;
389         response_version = "1.1";
390     }
391
392     Z_GDU *gdu = z_get_HTTP_Response(m_odr, code);
393     Z_HTTP_Response *hres = gdu->u.HTTP_Response;
394     hres->version = odr_strdup(m_odr, response_version);
395     if (keepalive)
396         z_HTTP_header_add(m_odr, &hres->headers, "Connection", "Keep-Alive");
397     
398     return gdu;
399 }
400
401 Z_ReferenceId **yp2::util::get_referenceId(Z_APDU *apdu)
402 {
403     switch (apdu->which)
404     {
405     case  Z_APDU_initRequest:
406         return &apdu->u.initRequest->referenceId; 
407     case  Z_APDU_initResponse:
408         return &apdu->u.initResponse->referenceId;
409     case  Z_APDU_searchRequest:
410         return &apdu->u.searchRequest->referenceId;
411     case  Z_APDU_searchResponse:
412         return &apdu->u.searchResponse->referenceId;
413     case  Z_APDU_presentRequest:
414         return &apdu->u.presentRequest->referenceId;
415     case  Z_APDU_presentResponse:
416         return &apdu->u.presentResponse->referenceId;
417     case  Z_APDU_deleteResultSetRequest:
418         return &apdu->u.deleteResultSetRequest->referenceId;
419     case  Z_APDU_deleteResultSetResponse:
420         return &apdu->u.deleteResultSetResponse->referenceId;
421     case  Z_APDU_accessControlRequest:
422         return &apdu->u.accessControlRequest->referenceId;
423     case  Z_APDU_accessControlResponse:
424         return &apdu->u.accessControlResponse->referenceId;
425     case  Z_APDU_resourceControlRequest:
426         return &apdu->u.resourceControlRequest->referenceId;
427     case  Z_APDU_resourceControlResponse:
428         return &apdu->u.resourceControlResponse->referenceId;
429     case  Z_APDU_triggerResourceControlRequest:
430         return &apdu->u.triggerResourceControlRequest->referenceId;
431     case  Z_APDU_resourceReportRequest:
432         return &apdu->u.resourceReportRequest->referenceId;
433     case  Z_APDU_resourceReportResponse:
434         return &apdu->u.resourceReportResponse->referenceId;
435     case  Z_APDU_scanRequest:
436         return &apdu->u.scanRequest->referenceId;
437     case  Z_APDU_scanResponse:
438         return &apdu->u.scanResponse->referenceId;
439     case  Z_APDU_sortRequest:
440         return &apdu->u.sortRequest->referenceId;
441     case  Z_APDU_sortResponse:
442         return &apdu->u.sortResponse->referenceId;
443     case  Z_APDU_segmentRequest:
444         return &apdu->u.segmentRequest->referenceId;
445     case  Z_APDU_extendedServicesRequest:
446         return &apdu->u.extendedServicesRequest->referenceId;
447     case  Z_APDU_extendedServicesResponse:
448         return &apdu->u.extendedServicesResponse->referenceId;
449     case  Z_APDU_close:
450         return &apdu->u.close->referenceId;
451     }
452     return 0;
453 }
454
455
456 /*
457  * Local variables:
458  * c-basic-offset: 4
459  * indent-tabs-mode: nil
460  * c-file-style: "stroustrup"
461  * End:
462  * vim: shiftwidth=4 tabstop=8 expandtab
463  */