Content-Type charset fixes YAZ-701
[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 j = 0, i = 0;
238             int sep = 0;
239             charset_p += 10; /* skip ; charset=  */
240             if (charset_p[i] == '"' || charset_p[i] == '\'')
241                 sep = charset_p[i++];
242             *charset = odr_strdup(o, charset_p);
243             while (charset_p[i] && charset_p[i] != sep)
244             {
245                 if (charset_p[i] == '\\' && charset_p[i+1])
246                     i++;
247                 (*charset)[j++] = charset_p[i++];
248             }
249             (*charset)[j] = '\0';
250         }
251     }
252 }
253
254 int yaz_srw_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
255                    Z_SOAP **soap_package, ODR decode, char **charset)
256 {
257     if (!strcmp(hreq->method, "POST"))
258     {
259         const char *content_type = z_HTTP_header_lookup(hreq->headers,
260                                                         "Content-Type");
261         if (content_type &&
262             (!yaz_strcmp_del("text/xml", content_type, "; ") ||
263              !yaz_strcmp_del("application/soap+xml", content_type, "; ") ||
264              !yaz_strcmp_del("text/plain", content_type, "; ")))
265         {
266             char *db = "Default";
267             const char *p0 = hreq->path, *p1;
268             int ret = -1;
269
270             static Z_SOAP_Handler soap_handlers[5] = {
271 #if YAZ_HAVE_XML2
272                 { YAZ_XMLNS_SRU_v1_1, 0, (Z_SOAP_fun) yaz_srw_codec },
273                 { YAZ_XMLNS_SRU_v1_0, 0, (Z_SOAP_fun) yaz_srw_codec },
274                 { YAZ_XMLNS_UPDATE_v0_9, 0, (Z_SOAP_fun) yaz_ucp_codec },
275                 { YAZ_XMLNS_SRU_v2_mask, 0, (Z_SOAP_fun) yaz_srw_codec },
276 #endif
277                 {0, 0, 0}
278             };
279
280             if (*p0 == '/')
281                 p0++;
282             p1 = strchr(p0, '?');
283             if (!p1)
284                 p1 = p0 + strlen(p0);
285             if (p1 != p0)
286                 db = yaz_decode_sru_dbpath_odr(decode, p0, p1 - p0);
287
288             ret = z_soap_codec(decode, soap_package,
289                                &hreq->content_buf, &hreq->content_len,
290                                soap_handlers);
291             if (ret == 0 && (*soap_package)->which == Z_SOAP_generic)
292             {
293                 *srw_pdu = (Z_SRW_PDU*) (*soap_package)->u.generic->p;
294                 yaz_srw_decodeauth(*srw_pdu, hreq, 0, 0, decode);
295
296                 /* last entry in handlers - SRU 2.0 - is turned into
297                    offset 0.. due to other pieces relying on it */
298                 if ((*soap_package)->u.generic->no == 3)
299                     (*soap_package)->u.generic->no = 0;
300                 if ((*srw_pdu)->which == Z_SRW_searchRetrieve_request &&
301                     (*srw_pdu)->u.request->database == 0)
302                     (*srw_pdu)->u.request->database = db;
303
304                 if ((*srw_pdu)->which == Z_SRW_explain_request &&
305                     (*srw_pdu)->u.explain_request->database == 0)
306                     (*srw_pdu)->u.explain_request->database = db;
307
308                 if ((*srw_pdu)->which == Z_SRW_scan_request &&
309                     (*srw_pdu)->u.scan_request->database == 0)
310                     (*srw_pdu)->u.scan_request->database = db;
311
312                 if ((*srw_pdu)->which == Z_SRW_update_request &&
313                     (*srw_pdu)->u.update_request->database == 0)
314                     (*srw_pdu)->u.update_request->database = db;
315
316                 return 0;
317             }
318             return 1;
319         }
320     }
321     return 2;
322 }
323
324 #if YAZ_HAVE_XML2
325 static int yaz_sru_decode_integer(ODR odr, const char *pname,
326                                   const char *valstr, Odr_int **valp,
327                                   Z_SRW_diagnostic **diag, int *num_diag,
328                                   int min_value)
329 {
330     int ival;
331     if (!valstr)
332         return 0;
333     if (sscanf(valstr, "%d", &ival) != 1)
334     {
335         yaz_add_srw_diagnostic(odr, diag, num_diag,
336                                YAZ_SRW_UNSUPP_PARAMETER_VALUE, pname);
337         return 0;
338     }
339     if (min_value >= 0 && ival < min_value)
340     {
341         yaz_add_srw_diagnostic(odr, diag, num_diag,
342                                YAZ_SRW_UNSUPP_PARAMETER_VALUE, pname);
343         return 0;
344     }
345     *valp = odr_intdup(odr, ival);
346     return 1;
347 }
348 #endif
349
350 /**
351    http://www.loc.gov/z3950/agency/zing/srw/service.html
352 */
353 int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
354                    Z_SOAP **soap_package, ODR decode, char **charset,
355                    Z_SRW_diagnostic **diag, int *num_diag)
356 {
357 #if YAZ_HAVE_XML2
358     static Z_SOAP_Handler soap_handlers[2] = {
359         {YAZ_XMLNS_SRU_v1_1, 0, (Z_SOAP_fun) yaz_srw_codec},
360         {0, 0, 0}
361     };
362 #endif
363     const char *content_type = z_HTTP_header_lookup(hreq->headers,
364                                                     "Content-Type");
365
366     /*
367       SRU GET: ignore content type.
368       SRU POST: we support "application/x-www-form-urlencoded";
369       not  "multipart/form-data" .
370     */
371     if (!strcmp(hreq->method, "GET")
372         ||
373         (!strcmp(hreq->method, "POST") && content_type &&
374          !yaz_strcmp_del("application/x-www-form-urlencoded",
375                          content_type, "; ")))
376     {
377         char *db = "Default";
378         const char *p0 = hreq->path, *p1;
379 #if YAZ_HAVE_XML2
380         const char *operation = 0;
381         char *version = 0;
382         char *query = 0;
383         char *queryType = "cql";
384         char *username = 0;
385         char *password = 0;
386         char *sortKeys = 0;
387         char *stylesheet = 0;
388         char *scanClause = 0;
389         char *recordXPath = 0;
390         char *recordSchema = 0;
391         char *recordXMLEscaping = 0;
392         char *recordPacking = 0;
393         char *maximumRecords = 0;
394         char *startRecord = 0;
395         char *maximumTerms = 0;
396         char *responsePosition = 0;
397         const char *facetLimit = 0;
398         const char *facetStart = 0;
399         const char *facetSort = 0;
400         Z_SRW_extra_arg *extra_args = 0;
401 #endif
402         char **uri_name;
403         char **uri_val;
404
405         grab_charset(decode, content_type, charset);
406         if (charset && *charset == 0 && !strcmp(hreq->method, "GET"))
407             *charset = "UTF-8";
408
409         if (*p0 == '/')
410             p0++;
411         p1 = strchr(p0, '?');
412         if (!p1)
413             p1 = p0 + strlen(p0);
414         if (p1 != p0)
415             db = yaz_decode_sru_dbpath_odr(decode, p0, p1 - p0);
416         if (!strcmp(hreq->method, "POST"))
417             p1 = hreq->content_buf;
418         yaz_uri_to_array(p1, decode, &uri_name, &uri_val);
419 #if YAZ_HAVE_XML2
420         if (uri_name)
421         {
422             int i;
423             for (i = 0; uri_name[i]; i++)
424             {
425                 char *n = uri_name[i];
426                 char *v = uri_val[i];
427                 if (!strcmp(n, "query"))
428                     query = v;
429                 else if (!strcmp(n, "x-pquery"))
430                 {
431                     query = v;
432                     queryType = "pqf";
433                 }
434                 else if (!strcmp(n, "queryType"))
435                     queryType = v;
436                 else if (!strcmp(n, "x-username"))
437                     username = v;
438                 else if (!strcmp(n, "x-password"))
439                     password = v;
440                 else if (!strcmp(n, "operation"))
441                     operation = v;
442                 else if (!strcmp(n, "stylesheet"))
443                     stylesheet = v;
444                 else if (!strcmp(n, "sortKeys"))
445                     sortKeys = v;
446                 else if (!strcmp(n, "recordXPath"))
447                     recordXPath = v;
448                 else if (!strcmp(n, "recordSchema"))
449                     recordSchema = v;
450                 else if (!strcmp(n, "recordPacking"))
451                     recordPacking = v;
452                 else if (!strcmp(n, "recordXMLEscaping"))
453                     recordXMLEscaping = v;
454                 else if (!strcmp(n, "version"))
455                     version = v;
456                 else if (!strcmp(n, "scanClause"))
457                     scanClause = v;
458                 else if (!strcmp(n, "x-pScanClause"))
459                 {
460                     scanClause = v;
461                     queryType = "pqf";
462                 }
463                 else if (!strcmp(n, "maximumRecords"))
464                     maximumRecords = v;
465                 else if (!strcmp(n, "startRecord"))
466                     startRecord = v;
467                 else if (!strcmp(n, "maximumTerms"))
468                     maximumTerms = v;
469                 else if (!strcmp(n, "responsePosition"))
470                     responsePosition = v;
471                 else if (!strcmp(n, "facetLimit"))
472                     facetLimit = v;
473                 else if (!strcmp(n, "facetStart"))
474                     facetStart = v;
475                 else if (!strcmp(n, "facetSort"))
476                     facetSort = v;
477                 else if (!strcmp(n, "extraRequestData"))
478                     ; /* ignoring extraRequestData */
479                 else if (n[0] == 'x' && n[1] == '-')
480                 {
481                     Z_SRW_extra_arg **l = &extra_args;
482                     while (*l)
483                         l = &(*l)->next;
484                     *l = (Z_SRW_extra_arg *) odr_malloc(decode, sizeof(**l));
485                     (*l)->name = odr_strdup(decode, n);
486                     (*l)->value = odr_strdup(decode, v);
487                     (*l)->next = 0;
488                 }
489                 else
490                 {
491                     if (*num_diag < 10)
492                         yaz_add_srw_diagnostic(decode, diag, num_diag,
493                                                YAZ_SRW_UNSUPP_PARAMETER, n);
494                 }
495             }
496         }
497         if (!operation)
498         {
499             if (query)
500                 operation = "searchRetrieve";
501             else if (scanClause)
502                 operation = "scan";
503         }
504         version = yaz_negotiate_sru_version(version);
505
506         if (!version)
507         {   /* negotiation failed. */
508             yaz_add_srw_diagnostic(decode, diag, num_diag,
509                                    YAZ_SRW_UNSUPP_VERSION, "2.0");
510             version = "2.0";
511         }
512         if (!operation)
513         {
514             if (uri_name)
515                 yaz_add_srw_diagnostic(
516                     decode, diag, num_diag,
517                     YAZ_SRW_MANDATORY_PARAMETER_NOT_SUPPLIED, "operation");
518             operation = "explain";
519         }
520         if (strcmp(version, "2.0"))
521         {
522             if (recordXMLEscaping)
523             {
524                 yaz_add_srw_diagnostic(decode, diag, num_diag,
525                                        YAZ_SRW_UNSUPP_PARAMETER,
526                                        "recordXMLEscaping");
527
528             }
529             recordXMLEscaping = recordPacking;
530             recordPacking = "packed";
531         }
532         if (!recordXMLEscaping)
533             recordXMLEscaping = "xml";
534         if (!strcmp(operation, "searchRetrieve"))
535         {
536             Z_SRW_PDU *sr = yaz_srw_get(decode, Z_SRW_searchRetrieve_request);
537
538             sr->srw_version = version;
539             sr->extra_args = extra_args;
540             *srw_pdu = sr;
541             yaz_srw_decodeauth(sr, hreq, username, password, decode);
542
543             sr->u.request->queryType = queryType;
544             sr->u.request->query = query;
545
546             if (!query)
547                 yaz_add_srw_diagnostic(
548                     decode, diag, num_diag,
549                     YAZ_SRW_MANDATORY_PARAMETER_NOT_SUPPLIED, "query");
550
551             if (sortKeys)
552             {
553                 sr->u.request->sort_type = Z_SRW_sort_type_sort;
554                 sr->u.request->sort.sortKeys = sortKeys;
555             }
556             sr->u.request->recordXPath = recordXPath;
557             sr->u.request->recordSchema = recordSchema;
558             sr->u.request->recordPacking = recordXMLEscaping;
559             sr->u.request->packing = recordPacking;
560             sr->u.request->stylesheet = stylesheet;
561             yaz_sru_facet_request(decode , &sr->u.request->facetList,
562                                   &facetLimit, &facetStart, &facetSort);
563
564             yaz_sru_decode_integer(decode, "maximumRecords", maximumRecords,
565                                    &sr->u.request->maximumRecords,
566                                    diag, num_diag, 0);
567
568             yaz_sru_decode_integer(decode, "startRecord", startRecord,
569                                    &sr->u.request->startRecord,
570                                    diag, num_diag, 1);
571
572             sr->u.request->database = db;
573
574             (*soap_package) = (Z_SOAP *)
575                 odr_malloc(decode, sizeof(**soap_package));
576             (*soap_package)->which = Z_SOAP_generic;
577
578             (*soap_package)->u.generic = (Z_SOAP_Generic *)
579                 odr_malloc(decode, sizeof(*(*soap_package)->u.generic));
580
581             (*soap_package)->u.generic->p = sr;
582             (*soap_package)->u.generic->ns = soap_handlers[0].ns;
583             (*soap_package)->u.generic->no = 0;
584
585             (*soap_package)->ns = "SRU";
586
587             return 0;
588         }
589         else if (!strcmp(operation, "explain"))
590         {
591             /* Transfer SRU explain parameters to common struct */
592             /* http://www.loc.gov/z3950/agency/zing/srw/explain.html */
593             Z_SRW_PDU *sr = yaz_srw_get(decode, Z_SRW_explain_request);
594
595             sr->srw_version = version;
596             sr->extra_args = extra_args;
597             yaz_srw_decodeauth(sr, hreq, username, password, decode);
598             *srw_pdu = sr;
599             sr->u.explain_request->recordPacking = recordXMLEscaping;
600             sr->u.explain_request->packing = recordPacking;
601             sr->u.explain_request->database = db;
602
603             sr->u.explain_request->stylesheet = stylesheet;
604
605             (*soap_package) = (Z_SOAP *)
606                 odr_malloc(decode, sizeof(**soap_package));
607             (*soap_package)->which = Z_SOAP_generic;
608
609             (*soap_package)->u.generic = (Z_SOAP_Generic *)
610                 odr_malloc(decode, sizeof(*(*soap_package)->u.generic));
611
612             (*soap_package)->u.generic->p = sr;
613             (*soap_package)->u.generic->ns = soap_handlers[0].ns;
614             (*soap_package)->u.generic->no = 0;
615
616             (*soap_package)->ns = "SRU";
617
618             return 0;
619         }
620         else if (!strcmp(operation, "scan"))
621         {
622             /* Transfer SRU scan parameters to common struct */
623             /* http://www.loc.gov/z3950/agency/zing/srw/scan.html */
624             Z_SRW_PDU *sr = yaz_srw_get(decode, Z_SRW_scan_request);
625
626             sr->srw_version = version;
627             sr->extra_args = extra_args;
628             *srw_pdu = sr;
629             yaz_srw_decodeauth(sr, hreq, username, password, decode);
630
631             sr->u.scan_request->queryType = queryType;
632             sr->u.scan_request->scanClause = scanClause;
633
634             if (!scanClause)
635                 yaz_add_srw_diagnostic(
636                     decode, diag, num_diag,
637                     YAZ_SRW_MANDATORY_PARAMETER_NOT_SUPPLIED, "scanClause");
638             sr->u.scan_request->database = db;
639
640             yaz_sru_decode_integer(decode, "maximumTerms",
641                                    maximumTerms,
642                                    &sr->u.scan_request->maximumTerms,
643                                    diag, num_diag, 0);
644
645             yaz_sru_decode_integer(decode, "responsePosition",
646                                    responsePosition,
647                                    &sr->u.scan_request->responsePosition,
648                                    diag, num_diag, 0);
649
650             sr->u.scan_request->stylesheet = stylesheet;
651
652             (*soap_package) = (Z_SOAP *)
653                 odr_malloc(decode, sizeof(**soap_package));
654             (*soap_package)->which = Z_SOAP_generic;
655
656             (*soap_package)->u.generic = (Z_SOAP_Generic *)
657                 odr_malloc(decode, sizeof(*(*soap_package)->u.generic));
658
659             (*soap_package)->u.generic->p = sr;
660             (*soap_package)->u.generic->ns = soap_handlers[0].ns;
661             (*soap_package)->u.generic->no = 0;
662
663             (*soap_package)->ns = "SRU";
664
665             return 0;
666         }
667         else
668         {
669             /* unsupported operation ... */
670             /* Act as if we received a explain request and throw diagnostic. */
671
672             Z_SRW_PDU *sr = yaz_srw_get(decode, Z_SRW_explain_request);
673
674             sr->srw_version = version;
675             *srw_pdu = sr;
676             sr->u.explain_request->recordPacking = recordPacking;
677             sr->u.explain_request->database = db;
678
679             sr->u.explain_request->stylesheet = stylesheet;
680
681             (*soap_package) = (Z_SOAP *)
682                 odr_malloc(decode, sizeof(**soap_package));
683             (*soap_package)->which = Z_SOAP_generic;
684
685             (*soap_package)->u.generic = (Z_SOAP_Generic *)
686                 odr_malloc(decode, sizeof(*(*soap_package)->u.generic));
687
688             (*soap_package)->u.generic->p = sr;
689             (*soap_package)->u.generic->ns = soap_handlers[0].ns;
690             (*soap_package)->u.generic->no = 0;
691
692             (*soap_package)->ns = "SRU";
693
694             yaz_add_srw_diagnostic(decode, diag, num_diag,
695                                    YAZ_SRW_UNSUPP_OPERATION, operation);
696             return 0;
697         }
698 #else
699         return 1;
700 #endif
701     }
702     return 2;
703 }
704
705 Z_SRW_extra_record *yaz_srw_get_extra_record(ODR o)
706 {
707     Z_SRW_extra_record *res = (Z_SRW_extra_record *)
708         odr_malloc(o, sizeof(*res));
709
710     res->extraRecordData_buf = 0;
711     res->extraRecordData_len = 0;
712     res->recordIdentifier = 0;
713     return res;
714 }
715
716
717 Z_SRW_record *yaz_srw_get_records(ODR o, int n)
718 {
719     Z_SRW_record *res = (Z_SRW_record *) odr_malloc(o, n * sizeof(*res));
720     int i;
721
722     for (i = 0; i<n; i++)
723     {
724         res[i].recordSchema = 0;
725         res[i].recordPacking = Z_SRW_recordPacking_string;
726         res[i].recordData_buf = 0;
727         res[i].recordData_len = 0;
728         res[i].recordPosition = 0;
729     }
730     return res;
731 }
732
733 Z_SRW_record *yaz_srw_get_record(ODR o)
734 {
735     return yaz_srw_get_records(o, 1);
736 }
737
738 static Z_SRW_PDU *yaz_srw_get_core_ver(ODR o, const char *version)
739 {
740     Z_SRW_PDU *p = (Z_SRW_PDU *) odr_malloc(o, sizeof(*p));
741     p->srw_version = odr_strdup(o, version);
742     p->username = 0;
743     p->password = 0;
744     p->extra_args = 0;
745     p->extraResponseData_buf = 0;
746     p->extraResponseData_len = 0;
747     return p;
748 }
749
750 Z_SRW_PDU *yaz_srw_get_core_v_2_0(ODR o)
751 {
752     return yaz_srw_get_core_ver(o, "2.0");
753 }
754
755 Z_SRW_PDU *yaz_srw_get(ODR o, int which)
756 {
757     return yaz_srw_get_pdu(o, which, "2.0");
758 }
759
760 Z_SRW_PDU *yaz_srw_get_pdu(ODR o, int which, const char *version)
761 {
762     Z_SRW_PDU *sr = yaz_srw_get_core_ver(o, version);
763
764     sr->which = which;
765     switch(which)
766     {
767     case Z_SRW_searchRetrieve_request:
768         sr->u.request = (Z_SRW_searchRetrieveRequest *)
769             odr_malloc(o, sizeof(*sr->u.request));
770         sr->u.request->queryType = "cql";
771         sr->u.request->query = 0;
772         sr->u.request->sort_type = Z_SRW_sort_type_none;
773         sr->u.request->sort.none = 0;
774         sr->u.request->startRecord = 0;
775         sr->u.request->maximumRecords = 0;
776         sr->u.request->recordSchema = 0;
777         sr->u.request->recordPacking = 0;
778         sr->u.request->packing = 0;
779         sr->u.request->recordXPath = 0;
780         sr->u.request->database = 0;
781         sr->u.request->resultSetTTL = 0;
782         sr->u.request->stylesheet = 0;
783         sr->u.request->facetList = 0;
784         break;
785     case Z_SRW_searchRetrieve_response:
786         sr->u.response = (Z_SRW_searchRetrieveResponse *)
787             odr_malloc(o, sizeof(*sr->u.response));
788         sr->u.response->numberOfRecords = 0;
789         sr->u.response->resultCountPrecision = 0;
790         sr->u.response->resultSetId = 0;
791         sr->u.response->resultSetIdleTime = 0;
792         sr->u.response->records = 0;
793         sr->u.response->num_records = 0;
794         sr->u.response->diagnostics = 0;
795         sr->u.response->num_diagnostics = 0;
796         sr->u.response->nextRecordPosition = 0;
797         sr->u.response->extra_records = 0;
798         sr->u.response->facetList = 0;
799         sr->u.response->suggestions = 0;
800         break;
801     case Z_SRW_explain_request:
802         sr->u.explain_request = (Z_SRW_explainRequest *)
803             odr_malloc(o, sizeof(*sr->u.explain_request));
804         sr->u.explain_request->recordPacking = 0;
805         sr->u.explain_request->packing = 0;
806         sr->u.explain_request->database = 0;
807         sr->u.explain_request->stylesheet = 0;
808         break;
809     case Z_SRW_explain_response:
810         sr->u.explain_response = (Z_SRW_explainResponse *)
811             odr_malloc(o, sizeof(*sr->u.explain_response));
812         sr->u.explain_response->record.recordData_buf = 0;
813         sr->u.explain_response->record.recordData_len = 0;
814         sr->u.explain_response->record.recordSchema = 0;
815         sr->u.explain_response->record.recordPosition = 0;
816         sr->u.explain_response->record.recordPacking =
817             Z_SRW_recordPacking_string;
818         sr->u.explain_response->diagnostics = 0;
819         sr->u.explain_response->num_diagnostics = 0;
820         sr->u.explain_response->extra_record = 0;
821         break;
822     case Z_SRW_scan_request:
823         sr->u.scan_request = (Z_SRW_scanRequest *)
824             odr_malloc(o, sizeof(*sr->u.scan_request));
825         sr->u.scan_request->database = 0;
826         sr->u.scan_request->stylesheet = 0;
827         sr->u.scan_request->maximumTerms = 0;
828         sr->u.scan_request->responsePosition = 0;
829         sr->u.scan_request->queryType = "cql";
830         sr->u.scan_request->scanClause = 0;
831         break;
832     case Z_SRW_scan_response:
833         sr->u.scan_response = (Z_SRW_scanResponse *)
834             odr_malloc(o, sizeof(*sr->u.scan_response));
835         sr->u.scan_response->terms = 0;
836         sr->u.scan_response->num_terms = 0;
837         sr->u.scan_response->diagnostics = 0;
838         sr->u.scan_response->num_diagnostics = 0;
839         break;
840     case Z_SRW_update_request:
841         sr->u.update_request = (Z_SRW_updateRequest *)
842             odr_malloc(o, sizeof(*sr->u.update_request));
843         sr->u.update_request->database = 0;
844         sr->u.update_request->stylesheet = 0;
845         sr->u.update_request->record = 0;
846         sr->u.update_request->recordId = 0;
847         sr->u.update_request->recordVersions = 0;
848         sr->u.update_request->num_recordVersions = 0;
849         sr->u.update_request->extra_record = 0;
850         sr->u.update_request->extraRequestData_buf = 0;
851         sr->u.update_request->extraRequestData_len = 0;
852         sr->u.request->database = 0;
853         break;
854     case Z_SRW_update_response:
855         sr->u.update_response = (Z_SRW_updateResponse *)
856             odr_malloc(o, sizeof(*sr->u.update_response));
857         sr->u.update_response->operationStatus = 0;
858         sr->u.update_response->recordId = 0;
859         sr->u.update_response->recordVersions = 0;
860         sr->u.update_response->num_recordVersions = 0;
861         sr->u.update_response->record = 0;
862         sr->u.update_response->extra_record = 0;
863         sr->u.update_response->extraResponseData_buf = 0;
864         sr->u.update_response->extraResponseData_len = 0;
865         sr->u.update_response->diagnostics = 0;
866         sr->u.update_response->num_diagnostics = 0;
867     }
868     return sr;
869 }
870
871 void yaz_add_name_value_int(ODR o, char **name, char **value, int *i,
872                             char *a_name, Odr_int *val)
873 {
874     if (val)
875     {
876         name[*i] = a_name;
877         value[*i] = (char *) odr_malloc(o, 40);
878         sprintf(value[*i], ODR_INT_PRINTF, *val);
879         (*i)++;
880     }
881 }
882
883 void yaz_add_name_value_str(ODR o, char **name, char **value,  int *i,
884                             char *a_name, char *val)
885 {
886     if (val)
887     {
888         name[*i] = a_name;
889         value[*i] = val;
890         (*i)++;
891     }
892 }
893
894 static int yaz_get_sru_parms(const Z_SRW_PDU *srw_pdu, ODR encode,
895                              char **name, char **value, int max_names)
896 {
897     int version2 = strcmp(srw_pdu->srw_version, "2.") > 0;
898     int i = 0;
899     char *queryType;
900     yaz_add_name_value_str(encode, name, value, &i, "version",
901                            srw_pdu->srw_version);
902     name[i] = "operation";
903     switch (srw_pdu->which)
904     {
905     case Z_SRW_searchRetrieve_request:
906         value[i++] = "searchRetrieve";
907         queryType = srw_pdu->u.request->queryType;
908         if (version2)
909         {
910             if (queryType && strcmp(queryType, "cql"))
911                 yaz_add_name_value_str(encode, name, value, &i, "queryType",
912                                        queryType);
913             yaz_add_name_value_str(encode, name, value, &i, "query",
914                                    srw_pdu->u.request->query);
915         }
916         else
917         {
918             if (!strcmp(queryType, "cql"))
919             {
920                 yaz_add_name_value_str(encode, name, value, &i, "query",
921                                        srw_pdu->u.request->query);
922             }
923             else if (!strcmp(queryType, "pqf"))
924             {
925                 yaz_add_name_value_str(encode, name, value, &i, "x-pquery",
926                                        srw_pdu->u.request->query);
927             }
928             else if (!strcmp(queryType, "xcql"))
929             {
930                 yaz_add_name_value_str(encode, name, value, &i, "x-cql",
931                                        srw_pdu->u.request->query);
932             }
933         }
934         switch (srw_pdu->u.request->sort_type)
935         {
936         case Z_SRW_sort_type_none:
937             break;
938         case Z_SRW_sort_type_sort:
939             yaz_add_name_value_str(encode, name, value, &i, "sortKeys",
940                                    srw_pdu->u.request->sort.sortKeys);
941             break;
942         }
943         yaz_add_name_value_int(encode, name, value, &i, "startRecord",
944                                srw_pdu->u.request->startRecord);
945         yaz_add_name_value_int(encode, name, value, &i, "maximumRecords",
946                                srw_pdu->u.request->maximumRecords);
947         yaz_add_name_value_str(encode, name, value, &i, "recordSchema",
948                                srw_pdu->u.request->recordSchema);
949         if (version2)
950         {
951             yaz_add_name_value_str(encode, name, value, &i, "recordXMLEscaping",
952                                    srw_pdu->u.request->recordPacking);
953             yaz_add_name_value_str(encode, name, value, &i, "recordPacking",
954                                    srw_pdu->u.request->packing);
955         }
956         else
957             yaz_add_name_value_str(encode, name, value, &i, "recordPacking",
958                                    srw_pdu->u.request->recordPacking);
959         yaz_add_name_value_str(encode, name, value, &i, "recordXPath",
960                                srw_pdu->u.request->recordXPath);
961         yaz_add_name_value_str(encode, name, value, &i, "stylesheet",
962                                srw_pdu->u.request->stylesheet);
963         yaz_add_name_value_int(encode, name, value, &i, "resultSetTTL",
964                                srw_pdu->u.request->resultSetTTL);
965         {
966             const char *facetLimit = 0;
967             const char *facetStart = 0;
968             const char *facetSort = 0;
969             yaz_sru_facet_request(encode, &srw_pdu->u.request->facetList,
970                                   &facetLimit, &facetStart, &facetSort);
971             yaz_add_name_value_str(encode, name, value, &i, "facetLimit",
972                                    (char *) facetLimit);
973             yaz_add_name_value_str(encode, name, value, &i, "facetStart",
974                                    (char *) facetStart);
975             yaz_add_name_value_str(encode, name, value, &i, "facetSort",
976                                    (char *) facetSort);
977         }
978         break;
979     case Z_SRW_explain_request:
980         value[i++] = "explain";
981
982         if (version2)
983         {
984             yaz_add_name_value_str(encode, name, value, &i, "recordXMLEscaping",
985                                    srw_pdu->u.explain_request->recordPacking);
986             yaz_add_name_value_str(encode, name, value, &i, "recordPacking",
987                                    srw_pdu->u.explain_request->packing);
988         }
989         else
990             yaz_add_name_value_str(encode, name, value, &i, "recordPacking",
991                                    srw_pdu->u.explain_request->recordPacking);
992         yaz_add_name_value_str(encode, name, value, &i, "stylesheet",
993                                srw_pdu->u.explain_request->stylesheet);
994         break;
995     case Z_SRW_scan_request:
996         value[i++] = "scan";
997         queryType = srw_pdu->u.scan_request->queryType;
998         if (version2)
999         {
1000             if (queryType && strcmp(queryType, "cql"))
1001                 yaz_add_name_value_str(encode, name, value, &i, "queryType",
1002                                        queryType);
1003             yaz_add_name_value_str(encode, name, value, &i, "scanClause",
1004                                    srw_pdu->u.scan_request->scanClause);
1005         }
1006         else
1007         {
1008             if (!queryType || !strcmp(queryType, "cql"))
1009                 yaz_add_name_value_str(encode, name, value, &i, "scanClause",
1010                                        srw_pdu->u.scan_request->scanClause);
1011             else if (!strcmp(queryType, "pqf"))
1012                 yaz_add_name_value_str(encode, name, value, &i, "x-pScanClause",
1013                                        srw_pdu->u.scan_request->scanClause);
1014             else if (!strcmp(queryType, "xcql"))
1015                 yaz_add_name_value_str(encode, name, value, &i,
1016                                        "x-cqlScanClause",
1017                                        srw_pdu->u.scan_request->scanClause);
1018         }
1019         yaz_add_name_value_int(encode, name, value, &i, "responsePosition",
1020                                srw_pdu->u.scan_request->responsePosition);
1021         yaz_add_name_value_int(encode, name, value, &i, "maximumTerms",
1022                                srw_pdu->u.scan_request->maximumTerms);
1023         yaz_add_name_value_str(encode, name, value, &i, "stylesheet",
1024                                srw_pdu->u.scan_request->stylesheet);
1025         break;
1026     case Z_SRW_update_request:
1027         value[i++] = "update";
1028         break;
1029     default:
1030         return -1;
1031     }
1032     if (srw_pdu->extra_args)
1033     {
1034         Z_SRW_extra_arg *ea = srw_pdu->extra_args;
1035         for (; ea && i < max_names-1; ea = ea->next)
1036         {
1037             name[i] = ea->name;
1038             value[i] = ea->value;
1039             i++;
1040         }
1041     }
1042     name[i++] = 0;
1043
1044     return 0;
1045 }
1046
1047 int yaz_sru_get_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
1048                        ODR encode, const char *charset)
1049 {
1050     char *name[MAX_SRU_PARAMETERS], *value[MAX_SRU_PARAMETERS]; /* definite upper limit for SRU params */
1051     char *uri_args;
1052     char *path;
1053
1054     z_HTTP_header_add_basic_auth(encode, &hreq->headers,
1055                                  srw_pdu->username, srw_pdu->password);
1056     if (yaz_get_sru_parms(srw_pdu, encode, name, value, MAX_SRU_PARAMETERS))
1057         return -1;
1058     yaz_array_to_uri(&uri_args, encode, name, value);
1059
1060     hreq->method = "GET";
1061
1062     path = (char *)
1063         odr_malloc(encode, strlen(hreq->path) + strlen(uri_args) + 4);
1064
1065     sprintf(path, "%s?%s", hreq->path, uri_args);
1066     hreq->path = path;
1067
1068     z_HTTP_header_add_content_type(encode, &hreq->headers,
1069                                    "text/xml", charset);
1070     return 0;
1071 }
1072
1073 int yaz_sru_post_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
1074                         ODR encode, const char *charset)
1075 {
1076     char *name[MAX_SRU_PARAMETERS], *value[MAX_SRU_PARAMETERS]; /* definite upper limit for SRU params */
1077     char *uri_args;
1078
1079     z_HTTP_header_add_basic_auth(encode, &hreq->headers,
1080                                  srw_pdu->username, srw_pdu->password);
1081     if (yaz_get_sru_parms(srw_pdu, encode, name, value, MAX_SRU_PARAMETERS))
1082         return -1;
1083
1084     yaz_array_to_uri(&uri_args, encode, name, value);
1085
1086     hreq->method = "POST";
1087
1088     hreq->content_buf = uri_args;
1089     hreq->content_len = strlen(uri_args);
1090
1091     z_HTTP_header_add_content_type(encode, &hreq->headers,
1092                                    "application/x-www-form-urlencoded",
1093                                    charset);
1094     return 0;
1095 }
1096
1097 int yaz_sru_soap_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
1098                         ODR odr, const char *charset)
1099 {
1100     Z_SOAP_Handler handlers[3] = {
1101 #if YAZ_HAVE_XML2
1102         {YAZ_XMLNS_SRU_v1_1, 0, (Z_SOAP_fun) yaz_srw_codec},
1103         {YAZ_XMLNS_UPDATE_v0_9, 0, (Z_SOAP_fun) yaz_ucp_codec},
1104 #endif
1105         {0, 0, 0}
1106     };
1107     Z_SOAP *p = (Z_SOAP*) odr_malloc(odr, sizeof(*p));
1108
1109     z_HTTP_header_add_basic_auth(odr, &hreq->headers,
1110                                  srw_pdu->username, srw_pdu->password);
1111     z_HTTP_header_add_content_type(odr,
1112                                    &hreq->headers,
1113                                    "text/xml", 0 /* no charset in MIME */);
1114
1115     z_HTTP_header_add(odr, &hreq->headers,
1116                       "SOAPAction", "\"\"");
1117     p->which = Z_SOAP_generic;
1118     p->u.generic = (Z_SOAP_Generic *) odr_malloc(odr, sizeof(*p->u.generic));
1119     p->u.generic->no = 0;
1120     p->u.generic->ns = 0;
1121     p->u.generic->p = srw_pdu;
1122     p->ns = "http://schemas.xmlsoap.org/soap/envelope/";
1123
1124 #if YAZ_HAVE_XML2
1125     if (srw_pdu->which == Z_SRW_update_request ||
1126         srw_pdu->which == Z_SRW_update_response)
1127         p->u.generic->no = 1; /* second handler */
1128 #endif
1129     return z_soap_codec_enc(odr, &p,
1130                             &hreq->content_buf,
1131                             &hreq->content_len, handlers,
1132                             charset);
1133 }
1134
1135 Z_SRW_recordVersion *yaz_srw_get_record_versions(ODR odr, int num)
1136 {
1137     Z_SRW_recordVersion *ver
1138         = (Z_SRW_recordVersion *) odr_malloc(odr,num * sizeof(*ver));
1139     int i;
1140     for (i = 0; i < num; ++i)
1141     {
1142         ver[i].versionType = 0;
1143         ver[i].versionValue = 0;
1144     }
1145     return ver;
1146 }
1147
1148 const char *yaz_srw_pack_to_str(int pack)
1149 {
1150     switch(pack)
1151     {
1152     case Z_SRW_recordPacking_string:
1153         return "string";
1154     case Z_SRW_recordPacking_XML:
1155         return "xml";
1156     case Z_SRW_recordPacking_URL:
1157         return "url";
1158     }
1159     return 0;
1160 }
1161
1162 int yaz_srw_str_to_pack(const char *str)
1163 {
1164     if (!yaz_matchstr(str, "string"))
1165         return Z_SRW_recordPacking_string;
1166     if (!yaz_matchstr(str, "xml"))
1167         return Z_SRW_recordPacking_XML;
1168     if (!yaz_matchstr(str, "url"))
1169         return Z_SRW_recordPacking_URL;
1170     return -1;
1171 }
1172
1173 void yaz_encode_sru_extra(Z_SRW_PDU *sr, ODR odr, const char *extra_args)
1174 {
1175     if (extra_args)
1176     {
1177         char **name;
1178         char **val;
1179         Z_SRW_extra_arg **ea = &sr->extra_args;
1180         yaz_uri_to_array(extra_args, odr, &name, &val);
1181
1182         /** append rather than override */
1183         while (*ea)
1184             ea = &(*ea)->next;
1185         while (*name)
1186         {
1187             *ea = (Z_SRW_extra_arg *) odr_malloc(odr, sizeof(**ea));
1188             (*ea)->name = *name;
1189             (*ea)->value = *val;
1190             ea = &(*ea)->next;
1191             val++;
1192             name++;
1193         }
1194         *ea = 0;
1195     }
1196 }
1197
1198
1199 /*
1200  * Local variables:
1201  * c-basic-offset: 4
1202  * c-file-style: "Stroustrup"
1203  * indent-tabs-mode: nil
1204  * End:
1205  * vim: shiftwidth=4 tabstop=8 expandtab
1206  */
1207