2 * Copyright (c) 2002-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: srwutil.c,v 1.1 2003-12-20 00:51:19 adam Exp $
10 static int hex_digit (int ch)
12 if (ch >= '0' && ch <= '9')
14 else if (ch >= 'a' && ch <= 'f')
16 else if (ch >= 'A' && ch <= 'F')
21 char *yaz_uri_val(const char *path, const char *name, ODR o)
23 size_t nlen = strlen(name);
29 const char *p1 = strchr(path, '=');
32 if ((size_t)(p1 - path) == nlen && !memcmp(path, name, nlen))
38 p1 = strchr(path, '&');
40 p1 = strlen(path) + path;
41 ret = odr_malloc(o, p1 - path + 1);
42 while (*path && *path != '&')
49 else if (*path == '%' && path[1] && path[2])
51 ret[i++] = hex_digit (path[1])*16 + hex_digit (path[2]);
60 path = strchr(p1, '&');
67 void yaz_uri_val_int(const char *path, const char *name, ODR o, int **intp)
69 const char *v = yaz_uri_val(path, name, o);
71 *intp = odr_intdup(o, atoi(v));
74 int yaz_check_for_srw(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
75 char **soap_ns, ODR decode)
77 if (!strcmp(hreq->method, "POST"))
79 const char *content_type = z_HTTP_header_lookup(hreq->headers,
81 if (content_type && !yaz_strcmp_del("text/xml", content_type, "; "))
84 const char *p0 = hreq->path, *p1;
85 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 Z_SOAP *soap_package = 0;
159 static Z_SOAP_Handler soap_handlers[2] = {
160 {"http://www.loc.gov/zing/srw/", 0,
161 (Z_SOAP_fun) yaz_srw_codec},
168 p1 = strchr(p0, '?');
170 p1 = p0 + strlen(p0);
173 db = (char*) odr_malloc(decode, p1 - p0 + 1);
174 memcpy (db, p0, p1 - p0);
179 operation = yaz_uri_val(p1, "operation", decode);
181 operation = "explain";
182 if (p1 && !strcmp(operation, "searchRetrieve"))
184 Z_SRW_PDU *sr = yaz_srw_get(decode, Z_SRW_searchRetrieve_request);
185 char *query = yaz_uri_val(p1, "query", decode);
186 char *pQuery = yaz_uri_val(p1, "pQuery", decode);
187 char *sortKeys = yaz_uri_val(p1, "sortKeys", decode);
192 sr->u.request->query_type = Z_SRW_query_type_cql;
193 sr->u.request->query.cql = query;
197 sr->u.request->query_type = Z_SRW_query_type_pqf;
198 sr->u.request->query.pqf = pQuery;
202 sr->u.request->sort_type = Z_SRW_sort_type_sort;
203 sr->u.request->sort.sortKeys = sortKeys;
205 sr->u.request->recordSchema = yaz_uri_val(p1, "recordSchema", decode);
206 sr->u.request->recordPacking = yaz_uri_val(p1, "recordPacking", decode);
207 if (!sr->u.request->recordPacking)
208 sr->u.request->recordPacking = "xml";
209 yaz_uri_val_int(p1, "maximumRecords", decode,
210 &sr->u.request->maximumRecords);
211 yaz_uri_val_int(p1, "startRecord", decode,
212 &sr->u.request->startRecord);
214 sr->u.request->database = db;
218 else if (p1 && !strcmp(operation, "explain"))
220 Z_SRW_PDU *sr = yaz_srw_get(decode, Z_SRW_explain_request);
223 sr->u.explain_request->recordPacking =
224 yaz_uri_val(p1, "recordPacking", decode);
225 if (!sr->u.explain_request->recordPacking)
226 sr->u.explain_request->recordPacking = "xml";
227 sr->u.explain_request->database = db;