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