Public MP headers in include/metaproxy
[metaproxy-moved-to-github.git] / src / sru_util.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2010 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 = package.origin().listen_host();
53     sruinfo.port = mp_util::to_string(package.origin().listen_port());
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         explain_xml 
107             = mp_util::to_string(
108                 "<explain  xmlns=\"" + xmlns_explain + "\">\n"
109                 "  <serverInfo protocol='SRU'>\n"
110                 "    <host>")
111             + sruinfo.host
112             + mp_util::to_string("</host>\n"
113                                  "    <port>")
114             + sruinfo.port
115             + mp_util::to_string("</port>\n"
116                                  "    <database>")
117             + sruinfo.database
118             + mp_util::to_string("</database>\n"
119                                  "  </serverInfo>\n"
120                                  "</explain>\n");
121     }
122     else {
123         // make new XML DOC with given explain node
124         xmlDocPtr doc =  xmlNewDoc(BAD_CAST "1.0");
125         xmlDocSetRootElement(doc, (xmlNode*)explain);
126
127         xmlChar *xmlbuff;
128         int xmlbuffsz;
129         xmlDocDumpFormatMemory(doc, &xmlbuff, &xmlbuffsz, 1);
130
131         explain_xml.assign((const char*)xmlbuff, 0, xmlbuffsz);
132     }
133
134
135     // z3950'fy recordPacking
136     int record_packing = Z_SRW_recordPacking_XML;
137     if (er_req && er_req->recordPacking && 's' == *(er_req->recordPacking))
138         record_packing = Z_SRW_recordPacking_string;    
139     
140     // preparing explain record insert
141     Z_SRW_explainResponse *sru_res = sru_pdu_res->u.explain_response;
142     
143     // inserting one and only explain record
144     
145     sru_res->record.recordPosition = odr_intdup(odr_en, 1);
146     sru_res->record.recordPacking = record_packing;
147     sru_res->record.recordSchema = (char *)xmlns_explain.c_str();
148     sru_res->record.recordData_len = 1 + explain_xml.size();
149     sru_res->record.recordData_buf
150         = odr_strdupn(odr_en, (const char *)explain_xml.c_str(), 
151                       1 + explain_xml.size());
152
153     return true;
154 }
155
156
157 bool mp_util::build_sru_response(mp::Package &package, 
158                                  mp::odr &odr_en,
159                                  Z_SOAP *soap,
160                                  const Z_SRW_PDU *sru_pdu_res,
161                                  char *charset,
162                                  const char *stylesheet) 
163 {
164
165     // SRU request package translation to Z3950 package
166     //if (sru_pdu_res)
167     //    std::cout << *(const_cast<Z_SRW_PDU *>(sru_pdu_res)) << "\n";
168     //else
169     //    std::cout << "SRU empty\n";
170
171     
172     Z_GDU *zgdu_req = package.request().get();
173     if  (zgdu_req && zgdu_req->which == Z_GDU_HTTP_Request)
174     {    
175         Z_GDU *zgdu_res //= z_get_HTTP_Response(odr_en, 200);
176             = odr_en.create_HTTP_Response(package.session(), 
177                                           zgdu_req->u.HTTP_Request, 
178                                           200);
179
180         // adding HTTP response code and headers
181         Z_HTTP_Response * http_res = zgdu_res->u.HTTP_Response;
182         //http_res->code = http_code;
183         
184         std::string ctype("text/xml");
185         if (charset)
186         {
187             ctype += "; charset=";
188             ctype += charset;
189         }
190
191         z_HTTP_header_add(odr_en, 
192                           &http_res->headers, "Content-Type", ctype.c_str());
193
194          // packaging Z_SOAP into HTML response
195          static Z_SOAP_Handler soap_handlers[4] = {
196              {(char *)YAZ_XMLNS_SRU_v1_1, 0, (Z_SOAP_fun) yaz_srw_codec},
197              {(char *)YAZ_XMLNS_SRU_v1_0, 0,  (Z_SOAP_fun) yaz_srw_codec},
198              {(char *)YAZ_XMLNS_UPDATE_v0_9, 0, (Z_SOAP_fun) yaz_ucp_codec},
199              {0, 0, 0}
200          };
201
202
203          // empty stylesheet means NO stylesheet
204          if (stylesheet && *stylesheet == '\0')
205              stylesheet = 0;
206          
207          // encoding SRU package
208          
209          soap->u.generic->p  = (void*) sru_pdu_res;         
210          //int ret = 
211          z_soap_codec_enc_xsl(odr_en, &soap, 
212                               &http_res->content_buf, &http_res->content_len,
213                               soap_handlers, charset, stylesheet);
214          
215
216          package.response() = zgdu_res;
217          return true;
218     }
219     package.session().close();
220     return false;
221 }
222
223
224
225 Z_SRW_PDU * mp_util::decode_sru_request(mp::Package &package,
226                                         mp::odr &odr_de,
227                                         mp::odr &odr_en,
228                                         Z_SRW_PDU *sru_pdu_res,
229                                         Z_SOAP **soap,
230                                         char *charset,
231                                         char *stylesheet) 
232 {
233     Z_GDU *zgdu_req = package.request().get();
234     Z_SRW_PDU *sru_pdu_req = 0;
235
236     //assert((zgdu_req->which == Z_GDU_HTTP_Request));
237     
238     //ignoring all non HTTP_Request packages
239     if (!zgdu_req || !(zgdu_req->which == Z_GDU_HTTP_Request))
240     {
241         return 0;
242     }
243     
244     Z_HTTP_Request* http_req =  zgdu_req->u.HTTP_Request;
245     if (! http_req)
246         return 0;
247
248     // checking if we got a SRU GET/POST/SOAP HTTP package
249     // closing connection if we did not ...
250     if (0 == yaz_sru_decode(http_req, &sru_pdu_req, soap, 
251                             odr_de, &charset, 
252                             &(sru_pdu_res->u.response->diagnostics), 
253                             &(sru_pdu_res->u.response->num_diagnostics)))
254     {
255         if (sru_pdu_res->u.response->num_diagnostics)
256         {
257             //sru_pdu_res = sru_pdu_res_exp;
258             package.session().close();
259             return 0;
260         }
261         return sru_pdu_req;
262     }
263     else if (0 == yaz_srw_decode(http_req, &sru_pdu_req, soap, 
264                                  odr_de, &charset))
265         return sru_pdu_req;
266     else 
267     {
268         //sru_pdu_res = sru_pdu_res_exp;
269         package.session().close();
270         return 0;
271     }
272     return 0;
273 }
274
275
276 bool 
277 mp_util::check_sru_query_exists(mp::Package &package, 
278                                 mp::odr &odr_en,
279                                 Z_SRW_PDU *sru_pdu_res, 
280                                 Z_SRW_searchRetrieveRequest const *sr_req)
281 {
282     if ((sr_req->query_type == Z_SRW_query_type_cql && !sr_req->query.cql))
283     {
284         yaz_add_srw_diagnostic(odr_en,
285                                &(sru_pdu_res->u.response->diagnostics), 
286                                &(sru_pdu_res->u.response->num_diagnostics), 
287                                7, "query");
288         yaz_add_srw_diagnostic(odr_en,
289                                &(sru_pdu_res->u.response->diagnostics), 
290                                &(sru_pdu_res->u.response->num_diagnostics), 
291                                10, "CQL query is empty");
292         return false;
293     }
294     if ((sr_req->query_type == Z_SRW_query_type_xcql && !sr_req->query.xcql))
295     {
296          yaz_add_srw_diagnostic(odr_en,
297                                &(sru_pdu_res->u.response->diagnostics), 
298                                &(sru_pdu_res->u.response->num_diagnostics), 
299                                 10, "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                                10, "PQF query is empty");
308         return false;
309     }
310     return true;
311 }
312
313
314 Z_ElementSetNames * 
315 mp_util::build_esn_from_schema(mp::odr &odr_en, const char *schema)
316 {
317     if (!schema)
318         return 0;
319     
320     Z_ElementSetNames *esn 
321         = (Z_ElementSetNames *) odr_malloc(odr_en, sizeof(Z_ElementSetNames));
322     esn->which = Z_ElementSetNames_generic;
323     esn->u.generic = odr_strdup(odr_en, schema);
324     return esn; 
325 }
326
327
328 std::ostream& std::operator<<(std::ostream& os, Z_SRW_PDU& srw_pdu) 
329 {
330     os << "SRU";
331     
332     switch(srw_pdu.which) {
333     case  Z_SRW_searchRetrieve_request:
334         os << " " << "searchRetrieveRequest";
335         {
336             Z_SRW_searchRetrieveRequest *sr = srw_pdu.u.request;
337             if (sr)
338             {
339                 if (sr->database)
340                     os << " " << (sr->database);
341                 else
342                     os << " -";
343                 if (sr->startRecord)
344                     os << " " << *(sr->startRecord);
345                 else
346                     os << " -";
347                 if (sr->maximumRecords)
348                     os << " " << *(sr->maximumRecords);
349                 else
350                     os << " -";
351                 if (sr->recordPacking)
352                     os << " " << (sr->recordPacking);
353                 else
354                     os << " -";
355
356                 if (sr->recordSchema)
357                     os << " " << (sr->recordSchema);
358                 else
359                     os << " -";
360                 
361                 switch (sr->query_type){
362                 case Z_SRW_query_type_cql:
363                     os << " CQL";
364                     if (sr->query.cql)
365                         os << " " << sr->query.cql;
366                     break;
367                 case Z_SRW_query_type_xcql:
368                     os << " XCQL";
369                     break;
370                 case Z_SRW_query_type_pqf:
371                     os << " PQF";
372                     if (sr->query.pqf)
373                         os << " " << sr->query.pqf;
374                     break;
375                 }
376             }
377         }
378         break;
379     case  Z_SRW_searchRetrieve_response:
380         os << " " << "searchRetrieveResponse";
381         {
382             Z_SRW_searchRetrieveResponse *sr = srw_pdu.u.response;
383             if (sr)
384             {
385                 if (! (sr->num_diagnostics))
386                 {
387                     os << " OK";
388                     if (sr->numberOfRecords)
389                         os << " " << *(sr->numberOfRecords);
390                     else
391                         os << " -";
392                     //if (sr->num_records)
393                     os << " " << (sr->num_records);
394                     //else
395                     //os << " -";
396                     if (sr->nextRecordPosition)
397                         os << " " << *(sr->nextRecordPosition);
398                     else
399                         os << " -";
400                 } 
401                 else
402                 {
403                     os << " DIAG";
404                     if (sr->diagnostics && sr->diagnostics->uri)
405                         os << " " << (sr->diagnostics->uri);
406                     else
407                         os << " -";
408                     if (sr->diagnostics && sr->diagnostics->message)
409                         os << " " << (sr->diagnostics->message);
410                     else
411                         os << " -";
412                     if (sr->diagnostics && sr->diagnostics->details)
413                         os << " " << (sr->diagnostics->details);
414                     else
415                         os << " -";
416                 }
417                 
418                     
419             }
420         }
421         break;
422     case  Z_SRW_explain_request:
423         os << " " << "explainRequest";
424         break;
425     case  Z_SRW_explain_response:
426         os << " " << "explainResponse";
427         break;
428     case  Z_SRW_scan_request:
429         os << " " << "scanRequest";
430         break;
431     case  Z_SRW_scan_response:
432         os << " " << "scanResponse";
433         break;
434     case  Z_SRW_update_request:
435         os << " " << "updateRequest";
436         break;
437     case  Z_SRW_update_response:
438         os << " " << "updateResponse";
439         break;
440     default: 
441         os << " " << "UNKNOWN";    
442     }
443
444     return os;    
445 }
446
447 /*
448  * Local variables:
449  * c-basic-offset: 4
450  * c-file-style: "Stroustrup"
451  * indent-tabs-mode: nil
452  * End:
453  * vim: shiftwidth=4 tabstop=8 expandtab
454  */
455