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