Fix NS for SRU 2.0 diagnostics
[yaz-moved-to-github.git] / src / srwutil.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 srwutil.c
7  * \brief Implements SRW/SRU utilities.
8  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <stdlib.h>
14 #include <assert.h>
15 #include <yaz/srw.h>
16 #include <yaz/matchstr.h>
17 #include <yaz/base64.h>
18 #include <yaz/yaz-iconv.h>
19 #include "sru-p.h"
20
21 #define MAX_SRU_PARAMETERS 30
22
23 static char *yaz_decode_sru_dbpath_odr(ODR n, const char *uri, size_t len)
24 {
25     return odr_strdupn(n, uri, len);
26 }
27
28 void yaz_encode_sru_dbpath_buf(char *dst, const char *db)
29 {
30     assert(db);
31     *dst = '/';
32     strcpy(dst+1, db);
33 }
34
35 char *yaz_encode_sru_dbpath_odr(ODR out, const char *db)
36 {
37     char *dst = odr_malloc(out, 3 * strlen(db) + 2);
38     yaz_encode_sru_dbpath_buf(dst, db);
39     return dst;
40 }
41
42 Z_AttributeList *yaz_use_attribute_create(ODR o, const char *name)
43 {
44     Z_AttributeList *attributes= (Z_AttributeList *)
45         odr_malloc(o, sizeof(*attributes));
46     Z_AttributeElement ** elements;
47     attributes->num_attributes = 1;
48     elements = (Z_AttributeElement**)
49         odr_malloc(o, attributes->num_attributes * sizeof(*elements));
50     elements[0] = (Z_AttributeElement*) odr_malloc(o,sizeof(**elements));
51     elements[0]->attributeType = odr_intdup(o, 1);
52     elements[0]->attributeSet = odr_nullval();
53     elements[0]->which = Z_AttributeValue_complex;
54     elements[0]->value.complex = (Z_ComplexAttribute *)
55         odr_malloc(o, sizeof(Z_ComplexAttribute));
56     elements[0]->value.complex->num_list = 1;
57     elements[0]->value.complex->list = (Z_StringOrNumeric **)
58         odr_malloc(o, 1 * sizeof(Z_StringOrNumeric *));
59     elements[0]->value.complex->list[0] = (Z_StringOrNumeric *)
60         odr_malloc(o, sizeof(Z_StringOrNumeric));
61     elements[0]->value.complex->list[0]->which = Z_StringOrNumeric_string;
62     elements[0]->value.complex->list[0]->u.string = odr_strdup(o, name);
63     elements[0]->value.complex->semanticAction = 0;
64     elements[0]->value.complex->num_semanticAction = 0;
65     attributes->attributes = elements;
66     return attributes;
67 }
68
69 #if YAZ_HAVE_XML2
70 const char *yaz_element_attribute_value_get(xmlNodePtr ptr,
71                                             const char *node_name,
72                                             const char *attribute_name)
73 {
74     struct _xmlAttr *attr;
75     // check if the node name matches
76     if (strcmp((const char*) ptr->name, node_name))
77         return 0;
78     // check if the attribute name and return the value
79     for (attr = ptr->properties; attr; attr = attr->next)
80         if (attr->children && attr->children->type == XML_TEXT_NODE)
81         {
82             if (!strcmp((const char *) attr->name, attribute_name))
83                 return (const char *) attr->children->content;
84         }
85     return 0;
86 }
87 #endif
88
89 int yaz_srw_check_content_type(Z_HTTP_Response *hres)
90 {
91     const char *content_type = z_HTTP_header_lookup(hres->headers,
92                                                     "Content-Type");
93     if (content_type)
94     {
95         if (!yaz_strcmp_del("text/xml", content_type, "; "))
96             return 1;
97         if (!yaz_strcmp_del("application/xml", content_type, "; "))
98             return 1;
99     }
100     return 0;
101 }
102
103 /**
104  * Look for authentication tokens in HTTP Basic parameters or in x-username/x-password
105  * parameters. Added by SH.
106  */
107 static void yaz_srw_decodeauth(Z_SRW_PDU *sr, Z_HTTP_Request *hreq,
108                                char *username, char *password, ODR decode)
109 {
110     const char *basic = z_HTTP_header_lookup(hreq->headers, "Authorization");
111
112     if (username)
113         sr->username = username;
114     if (password)
115         sr->password = password;
116
117     if (basic)
118     {
119         int len;
120         char out[256];
121         char ubuf[256] = "", pbuf[256] = "", *p;
122         if (strncmp(basic, "Basic ", 6))
123             return;
124         basic += 6;
125         len = strlen(basic);
126         if (!len || len > 256)
127             return;
128         yaz_base64decode(basic, out);
129         /* Format of out should be username:password at this point */
130         strcpy(ubuf, out);
131         if ((p = strchr(ubuf, ':')))
132         {
133             *(p++) = '\0';
134             if (*p)
135                 strcpy(pbuf, p);
136         }
137         if (*ubuf)
138             sr->username = odr_strdup(decode, ubuf);
139         if (*pbuf)
140             sr->password = odr_strdup(decode, pbuf);
141     }
142 }
143
144 void yaz_mk_srw_diagnostic(ODR o, Z_SRW_diagnostic *d,
145                            const char *uri, const char *message,
146                            const char *details)
147 {
148     d->uri = odr_strdup(o, uri);
149     if (message)
150         d->message = odr_strdup(o, message);
151     else
152         d->message = 0;
153     if (details)
154         d->details = odr_strdup(o, details);
155     else
156         d->details = 0;
157 }
158
159 void yaz_mk_std_diagnostic(ODR o, Z_SRW_diagnostic *d,
160                            int code, const char *details)
161 {
162     char uri[40];
163
164     sprintf(uri, "info:srw/diagnostic/1/%d", code);
165     yaz_mk_srw_diagnostic(o, d, uri, 0, details);
166 }
167
168 void yaz_add_srw_diagnostic_uri(ODR o, Z_SRW_diagnostic **d,
169                                 int *num, const char *uri,
170                                 const char *message, const char *details)
171 {
172     Z_SRW_diagnostic *d_new;
173     d_new = (Z_SRW_diagnostic *) odr_malloc(o, (*num + 1)* sizeof(**d));
174     if (*num)
175         memcpy(d_new, *d, *num *sizeof(**d));
176     *d = d_new;
177
178     yaz_mk_srw_diagnostic(o, *d + *num, uri, message, details);
179     (*num)++;
180 }
181
182 void yaz_add_srw_diagnostic(ODR o, Z_SRW_diagnostic **d,
183                             int *num, int code, const char *addinfo)
184 {
185     char uri[40];
186
187     sprintf(uri, "info:srw/diagnostic/1/%d", code);
188     yaz_add_srw_diagnostic_uri(o, d, num, uri, 0, addinfo);
189 }
190
191
192 void yaz_add_sru_update_diagnostic(ODR o, Z_SRW_diagnostic **d,
193                                    int *num, int code, const char *addinfo)
194 {
195     char uri[40];
196
197     sprintf(uri, "info:srw/diagnostic/12/%d", code);
198     yaz_add_srw_diagnostic_uri(o, d, num, uri, 0, addinfo);
199 }
200
201
202 void yaz_mk_sru_surrogate(ODR o, Z_SRW_record *record, int pos,
203                           int code, const char *details)
204 {
205     const char *message = yaz_diag_srw_str(code);
206     int len = 200;
207     if (message)
208         len += strlen(message);
209     if (details)
210         len += strlen(details);
211
212     record->recordData_buf = (char *) odr_malloc(o, len);
213
214     sprintf(record->recordData_buf, "<diagnostic "
215             "xmlns=\"http://www.loc.gov/zing/srw/diagnostic/\">\n"
216             " <uri>info:srw/diagnostic/1/%d</uri>\n", code);
217     if (details)
218         sprintf(record->recordData_buf + strlen(record->recordData_buf),
219                 " <details>%s</details>\n", details);
220     if (message)
221         sprintf(record->recordData_buf + strlen(record->recordData_buf),
222                 " <message>%s</message>\n", message);
223     sprintf(record->recordData_buf + strlen(record->recordData_buf),
224             "</diagnostic>\n");
225     record->recordData_len = strlen(record->recordData_buf);
226     record->recordPosition = odr_intdup(o, pos);
227     record->recordSchema = "info:srw/schema/1/diagnostics-v1.1";
228 }
229
230 static void grab_charset(ODR o, const char *content_type, char **charset)
231 {
232     if (charset)
233     {
234         const char *charset_p = 0;
235         if (content_type && (charset_p = strstr(content_type, "; charset=")))
236         {
237             int i = 0;
238             charset_p += 10;
239             while (i < 20 && charset_p[i] &&
240                    !strchr("; \n\r", charset_p[i]))
241                 i++;
242             *charset = odr_strdupn(o, charset_p, i);
243         }
244     }
245 }
246
247 int yaz_srw_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
248                    Z_SOAP **soap_package, ODR decode, char **charset)
249 {
250     if (!strcmp(hreq->method, "POST"))
251     {
252         const char *content_type = z_HTTP_header_lookup(hreq->headers,
253                                                         "Content-Type");
254         if (content_type &&
255             (!yaz_strcmp_del("text/xml", content_type, "; ") ||
256              !yaz_strcmp_del("application/soap+xml", content_type, "; ") ||
257              !yaz_strcmp_del("text/plain", content_type, "; ")))
258         {
259             char *db = "Default";
260             const char *p0 = hreq->path, *p1;
261             int ret = -1;
262
263             static Z_SOAP_Handler soap_handlers[5] = {
264 #if YAZ_HAVE_XML2
265                 { YAZ_XMLNS_SRU_v1_1, 0, (Z_SOAP_fun) yaz_srw_codec },
266                 { YAZ_XMLNS_SRU_v1_0, 0, (Z_SOAP_fun) yaz_srw_codec },
267                 { YAZ_XMLNS_UPDATE_v0_9, 0, (Z_SOAP_fun) yaz_ucp_codec },
268                 { YAZ_XMLNS_SRU_v2_mask, 0, (Z_SOAP_fun) yaz_srw_codec },
269 #endif
270                 {0, 0, 0}
271             };
272
273             if (*p0 == '/')
274                 p0++;
275             p1 = strchr(p0, '?');
276             if (!p1)
277                 p1 = p0 + strlen(p0);
278             if (p1 != p0)
279                 db = yaz_decode_sru_dbpath_odr(decode, p0, p1 - p0);
280             grab_charset(decode, content_type, charset);
281
282             ret = z_soap_codec(decode, soap_package,
283                                &hreq->content_buf, &hreq->content_len,
284                                soap_handlers);
285             if (ret == 0 && (*soap_package)->which == Z_SOAP_generic)
286             {
287                 *srw_pdu = (Z_SRW_PDU*) (*soap_package)->u.generic->p;
288                 yaz_srw_decodeauth(*srw_pdu, hreq, 0, 0, decode);
289
290                 /* last entry in handlers - SRU 2.0 - is turned into
291                    offset 0.. due to other pieces relying on it */
292                 if ((*soap_package)->u.generic->no == 3)
293                     (*soap_package)->u.generic->no = 0;
294                 if ((*srw_pdu)->which == Z_SRW_searchRetrieve_request &&
295                     (*srw_pdu)->u.request->database == 0)
296                     (*srw_pdu)->u.request->database = db;
297
298                 if ((*srw_pdu)->which == Z_SRW_explain_request &&
299                     (*srw_pdu)->u.explain_request->database == 0)
300                     (*srw_pdu)->u.explain_request->database = db;
301
302                 if ((*srw_pdu)->which == Z_SRW_scan_request &&
303                     (*srw_pdu)->u.scan_request->database == 0)
304                     (*srw_pdu)->u.scan_request->database = db;
305
306                 if ((*srw_pdu)->which == Z_SRW_update_request &&
307                     (*srw_pdu)->u.update_request->database == 0)
308                     (*srw_pdu)->u.update_request->database = db;
309
310                 return 0;
311             }
312             return 1;
313         }
314     }
315     return 2;
316 }
317
318 #if YAZ_HAVE_XML2
319 static int yaz_sru_decode_integer(ODR odr, const char *pname,
320                                   const char *valstr, Odr_int **valp,
321                                   Z_SRW_diagnostic **diag, int *num_diag,
322                                   int min_value)
323 {
324     int ival;
325     if (!valstr)
326         return 0;
327     if (sscanf(valstr, "%d", &ival) != 1)
328     {
329         yaz_add_srw_diagnostic(odr, diag, num_diag,
330                                YAZ_SRW_UNSUPP_PARAMETER_VALUE, pname);
331         return 0;
332     }
333     if (min_value >= 0 && ival < min_value)
334     {
335         yaz_add_srw_diagnostic(odr, diag, num_diag,
336                                YAZ_SRW_UNSUPP_PARAMETER_VALUE, pname);
337         return 0;
338     }
339     *valp = odr_intdup(odr, ival);
340     return 1;
341 }
342 #endif
343
344 /**
345    http://www.loc.gov/z3950/agency/zing/srw/service.html
346 */
347 int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
348                    Z_SOAP **soap_package, ODR decode, char **charset,
349                    Z_SRW_diagnostic **diag, int *num_diag)
350 {
351 #if YAZ_HAVE_XML2
352     static Z_SOAP_Handler soap_handlers[2] = {
353         {YAZ_XMLNS_SRU_v1_1, 0, (Z_SOAP_fun) yaz_srw_codec},
354         {0, 0, 0}
355     };
356 #endif
357     const char *content_type = z_HTTP_header_lookup(hreq->headers,
358                                                     "Content-Type");
359
360     /*
361       SRU GET: ignore content type.
362       SRU POST: we support "application/x-www-form-urlencoded";
363       not  "multipart/form-data" .
364     */
365     if (!strcmp(hreq->method, "GET")
366         ||
367         (!strcmp(hreq->method, "POST") && content_type &&
368          !yaz_strcmp_del("application/x-www-form-urlencoded",
369                          content_type, "; ")))
370     {
371         char *db = "Default";
372         const char *p0 = hreq->path, *p1;
373 #if YAZ_HAVE_XML2
374         const char *operation = 0;
375         char *version = 0;
376         char *query = 0;
377         char *queryType = "cql";
378         char *username = 0;
379         char *password = 0;
380         char *sortKeys = 0;
381         char *stylesheet = 0;
382         char *scanClause = 0;
383         char *recordXPath = 0;
384         char *recordSchema = 0;
385         char *recordXMLEscaping = 0;
386         char *recordPacking = 0;
387         char *maximumRecords = 0;
388         char *startRecord = 0;
389         char *maximumTerms = 0;
390         char *responsePosition = 0;
391         const char *facetLimit = 0;
392         const char *facetStart = 0;
393         const char *facetSort = 0;
394         Z_SRW_extra_arg *extra_args = 0;
395 #endif
396         char **uri_name;
397         char **uri_val;
398
399         grab_charset(decode, content_type, charset);
400         if (charset && *charset == 0 && !strcmp(hreq->method, "GET"))
401             *charset = "UTF-8";
402
403         if (*p0 == '/')
404             p0++;
405         p1 = strchr(p0, '?');
406         if (!p1)
407             p1 = p0 + strlen(p0);
408         if (p1 != p0)
409             db = yaz_decode_sru_dbpath_odr(decode, p0, p1 - p0);
410         if (!strcmp(hreq->method, "POST"))
411             p1 = hreq->content_buf;
412         yaz_uri_to_array(p1, decode, &uri_name, &uri_val);
413 #if YAZ_HAVE_XML2
414         if (uri_name)
415         {
416             int i;
417             for (i = 0; uri_name[i]; i++)
418             {
419                 char *n = uri_name[i];
420                 char *v = uri_val[i];
421                 if (!strcmp(n, "query"))
422                     query = v;
423                 else if (!strcmp(n, "x-pquery"))
424                 {
425                     query = v;
426                     queryType = "pqf";
427                 }
428                 else if (!strcmp(n, "queryType"))
429                     queryType = v;
430                 else if (!strcmp(n, "x-username"))
431                     username = v;
432                 else if (!strcmp(n, "x-password"))
433                     password = v;
434                 else if (!strcmp(n, "operation"))
435                     operation = v;
436                 else if (!strcmp(n, "stylesheet"))
437                     stylesheet = v;
438                 else if (!strcmp(n, "sortKeys"))
439                     sortKeys = v;
440                 else if (!strcmp(n, "recordXPath"))
441                     recordXPath = v;
442                 else if (!strcmp(n, "recordSchema"))
443                     recordSchema = v;
444                 else if (!strcmp(n, "recordPacking"))
445                     recordPacking = v;
446                 else if (!strcmp(n, "recordXMLEscaping"))
447                     recordXMLEscaping = v;
448                 else if (!strcmp(n, "version"))
449                     version = v;
450                 else if (!strcmp(n, "scanClause"))
451                     scanClause = v;
452                 else if (!strcmp(n, "x-pScanClause"))
453                 {
454                     scanClause = v;
455                     queryType = "pqf";
456                 }
457                 else if (!strcmp(n, "maximumRecords"))
458                     maximumRecords = v;
459                 else if (!strcmp(n, "startRecord"))
460                     startRecord = v;
461                 else if (!strcmp(n, "maximumTerms"))
462                     maximumTerms = v;
463                 else if (!strcmp(n, "responsePosition"))
464                     responsePosition = v;
465                 else if (!strcmp(n, "facetLimit"))
466                     facetLimit = v;
467                 else if (!strcmp(n, "facetStart"))
468                     facetStart = v;
469                 else if (!strcmp(n, "facetSort"))
470                     facetSort = v;
471                 else if (!strcmp(n, "extraRequestData"))
472                     ; /* ignoring extraRequestData */
473                 else if (n[0] == 'x' && n[1] == '-')
474                 {
475                     Z_SRW_extra_arg **l = &extra_args;
476                     while (*l)
477                         l = &(*l)->next;
478                     *l = (Z_SRW_extra_arg *) odr_malloc(decode, sizeof(**l));
479                     (*l)->name = odr_strdup(decode, n);
480                     (*l)->value = odr_strdup(decode, v);
481                     (*l)->next = 0;
482                 }
483                 else
484                 {
485                     if (*num_diag < 10)
486                         yaz_add_srw_diagnostic(decode, diag, num_diag,
487                                                YAZ_SRW_UNSUPP_PARAMETER, n);
488                 }
489             }
490         }
491         if (!operation)
492         {
493             if (query)
494                 operation = "searchRetrieve";
495             else if (scanClause)
496                 operation = "scan";
497         }
498         version = yaz_negotiate_sru_version(version);
499
500         if (!version)
501         {   /* negotiation failed. */
502             yaz_add_srw_diagnostic(decode, diag, num_diag,
503                                    YAZ_SRW_UNSUPP_VERSION, "2.0");
504             version = "2.0";
505         }
506         if (!operation)
507         {
508             if (uri_name)
509                 yaz_add_srw_diagnostic(
510                     decode, diag, num_diag,
511                     YAZ_SRW_MANDATORY_PARAMETER_NOT_SUPPLIED, "operation");
512             operation = "explain";
513         }
514         if (strcmp(version, "2.0"))
515         {
516             if (recordXMLEscaping)
517             {
518                 yaz_add_srw_diagnostic(decode, diag, num_diag,
519                                        YAZ_SRW_UNSUPP_PARAMETER,
520                                        "recordXMLEscaping");
521
522             }
523             recordXMLEscaping = recordPacking;
524             recordPacking = "packed";
525         }
526         if (!recordXMLEscaping)
527             recordXMLEscaping = "xml";
528         if (!strcmp(operation, "searchRetrieve"))
529         {
530             Z_SRW_PDU *sr = yaz_srw_get(decode, Z_SRW_searchRetrieve_request);
531
532             sr->srw_version = version;
533             sr->extra_args = extra_args;
534             *srw_pdu = sr;
535             yaz_srw_decodeauth(sr, hreq, username, password, decode);
536
537             sr->u.request->queryType = queryType;
538             sr->u.request->query = query;
539
540             if (!query)
541                 yaz_add_srw_diagnostic(
542                     decode, diag, num_diag,
543                     YAZ_SRW_MANDATORY_PARAMETER_NOT_SUPPLIED, "query");
544
545             if (sortKeys)
546             {
547                 sr->u.request->sort_type = Z_SRW_sort_type_sort;
548                 sr->u.request->sort.sortKeys = sortKeys;
549             }
550             sr->u.request->recordXPath = recordXPath;
551             sr->u.request->recordSchema = recordSchema;
552             sr->u.request->recordPacking = recordXMLEscaping;
553             sr->u.request->packing = recordPacking;
554             sr->u.request->stylesheet = stylesheet;
555             yaz_sru_facet_request(decode , &sr->u.request->facetList,
556                                   &facetLimit, &facetStart, &facetSort);
557
558             yaz_sru_decode_integer(decode, "maximumRecords", maximumRecords,
559                                    &sr->u.request->maximumRecords,
560                                    diag, num_diag, 0);
561
562             yaz_sru_decode_integer(decode, "startRecord", startRecord,
563                                    &sr->u.request->startRecord,
564                                    diag, num_diag, 1);
565
566             sr->u.request->database = db;
567
568             (*soap_package) = (Z_SOAP *)
569                 odr_malloc(decode, sizeof(**soap_package));
570             (*soap_package)->which = Z_SOAP_generic;
571
572             (*soap_package)->u.generic = (Z_SOAP_Generic *)
573                 odr_malloc(decode, sizeof(*(*soap_package)->u.generic));
574
575             (*soap_package)->u.generic->p = sr;
576             (*soap_package)->u.generic->ns = soap_handlers[0].ns;
577             (*soap_package)->u.generic->no = 0;
578
579             (*soap_package)->ns = "SRU";
580
581             return 0;
582         }
583         else if (!strcmp(operation, "explain"))
584         {
585             /* Transfer SRU explain parameters to common struct */
586             /* http://www.loc.gov/z3950/agency/zing/srw/explain.html */
587             Z_SRW_PDU *sr = yaz_srw_get(decode, Z_SRW_explain_request);
588
589             sr->srw_version = version;
590             sr->extra_args = extra_args;
591             yaz_srw_decodeauth(sr, hreq, username, password, decode);
592             *srw_pdu = sr;
593             sr->u.explain_request->recordPacking = recordXMLEscaping;
594             sr->u.explain_request->packing = recordPacking;
595             sr->u.explain_request->database = db;
596
597             sr->u.explain_request->stylesheet = stylesheet;
598
599             (*soap_package) = (Z_SOAP *)
600                 odr_malloc(decode, sizeof(**soap_package));
601             (*soap_package)->which = Z_SOAP_generic;
602
603             (*soap_package)->u.generic = (Z_SOAP_Generic *)
604                 odr_malloc(decode, sizeof(*(*soap_package)->u.generic));
605
606             (*soap_package)->u.generic->p = sr;
607             (*soap_package)->u.generic->ns = soap_handlers[0].ns;
608             (*soap_package)->u.generic->no = 0;
609
610             (*soap_package)->ns = "SRU";
611
612             return 0;
613         }
614         else if (!strcmp(operation, "scan"))
615         {
616             /* Transfer SRU scan parameters to common struct */
617             /* http://www.loc.gov/z3950/agency/zing/srw/scan.html */
618             Z_SRW_PDU *sr = yaz_srw_get(decode, Z_SRW_scan_request);
619
620             sr->srw_version = version;
621             sr->extra_args = extra_args;
622             *srw_pdu = sr;
623             yaz_srw_decodeauth(sr, hreq, username, password, decode);
624
625             sr->u.scan_request->queryType = queryType;
626             sr->u.scan_request->scanClause = scanClause;
627
628             if (!scanClause)
629                 yaz_add_srw_diagnostic(
630                     decode, diag, num_diag,
631                     YAZ_SRW_MANDATORY_PARAMETER_NOT_SUPPLIED, "scanClause");
632             sr->u.scan_request->database = db;
633
634             yaz_sru_decode_integer(decode, "maximumTerms",
635                                    maximumTerms,
636                                    &sr->u.scan_request->maximumTerms,
637                                    diag, num_diag, 0);
638
639             yaz_sru_decode_integer(decode, "responsePosition",
640                                    responsePosition,
641                                    &sr->u.scan_request->responsePosition,
642                                    diag, num_diag, 0);
643
644             sr->u.scan_request->stylesheet = stylesheet;
645
646             (*soap_package) = (Z_SOAP *)
647                 odr_malloc(decode, sizeof(**soap_package));
648             (*soap_package)->which = Z_SOAP_generic;
649
650             (*soap_package)->u.generic = (Z_SOAP_Generic *)
651                 odr_malloc(decode, sizeof(*(*soap_package)->u.generic));
652
653             (*soap_package)->u.generic->p = sr;
654             (*soap_package)->u.generic->ns = soap_handlers[0].ns;
655             (*soap_package)->u.generic->no = 0;
656
657             (*soap_package)->ns = "SRU";
658
659             return 0;
660         }
661         else
662         {
663             /* unsupported operation ... */
664             /* Act as if we received a explain request and throw diagnostic. */
665
666             Z_SRW_PDU *sr = yaz_srw_get(decode, Z_SRW_explain_request);
667
668             sr->srw_version = version;
669             *srw_pdu = sr;
670             sr->u.explain_request->recordPacking = recordPacking;
671             sr->u.explain_request->database = db;
672
673             sr->u.explain_request->stylesheet = stylesheet;
674
675             (*soap_package) = (Z_SOAP *)
676                 odr_malloc(decode, sizeof(**soap_package));
677             (*soap_package)->which = Z_SOAP_generic;
678
679             (*soap_package)->u.generic = (Z_SOAP_Generic *)
680                 odr_malloc(decode, sizeof(*(*soap_package)->u.generic));
681
682             (*soap_package)->u.generic->p = sr;
683             (*soap_package)->u.generic->ns = soap_handlers[0].ns;
684             (*soap_package)->u.generic->no = 0;
685
686             (*soap_package)->ns = "SRU";
687
688             yaz_add_srw_diagnostic(decode, diag, num_diag,
689                                    YAZ_SRW_UNSUPP_OPERATION, operation);
690             return 0;
691         }
692 #else
693         return 1;
694 #endif
695     }
696     return 2;
697 }
698
699 Z_SRW_extra_record *yaz_srw_get_extra_record(ODR o)
700 {
701     Z_SRW_extra_record *res = (Z_SRW_extra_record *)
702         odr_malloc(o, sizeof(*res));
703
704     res->extraRecordData_buf = 0;
705     res->extraRecordData_len = 0;
706     res->recordIdentifier = 0;
707     return res;
708 }
709
710
711 Z_SRW_record *yaz_srw_get_records(ODR o, int n)
712 {
713     Z_SRW_record *res = (Z_SRW_record *) odr_malloc(o, n * sizeof(*res));
714     int i;
715
716     for (i = 0; i<n; i++)
717     {
718         res[i].recordSchema = 0;
719         res[i].recordPacking = Z_SRW_recordPacking_string;
720         res[i].recordData_buf = 0;
721         res[i].recordData_len = 0;
722         res[i].recordPosition = 0;
723     }
724     return res;
725 }
726
727 Z_SRW_record *yaz_srw_get_record(ODR o)
728 {
729     return yaz_srw_get_records(o, 1);
730 }
731
732 static Z_SRW_PDU *yaz_srw_get_core_ver(ODR o, const char *version)
733 {
734     Z_SRW_PDU *p = (Z_SRW_PDU *) odr_malloc(o, sizeof(*p));
735     p->srw_version = odr_strdup(o, version);
736     p->username = 0;
737     p->password = 0;
738     p->extra_args = 0;
739     p->extraResponseData_buf = 0;
740     p->extraResponseData_len = 0;
741     return p;
742 }
743
744 Z_SRW_PDU *yaz_srw_get_core_v_2_0(ODR o)
745 {
746     return yaz_srw_get_core_ver(o, "2.0");
747 }
748
749 Z_SRW_PDU *yaz_srw_get(ODR o, int which)
750 {
751     return yaz_srw_get_pdu(o, which, "2.0");
752 }
753
754 Z_SRW_PDU *yaz_srw_get_pdu(ODR o, int which, const char *version)
755 {
756     Z_SRW_PDU *sr = yaz_srw_get_core_ver(o, version);
757
758     sr->which = which;
759     switch(which)
760     {
761     case Z_SRW_searchRetrieve_request:
762         sr->u.request = (Z_SRW_searchRetrieveRequest *)
763             odr_malloc(o, sizeof(*sr->u.request));
764         sr->u.request->queryType = "cql";
765         sr->u.request->query = 0;
766         sr->u.request->sort_type = Z_SRW_sort_type_none;
767         sr->u.request->sort.none = 0;
768         sr->u.request->startRecord = 0;
769         sr->u.request->maximumRecords = 0;
770         sr->u.request->recordSchema = 0;
771         sr->u.request->recordPacking = 0;
772         sr->u.request->packing = 0;
773         sr->u.request->recordXPath = 0;
774         sr->u.request->database = 0;
775         sr->u.request->resultSetTTL = 0;
776         sr->u.request->stylesheet = 0;
777         sr->u.request->facetList = 0;
778         break;
779     case Z_SRW_searchRetrieve_response:
780         sr->u.response = (Z_SRW_searchRetrieveResponse *)
781             odr_malloc(o, sizeof(*sr->u.response));
782         sr->u.response->numberOfRecords = 0;
783         sr->u.response->resultCountPrecision = 0;
784         sr->u.response->resultSetId = 0;
785         sr->u.response->resultSetIdleTime = 0;
786         sr->u.response->records = 0;
787         sr->u.response->num_records = 0;
788         sr->u.response->diagnostics = 0;
789         sr->u.response->num_diagnostics = 0;
790         sr->u.response->nextRecordPosition = 0;
791         sr->u.response->extra_records = 0;
792         sr->u.response->facetList = 0;
793         sr->u.response->suggestions = 0;
794         break;
795     case Z_SRW_explain_request:
796         sr->u.explain_request = (Z_SRW_explainRequest *)
797             odr_malloc(o, sizeof(*sr->u.explain_request));
798         sr->u.explain_request->recordPacking = 0;
799         sr->u.explain_request->packing = 0;
800         sr->u.explain_request->database = 0;
801         sr->u.explain_request->stylesheet = 0;
802         break;
803     case Z_SRW_explain_response:
804         sr->u.explain_response = (Z_SRW_explainResponse *)
805             odr_malloc(o, sizeof(*sr->u.explain_response));
806         sr->u.explain_response->record.recordData_buf = 0;
807         sr->u.explain_response->record.recordData_len = 0;
808         sr->u.explain_response->record.recordSchema = 0;
809         sr->u.explain_response->record.recordPosition = 0;
810         sr->u.explain_response->record.recordPacking =
811             Z_SRW_recordPacking_string;
812         sr->u.explain_response->diagnostics = 0;
813         sr->u.explain_response->num_diagnostics = 0;
814         sr->u.explain_response->extra_record = 0;
815         break;
816     case Z_SRW_scan_request:
817         sr->u.scan_request = (Z_SRW_scanRequest *)
818             odr_malloc(o, sizeof(*sr->u.scan_request));
819         sr->u.scan_request->database = 0;
820         sr->u.scan_request->stylesheet = 0;
821         sr->u.scan_request->maximumTerms = 0;
822         sr->u.scan_request->responsePosition = 0;
823         sr->u.scan_request->queryType = "cql";
824         sr->u.scan_request->scanClause = 0;
825         break;
826     case Z_SRW_scan_response:
827         sr->u.scan_response = (Z_SRW_scanResponse *)
828             odr_malloc(o, sizeof(*sr->u.scan_response));
829         sr->u.scan_response->terms = 0;
830         sr->u.scan_response->num_terms = 0;
831         sr->u.scan_response->diagnostics = 0;
832         sr->u.scan_response->num_diagnostics = 0;
833         break;
834     case Z_SRW_update_request:
835         sr->u.update_request = (Z_SRW_updateRequest *)
836             odr_malloc(o, sizeof(*sr->u.update_request));
837         sr->u.update_request->database = 0;
838         sr->u.update_request->stylesheet = 0;
839         sr->u.update_request->record = 0;
840         sr->u.update_request->recordId = 0;
841         sr->u.update_request->recordVersions = 0;
842         sr->u.update_request->num_recordVersions = 0;
843         sr->u.update_request->extra_record = 0;
844         sr->u.update_request->extraRequestData_buf = 0;
845         sr->u.update_request->extraRequestData_len = 0;
846         sr->u.request->database = 0;
847         break;
848     case Z_SRW_update_response:
849         sr->u.update_response = (Z_SRW_updateResponse *)
850             odr_malloc(o, sizeof(*sr->u.update_response));
851         sr->u.update_response->operationStatus = 0;
852         sr->u.update_response->recordId = 0;
853         sr->u.update_response->recordVersions = 0;
854         sr->u.update_response->num_recordVersions = 0;
855         sr->u.update_response->record = 0;
856         sr->u.update_response->extra_record = 0;
857         sr->u.update_response->extraResponseData_buf = 0;
858         sr->u.update_response->extraResponseData_len = 0;
859         sr->u.update_response->diagnostics = 0;
860         sr->u.update_response->num_diagnostics = 0;
861     }
862     return sr;
863 }
864
865 void yaz_add_name_value_int(ODR o, char **name, char **value, int *i,
866                             char *a_name, Odr_int *val)
867 {
868     if (val)
869     {
870         name[*i] = a_name;
871         value[*i] = (char *) odr_malloc(o, 40);
872         sprintf(value[*i], ODR_INT_PRINTF, *val);
873         (*i)++;
874     }
875 }
876
877 void yaz_add_name_value_str(ODR o, char **name, char **value,  int *i,
878                             char *a_name, char *val)
879 {
880     if (val)
881     {
882         name[*i] = a_name;
883         value[*i] = val;
884         (*i)++;
885     }
886 }
887
888 static int yaz_get_sru_parms(const Z_SRW_PDU *srw_pdu, ODR encode,
889                              char **name, char **value, int max_names)
890 {
891     int version2 = strcmp(srw_pdu->srw_version, "2.") > 0;
892     int i = 0;
893     char *queryType;
894     yaz_add_name_value_str(encode, name, value, &i, "version",
895                            srw_pdu->srw_version);
896     name[i] = "operation";
897     switch (srw_pdu->which)
898     {
899     case Z_SRW_searchRetrieve_request:
900         value[i++] = "searchRetrieve";
901         queryType = srw_pdu->u.request->queryType;
902         if (version2)
903         {
904             if (queryType && strcmp(queryType, "cql"))
905                 yaz_add_name_value_str(encode, name, value, &i, "queryType",
906                                        queryType);
907             yaz_add_name_value_str(encode, name, value, &i, "query",
908                                    srw_pdu->u.request->query);
909         }
910         else
911         {
912             if (!strcmp(queryType, "cql"))
913             {
914                 yaz_add_name_value_str(encode, name, value, &i, "query",
915                                        srw_pdu->u.request->query);
916             }
917             else if (!strcmp(queryType, "pqf"))
918             {
919                 yaz_add_name_value_str(encode, name, value, &i, "x-pquery",
920                                        srw_pdu->u.request->query);
921             }
922             else if (!strcmp(queryType, "xcql"))
923             {
924                 yaz_add_name_value_str(encode, name, value, &i, "x-cql",
925                                        srw_pdu->u.request->query);
926             }
927         }
928         switch (srw_pdu->u.request->sort_type)
929         {
930         case Z_SRW_sort_type_none:
931             break;
932         case Z_SRW_sort_type_sort:
933             yaz_add_name_value_str(encode, name, value, &i, "sortKeys",
934                                    srw_pdu->u.request->sort.sortKeys);
935             break;
936         }
937         yaz_add_name_value_int(encode, name, value, &i, "startRecord",
938                                srw_pdu->u.request->startRecord);
939         yaz_add_name_value_int(encode, name, value, &i, "maximumRecords",
940                                srw_pdu->u.request->maximumRecords);
941         yaz_add_name_value_str(encode, name, value, &i, "recordSchema",
942                                srw_pdu->u.request->recordSchema);
943         if (version2)
944         {
945             yaz_add_name_value_str(encode, name, value, &i, "recordXMLEscaping",
946                                    srw_pdu->u.request->recordPacking);
947             yaz_add_name_value_str(encode, name, value, &i, "recordPacking",
948                                    srw_pdu->u.request->packing);
949         }
950         else
951             yaz_add_name_value_str(encode, name, value, &i, "recordPacking",
952                                    srw_pdu->u.request->recordPacking);
953         yaz_add_name_value_str(encode, name, value, &i, "recordXPath",
954                                srw_pdu->u.request->recordXPath);
955         yaz_add_name_value_str(encode, name, value, &i, "stylesheet",
956                                srw_pdu->u.request->stylesheet);
957         yaz_add_name_value_int(encode, name, value, &i, "resultSetTTL",
958                                srw_pdu->u.request->resultSetTTL);
959         {
960             const char *facetLimit = 0;
961             const char *facetStart = 0;
962             const char *facetSort = 0;
963             yaz_sru_facet_request(encode, &srw_pdu->u.request->facetList,
964                                   &facetLimit, &facetStart, &facetSort);
965             yaz_add_name_value_str(encode, name, value, &i, "facetLimit",
966                                    (char *) facetLimit);
967             yaz_add_name_value_str(encode, name, value, &i, "facetStart",
968                                    (char *) facetStart);
969             yaz_add_name_value_str(encode, name, value, &i, "facetSort",
970                                    (char *) facetSort);
971         }
972         break;
973     case Z_SRW_explain_request:
974         value[i++] = "explain";
975
976         if (version2)
977         {
978             yaz_add_name_value_str(encode, name, value, &i, "recordXMLEscaping",
979                                    srw_pdu->u.explain_request->recordPacking);
980             yaz_add_name_value_str(encode, name, value, &i, "recordPacking",
981                                    srw_pdu->u.explain_request->packing);
982         }
983         else
984             yaz_add_name_value_str(encode, name, value, &i, "recordPacking",
985                                    srw_pdu->u.explain_request->recordPacking);
986         yaz_add_name_value_str(encode, name, value, &i, "stylesheet",
987                                srw_pdu->u.explain_request->stylesheet);
988         break;
989     case Z_SRW_scan_request:
990         value[i++] = "scan";
991         queryType = srw_pdu->u.scan_request->queryType;
992         if (version2)
993         {
994             if (queryType && strcmp(queryType, "cql"))
995                 yaz_add_name_value_str(encode, name, value, &i, "queryType",
996                                        queryType);
997             yaz_add_name_value_str(encode, name, value, &i, "scanClause",
998                                    srw_pdu->u.scan_request->scanClause);
999         }
1000         else
1001         {
1002             if (!queryType || !strcmp(queryType, "cql"))
1003                 yaz_add_name_value_str(encode, name, value, &i, "scanClause",
1004                                        srw_pdu->u.scan_request->scanClause);
1005             else if (!strcmp(queryType, "pqf"))
1006                 yaz_add_name_value_str(encode, name, value, &i, "x-pScanClause",
1007                                        srw_pdu->u.scan_request->scanClause);
1008             else if (!strcmp(queryType, "xcql"))
1009                 yaz_add_name_value_str(encode, name, value, &i,
1010                                        "x-cqlScanClause",
1011                                        srw_pdu->u.scan_request->scanClause);
1012         }
1013         yaz_add_name_value_int(encode, name, value, &i, "responsePosition",
1014                                srw_pdu->u.scan_request->responsePosition);
1015         yaz_add_name_value_int(encode, name, value, &i, "maximumTerms",
1016                                srw_pdu->u.scan_request->maximumTerms);
1017         yaz_add_name_value_str(encode, name, value, &i, "stylesheet",
1018                                srw_pdu->u.scan_request->stylesheet);
1019         break;
1020     case Z_SRW_update_request:
1021         value[i++] = "update";
1022         break;
1023     default:
1024         return -1;
1025     }
1026     if (srw_pdu->extra_args)
1027     {
1028         Z_SRW_extra_arg *ea = srw_pdu->extra_args;
1029         for (; ea && i < max_names-1; ea = ea->next)
1030         {
1031             name[i] = ea->name;
1032             value[i] = ea->value;
1033             i++;
1034         }
1035     }
1036     name[i++] = 0;
1037
1038     return 0;
1039 }
1040
1041 int yaz_sru_get_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
1042                        ODR encode, const char *charset)
1043 {
1044     char *name[MAX_SRU_PARAMETERS], *value[MAX_SRU_PARAMETERS]; /* definite upper limit for SRU params */
1045     char *uri_args;
1046     char *path;
1047
1048     z_HTTP_header_add_basic_auth(encode, &hreq->headers,
1049                                  srw_pdu->username, srw_pdu->password);
1050     if (yaz_get_sru_parms(srw_pdu, encode, name, value, MAX_SRU_PARAMETERS))
1051         return -1;
1052     yaz_array_to_uri(&uri_args, encode, name, value);
1053
1054     hreq->method = "GET";
1055
1056     path = (char *)
1057         odr_malloc(encode, strlen(hreq->path) + strlen(uri_args) + 4);
1058
1059     sprintf(path, "%s?%s", hreq->path, uri_args);
1060     hreq->path = path;
1061
1062     z_HTTP_header_add_content_type(encode, &hreq->headers,
1063                                    "text/xml", charset);
1064     return 0;
1065 }
1066
1067 int yaz_sru_post_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
1068                         ODR encode, const char *charset)
1069 {
1070     char *name[MAX_SRU_PARAMETERS], *value[MAX_SRU_PARAMETERS]; /* definite upper limit for SRU params */
1071     char *uri_args;
1072
1073     z_HTTP_header_add_basic_auth(encode, &hreq->headers,
1074                                  srw_pdu->username, srw_pdu->password);
1075     if (yaz_get_sru_parms(srw_pdu, encode, name, value, MAX_SRU_PARAMETERS))
1076         return -1;
1077
1078     yaz_array_to_uri(&uri_args, encode, name, value);
1079
1080     hreq->method = "POST";
1081
1082     hreq->content_buf = uri_args;
1083     hreq->content_len = strlen(uri_args);
1084
1085     z_HTTP_header_add_content_type(encode, &hreq->headers,
1086                                    "application/x-www-form-urlencoded",
1087                                    charset);
1088     return 0;
1089 }
1090
1091 int yaz_sru_soap_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
1092                         ODR odr, const char *charset)
1093 {
1094     Z_SOAP_Handler handlers[3] = {
1095 #if YAZ_HAVE_XML2
1096         {YAZ_XMLNS_SRU_v1_1, 0, (Z_SOAP_fun) yaz_srw_codec},
1097         {YAZ_XMLNS_UPDATE_v0_9, 0, (Z_SOAP_fun) yaz_ucp_codec},
1098 #endif
1099         {0, 0, 0}
1100     };
1101     Z_SOAP *p = (Z_SOAP*) odr_malloc(odr, sizeof(*p));
1102
1103     z_HTTP_header_add_basic_auth(odr, &hreq->headers,
1104                                  srw_pdu->username, srw_pdu->password);
1105     z_HTTP_header_add_content_type(odr,
1106                                    &hreq->headers,
1107                                    "text/xml", charset);
1108
1109     z_HTTP_header_add(odr, &hreq->headers,
1110                       "SOAPAction", "\"\"");
1111     p->which = Z_SOAP_generic;
1112     p->u.generic = (Z_SOAP_Generic *) odr_malloc(odr, sizeof(*p->u.generic));
1113     p->u.generic->no = 0;
1114     p->u.generic->ns = 0;
1115     p->u.generic->p = srw_pdu;
1116     p->ns = "http://schemas.xmlsoap.org/soap/envelope/";
1117
1118 #if YAZ_HAVE_XML2
1119     if (srw_pdu->which == Z_SRW_update_request ||
1120         srw_pdu->which == Z_SRW_update_response)
1121         p->u.generic->no = 1; /* second handler */
1122 #endif
1123     return z_soap_codec_enc(odr, &p,
1124                             &hreq->content_buf,
1125                             &hreq->content_len, handlers,
1126                             charset);
1127 }
1128
1129 Z_SRW_recordVersion *yaz_srw_get_record_versions(ODR odr, int num)
1130 {
1131     Z_SRW_recordVersion *ver
1132         = (Z_SRW_recordVersion *) odr_malloc(odr,num * sizeof(*ver));
1133     int i;
1134     for (i = 0; i < num; ++i)
1135     {
1136         ver[i].versionType = 0;
1137         ver[i].versionValue = 0;
1138     }
1139     return ver;
1140 }
1141
1142 const char *yaz_srw_pack_to_str(int pack)
1143 {
1144     switch(pack)
1145     {
1146     case Z_SRW_recordPacking_string:
1147         return "string";
1148     case Z_SRW_recordPacking_XML:
1149         return "xml";
1150     case Z_SRW_recordPacking_URL:
1151         return "url";
1152     }
1153     return 0;
1154 }
1155
1156 int yaz_srw_str_to_pack(const char *str)
1157 {
1158     if (!yaz_matchstr(str, "string"))
1159         return Z_SRW_recordPacking_string;
1160     if (!yaz_matchstr(str, "xml"))
1161         return Z_SRW_recordPacking_XML;
1162     if (!yaz_matchstr(str, "url"))
1163         return Z_SRW_recordPacking_URL;
1164     return -1;
1165 }
1166
1167 void yaz_encode_sru_extra(Z_SRW_PDU *sr, ODR odr, const char *extra_args)
1168 {
1169     if (extra_args)
1170     {
1171         char **name;
1172         char **val;
1173         Z_SRW_extra_arg **ea = &sr->extra_args;
1174         yaz_uri_to_array(extra_args, odr, &name, &val);
1175
1176         /** append rather than override */
1177         while (*ea)
1178             ea = &(*ea)->next;
1179         while (*name)
1180         {
1181             *ea = (Z_SRW_extra_arg *) odr_malloc(odr, sizeof(**ea));
1182             (*ea)->name = *name;
1183             (*ea)->value = *val;
1184             ea = &(*ea)->next;
1185             val++;
1186             name++;
1187         }
1188         *ea = 0;
1189     }
1190 }
1191
1192
1193 /*
1194  * Local variables:
1195  * c-basic-offset: 4
1196  * c-file-style: "Stroustrup"
1197  * indent-tabs-mode: nil
1198  * End:
1199  * vim: shiftwidth=4 tabstop=8 expandtab
1200  */
1201