X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Fsrwutil.c;h=c476d1105b23a7a2bcc0088dd6673a93a2b8113e;hp=c1b70f650605e1608677f867cf1e0e646802e6b5;hb=84d7b06c13daa609e93f353e655c4b02f936d65c;hpb=b3d0700ef5df9c96be4c08dcb9ac2a7e123c9f48 diff --git a/src/srwutil.c b/src/srwutil.c index c1b70f6..c476d11 100644 --- a/src/srwutil.c +++ b/src/srwutil.c @@ -1,10 +1,15 @@ /* - * Copyright (c) 2002-2003, Index Data. + * Copyright (C) 1995-2005, Index Data ApS * See the file LICENSE for details. * - * $Id: srwutil.c,v 1.4 2004-01-06 09:10:02 adam Exp $ + * $Id: srwutil.c,v 1.27 2005-02-01 14:43:50 adam Exp $ + */ +/** + * \file srwutil.c + * \brief Implements SRW/SRU utilities. */ +#include #include #include @@ -19,6 +24,65 @@ static int hex_digit (int ch) return 0; } +int yaz_uri_array(const char *path, ODR o, char ***name, char ***val) +{ + int no = 2; + const char *cp; + *name = 0; + if (*path != '?') + return no; + path++; + cp = path; + while ((cp = strchr(cp, '&'))) + { + cp++; + no++; + } + *name = odr_malloc(o, no * sizeof(char**)); + *val = odr_malloc(o, no * sizeof(char**)); + + for (no = 0; *path; no++) + { + const char *p1 = strchr(path, '='); + size_t i = 0; + char *ret; + if (!p1) + break; + + (*name)[no] = odr_malloc(o, (p1-path)+1); + memcpy((*name)[no], path, p1-path); + (*name)[no][p1-path] = '\0'; + + path = p1 + 1; + p1 = strchr(path, '&'); + if (!p1) + p1 = strlen(path) + path; + (*val)[no] = ret = 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'; + + if (*path) + path++; + } + (*name)[no] = 0; + (*val)[no] = 0; + return no; +} + char *yaz_uri_val(const char *path, const char *name, ODR o) { size_t nlen = strlen(name); @@ -39,7 +103,7 @@ char *yaz_uri_val(const char *path, const char *name, ODR o) p1 = strchr(path, '&'); if (!p1) p1 = strlen(path) + path; - ret = odr_malloc(o, p1 - path + 1); + ret = (char *) odr_malloc(o, p1 - path + 1); while (*path && *path != '&') { if (*path == '+') @@ -72,6 +136,31 @@ void yaz_uri_val_int(const char *path, const char *name, ODR o, int **intp) *intp = odr_intdup(o, atoi(v)); } +void yaz_mk_std_diagnostic(ODR o, Z_SRW_diagnostic *d, + int code, const char *details) +{ + d->uri = (char *) odr_malloc(o, 50); + sprintf(d->uri, "info:srw/diagnostic/1/%d", code); + d->message = 0; + if (details) + d->details = odr_strdup(o, details); + else + d->details = 0; +} + +void yaz_add_srw_diagnostic(ODR o, Z_SRW_diagnostic **d, + int *num, int code, const char *addinfo) +{ + Z_SRW_diagnostic *d_new; + d_new = (Z_SRW_diagnostic *) odr_malloc (o, (*num + 1)* sizeof(**d)); + if (*num) + memcpy (d_new, *d, *num *sizeof(**d)); + *d = d_new; + + yaz_mk_std_diagnostic(o, *d + *num, code, addinfo); + (*num)++; +} + int yaz_srw_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu, Z_SOAP **soap_package, ODR decode, char **charset) { @@ -79,7 +168,9 @@ int yaz_srw_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu, { const char *content_type = z_HTTP_header_lookup(hreq->headers, "Content-Type"); - if (content_type && !yaz_strcmp_del("text/xml", content_type, "; ")) + if (content_type && + (!yaz_strcmp_del("text/xml", content_type, "; ") || + !yaz_strcmp_del("text/plain", content_type, "; "))) { char *db = "Default"; const char *p0 = hreq->path, *p1; @@ -146,8 +237,12 @@ int yaz_srw_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu, return 2; } +/** + http://www.loc.gov/z3950/agency/zing/srw/service.html +*/ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu, - Z_SOAP **soap_package, ODR decode, char **charset) + Z_SOAP **soap_package, ODR decode, char **charset, + Z_SRW_diagnostic **diag, int *num_diag) { #if HAVE_XML2 static Z_SOAP_Handler soap_handlers[2] = { @@ -160,10 +255,27 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu, { char *db = "Default"; const char *p0 = hreq->path, *p1; +#if HAVE_XML2 const char *operation = 0; + char *version = 0; char *query = 0; char *pQuery = 0; - + char *sortKeys = 0; + char *stylesheet = 0; + char *scanClause = 0; + char *pScanClause = 0; + char *recordXPath = 0; + char *recordSchema = 0; + char *recordPacking = "xml"; /* xml packing is default for SRU */ + char *maximumRecords = 0; + char *startRecord = 0; + char *maximumTerms = 0; + char *responsePosition = 0; + char *extraRequestData = 0; +#endif + char **uri_name; + char **uri_val; + if (charset) *charset = 0; if (*p0 == '/') @@ -177,42 +289,100 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu, memcpy (db, p0, p1 - p0); db[p1 - p0] = '\0'; } + yaz_uri_array(p1, decode, &uri_name, &uri_val); #if HAVE_XML2 - query = yaz_uri_val(p1, "query", decode); - pQuery = yaz_uri_val(p1, "pQuery", decode); - operation = yaz_uri_val(p1, "operation", decode); + if (uri_name) + { + int i; + for (i = 0; uri_name[i]; i++) + { + char *n = uri_name[i]; + char *v = uri_val[i]; + if (!strcmp(n, "query")) + query = v; + else if (!strcmp(n, "x-pquery")) + pQuery = v; + else if (!strcmp(n, "operation")) + operation = v; + else if (!strcmp(n, "stylesheet")) + stylesheet = v; + else if (!strcmp(n, "sortKeys")) + sortKeys = v; + else if (!strcmp(n, "recordXPath")) + recordXPath = v; + else if (!strcmp(n, "recordSchema")) + recordSchema = v; + else if (!strcmp(n, "recordPacking")) + recordPacking = v; + else if (!strcmp(n, "version")) + version = v; + else if (!strcmp(n, "scanClause")) + scanClause = v; + else if (!strcmp(n, "x-pScanClause")) + pScanClause = v; + else if (!strcmp(n, "maximumRecords")) + maximumRecords = v; + else if (!strcmp(n, "startRecord")) + startRecord = v; + else if (!strcmp(n, "maximumTerms")) + maximumTerms = v; + else if (!strcmp(n, "responsePosition")) + responsePosition = v; + else if (!strcmp(n, "extraRequestData")) + extraRequestData = v; + else + yaz_add_srw_diagnostic(decode, diag, num_diag, 8, n); + } + } + if (!version) + { + if (uri_name) + yaz_add_srw_diagnostic(decode, diag, num_diag, 7, "version"); + version = "1.1"; + } + if (strcmp(version, "1.1")) + yaz_add_srw_diagnostic(decode, diag, num_diag, 5, "1.1"); if (!operation) + { + if (uri_name) + yaz_add_srw_diagnostic(decode, diag, num_diag, 7, "operation"); operation = "explain"; - if ((operation && !strcmp(operation, "searchRetrieve")) - || pQuery || query) + } + if (!strcmp(operation, "searchRetrieve")) { Z_SRW_PDU *sr = yaz_srw_get(decode, Z_SRW_searchRetrieve_request); - char *sortKeys = yaz_uri_val(p1, "sortKeys", decode); - + + sr->srw_version = version; *srw_pdu = sr; if (query) { sr->u.request->query_type = Z_SRW_query_type_cql; sr->u.request->query.cql = query; } - if (pQuery) + else if (pQuery) { sr->u.request->query_type = Z_SRW_query_type_pqf; sr->u.request->query.pqf = pQuery; } + else + yaz_add_srw_diagnostic(decode, diag, num_diag, 7, "query"); + if (sortKeys) { sr->u.request->sort_type = Z_SRW_sort_type_sort; sr->u.request->sort.sortKeys = sortKeys; } - sr->u.request->recordSchema = yaz_uri_val(p1, "recordSchema", decode); - sr->u.request->recordPacking = yaz_uri_val(p1, "recordPacking", decode); - if (!sr->u.request->recordPacking) - sr->u.request->recordPacking = "xml"; - yaz_uri_val_int(p1, "maximumRecords", decode, - &sr->u.request->maximumRecords); - yaz_uri_val_int(p1, "startRecord", decode, - &sr->u.request->startRecord); + sr->u.request->recordXPath = recordXPath; + sr->u.request->recordSchema = recordSchema; + sr->u.request->recordPacking = recordPacking; + sr->u.request->stylesheet = stylesheet; + + if (maximumRecords) + sr->u.request->maximumRecords = + odr_intdup(decode, atoi(maximumRecords)); + if (startRecord) + sr->u.request->startRecord = + odr_intdup(decode, atoi(startRecord)); sr->u.request->database = db; @@ -230,17 +400,94 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu, return 0; } - else if (p1 && !strcmp(operation, "explain")) + else if (!strcmp(operation, "explain")) + { + /* Transfer SRU explain parameters to common struct */ + /* http://www.loc.gov/z3950/agency/zing/srw/explain.html */ + Z_SRW_PDU *sr = yaz_srw_get(decode, Z_SRW_explain_request); + + sr->srw_version = version; + *srw_pdu = sr; + sr->u.explain_request->recordPacking = recordPacking; + sr->u.explain_request->database = db; + + sr->u.explain_request->stylesheet = stylesheet; + + (*soap_package) = odr_malloc(decode, sizeof(**soap_package)); + (*soap_package)->which = Z_SOAP_generic; + + (*soap_package)->u.generic = + odr_malloc(decode, sizeof(*(*soap_package)->u.generic)); + + (*soap_package)->u.generic->p = sr; + (*soap_package)->u.generic->ns = soap_handlers[0].ns; + (*soap_package)->u.generic->no = 0; + + (*soap_package)->ns = "SRU"; + + return 0; + } + else if (!strcmp(operation, "scan")) { + /* Transfer SRU scan parameters to common struct */ + /* http://www.loc.gov/z3950/agency/zing/srw/scan.html */ + Z_SRW_PDU *sr = yaz_srw_get(decode, Z_SRW_scan_request); + + sr->srw_version = version; + *srw_pdu = sr; + + if (scanClause) + { + sr->u.scan_request->query_type = Z_SRW_query_type_cql; + sr->u.scan_request->scanClause.cql = scanClause; + } + else if (pScanClause) + { + sr->u.scan_request->query_type = Z_SRW_query_type_pqf; + sr->u.scan_request->scanClause.pqf = pScanClause; + } + else + yaz_add_srw_diagnostic(decode, diag, num_diag, 7, + "scanClause"); + sr->u.scan_request->database = db; + + if (maximumTerms) + sr->u.scan_request->maximumTerms = + odr_intdup(decode, atoi(maximumTerms)); + if (responsePosition) + sr->u.scan_request->responsePosition = + odr_intdup(decode, atoi(responsePosition)); + + sr->u.scan_request->stylesheet = stylesheet; + + (*soap_package) = odr_malloc(decode, sizeof(**soap_package)); + (*soap_package)->which = Z_SOAP_generic; + + (*soap_package)->u.generic = + odr_malloc(decode, sizeof(*(*soap_package)->u.generic)); + + (*soap_package)->u.generic->p = sr; + (*soap_package)->u.generic->ns = soap_handlers[0].ns; + (*soap_package)->u.generic->no = 0; + + (*soap_package)->ns = "SRU"; + + return 0; + } + else + { + /* unsupported operation ... */ + /* Act as if we received a explain request and throw diagnostic. */ + Z_SRW_PDU *sr = yaz_srw_get(decode, Z_SRW_explain_request); + sr->srw_version = version; *srw_pdu = sr; - sr->u.explain_request->recordPacking = - yaz_uri_val(p1, "recordPacking", decode); - if (!sr->u.explain_request->recordPacking) - sr->u.explain_request->recordPacking = "xml"; + sr->u.explain_request->recordPacking = recordPacking; sr->u.explain_request->database = db; + sr->u.explain_request->stylesheet = stylesheet; + (*soap_package) = odr_malloc(decode, sizeof(**soap_package)); (*soap_package)->which = Z_SOAP_generic; @@ -253,6 +500,7 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu, (*soap_package)->ns = "SRU"; + yaz_add_srw_diagnostic(decode, diag, num_diag, 4, operation); return 0; } #endif @@ -263,7 +511,7 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu, Z_SRW_PDU *yaz_srw_get(ODR o, int which) { - Z_SRW_PDU *sr = odr_malloc(o, sizeof(*o)); + Z_SRW_PDU *sr = (Z_SRW_PDU *) odr_malloc(o, sizeof(*o)); sr->srw_version = odr_strdup(o, "1.1"); sr->which = which; @@ -302,6 +550,7 @@ Z_SRW_PDU *yaz_srw_get(ODR o, int which) odr_malloc(o, sizeof(*sr->u.explain_request)); sr->u.explain_request->recordPacking = 0; sr->u.explain_request->database = 0; + sr->u.explain_request->stylesheet = 0; break; case Z_SRW_explain_response: sr->u.explain_response = (Z_SRW_explainResponse *) @@ -314,108 +563,29 @@ Z_SRW_PDU *yaz_srw_get(ODR o, int which) Z_SRW_recordPacking_string; sr->u.explain_response->diagnostics = 0; sr->u.explain_response->num_diagnostics = 0; + break; + case Z_SRW_scan_request: + sr->u.scan_request = (Z_SRW_scanRequest *) + odr_malloc(o, sizeof(*sr->u.scan_request)); + sr->u.scan_request->database = 0; + sr->u.scan_request->stylesheet = 0; + sr->u.scan_request->maximumTerms = 0; + sr->u.scan_request->responsePosition = 0; + sr->u.scan_request->query_type = Z_SRW_query_type_cql; + sr->u.scan_request->scanClause.cql = 0; + break; + case Z_SRW_scan_response: + sr->u.scan_response = (Z_SRW_scanResponse *) + odr_malloc(o, sizeof(*sr->u.scan_response)); + sr->u.scan_response->terms = 0; + sr->u.scan_response->num_terms = 0; + sr->u.scan_response->diagnostics = 0; + sr->u.scan_response->num_diagnostics = 0; } return sr; } -static struct { - int code; - const char *msg; -} yaz_srw_codes [] = { -{1, "Permanent system error"}, -{2, "System temporarily unavailable"}, -{3, "Authentication error"}, -/* Diagnostics Relating to CQL */ -{10, "Illegal query"}, -{11, "Unsupported query type (XCQL vs CQL)"}, -{12, "Too many characters in query"}, -{13, "Unbalanced or illegal use of parentheses"}, -{14, "Unbalanced or illegal use of quotes"}, -{15, "Illegal or unsupported context set"}, -{16, "Illegal or unsupported index"}, -{17, "Illegal or unsupported combination of index and context set"}, -{18, "Illegal or unsupported combination of indexes"}, -{19, "Illegal or unsupported relation"}, -{20, "Illegal or unsupported relation modifier"}, -{21, "Illegal or unsupported combination of relation modifers"}, -{22, "Illegal or unsupported combination of relation and index"}, -{23, "Too many characters in term"}, -{24, "Illegal combination of relation and term"}, -{25, "Special characters not quoted in term"}, -{26, "Non special character escaped in term"}, -{27, "Empty term unsupported"}, -{28, "Masking character not supported"}, -{29, "Masked words too short"}, -{30, "Too many masking characters in term"}, -{31, "Anchoring character not supported"}, -{32, "Anchoring character in illegal or unsupported position"}, -{33, "Combination of proximity/adjacency and masking characters not supported"}, -{34, "Combination of proximity/adjacency and anchoring characters not supported"}, -{35, "Terms only exclusion (stop) words"}, -{36, "Term in invalid format for index or relation"}, -{37, "Illegal or unsupported boolean operator"}, -{38, "Too many boolean operators in query"}, -{39, "Proximity not supported"}, -{40, "Illegal or unsupported proximity relation"}, -{41, "Illegal or unsupported proximity distance"}, -{42, "Illegal or unsupported proximity unit"}, -{43, "Illegal or unsupported proximity ordering"}, -{44, "Illegal or unsupported combination of proximity modifiers"}, -{45, "context set name (prefix) assigned to multiple identifiers"}, -/* Diagnostics Relating to Result Sets */ -{50, "Result sets not supported"}, -{51, "Result set does not exist"}, -{52, "Result set temporarily unavailable"}, -{53, "Result sets only supported for retrieval"}, -{54, "Retrieval may only occur from an existing result set"}, -{55, "Combination of result sets with search terms not supported"}, -{56, "Only combination of single result set with search terms supported"}, -{57, "Result set created but no records available"}, -{58, "Result set created with unpredictable partial results available"}, -{59, "Result set created with valid partial results available"}, -/* Diagnostics Relating to Records */ -{60, "Too many records retrieved"}, -{61, "First record position out of range"}, -{62, "Negative number of records requested"}, -{63, "System error in retrieving records"}, -{64, "Record temporarily unavailable"}, -{65, "Record does not exist"}, -{66, "Unknown schema for retrieval"}, -{67, "Record not available in this schema"}, -{68, "Not authorised to send record"}, -{69, "Not authorised to send record in this schema"}, -{70, "Record too large to send"}, -/* Diagnostics Relating to Sorting */ -{80, "Sort not supported"}, -{81, "Unsupported sort type (sortKeys vs xSortKeys)"}, -{82, "Illegal or unsupported sort sequence"}, -{83, "Too many records"}, -{84, "Too many sort keys"}, -{85, "Duplicate sort keys"}, -{86, "Incompatible record formats"}, -{87, "Unsupported schema for sort"}, -{88, "Unsupported tag path for sort"}, -{89, "Tag path illegal or unsupported for schema"}, -{90, "Illegal or unsupported direction value"}, -{91, "Illegal or unsupported case value"}, -{92, "Illegal or unsupported missing value action"}, -/* Diagnostics Relating to Explain */ -{100, "Explain not supported"}, -{101, "Explain request type not supported (SOAP vs GET)"}, -{102, "Explain record temporarily unavailable"}, -{0, 0} -}; - -const char *yaz_diag_srw_str (int code) -{ - int i; - for (i = 0; yaz_srw_codes[i].code; i++) - if (yaz_srw_codes[i].code == code) - return yaz_srw_codes[i].msg; - return 0; -} - /* bib1:srw */ static int srw_bib1_map[] = { @@ -428,6 +598,7 @@ static int srw_bib1_map[] = { 7, 30, 8, 32, 9, 29, + 108, 10, /* Malformed query : Syntax error */ 10, 10, 11, 12, 11, 23, @@ -465,7 +636,6 @@ static int srw_bib1_map[] = { 105, 3, 106, 66, 107, 11, - 108, 10, 108, 13, 108, 14, 108, 25, @@ -473,7 +643,7 @@ static int srw_bib1_map[] = { 108, 27, 108, 45, - 109, 1, + 109, 2, 110, 37, 111, 1, 112, 58, @@ -482,6 +652,7 @@ static int srw_bib1_map[] = { 115, 16, 116, 16, 117, 19, + 117, 20, 118, 22, 119, 32, 119, 31,