Windows: use Boost 1.59, msvc 14.0
[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     if (!sr_req->query)
279     {
280         yaz_add_srw_diagnostic(odr_en,
281                                &(sru_pdu_res->u.response->diagnostics),
282                                &(sru_pdu_res->u.response->num_diagnostics),
283                                YAZ_SRW_MANDATORY_PARAMETER_NOT_SUPPLIED,
284                                "query");
285         yaz_add_srw_diagnostic(odr_en,
286                                &(sru_pdu_res->u.response->diagnostics),
287                                &(sru_pdu_res->u.response->num_diagnostics),
288                                YAZ_SRW_QUERY_SYNTAX_ERROR,
289                                "CQL query is empty");
290         return false;
291     }
292     return true;
293 }
294
295
296 Z_ElementSetNames *
297 mp_util::build_esn_from_schema(mp::odr &odr_en, const char *schema)
298 {
299     if (!schema)
300         return 0;
301
302     Z_ElementSetNames *esn
303         = (Z_ElementSetNames *) odr_malloc(odr_en, sizeof(Z_ElementSetNames));
304     esn->which = Z_ElementSetNames_generic;
305     esn->u.generic = odr_strdup(odr_en, schema);
306     return esn;
307 }
308
309
310 std::ostream& std::operator<<(std::ostream& os, Z_SRW_PDU& srw_pdu)
311 {
312     os << "SRU";
313
314     switch (srw_pdu.which)
315     {
316     case  Z_SRW_searchRetrieve_request:
317         os << " " << "searchRetrieveRequest";
318         {
319             Z_SRW_searchRetrieveRequest *sr = srw_pdu.u.request;
320             if (sr)
321             {
322                 if (sr->database)
323                     os << " " << (sr->database);
324                 else
325                     os << " -";
326                 if (sr->startRecord)
327                     os << " " << *(sr->startRecord);
328                 else
329                     os << " -";
330                 if (sr->maximumRecords)
331                     os << " " << *(sr->maximumRecords);
332                 else
333                     os << " -";
334                 if (sr->recordPacking)
335                     os << " " << (sr->recordPacking);
336                 else
337                     os << " -";
338
339                 if (sr->recordSchema)
340                     os << " " << (sr->recordSchema);
341                 else
342                     os << " -";
343                 os << " " << (sr->queryType ? sr->queryType : "cql")
344                    << " " << sr->query;
345             }
346         }
347         break;
348     case  Z_SRW_searchRetrieve_response:
349         os << " " << "searchRetrieveResponse";
350         {
351             Z_SRW_searchRetrieveResponse *sr = srw_pdu.u.response;
352             if (sr)
353             {
354                 if (! (sr->num_diagnostics))
355                 {
356                     os << " OK";
357                     if (sr->numberOfRecords)
358                         os << " " << *(sr->numberOfRecords);
359                     else
360                         os << " -";
361                     //if (sr->num_records)
362                     os << " " << (sr->num_records);
363                     //else
364                     //os << " -";
365                     if (sr->nextRecordPosition)
366                         os << " " << *(sr->nextRecordPosition);
367                     else
368                         os << " -";
369                 }
370                 else
371                 {
372                     os << " DIAG";
373                     if (sr->diagnostics && sr->diagnostics->uri)
374                         os << " " << (sr->diagnostics->uri);
375                     else
376                         os << " -";
377                     if (sr->diagnostics && sr->diagnostics->message)
378                         os << " " << (sr->diagnostics->message);
379                     else
380                         os << " -";
381                     if (sr->diagnostics && sr->diagnostics->details)
382                         os << " " << (sr->diagnostics->details);
383                     else
384                         os << " -";
385                 }
386
387
388             }
389         }
390         break;
391     case  Z_SRW_explain_request:
392         os << " " << "explainRequest";
393         break;
394     case  Z_SRW_explain_response:
395         os << " " << "explainResponse";
396         break;
397     case  Z_SRW_scan_request:
398         os << " " << "scanRequest";
399         break;
400     case  Z_SRW_scan_response:
401         os << " " << "scanResponse";
402         break;
403     case  Z_SRW_update_request:
404         os << " " << "updateRequest";
405         break;
406     case  Z_SRW_update_response:
407         os << " " << "updateResponse";
408         break;
409     default:
410         os << " " << "UNKNOWN";
411     }
412
413     return os;
414 }
415
416 /*
417  * Local variables:
418  * c-basic-offset: 4
419  * c-file-style: "Stroustrup"
420  * indent-tabs-mode: nil
421  * End:
422  * vim: shiftwidth=4 tabstop=8 expandtab
423  */
424