- size_t nlen = strlen(name);
- if (*path != '?')
- return 0;
- path++;
- while (path && *path)
- {
- const char *p1 = strchr(path, '=');
- if (!p1)
- break;
- if ((size_t)(p1 - path) == nlen && !memcmp(path, name, nlen))
- {
- size_t i = 0;
- char *ret;
-
- path = p1 + 1;
- p1 = strchr(path, '&');
- if (!p1)
- p1 = strlen(path) + path;
- ret = (char*) odr_malloc(o, p1 - path + 1);
- while (*path && *path != '&')
- {
- if (*path == '+')
- {
- ret[i++] = ' ';
- path++;
- }
- else if (*path == '%' && path[1] && path[2])
- {
- ret[i++] = hex_digit (path[1])*16 + hex_digit (path[2]);
- path = path + 3;
- }
- else
- ret[i++] = *path++;
- }
- ret[i] = '\0';
- return ret;
- }
- path = strchr(p1, '&');
- if (path)
- path++;
- }
- return 0;
-}
-
-void uri_val_int(const char *path, const char *name, ODR o, int **intp)
-{
- const char *v = uri_val(path, name, o);
- if (v)
- *intp = odr_intdup(o, atoi(v));
-}
-
-int yaz_check_for_sru(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
- char **soap_ns, ODR decode)
-{
- if (!strcmp(hreq->method, "GET"))
- {
- char *db = "Default";
- const char *p0 = hreq->path, *p1;
-#if HAVE_XML2
- int ret = -1;
- char *charset = 0;
- Z_SOAP *soap_package = 0;
- static Z_SOAP_Handler soap_handlers[2] = {
- {"http://www.loc.gov/zing/srw/v1.0/", 0,
- (Z_SOAP_fun) yaz_srw_codec},
- {0, 0, 0}
- };
-#endif
-
- if (*p0 == '/')
- p0++;
- p1 = strchr(p0, '?');
- if (!p1)
- p1 = p0 + strlen(p0);
- if (p1 != p0)
- {
- db = (char*) odr_malloc(decode, p1 - p0 + 1);
- memcpy (db, p0, p1 - p0);
- db[p1 - p0] = '\0';
- }
-#if HAVE_XML2
- if (p1 && *p1 == '?' && p1[1])
- {
- Z_SRW_PDU *sr = yaz_srw_get(decode, Z_SRW_searchRetrieve_request);
- char *query = uri_val(p1, "query", decode);
- char *pQuery = uri_val(p1, "pQuery", decode);
- char *sortKeys = uri_val(p1, "sortKeys", decode);
-
- *srw_pdu = sr;
- if (query)
- {
- sr->u.request->query_type = Z_SRW_query_type_cql;
- sr->u.request->query.cql = query;
- }
- if (pQuery)
- {
- sr->u.request->query_type = Z_SRW_query_type_pqf;
- sr->u.request->query.pqf = pQuery;
- }
- if (sortKeys)
- {
- sr->u.request->sort_type = Z_SRW_sort_type_sort;
- sr->u.request->sort.sortKeys = sortKeys;
- }
- sr->u.request->recordSchema = uri_val(p1, "recordSchema", decode);
- sr->u.request->recordPacking = uri_val(p1, "recordPacking", decode);
- if (!sr->u.request->recordPacking)
- sr->u.request->recordPacking = "xml";
- uri_val_int(p1, "maximumRecords", decode,
- &sr->u.request->maximumRecords);
- uri_val_int(p1, "startRecord", decode,
- &sr->u.request->startRecord);
- if (sr->u.request->startRecord)
- yaz_log(LOG_LOG, "startRecord=%d", *sr->u.request->startRecord);
- sr->u.request->database = db;
- *soap_ns = "SRU";
- return 0;
- }
-#endif
- return 1;
- }
- return 2;