2 * Copyright (c) 2002-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: srwutil.c,v 1.2 2003-12-30 00:29:53 adam Exp $
9 #include <yaz/yaz-iconv.h>
11 static int hex_digit (int ch)
13 if (ch >= '0' && ch <= '9')
15 else if (ch >= 'a' && ch <= 'f')
17 else if (ch >= 'A' && ch <= 'F')
22 char *yaz_uri_val(const char *path, const char *name, ODR o)
24 size_t nlen = strlen(name);
30 const char *p1 = strchr(path, '=');
33 if ((size_t)(p1 - path) == nlen && !memcmp(path, name, nlen))
39 p1 = strchr(path, '&');
41 p1 = strlen(path) + path;
42 ret = odr_malloc(o, p1 - path + 1);
43 while (*path && *path != '&')
50 else if (*path == '%' && path[1] && path[2])
52 ret[i++] = hex_digit (path[1])*16 + hex_digit (path[2]);
61 path = strchr(p1, '&');
68 void yaz_uri_val_int(const char *path, const char *name, ODR o, int **intp)
70 const char *v = yaz_uri_val(path, name, o);
72 *intp = odr_intdup(o, atoi(v));
75 int yaz_check_for_srw(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
76 char **soap_ns, ODR decode)
78 if (!strcmp(hreq->method, "POST"))
80 const char *content_type = z_HTTP_header_lookup(hreq->headers,
82 if (content_type && !yaz_strcmp_del("text/xml", content_type, "; "))
85 const char *p0 = hreq->path, *p1;
86 Z_SOAP *soap_package = 0;
88 const char *charset_p = 0;
91 static Z_SOAP_Handler soap_handlers[2] = {
93 {"http://www.loc.gov/zing/srw/", 0,
94 (Z_SOAP_fun) yaz_srw_codec},
101 p1 = strchr(p0, '?');
103 p1 = p0 + strlen(p0);
106 db = (char*) odr_malloc(decode, p1 - p0 + 1);
107 memcpy (db, p0, p1 - p0);
111 if ((charset_p = strstr(content_type, "; charset=")))
115 while (i < 20 && charset_p[i] &&
116 !strchr("; \n\r", charset_p[i]))
118 charset = (char*) odr_malloc(decode, i+1);
119 memcpy(charset, charset_p, i);
122 ret = z_soap_codec(decode, &soap_package,
123 &hreq->content_buf, &hreq->content_len,
125 if (!ret && soap_package->which == Z_SOAP_generic &&
126 soap_package->u.generic->no == 0)
128 *srw_pdu = (Z_SRW_PDU*) soap_package->u.generic->p;
130 if ((*srw_pdu)->which == Z_SRW_searchRetrieve_request &&
131 (*srw_pdu)->u.request->database == 0)
132 (*srw_pdu)->u.request->database = db;
134 if ((*srw_pdu)->which == Z_SRW_explain_request &&
135 (*srw_pdu)->u.explain_request->database == 0)
136 (*srw_pdu)->u.explain_request->database = db;
138 *soap_ns = odr_strdup(decode, soap_package->ns);
147 int yaz_check_for_sru(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
148 char **soap_ns, ODR decode)
150 if (!strcmp(hreq->method, "GET"))
152 char *db = "Default";
153 const char *p0 = hreq->path, *p1;
154 const char *operation = 0;
158 p1 = strchr(p0, '?');
160 p1 = p0 + strlen(p0);
163 db = (char*) odr_malloc(decode, p1 - p0 + 1);
164 memcpy (db, p0, p1 - p0);
169 operation = yaz_uri_val(p1, "operation", decode);
171 operation = "explain";
172 if (p1 && !strcmp(operation, "searchRetrieve"))
174 Z_SRW_PDU *sr = yaz_srw_get(decode, Z_SRW_searchRetrieve_request);
175 char *query = yaz_uri_val(p1, "query", decode);
176 char *pQuery = yaz_uri_val(p1, "pQuery", decode);
177 char *sortKeys = yaz_uri_val(p1, "sortKeys", decode);
182 sr->u.request->query_type = Z_SRW_query_type_cql;
183 sr->u.request->query.cql = query;
187 sr->u.request->query_type = Z_SRW_query_type_pqf;
188 sr->u.request->query.pqf = pQuery;
192 sr->u.request->sort_type = Z_SRW_sort_type_sort;
193 sr->u.request->sort.sortKeys = sortKeys;
195 sr->u.request->recordSchema = yaz_uri_val(p1, "recordSchema", decode);
196 sr->u.request->recordPacking = yaz_uri_val(p1, "recordPacking", decode);
197 if (!sr->u.request->recordPacking)
198 sr->u.request->recordPacking = "xml";
199 yaz_uri_val_int(p1, "maximumRecords", decode,
200 &sr->u.request->maximumRecords);
201 yaz_uri_val_int(p1, "startRecord", decode,
202 &sr->u.request->startRecord);
204 sr->u.request->database = db;
208 else if (p1 && !strcmp(operation, "explain"))
210 Z_SRW_PDU *sr = yaz_srw_get(decode, Z_SRW_explain_request);
213 sr->u.explain_request->recordPacking =
214 yaz_uri_val(p1, "recordPacking", decode);
215 if (!sr->u.explain_request->recordPacking)
216 sr->u.explain_request->recordPacking = "xml";
217 sr->u.explain_request->database = db;