Merge branch 'master' into sru_2_0
[yaz-moved-to-github.git] / src / zoom-sru.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2013 Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file zoom-sru.c
7  * \brief Implements ZOOM SRU
8  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <assert.h>
14 #include <string.h>
15 #include <errno.h>
16 #include "zoom-p.h"
17
18 #include <yaz/log.h>
19 #include <yaz/pquery.h>
20
21 #if YAZ_HAVE_XML2
22 static void set_SRU_error(ZOOM_connection c, Z_SRW_diagnostic *d)
23 {
24     const char *uri = d->uri;
25     if (uri)
26         ZOOM_set_dset_error(c, ZOOM_uri_to_code(uri), uri, d->details, 0);
27 }
28 #endif
29
30
31 #if YAZ_HAVE_XML2
32 static zoom_ret send_srw(ZOOM_connection c, Z_SRW_PDU *sr)
33 {
34     Z_GDU *gdu;
35     const char *database =  ZOOM_options_get(c->options, "databaseName");
36
37     gdu = z_get_HTTP_Request_uri(c->odr_out, c->host_port,
38                                  database,
39                                  c->proxy ? 1 : 0);
40
41     if (c->sru_mode == zoom_sru_get)
42     {
43         yaz_sru_get_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
44     }
45     else if (c->sru_mode == zoom_sru_post)
46     {
47         yaz_sru_post_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
48     }
49     else if (c->sru_mode == zoom_sru_soap)
50     {
51         yaz_sru_soap_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
52     }
53     else if (c->sru_mode == zoom_sru_solr)
54     {
55         yaz_solr_encode_request(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
56     }
57     return ZOOM_send_GDU(c, gdu);
58 }
59 #endif
60
61 #if YAZ_HAVE_XML2
62 static Z_SRW_PDU *ZOOM_srw_get_pdu(ZOOM_connection c, int type)
63 {
64     Z_SRW_PDU *sr = yaz_srw_get_pdu(c->odr_out, type, c->sru_version);
65     if (c->url_authentication && c->user)
66     {
67         Z_SRW_extra_arg **ea = &sr->extra_args;
68         while (*ea)
69             ea = &(*ea)->next;
70         *ea = (Z_SRW_extra_arg *) odr_malloc(c->odr_out, sizeof(**ea));
71         (*ea)->name = "x-username";
72         (*ea)->value = c->user;
73         ea = &(*ea)->next;
74         if (c->password)
75         {
76             *ea = (Z_SRW_extra_arg *) odr_malloc(c->odr_out, sizeof(**ea));
77             (*ea)->name = "x-password";
78             (*ea)->value = c->password;
79             ea = &(*ea)->next;
80         }
81         *ea = 0;
82     }
83     else
84     {
85         sr->username = c->user;
86         sr->password = c->password;
87     }
88     return sr;
89 }
90 #endif
91
92 #if YAZ_HAVE_XML2
93 zoom_ret ZOOM_connection_srw_send_scan(ZOOM_connection c)
94 {
95     ZOOM_scanset scan;
96     Z_SRW_PDU *sr = 0;
97     const char *option_val = 0;
98     Z_Query *z_query;
99
100     if (!c->tasks)
101         return zoom_complete;
102     assert (c->tasks->which == ZOOM_TASK_SCAN);
103     scan = c->tasks->u.scan.scan;
104
105     sr = ZOOM_srw_get_pdu(c, Z_SRW_scan_request);
106
107     z_query = ZOOM_query_get_Z_Query(scan->query);
108     /* SRU scan can only carry CQL and PQF */
109     if (z_query->which == Z_Query_type_104)
110     {
111         sr->u.scan_request->queryType = "cql";
112         sr->u.scan_request->scanClause =
113             odr_strdup(c->odr_out, ZOOM_query_get_query_string(scan->query));
114     }
115     else if (z_query->which == Z_Query_type_1
116              || z_query->which == Z_Query_type_101)
117     {
118         sr->u.scan_request->queryType = "pqf";
119         sr->u.scan_request->scanClause =
120             odr_strdup(c->odr_out, ZOOM_query_get_query_string(scan->query));
121     }
122     else
123     {
124         ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
125         return zoom_complete;
126     }
127
128     sr->u.scan_request->maximumTerms = odr_intdup(
129         c->odr_out, ZOOM_options_get_int(scan->options, "number", 10));
130
131     sr->u.scan_request->responsePosition = odr_intdup(
132         c->odr_out, ZOOM_options_get_int(scan->options, "position", 1));
133
134     option_val = ZOOM_options_get(scan->options, "extraArgs");
135     yaz_encode_sru_extra(sr, c->odr_out, option_val);
136     return send_srw(c, sr);
137 }
138 #else
139 zoom_ret ZOOM_connection_srw_send_scan(ZOOM_connection c)
140 {
141     return zoom_complete;
142 }
143 #endif
144
145 #if YAZ_HAVE_XML2
146 zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
147 {
148     const char *facets = 0;
149     int i;
150     int *start, *count;
151     ZOOM_resultset resultset = 0;
152     Z_SRW_PDU *sr = 0;
153     const char *option_val = 0;
154     const char *schema = 0;
155     Z_Query *z_query;
156     Z_FacetList *facet_list = 0;
157     if (c->error)                  /* don't continue on error */
158         return zoom_complete;
159     assert(c->tasks);
160     switch(c->tasks->which)
161     {
162     case ZOOM_TASK_SEARCH:
163         resultset = c->tasks->u.search.resultset;
164         if (!resultset->setname)
165             resultset->setname = xstrdup("default");
166         ZOOM_options_set(resultset->options, "setname", resultset->setname);
167         start = &c->tasks->u.search.start;
168         count = &c->tasks->u.search.count;
169         facets = ZOOM_options_get(resultset->options, "facets");
170         if (facets)
171             facet_list = yaz_pqf_parse_facet_list(c->odr_out, facets);
172         schema = c->tasks->u.search.schema;
173         break;
174     case ZOOM_TASK_RETRIEVE:
175         resultset = c->tasks->u.retrieve.resultset;
176         schema = c->tasks->u.retrieve.schema;
177
178         start = &c->tasks->u.retrieve.start;
179         count = &c->tasks->u.retrieve.count;
180
181         if (*start >= resultset->size)
182             return zoom_complete;
183         if (*start + *count > resultset->size)
184             *count = resultset->size - *start;
185
186         for (i = 0; i < *count; i++)
187         {
188             ZOOM_record rec =
189                 ZOOM_record_cache_lookup(resultset, i + *start,
190                                          c->tasks->u.retrieve.syntax,
191                                          c->tasks->u.retrieve.elementSetName,
192                                          schema);
193             if (!rec)
194                 break;
195             else
196             {
197                 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
198                 ZOOM_connection_put_event(c, event);
199             }
200         }
201         *start += i;
202         *count -= i;
203
204         if (*count == 0)
205             return zoom_complete;
206         break;
207     default:
208         return zoom_complete;
209     }
210     assert(resultset->query);
211
212     sr = ZOOM_srw_get_pdu(c, Z_SRW_searchRetrieve_request);
213     z_query = ZOOM_query_get_Z_Query(resultset->query);
214
215     if (z_query->which == Z_Query_type_104
216         && z_query->u.type_104->which == Z_External_CQL)
217     {
218         sr->u.request->queryType = "cql";
219         sr->u.request->query = z_query->u.type_104->u.cql;
220     }
221     else if (z_query->which == Z_Query_type_1 && z_query->u.type_1)
222     {
223         sr->u.request->queryType = "pqf";
224         sr->u.request->query =
225             odr_strdup(c->odr_out,
226                        ZOOM_query_get_query_string(resultset->query));
227     }
228     else
229     {
230         ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
231         return zoom_complete;
232     }
233
234     option_val = ZOOM_query_get_sru11(resultset->query);
235     if (option_val)
236     {
237         sr->u.request->sort_type = Z_SRW_sort_type_sort;
238         sr->u.request->sort.sortKeys = odr_strdup(c->odr_out, option_val);
239     }
240     sr->u.request->startRecord = odr_intdup(c->odr_out, *start + 1);
241     sr->u.request->maximumRecords = odr_intdup(
242         c->odr_out, (resultset->step > 0 && resultset->step < *count) ?
243         resultset->step : *count);
244     sr->u.request->recordSchema = odr_strdup_null(c->odr_out, schema);
245     sr->u.request->facetList = facet_list;
246
247     option_val = ZOOM_resultset_option_get(resultset, "recordPacking");
248     if (option_val)
249         sr->u.request->recordPacking = odr_strdup(c->odr_out, option_val);
250
251     option_val = ZOOM_resultset_option_get(resultset, "extraArgs");
252     yaz_encode_sru_extra(sr, c->odr_out, option_val);
253     return send_srw(c, sr);
254 }
255 #else
256 zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
257 {
258     return zoom_complete;
259 }
260 #endif
261
262 #if YAZ_HAVE_XML2
263 static zoom_ret handle_srw_response(ZOOM_connection c,
264                                     Z_SRW_searchRetrieveResponse *res)
265 {
266     ZOOM_resultset resultset = 0;
267     int i;
268     NMEM nmem;
269     ZOOM_Event event;
270     int *start, *count;
271     const char *syntax, *elementSetName, *schema;
272
273     if (!c->tasks)
274         return zoom_complete;
275
276     switch(c->tasks->which)
277     {
278     case ZOOM_TASK_SEARCH:
279         resultset = c->tasks->u.search.resultset;
280         start = &c->tasks->u.search.start;
281         count = &c->tasks->u.search.count;
282         syntax = c->tasks->u.search.syntax;
283         elementSetName = c->tasks->u.search.elementSetName;
284         schema = c->tasks->u.search.schema;
285         /* Required not for reporting client hit count multiple times into session */
286         if (!c->tasks->u.search.recv_search_fired) {
287             yaz_log(YLOG_DEBUG, "posting ZOOM_EVENT_RECV_SEARCH");
288             event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
289             ZOOM_connection_put_event(c, event);
290             c->tasks->u.search.recv_search_fired = 1;
291         }
292         if (res->facetList)
293             ZOOM_handle_facet_list(resultset, res->facetList);
294         break;
295     case ZOOM_TASK_RETRIEVE:
296         resultset = c->tasks->u.retrieve.resultset;
297         start = &c->tasks->u.retrieve.start;
298         count = &c->tasks->u.retrieve.count;
299         syntax = c->tasks->u.retrieve.syntax;
300         elementSetName = c->tasks->u.retrieve.elementSetName;
301         schema = c->tasks->u.retrieve.schema;
302         break;
303     default:
304         return zoom_complete;
305     }
306
307     resultset->size = 0;
308
309     if (res->resultSetId)
310         ZOOM_resultset_option_set(resultset, "resultSetId", res->resultSetId);
311
312     yaz_log(c->log_details, "%p handle_srw_response got SRW response OK", c);
313
314     if (res->num_diagnostics > 0)
315     {
316         set_SRU_error(c, &res->diagnostics[0]);
317     }
318     else
319     {
320         if (res->numberOfRecords) {
321             resultset->size = *res->numberOfRecords;
322         }
323         if (res->suggestions) {
324             ZOOM_resultset_option_set(resultset, "suggestions", res->suggestions);
325         }
326         for (i = 0; i<res->num_records; i++)
327         {
328             int pos = *start + i;
329             Z_SRW_record *sru_rec;
330             Z_SRW_diagnostic *diag = 0;
331             int num_diag;
332
333             Z_NamePlusRecord *npr = (Z_NamePlusRecord *)
334                 odr_malloc(c->odr_in, sizeof(Z_NamePlusRecord));
335
336             /* only trust recordPosition if >= calculated position */
337             if (res->records[i].recordPosition &&
338                 *res->records[i].recordPosition >= pos + 1)
339                 pos = *res->records[i].recordPosition - 1;
340
341             sru_rec = &res->records[i];
342
343             npr->databaseName = 0;
344             npr->which = Z_NamePlusRecord_databaseRecord;
345             npr->u.databaseRecord = (Z_External *)
346                 odr_malloc(c->odr_in, sizeof(Z_External));
347             npr->u.databaseRecord->descriptor = 0;
348             npr->u.databaseRecord->direct_reference =
349                 odr_oiddup(c->odr_in, yaz_oid_recsyn_xml);
350             npr->u.databaseRecord->indirect_reference = 0;
351             npr->u.databaseRecord->which = Z_External_octet;
352
353             npr->u.databaseRecord->u.octet_aligned =
354                 odr_create_Odr_oct(c->odr_in,
355                                    sru_rec->recordData_buf,
356                                    sru_rec->recordData_len);
357             if (sru_rec->recordSchema
358                 && !strcmp(sru_rec->recordSchema,
359                            "info:srw/schema/1/diagnostics-v1.1"))
360             {
361                 sru_decode_surrogate_diagnostics(sru_rec->recordData_buf,
362                                                  sru_rec->recordData_len,
363                                                  &diag, &num_diag,
364                                                  resultset->odr);
365             }
366             ZOOM_record_cache_add(resultset, npr, pos, syntax, elementSetName,
367                                   schema, diag);
368         }
369         *count -= i;
370         *start += i;
371         if (*count + *start > resultset->size)
372             *count = resultset->size - *start;
373         yaz_log(YLOG_DEBUG, "SRU result set size " ODR_INT_PRINTF " start %d count %d", resultset->size, *start, *count);
374         if (*count < 0)
375             *count = 0;
376         nmem = odr_extract_mem(c->odr_in);
377         nmem_transfer(odr_getmem(resultset->odr), nmem);
378         nmem_destroy(nmem);
379
380         if (*count > 0)
381             return ZOOM_connection_srw_send_search(c);
382     }
383     return zoom_complete;
384 }
385 #endif
386
387 #if YAZ_HAVE_XML2
388 static void handle_srw_scan_response(ZOOM_connection c,
389                                      Z_SRW_scanResponse *res)
390 {
391     NMEM nmem = odr_extract_mem(c->odr_in);
392     ZOOM_scanset scan;
393
394     if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
395         return;
396     scan = c->tasks->u.scan.scan;
397
398     if (res->num_diagnostics > 0)
399         set_SRU_error(c, &res->diagnostics[0]);
400
401     scan->scan_response = 0;
402     scan->srw_scan_response = res;
403     nmem_transfer(odr_getmem(scan->odr), nmem);
404
405     ZOOM_options_set_int(scan->options, "number", res->num_terms);
406     nmem_destroy(nmem);
407 }
408 #endif
409
410 int ZOOM_handle_sru(ZOOM_connection c, Z_HTTP_Response *hres,
411                     zoom_ret *cret, char **addinfo)
412 {
413 #if YAZ_HAVE_XML2
414     int ret = 0;
415
416     /* not redirect (normal response) */
417     if (!yaz_srw_check_content_type(hres))
418     {
419         *addinfo = "content-type";
420         ret = -1;
421     }
422     else if (c->sru_mode == zoom_sru_solr)
423     {
424         Z_SRW_PDU *sr;
425         ret = yaz_solr_decode_response(c->odr_in, hres, &sr);
426         if (ret == 0)
427         {
428             if (sr->which == Z_SRW_searchRetrieve_response)
429                 *cret = handle_srw_response(c, sr->u.response);
430             else if (sr->which == Z_SRW_scan_response)
431                 handle_srw_scan_response(c, sr->u.scan_response);
432         }
433     }
434     else
435     {
436         Z_SOAP *soap_package = 0;
437         ODR o = c->odr_in;
438         Z_SOAP_Handler soap_handlers[3] = {
439             {YAZ_XMLNS_SRU_v1_response, 0, (Z_SOAP_fun) yaz_srw_codec},
440             {YAZ_XMLNS_SRU_v2_mask, 0, (Z_SOAP_fun) yaz_srw_codec},
441             {0, 0, 0}
442         };
443         ret = z_soap_codec(o, &soap_package,
444                            &hres->content_buf, &hres->content_len,
445                            soap_handlers);
446         if (!ret && soap_package->which == Z_SOAP_generic)
447         {
448             Z_SRW_PDU *sr = (Z_SRW_PDU*) soap_package->u.generic->p;
449
450             ZOOM_options_set(c->options, "sru_version", sr->srw_version);
451             ZOOM_options_setl(c->options, "sru_extra_response_data",
452                               sr->extraResponseData_buf, sr->extraResponseData_len);
453             if (sr->which == Z_SRW_searchRetrieve_response)
454                 *cret = handle_srw_response(c, sr->u.response);
455             else if (sr->which == Z_SRW_scan_response)
456                 handle_srw_scan_response(c, sr->u.scan_response);
457             else
458                 ret = -1;
459         }
460         else if (!ret && (soap_package->which == Z_SOAP_fault
461                           || soap_package->which == Z_SOAP_error))
462         {
463             ZOOM_set_HTTP_error(c, hres->code,
464                                 soap_package->u.fault->fault_code,
465                                 soap_package->u.fault->fault_string);
466         }
467         else
468         {
469             size_t max_chars = 1000;
470             size_t sz = hres->content_len;
471             if (sz > max_chars - 1)
472                 sz = max_chars;
473             *addinfo = odr_malloc(c->odr_in, sz + 4);
474             memcpy(*addinfo, hres->content_buf, sz);
475             if (sz == max_chars)
476                 strcpy(*addinfo + sz, "...");
477             else
478                 strcpy(*addinfo + sz, "");
479             ret = -1;
480         }
481     }
482     return ret;
483 #else
484     return -1;
485 #endif
486 }
487
488 /*
489  * Local variables:
490  * c-basic-offset: 4
491  * c-file-style: "Stroustrup"
492  * indent-tabs-mode: nil
493  * End:
494  * vim: shiftwidth=4 tabstop=8 expandtab
495  */
496