Fix Metaproxy stops logging after check config failed MP-590
[metaproxy-moved-to-github.git] / src / sru_util.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 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 <metaproxy/util.hpp>
21
22 #include <iostream>
23 #include <string>
24
25 namespace mp = metaproxy_1;
26
27 // Doxygen doesn't like mp::gdu, so we use this instead
28 namespace mp_util = metaproxy_1::util;
29
30 const std::string xmlns_explain("http://explain.z3950.org/dtd/2.0/");
31
32 bool mp_util::build_sru_debug_package(mp::Package &package)
33 {
34     Z_GDU *zgdu_req = package.request().get();
35     if (zgdu_req && zgdu_req->which == Z_GDU_HTTP_Request)
36     {
37         Z_HTTP_Request* http_req =  zgdu_req->u.HTTP_Request;
38         std::string content = mp_util::http_headers_debug(*http_req);
39         int http_code = 400;
40         mp_util::http_response(package, content, http_code);
41         return true;
42     }
43     package.session().close();
44     return false;
45 }
46
47 mp_util::SRUServerInfo mp_util::get_sru_server_info(mp::Package &package)
48 {
49     mp_util::SRUServerInfo sruinfo;
50
51     // getting host and port info
52     sruinfo.host = "localhost";
53     sruinfo.port = "80";
54
55     // overwriting host and port info if set from HTTP Host header
56     Z_GDU *zgdu_req = package.request().get();
57     if (zgdu_req && zgdu_req->which == Z_GDU_HTTP_Request)
58     {
59         Z_HTTP_Request* http_req =  zgdu_req->u.HTTP_Request;
60         if (http_req)
61         {
62             std::string http_path = http_req->path;
63
64             // taking out GET parameters
65             std::string::size_type ipath = http_path.rfind("?");
66             if (ipath != std::string::npos)
67                 http_path.assign(http_path, 0, ipath);
68
69             // assign to database name
70             if (http_path.size() > 1)
71                 sruinfo.database.assign(http_path, 1, std::string::npos);
72
73             std::string http_host_address
74                 = mp_util::http_header_value(http_req->headers, "Host");
75
76             std::string::size_type iaddress = http_host_address.rfind(":");
77             if (iaddress != std::string::npos)
78             {
79                 sruinfo.host.assign(http_host_address, 0, iaddress);
80                 sruinfo.port.assign(http_host_address, iaddress + 1,
81                                     std::string::npos);
82             }
83         }
84     }
85
86     //std::cout << "sruinfo.database " << sruinfo.database << "\n";
87     //std::cout << "sruinfo.host " << sruinfo.host << "\n";
88     //std::cout << "sruinfo.port " << sruinfo.port << "\n";
89
90     return sruinfo;
91 }
92
93
94 bool mp_util::build_sru_explain(metaproxy_1::Package &package,
95                                 metaproxy_1::odr &odr_en,
96                                 Z_SRW_PDU *sru_pdu_res,
97                                 SRUServerInfo sruinfo,
98                                 const xmlNode *explain,
99                                 Z_SRW_explainRequest const *er_req)
100 {
101
102     // building SRU explain record
103     std::string explain_xml;
104
105     if (explain == 0)
106     {
107         explain_xml
108             = mp_util::to_string(
109                 "<explain  xmlns=\"" + xmlns_explain + "\">\n"
110                 "  <serverInfo protocol='SRU'>\n"
111                 "    <host>")
112             + sruinfo.host
113             + mp_util::to_string("</host>\n"
114                                  "    <port>")
115             + sruinfo.port
116             + mp_util::to_string("</port>\n"
117                                  "    <database>")
118             + sruinfo.database
119             + mp_util::to_string("</database>\n"
120                                  "  </serverInfo>\n"
121                                  "</explain>\n");
122     }
123     else
124     {
125         // make new XML DOC with given explain node
126         xmlDocPtr doc =  xmlNewDoc(BAD_CAST "1.0");
127         xmlDocSetRootElement(doc, (xmlNode*)explain);
128
129         xmlChar *xmlbuff;
130         int xmlbuffsz;
131         xmlDocDumpFormatMemory(doc, &xmlbuff, &xmlbuffsz, 1);
132
133         explain_xml.assign((const char*)xmlbuff, 0, xmlbuffsz);
134         xmlFree(xmlbuff);
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         {
190             ctype += "; charset=";
191             ctype += charset;
192         }
193
194         z_HTTP_header_add(odr_en,
195                           &http_res->headers, "Content-Type", ctype.c_str());
196
197         // packaging Z_SOAP into HTML response
198         static Z_SOAP_Handler soap_handlers[4] = {
199             {(char *)YAZ_XMLNS_SRU_v1_1, 0, (Z_SOAP_fun) yaz_srw_codec},
200             {(char *)YAZ_XMLNS_SRU_v1_0, 0,  (Z_SOAP_fun) yaz_srw_codec},
201             {(char *)YAZ_XMLNS_UPDATE_v0_9, 0, (Z_SOAP_fun) yaz_ucp_codec},
202             {0, 0, 0}
203         };
204
205
206         // empty stylesheet means NO stylesheet
207         if (stylesheet && *stylesheet == '\0')
208             stylesheet = 0;
209
210         // encoding SRU package
211
212         soap->u.generic->p  = (void*) sru_pdu_res;
213         //int ret =
214         z_soap_codec_enc_xsl(odr_en, &soap,
215                              &http_res->content_buf, &http_res->content_len,
216                              soap_handlers, charset, stylesheet);
217
218
219         package.response() = zgdu_res;
220         return true;
221     }
222     package.session().close();
223     return false;
224 }
225
226
227
228 Z_SRW_PDU * mp_util::decode_sru_request(mp::Package &package,
229                                         mp::odr &odr_de,
230                                         mp::odr &odr_en,
231                                         Z_SRW_diagnostic **diagnostic,
232                                         int *num_diagnostic,
233                                         Z_SOAP **soap,
234                                         char *charset)
235 {
236     Z_GDU *zgdu_req = package.request().get();
237     Z_SRW_PDU *sru_pdu_req = 0;
238
239     //assert((zgdu_req->which == Z_GDU_HTTP_Request));
240
241     //ignoring all non HTTP_Request packages
242     if (!zgdu_req || !(zgdu_req->which == Z_GDU_HTTP_Request))
243     {
244         return 0;
245     }
246
247     Z_HTTP_Request* http_req =  zgdu_req->u.HTTP_Request;
248     if (! http_req)
249         return 0;
250
251     // checking if we got a SRU GET/POST/SOAP HTTP package
252     // closing connection if we did not ...
253     if (0 == yaz_sru_decode(http_req, &sru_pdu_req, soap,
254                             odr_de, &charset,
255                             diagnostic, num_diagnostic))
256     {
257         return sru_pdu_req;
258     }
259     else if (0 == yaz_srw_decode(http_req, &sru_pdu_req, soap,
260                                  odr_de, &charset))
261         return sru_pdu_req;
262     else
263     {
264         //sru_pdu_res = sru_pdu_res_exp;
265         package.session().close();
266         return 0;
267     }
268     return 0;
269 }
270
271
272 bool
273 mp_util::check_sru_query_exists(mp::Package &package,
274                                 mp::odr &odr_en,
275                                 Z_SRW_PDU *sru_pdu_res,
276                                 Z_SRW_searchRetrieveRequest const *sr_req)
277 {
278 #ifdef Z_SRW_query_type_cql
279     if ((sr_req->query_type == Z_SRW_query_type_cql && !sr_req->query.cql))
280     {
281         yaz_add_srw_diagnostic(odr_en,
282                                &(sru_pdu_res->u.response->diagnostics),
283                                &(sru_pdu_res->u.response->num_diagnostics),
284                                YAZ_SRW_MANDATORY_PARAMETER_NOT_SUPPLIED,
285                                "query");
286         yaz_add_srw_diagnostic(odr_en,
287                                &(sru_pdu_res->u.response->diagnostics),
288                                &(sru_pdu_res->u.response->num_diagnostics),
289                                YAZ_SRW_QUERY_SYNTAX_ERROR,
290                                "CQL query is empty");
291         return false;
292     }
293     if ((sr_req->query_type == Z_SRW_query_type_xcql && !sr_req->query.xcql))
294     {
295         yaz_add_srw_diagnostic(odr_en,
296                                &(sru_pdu_res->u.response->diagnostics),
297                                &(sru_pdu_res->u.response->num_diagnostics),
298                                YAZ_SRW_QUERY_SYNTAX_ERROR,
299                                "XCQL query is empty");
300         return false;
301     }
302     if ((sr_req->query_type == Z_SRW_query_type_pqf && !sr_req->query.pqf))
303     {
304         yaz_add_srw_diagnostic(odr_en,
305                                &(sru_pdu_res->u.response->diagnostics),
306                                &(sru_pdu_res->u.response->num_diagnostics),
307                                YAZ_SRW_QUERY_SYNTAX_ERROR,
308                                "PQF query is empty");
309         return false;
310     }
311 #else
312     if (!sr_req->query)
313     {
314         yaz_add_srw_diagnostic(odr_en,
315                                &(sru_pdu_res->u.response->diagnostics),
316                                &(sru_pdu_res->u.response->num_diagnostics),
317                                YAZ_SRW_MANDATORY_PARAMETER_NOT_SUPPLIED,
318                                "query");
319         yaz_add_srw_diagnostic(odr_en,
320                                &(sru_pdu_res->u.response->diagnostics),
321                                &(sru_pdu_res->u.response->num_diagnostics),
322                                YAZ_SRW_QUERY_SYNTAX_ERROR,
323                                "CQL query is empty");
324         return false;
325     }
326 #endif
327     return true;
328 }
329
330
331 Z_ElementSetNames *
332 mp_util::build_esn_from_schema(mp::odr &odr_en, const char *schema)
333 {
334     if (!schema)
335         return 0;
336
337     Z_ElementSetNames *esn
338         = (Z_ElementSetNames *) odr_malloc(odr_en, sizeof(Z_ElementSetNames));
339     esn->which = Z_ElementSetNames_generic;
340     esn->u.generic = odr_strdup(odr_en, schema);
341     return esn;
342 }
343
344
345 std::ostream& std::operator<<(std::ostream& os, Z_SRW_PDU& srw_pdu)
346 {
347     os << "SRU";
348
349     switch (srw_pdu.which)
350     {
351     case  Z_SRW_searchRetrieve_request:
352         os << " " << "searchRetrieveRequest";
353         {
354             Z_SRW_searchRetrieveRequest *sr = srw_pdu.u.request;
355             if (sr)
356             {
357                 if (sr->database)
358                     os << " " << (sr->database);
359                 else
360                     os << " -";
361                 if (sr->startRecord)
362                     os << " " << *(sr->startRecord);
363                 else
364                     os << " -";
365                 if (sr->maximumRecords)
366                     os << " " << *(sr->maximumRecords);
367                 else
368                     os << " -";
369                 if (sr->recordPacking)
370                     os << " " << (sr->recordPacking);
371                 else
372                     os << " -";
373
374                 if (sr->recordSchema)
375                     os << " " << (sr->recordSchema);
376                 else
377                     os << " -";
378
379 #ifdef Z_SRW_query_type_cql
380                 switch (sr->query_type){
381                 case Z_SRW_query_type_cql:
382                     os << " CQL";
383                     if (sr->query.cql)
384                         os << " " << sr->query.cql;
385                     break;
386                 case Z_SRW_query_type_xcql:
387                     os << " XCQL";
388                     break;
389                 case Z_SRW_query_type_pqf:
390                     os << " PQF";
391                     if (sr->query.pqf)
392                         os << " " << sr->query.pqf;
393                     break;
394                 }
395 #else
396                 os << " " << (sr->queryType ? sr->queryType : "cql")
397                    << " " << sr->query;
398 #endif
399             }
400         }
401         break;
402     case  Z_SRW_searchRetrieve_response:
403         os << " " << "searchRetrieveResponse";
404         {
405             Z_SRW_searchRetrieveResponse *sr = srw_pdu.u.response;
406             if (sr)
407             {
408                 if (! (sr->num_diagnostics))
409                 {
410                     os << " OK";
411                     if (sr->numberOfRecords)
412                         os << " " << *(sr->numberOfRecords);
413                     else
414                         os << " -";
415                     //if (sr->num_records)
416                     os << " " << (sr->num_records);
417                     //else
418                     //os << " -";
419                     if (sr->nextRecordPosition)
420                         os << " " << *(sr->nextRecordPosition);
421                     else
422                         os << " -";
423                 }
424                 else
425                 {
426                     os << " DIAG";
427                     if (sr->diagnostics && sr->diagnostics->uri)
428                         os << " " << (sr->diagnostics->uri);
429                     else
430                         os << " -";
431                     if (sr->diagnostics && sr->diagnostics->message)
432                         os << " " << (sr->diagnostics->message);
433                     else
434                         os << " -";
435                     if (sr->diagnostics && sr->diagnostics->details)
436                         os << " " << (sr->diagnostics->details);
437                     else
438                         os << " -";
439                 }
440
441
442             }
443         }
444         break;
445     case  Z_SRW_explain_request:
446         os << " " << "explainRequest";
447         break;
448     case  Z_SRW_explain_response:
449         os << " " << "explainResponse";
450         break;
451     case  Z_SRW_scan_request:
452         os << " " << "scanRequest";
453         break;
454     case  Z_SRW_scan_response:
455         os << " " << "scanResponse";
456         break;
457     case  Z_SRW_update_request:
458         os << " " << "updateRequest";
459         break;
460     case  Z_SRW_update_response:
461         os << " " << "updateResponse";
462         break;
463     default:
464         os << " " << "UNKNOWN";
465     }
466
467     return os;
468 }
469
470 /*
471  * Local variables:
472  * c-basic-offset: 4
473  * c-file-style: "Stroustrup"
474  * indent-tabs-mode: nil
475  * End:
476  * vim: shiftwidth=4 tabstop=8 expandtab
477  */
478