Added debug logging to verify ZOOM_EVENT_RECV_SEARCH
[yaz-moved-to-github.git] / src / zoom-sru.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file zoom-sru.c
7  * \brief Implements ZOOM SRU
8  */
9
10 #include <assert.h>
11 #include <string.h>
12 #include <errno.h>
13 #include "zoom-p.h"
14
15 #include <yaz/log.h>
16 #include <yaz/pquery.h>
17
18 void handle_facet_list(ZOOM_resultset r, Z_FacetList *fl);
19
20 #if YAZ_HAVE_XML2
21 static void set_SRU_error(ZOOM_connection c, Z_SRW_diagnostic *d)
22 {
23     const char *uri = d->uri;
24     if (uri)
25         ZOOM_set_dset_error(c, ZOOM_uri_to_code(uri), uri, d->details, 0);
26 }
27 #endif
28
29
30 #if YAZ_HAVE_XML2
31 static zoom_ret send_srw(ZOOM_connection c, Z_SRW_PDU *sr)
32 {
33     Z_GDU *gdu;
34     ZOOM_Event event;
35     const char *database =  ZOOM_options_get(c->options, "databaseName");
36     char *fdatabase = 0;
37     
38     if (database)
39         fdatabase = yaz_encode_sru_dbpath_odr(c->odr_out, database);
40     gdu = z_get_HTTP_Request_host_path(c->odr_out, c->host_port,
41                                        fdatabase ? fdatabase : c->path);
42
43     if (c->sru_mode == zoom_sru_get)
44     {
45         yaz_sru_get_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
46     }
47     else if (c->sru_mode == zoom_sru_post)
48     {
49         yaz_sru_post_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
50     }
51     else if (c->sru_mode == zoom_sru_soap)
52     {
53         yaz_sru_soap_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
54     }
55     else if (c->sru_mode == zoom_sru_solr)
56     {
57         yaz_solr_encode_request(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
58     }
59     if (!z_GDU(c->odr_out, &gdu, 0, 0))
60         return zoom_complete;
61     if (c->odr_print)
62         z_GDU(c->odr_print, &gdu, 0, 0);
63     c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
64         
65     event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
66     ZOOM_connection_put_event(c, event);
67     odr_reset(c->odr_out);
68     return ZOOM_send_buf(c);
69 }
70 #endif
71
72 #if YAZ_HAVE_XML2
73 static Z_SRW_PDU *ZOOM_srw_get_pdu(ZOOM_connection c, int type)
74 {
75     Z_SRW_PDU *sr = yaz_srw_get_pdu(c->odr_out, type, c->sru_version);
76     sr->username = c->user;
77     sr->password = c->password;
78     return sr;
79 }
80 #endif
81
82 #if YAZ_HAVE_XML2
83 zoom_ret ZOOM_connection_srw_send_scan(ZOOM_connection c)
84 {
85     ZOOM_scanset scan;
86     Z_SRW_PDU *sr = 0;
87     const char *option_val = 0;
88     Z_Query *z_query;
89
90     if (!c->tasks)
91         return zoom_complete;
92     assert (c->tasks->which == ZOOM_TASK_SCAN);
93     scan = c->tasks->u.scan.scan;
94         
95     sr = ZOOM_srw_get_pdu(c, Z_SRW_scan_request);
96
97     z_query = ZOOM_query_get_Z_Query(scan->query);
98     /* SRU scan can only carry CQL and PQF */
99     if (z_query->which == Z_Query_type_104)
100     {
101         sr->u.scan_request->query_type = Z_SRW_query_type_cql;
102         sr->u.scan_request->scanClause.cql =
103             ZOOM_query_get_query_string(scan->query);
104     }
105     else if (z_query->which == Z_Query_type_1
106              || z_query->which == Z_Query_type_101)
107     {
108         sr->u.scan_request->query_type = Z_SRW_query_type_pqf;
109         sr->u.scan_request->scanClause.pqf =
110             ZOOM_query_get_query_string(scan->query);
111     }
112     else
113     {
114         ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
115         return zoom_complete;
116     }
117
118     sr->u.scan_request->maximumTerms = odr_intdup(
119         c->odr_out, ZOOM_options_get_int(scan->options, "number", 10));
120     
121     sr->u.scan_request->responsePosition = odr_intdup(
122         c->odr_out, ZOOM_options_get_int(scan->options, "position", 1));
123     
124     option_val = ZOOM_options_get(scan->options, "extraArgs");
125     yaz_encode_sru_extra(sr, c->odr_out, option_val);
126     return send_srw(c, sr);
127 }
128 #else
129 zoom_ret ZOOM_connection_srw_send_scan(ZOOM_connection c)
130 {
131     return zoom_complete;
132 }
133 #endif
134
135 #if YAZ_HAVE_XML2
136 zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
137 {
138     const char *facets = 0;
139     int i;
140     int *start, *count;
141     ZOOM_resultset resultset = 0;
142     Z_SRW_PDU *sr = 0;
143     const char *option_val = 0;
144     Z_Query *z_query;
145     Z_FacetList *facet_list = 0;
146     if (c->error)                  /* don't continue on error */
147         return zoom_complete;
148     assert(c->tasks);
149     switch(c->tasks->which)
150     {
151     case ZOOM_TASK_SEARCH:
152         resultset = c->tasks->u.search.resultset;
153         if (!resultset->setname)
154             resultset->setname = xstrdup("default");
155         ZOOM_options_set(resultset->options, "setname", resultset->setname);
156         start = &c->tasks->u.search.start;
157         count = &c->tasks->u.search.count;
158         facets = ZOOM_options_get(resultset->options, "facets");
159         if (facets) {
160             facet_list = yaz_pqf_parse_facet_list(c->odr_out, facets);
161         }
162         break;
163     case ZOOM_TASK_RETRIEVE:
164         resultset = c->tasks->u.retrieve.resultset;
165
166         start = &c->tasks->u.retrieve.start;
167         count = &c->tasks->u.retrieve.count;
168
169         if (*start >= resultset->size)
170             return zoom_complete;
171         if (*start + *count > resultset->size)
172             *count = resultset->size - *start;
173
174         for (i = 0; i < *count; i++)
175         {
176             ZOOM_record rec =
177                 ZOOM_record_cache_lookup(resultset, i + *start,
178                                          c->tasks->u.retrieve.syntax,
179                                          c->tasks->u.retrieve.elementSetName);
180             if (!rec)
181                 break;
182             else
183             {
184                 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
185                 ZOOM_connection_put_event(c, event);
186             }
187         }
188         *start += i;
189         *count -= i;
190
191         if (*count == 0)
192             return zoom_complete;
193         break;
194     default:
195         return zoom_complete;
196     }
197     assert(resultset->query);
198         
199     sr = ZOOM_srw_get_pdu(c, Z_SRW_searchRetrieve_request);
200     z_query = ZOOM_query_get_Z_Query(resultset->query);
201
202     if (z_query->which == Z_Query_type_104
203         && z_query->u.type_104->which == Z_External_CQL)
204     {
205         sr->u.request->query_type = Z_SRW_query_type_cql;
206         sr->u.request->query.cql = z_query->u.type_104->u.cql;
207     }
208     else if (z_query->which == Z_Query_type_1 && z_query->u.type_1)
209     {
210         sr->u.request->query_type = Z_SRW_query_type_pqf;
211         sr->u.request->query.pqf =
212             ZOOM_query_get_query_string(resultset->query);
213     }
214     else
215     {
216         ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
217         return zoom_complete;
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 = resultset->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;
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
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             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         break;
281     default:
282         return zoom_complete;
283     }
284
285     resultset->size = 0;
286
287     if (res->resultSetId)
288         ZOOM_resultset_option_set(resultset, "resultSetId", res->resultSetId);
289
290     yaz_log(c->log_details, "%p handle_srw_response got SRW response OK", c);
291
292     if (res->num_diagnostics > 0)
293     {
294         set_SRU_error(c, &res->diagnostics[0]);
295     }
296     else
297     {
298         if (res->numberOfRecords) {
299             resultset->size = *res->numberOfRecords;
300         }
301         for (i = 0; i<res->num_records; i++)
302         {
303             int pos;
304             Z_SRW_record *sru_rec;
305             Z_SRW_diagnostic *diag = 0;
306             int num_diag;
307             
308             Z_NamePlusRecord *npr = (Z_NamePlusRecord *)
309                 odr_malloc(c->odr_in, sizeof(Z_NamePlusRecord));
310             /*
311              * TODO This does not work with 0-based recordPositions.
312              * We will iterate over one twice
313              */
314             if (res->records[i].recordPosition && 
315                 *res->records[i].recordPosition > 0)
316                 pos = *res->records[i].recordPosition - 1;
317             else
318                 pos = *start + i;
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->which = Z_External_octet;
330             
331             npr->u.databaseRecord->u.octet_aligned = (Odr_oct *)
332                 odr_malloc(c->odr_in, sizeof(Odr_oct));
333             npr->u.databaseRecord->u.octet_aligned->buf = (unsigned char*)
334                 sru_rec->recordData_buf;
335             npr->u.databaseRecord->u.octet_aligned->len = 
336                 npr->u.databaseRecord->u.octet_aligned->size = 
337                 sru_rec->recordData_len;
338             
339             if (sru_rec->recordSchema 
340                 && !strcmp(sru_rec->recordSchema,
341                            "info:srw/schema/1/diagnostics-v1.1"))
342             {
343                 sru_decode_surrogate_diagnostics(sru_rec->recordData_buf,
344                                                  sru_rec->recordData_len,
345                                                  &diag, &num_diag,
346                                                  resultset->odr);
347             }
348             ZOOM_record_cache_add(resultset, npr, pos, syntax, elementSetName,
349                                   sru_rec->recordSchema, diag);
350         }
351         *count -= i;
352         *start += i;
353         if (*count + *start > resultset->size)
354             *count = resultset->size - *start;
355         yaz_log(YLOG_DEBUG, "SRU result set size " ODR_INT_PRINTF " start %d count %d", resultset->size, *start, *count);
356         if (*count < 0)
357             *count = 0;
358         nmem = odr_extract_mem(c->odr_in);
359         nmem_transfer(odr_getmem(resultset->odr), nmem);
360         nmem_destroy(nmem);
361
362         if (*count > 0)
363             return ZOOM_connection_srw_send_search(c);
364     }
365     return zoom_complete;
366 }
367 #endif
368
369 #if YAZ_HAVE_XML2
370 static void handle_srw_scan_response(ZOOM_connection c,
371                                      Z_SRW_scanResponse *res)
372 {
373     NMEM nmem = odr_extract_mem(c->odr_in);
374     ZOOM_scanset scan;
375
376     if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
377         return;
378     scan = c->tasks->u.scan.scan;
379
380     if (res->num_diagnostics > 0)
381         set_SRU_error(c, &res->diagnostics[0]);
382
383     scan->scan_response = 0;
384     scan->srw_scan_response = res;
385     nmem_transfer(odr_getmem(scan->odr), nmem);
386
387     ZOOM_options_set_int(scan->options, "number", res->num_terms);
388     nmem_destroy(nmem);
389 }
390 #endif
391
392 int ZOOM_handle_sru(ZOOM_connection c, Z_HTTP_Response *hres,
393                     zoom_ret *cret)
394 {
395     int ret = 0;
396     const char *addinfo = 0;
397
398     /* not redirect (normal response) */
399     if (!yaz_srw_check_content_type(hres))
400     {
401         addinfo = "content-type";
402         ret = -1;
403     }
404     else if (c->sru_mode == zoom_sru_solr)
405     {
406         Z_SRW_PDU *sr;
407         ret = yaz_solr_decode_response(c->odr_in, hres, &sr);
408         if (ret == 0)
409             if (sr->which == Z_SRW_searchRetrieve_response)
410                 *cret = handle_srw_response(c, sr->u.response);
411     }
412     else
413     {
414         Z_SOAP *soap_package = 0;
415         ODR o = c->odr_in;
416         Z_SOAP_Handler soap_handlers[2] = {
417             {YAZ_XMLNS_SRU_v1_1, 0, (Z_SOAP_fun) yaz_srw_codec},
418             {0, 0, 0}
419         };
420         ret = z_soap_codec(o, &soap_package,
421                            &hres->content_buf, &hres->content_len,
422                            soap_handlers);
423         if (!ret && soap_package->which == Z_SOAP_generic &&
424             soap_package->u.generic->no == 0)
425         {
426             Z_SRW_PDU *sr = (Z_SRW_PDU*) soap_package->u.generic->p;
427             
428             ZOOM_options_set(c->options, "sru_version", sr->srw_version);
429             ZOOM_options_setl(c->options, "sru_extra_response_data",
430                               sr->extraResponseData_buf, sr->extraResponseData_len);
431             if (sr->which == Z_SRW_searchRetrieve_response)
432                 *cret = handle_srw_response(c, sr->u.response);
433             else if (sr->which == Z_SRW_scan_response)
434                 handle_srw_scan_response(c, sr->u.scan_response);
435             else
436                 ret = -1;
437         }
438         else if (!ret && (soap_package->which == Z_SOAP_fault
439                           || soap_package->which == Z_SOAP_error))
440         {
441             ZOOM_set_HTTP_error(c, hres->code,
442                                 soap_package->u.fault->fault_code,
443                                 soap_package->u.fault->fault_string);
444         }
445         else
446             ret = -1;
447     }   
448     return ret;
449 }
450
451 /*
452  * Local variables:
453  * c-basic-offset: 4
454  * c-file-style: "Stroustrup"
455  * indent-tabs-mode: nil
456  * End:
457  * vim: shiftwidth=4 tabstop=8 expandtab
458  */
459