Reformat and remove unused code
[metaproxy-moved-to-github.git] / src / sru_util.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2009 Index Data
3
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include "sru_util.hpp"
20 #include "util.hpp"
21
22 //#include <yaz/wrbuf.h>
23 //#include <yaz/querytowrbuf.h>
24
25 #include <iostream>
26 #include <string>
27
28 namespace mp = metaproxy_1;
29
30 // Doxygen doesn't like mp::gdu, so we use this instead
31 namespace mp_util = metaproxy_1::util;
32
33 const std::string xmlns_explain("http://explain.z3950.org/dtd/2.0/");
34
35 bool mp_util::build_sru_debug_package(mp::Package &package)
36 {
37     Z_GDU *zgdu_req = package.request().get();
38     if (zgdu_req && zgdu_req->which == Z_GDU_HTTP_Request)
39     {    
40         Z_HTTP_Request* http_req =  zgdu_req->u.HTTP_Request;
41         std::string content = mp_util::http_headers_debug(*http_req);
42         int http_code = 400;    
43         mp_util::http_response(package, content, http_code);
44         return true;
45     }
46     package.session().close();
47     return false;
48 }
49
50 mp_util::SRUServerInfo mp_util::get_sru_server_info(mp::Package &package)
51 {
52     mp_util::SRUServerInfo sruinfo;
53
54     // getting host and port info
55     sruinfo.host = package.origin().listen_host();
56     sruinfo.port = mp_util::to_string(package.origin().listen_port());
57
58     // overwriting host and port info if set from HTTP Host header
59     Z_GDU *zgdu_req = package.request().get();
60     if (zgdu_req && zgdu_req->which == Z_GDU_HTTP_Request)
61     {
62         Z_HTTP_Request* http_req =  zgdu_req->u.HTTP_Request;
63         if (http_req)
64         {
65             std::string http_path = http_req->path;
66             
67             // taking out GET parameters
68             std::string::size_type ipath = http_path.rfind("?");
69             if (ipath != std::string::npos)
70                 http_path.assign(http_path, 0, ipath);
71
72             // assign to database name
73             if (http_path.size() > 1)
74                 sruinfo.database.assign(http_path, 1, std::string::npos);
75             
76             std::string http_host_address
77                 = mp_util::http_header_value(http_req->headers, "Host");
78
79             std::string::size_type iaddress = http_host_address.rfind(":");
80             if (iaddress != std::string::npos)
81             {
82                 sruinfo.host.assign(http_host_address, 0, iaddress);
83                 sruinfo.port.assign(http_host_address, iaddress + 1, 
84                                     std::string::npos);
85             }
86         }
87     }
88     
89     //std::cout << "sruinfo.database " << sruinfo.database << "\n";
90     //std::cout << "sruinfo.host " << sruinfo.host << "\n";
91     //std::cout << "sruinfo.port " << sruinfo.port << "\n";
92
93     return sruinfo;
94 }
95
96
97 bool mp_util::build_sru_explain(metaproxy_1::Package &package, 
98                                 metaproxy_1::odr &odr_en,
99                                 Z_SRW_PDU *sru_pdu_res,
100                                 SRUServerInfo sruinfo,
101                                 const xmlNode *explain,
102                                 Z_SRW_explainRequest const *er_req)
103 {
104
105     // building SRU explain record
106     std::string explain_xml;    
107
108     if (explain == 0){
109         explain_xml 
110             = mp_util::to_string(
111                 "<explain  xmlns=\"" + xmlns_explain + "\">\n"
112                 "  <serverInfo protocol='SRU'>\n"
113                 "    <host>")
114             + sruinfo.host
115             + mp_util::to_string("</host>\n"
116                                  "    <port>")
117             + sruinfo.port
118             + mp_util::to_string("</port>\n"
119                                  "    <database>")
120             + sruinfo.database
121             + mp_util::to_string("</database>\n"
122                                  "  </serverInfo>\n"
123                                  "</explain>\n");
124     }
125     else {
126         // make new XML DOC with given explain node
127         xmlDocPtr doc =  xmlNewDoc(BAD_CAST "1.0");
128         xmlDocSetRootElement(doc, (xmlNode*)explain);
129
130         xmlChar *xmlbuff;
131         int xmlbuffsz;
132         xmlDocDumpFormatMemory(doc, &xmlbuff, &xmlbuffsz, 1);
133
134         explain_xml.assign((const char*)xmlbuff, 0, xmlbuffsz);
135     }
136
137
138     // z3950'fy recordPacking
139     int record_packing = Z_SRW_recordPacking_XML;
140     if (er_req && er_req->recordPacking && 's' == *(er_req->recordPacking))
141         record_packing = Z_SRW_recordPacking_string;    
142     
143     // preparing explain record insert
144     Z_SRW_explainResponse *sru_res = sru_pdu_res->u.explain_response;
145     
146     // inserting one and only explain record
147     
148     sru_res->record.recordPosition = odr_intdup(odr_en, 1);
149     sru_res->record.recordPacking = record_packing;
150     sru_res->record.recordSchema = (char *)xmlns_explain.c_str();
151     sru_res->record.recordData_len = 1 + explain_xml.size();
152     sru_res->record.recordData_buf
153         = odr_strdupn(odr_en, (const char *)explain_xml.c_str(), 
154                       1 + explain_xml.size());
155
156     return true;
157 }
158
159
160 bool mp_util::build_sru_response(mp::Package &package, 
161                                              mp::odr &odr_en,
162                                              Z_SOAP *soap,
163                                              const Z_SRW_PDU *sru_pdu_res,
164                                              char *charset,
165                                              const char *stylesheet) 
166 {
167
168     // SRU request package translation to Z3950 package
169     //if (sru_pdu_res)
170     //    std::cout << *(const_cast<Z_SRW_PDU *>(sru_pdu_res)) << "\n";
171     //else
172     //    std::cout << "SRU empty\n";
173
174     
175     Z_GDU *zgdu_req = package.request().get();
176     if  (zgdu_req && zgdu_req->which == Z_GDU_HTTP_Request)
177     {    
178         Z_GDU *zgdu_res //= z_get_HTTP_Response(odr_en, 200);
179             = odr_en.create_HTTP_Response(package.session(), 
180                                           zgdu_req->u.HTTP_Request, 
181                                           200);
182
183         // adding HTTP response code and headers
184         Z_HTTP_Response * http_res = zgdu_res->u.HTTP_Response;
185         //http_res->code = http_code;
186         
187         std::string ctype("text/xml");
188         if (charset){
189             ctype += "; charset=";
190             ctype += charset;
191         }
192
193         z_HTTP_header_add(odr_en, 
194                           &http_res->headers, "Content-Type", ctype.c_str());
195
196          // packaging Z_SOAP into HTML response
197          static Z_SOAP_Handler soap_handlers[4] = {
198               {"http://www.loc.gov/zing/srw/", 0,
199                (Z_SOAP_fun) yaz_srw_codec},
200               {"http://www.loc.gov/zing/srw/v1.0/", 0,
201                (Z_SOAP_fun) yaz_srw_codec},
202               {"http://www.loc.gov/zing/srw/update/", 0,
203                (Z_SOAP_fun) yaz_ucp_codec},
204               {0, 0, 0}
205          };
206
207
208          // empty stylesheet means NO stylesheet
209          if (stylesheet && *stylesheet == '\0')
210              stylesheet = 0;
211          
212          // encoding SRU package
213          
214          soap->u.generic->p  = (void*) sru_pdu_res;         
215          //int ret = 
216          z_soap_codec_enc_xsl(odr_en, &soap, 
217                               &http_res->content_buf, &http_res->content_len,
218                               soap_handlers, charset, stylesheet);
219          
220
221          package.response() = zgdu_res;
222          return true;
223     }
224     package.session().close();
225     return false;
226 }
227
228
229
230 Z_SRW_PDU * mp_util::decode_sru_request(mp::Package &package,
231                                         mp::odr &odr_de,
232                                         mp::odr &odr_en,
233                                         Z_SRW_PDU *sru_pdu_res,
234                                         Z_SOAP **soap,
235                                         char *charset,
236                                         char *stylesheet) 
237 {
238     Z_GDU *zgdu_req = package.request().get();
239     Z_SRW_PDU *sru_pdu_req = 0;
240
241     //assert((zgdu_req->which == Z_GDU_HTTP_Request));
242     
243     //ignoring all non HTTP_Request packages
244     if (!zgdu_req || !(zgdu_req->which == Z_GDU_HTTP_Request))
245     {
246         return 0;
247     }
248     
249     Z_HTTP_Request* http_req =  zgdu_req->u.HTTP_Request;
250     if (! http_req)
251         return 0;
252
253     // checking if we got a SRU GET/POST/SOAP HTTP package
254     // closing connection if we did not ...
255     if (0 == yaz_sru_decode(http_req, &sru_pdu_req, soap, 
256                             odr_de, &charset, 
257                             &(sru_pdu_res->u.response->diagnostics), 
258                             &(sru_pdu_res->u.response->num_diagnostics)))
259     {
260         if (sru_pdu_res->u.response->num_diagnostics)
261         {
262             //sru_pdu_res = sru_pdu_res_exp;
263             package.session().close();
264             return 0;
265         }
266         return sru_pdu_req;
267     }
268     else if (0 == yaz_srw_decode(http_req, &sru_pdu_req, soap, 
269                                  odr_de, &charset))
270         return sru_pdu_req;
271     else 
272     {
273         //sru_pdu_res = sru_pdu_res_exp;
274         package.session().close();
275         return 0;
276     }
277     return 0;
278 }
279
280
281 bool 
282 mp_util::check_sru_query_exists(mp::Package &package, 
283                                 mp::odr &odr_en,
284                                 Z_SRW_PDU *sru_pdu_res, 
285                                 Z_SRW_searchRetrieveRequest const *sr_req)
286 {
287     if ((sr_req->query_type == Z_SRW_query_type_cql && !sr_req->query.cql))
288     {
289         yaz_add_srw_diagnostic(odr_en,
290                                &(sru_pdu_res->u.response->diagnostics), 
291                                &(sru_pdu_res->u.response->num_diagnostics), 
292                                7, "query");
293         yaz_add_srw_diagnostic(odr_en,
294                                &(sru_pdu_res->u.response->diagnostics), 
295                                &(sru_pdu_res->u.response->num_diagnostics), 
296                                10, "CQL query is empty");
297         return false;
298     }
299     if ((sr_req->query_type == Z_SRW_query_type_xcql && !sr_req->query.xcql))
300     {
301          yaz_add_srw_diagnostic(odr_en,
302                                &(sru_pdu_res->u.response->diagnostics), 
303                                &(sru_pdu_res->u.response->num_diagnostics), 
304                                 10, "XCQL query is empty");
305          return false;
306     }
307     if ((sr_req->query_type == Z_SRW_query_type_pqf && !sr_req->query.pqf))
308     {
309         yaz_add_srw_diagnostic(odr_en,
310                                &(sru_pdu_res->u.response->diagnostics), 
311                                &(sru_pdu_res->u.response->num_diagnostics), 
312                                10, "PQF query is empty");
313         return false;
314     }
315     return true;
316 }
317
318
319 Z_ElementSetNames * 
320 mp_util::build_esn_from_schema(mp::odr &odr_en, const char *schema)
321 {
322     if (!schema)
323         return 0;
324     
325     Z_ElementSetNames *esn 
326         = (Z_ElementSetNames *) odr_malloc(odr_en, sizeof(Z_ElementSetNames));
327     esn->which = Z_ElementSetNames_generic;
328     esn->u.generic = odr_strdup(odr_en, schema);
329     return esn; 
330 }
331
332
333 std::ostream& std::operator<<(std::ostream& os, Z_SRW_PDU& srw_pdu) 
334 {
335     os << "SRU";
336     
337     switch(srw_pdu.which) {
338     case  Z_SRW_searchRetrieve_request:
339         os << " " << "searchRetrieveRequest";
340         {
341             Z_SRW_searchRetrieveRequest *sr = srw_pdu.u.request;
342             if (sr)
343             {
344                 if (sr->database)
345                     os << " " << (sr->database);
346                 else
347                     os << " -";
348                 if (sr->startRecord)
349                     os << " " << *(sr->startRecord);
350                 else
351                     os << " -";
352                 if (sr->maximumRecords)
353                     os << " " << *(sr->maximumRecords);
354                 else
355                     os << " -";
356                 if (sr->recordPacking)
357                     os << " " << (sr->recordPacking);
358                 else
359                     os << " -";
360
361                 if (sr->recordSchema)
362                     os << " " << (sr->recordSchema);
363                 else
364                     os << " -";
365                 
366                 switch (sr->query_type){
367                 case Z_SRW_query_type_cql:
368                     os << " CQL";
369                     if (sr->query.cql)
370                         os << " " << sr->query.cql;
371                     break;
372                 case Z_SRW_query_type_xcql:
373                     os << " XCQL";
374                     break;
375                 case Z_SRW_query_type_pqf:
376                     os << " PQF";
377                     if (sr->query.pqf)
378                         os << " " << sr->query.pqf;
379                     break;
380                 }
381             }
382         }
383         break;
384     case  Z_SRW_searchRetrieve_response:
385         os << " " << "searchRetrieveResponse";
386         {
387             Z_SRW_searchRetrieveResponse *sr = srw_pdu.u.response;
388             if (sr)
389             {
390                 if (! (sr->num_diagnostics))
391                 {
392                     os << " OK";
393                     if (sr->numberOfRecords)
394                         os << " " << *(sr->numberOfRecords);
395                     else
396                         os << " -";
397                     //if (sr->num_records)
398                     os << " " << (sr->num_records);
399                     //else
400                     //os << " -";
401                     if (sr->nextRecordPosition)
402                         os << " " << *(sr->nextRecordPosition);
403                     else
404                         os << " -";
405                 } 
406                 else
407                 {
408                     os << " DIAG";
409                     if (sr->diagnostics && sr->diagnostics->uri)
410                         os << " " << (sr->diagnostics->uri);
411                     else
412                         os << " -";
413                     if (sr->diagnostics && sr->diagnostics->message)
414                         os << " " << (sr->diagnostics->message);
415                     else
416                         os << " -";
417                     if (sr->diagnostics && sr->diagnostics->details)
418                         os << " " << (sr->diagnostics->details);
419                     else
420                         os << " -";
421                 }
422                 
423                     
424             }
425         }
426         break;
427     case  Z_SRW_explain_request:
428         os << " " << "explainRequest";
429         break;
430     case  Z_SRW_explain_response:
431         os << " " << "explainResponse";
432         break;
433     case  Z_SRW_scan_request:
434         os << " " << "scanRequest";
435         break;
436     case  Z_SRW_scan_response:
437         os << " " << "scanResponse";
438         break;
439     case  Z_SRW_update_request:
440         os << " " << "updateRequest";
441         break;
442     case  Z_SRW_update_response:
443         os << " " << "updateResponse";
444         break;
445     default: 
446         os << " " << "UNKNOWN";    
447     }
448
449     return os;    
450 }
451
452 /*
453  * Local variables:
454  * c-basic-offset: 4
455  * c-file-style: "Stroustrup"
456  * indent-tabs-mode: nil
457  * End:
458  * vim: shiftwidth=4 tabstop=8 expandtab
459  */
460