X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Fsrwutil.c;h=1de920db0e078305bcb5879c7b5fee713f61f45e;hp=4f403a51153e30db71ab463d9d95c3116b2e9125;hb=9a50808021be48e8ff272f762b9f8245330a79e4;hpb=47ceb84b5ac99c72bcaef53be93494541e9a68c0 diff --git a/src/srwutil.c b/src/srwutil.c index 4f403a5..1de920d 100644 --- a/src/srwutil.c +++ b/src/srwutil.c @@ -1,5 +1,5 @@ /* This file is part of the YAZ toolkit. - * Copyright (C) 1995-2013 Index Data + * Copyright (C) Index Data * See the file LICENSE for details. */ /** @@ -20,6 +20,34 @@ #define MAX_SRU_PARAMETERS 30 +static Z_SRW_extra_arg **append_extra_arg(ODR odr, Z_SRW_extra_arg **l, + const char *n, const char *v) +{ + if (n && v && *v != '\0') + { + while (*l) + l = &(*l)->next; + *l = (Z_SRW_extra_arg *) odr_malloc(odr, sizeof(**l)); + (*l)->name = odr_strdup(odr, n); + (*l)->value = odr_strdup(odr, v); + (*l)->next = 0; + l = &(*l)->next; + } + return l; +} + +static Z_SRW_extra_arg **append_extra_arg_int(ODR odr, Z_SRW_extra_arg **l, + const char *n, Odr_int *v) +{ + if (v) + { + char str[32]; + sprintf(str, ODR_INT_PRINTF, *v); + l = append_extra_arg(odr, l, n, str); + } + return l; +} + static char *yaz_decode_sru_dbpath_odr(ODR n, const char *uri, size_t len) { return odr_strdupn(n, uri, len); @@ -39,33 +67,6 @@ char *yaz_encode_sru_dbpath_odr(ODR out, const char *db) return dst; } -Z_AttributeList *yaz_use_attribute_create(ODR o, const char *name) -{ - Z_AttributeList *attributes= (Z_AttributeList *) - odr_malloc(o, sizeof(*attributes)); - Z_AttributeElement ** elements; - attributes->num_attributes = 1; - elements = (Z_AttributeElement**) - odr_malloc(o, attributes->num_attributes * sizeof(*elements)); - elements[0] = (Z_AttributeElement*) odr_malloc(o,sizeof(**elements)); - elements[0]->attributeType = odr_intdup(o, 1); - elements[0]->attributeSet = odr_nullval(); - elements[0]->which = Z_AttributeValue_complex; - elements[0]->value.complex = (Z_ComplexAttribute *) - odr_malloc(o, sizeof(Z_ComplexAttribute)); - elements[0]->value.complex->num_list = 1; - elements[0]->value.complex->list = (Z_StringOrNumeric **) - odr_malloc(o, 1 * sizeof(Z_StringOrNumeric *)); - elements[0]->value.complex->list[0] = (Z_StringOrNumeric *) - odr_malloc(o, sizeof(Z_StringOrNumeric)); - elements[0]->value.complex->list[0]->which = Z_StringOrNumeric_string; - elements[0]->value.complex->list[0]->u.string = odr_strdup(o, name); - elements[0]->value.complex->semanticAction = 0; - elements[0]->value.complex->num_semanticAction = 0; - attributes->attributes = elements; - return attributes; -} - #if YAZ_HAVE_XML2 const char *yaz_element_attribute_value_get(xmlNodePtr ptr, const char *node_name, @@ -96,6 +97,8 @@ int yaz_srw_check_content_type(Z_HTTP_Response *hres) return 1; if (!yaz_strcmp_del("application/xml", content_type, "; ")) return 1; + if (!yaz_strcmp_del("application/sru+xml", content_type, "; ")) + return 1; } return 0; } @@ -141,13 +144,6 @@ static void yaz_srw_decodeauth(Z_SRW_PDU *sr, Z_HTTP_Request *hreq, } } -void yaz_uri_val_int(const char *path, const char *name, ODR o, Odr_int **intp) -{ - const char *v = yaz_uri_val(path, name, o); - if (v) - *intp = odr_intdup(o, atoi(v)); -} - void yaz_mk_srw_diagnostic(ODR o, Z_SRW_diagnostic *d, const char *uri, const char *message, const char *details) @@ -241,14 +237,21 @@ static void grab_charset(ODR o, const char *content_type, char **charset) const char *charset_p = 0; if (content_type && (charset_p = strstr(content_type, "; charset="))) { - int i = 0; - charset_p += 10; - while (i < 20 && charset_p[i] && - !strchr("; \n\r", charset_p[i])) - i++; - *charset = (char*) odr_malloc(o, i+1); - memcpy(*charset, charset_p, i); - (*charset)[i] = '\0'; + int j = 0, i = 0; + int sep = 0; + charset_p += 10; /* skip ; charset= */ + if (charset_p[i] == '"' || charset_p[i] == '\'') + sep = charset_p[i++]; + *charset = odr_strdup(o, charset_p); + while (charset_p[i] && charset_p[i] != sep) + { + if (!sep && strchr("; \n\r", charset_p[i])) + break; + if (charset_p[i] == '\\' && charset_p[i+1]) + i++; + (*charset)[j++] = charset_p[i++]; + } + (*charset)[j] = '\0'; } } } @@ -286,7 +289,6 @@ int yaz_srw_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu, p1 = p0 + strlen(p0); if (p1 != p0) db = yaz_decode_sru_dbpath_odr(decode, p0, p1 - p0); - grab_charset(decode, content_type, charset); ret = z_soap_codec(decode, soap_package, &hreq->content_buf, &hreq->content_len, @@ -397,6 +399,9 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu, char *startRecord = 0; char *maximumTerms = 0; char *responsePosition = 0; + const char *facetLimit = 0; + const char *facetStart = 0; + const char *facetSort = 0; Z_SRW_extra_arg *extra_args = 0; #endif char **uri_name; @@ -413,7 +418,7 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu, p1 = p0 + strlen(p0); if (p1 != p0) db = yaz_decode_sru_dbpath_odr(decode, p0, p1 - p0); - if (!strcmp(hreq->method, "POST")) + if (!strcmp(hreq->method, "POST") && hreq->content_buf) p1 = hreq->content_buf; yaz_uri_to_array(p1, decode, &uri_name, &uri_val); #if YAZ_HAVE_XML2 @@ -468,17 +473,17 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu, maximumTerms = v; else if (!strcmp(n, "responsePosition")) responsePosition = v; + else if (!strcmp(n, "facetLimit")) + facetLimit = v; + else if (!strcmp(n, "facetStart")) + facetStart = v; + else if (!strcmp(n, "facetSort")) + facetSort = v; else if (!strcmp(n, "extraRequestData")) ; /* ignoring extraRequestData */ else if (n[0] == 'x' && n[1] == '-') { - Z_SRW_extra_arg **l = &extra_args; - while (*l) - l = &(*l)->next; - *l = (Z_SRW_extra_arg *) odr_malloc(decode, sizeof(**l)); - (*l)->name = odr_strdup(decode, n); - (*l)->value = odr_strdup(decode, v); - (*l)->next = 0; + append_extra_arg(decode, &extra_args, n, v); } else { @@ -488,8 +493,15 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu, } } } - if (!operation && query) - operation = "searchRetrieve"; + if (!operation) + { + if (query) + operation = "searchRetrieve"; + else if (scanClause) + operation = "scan"; + else + operation = "explain"; + } version = yaz_negotiate_sru_version(version); if (!version) @@ -547,6 +559,8 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu, sr->u.request->recordPacking = recordXMLEscaping; sr->u.request->packing = recordPacking; sr->u.request->stylesheet = stylesheet; + yaz_sru_facet_request(decode , &sr->u.request->facetList, + &facetLimit, &facetStart, &facetSort); yaz_sru_decode_integer(decode, "maximumRecords", maximumRecords, &sr->u.request->maximumRecords, @@ -744,12 +758,68 @@ Z_SRW_PDU *yaz_srw_get(ODR o, int which) return yaz_srw_get_pdu(o, which, "2.0"); } +/* http://docs.oasis-open.org/search-ws/searchRetrieve/v1.0/os/schemas/sruResponse.xsd */ +Z_SRW_PDU *yaz_srw_get_pdu_e(ODR o, int which, Z_SRW_PDU *req) +{ + int version2 = !req->srw_version || strcmp(req->srw_version, "2.") > 0; + Z_SRW_PDU *res = yaz_srw_get_pdu(o, which, req->srw_version); + Z_SRW_extra_arg **l = &res->extra_args, *ea; + l = append_extra_arg(o, l, "version", req->srw_version); + if (req->which == Z_SRW_searchRetrieve_request && + which == Z_SRW_searchRetrieve_response) + { + if (req->u.request->queryType && + strcmp(req->u.request->queryType, "cql")) + l = append_extra_arg(o, l, "queryType", req->u.request->queryType); + l = append_extra_arg(o, l, "query", req->u.request->query); + l = append_extra_arg_int(o, l, "startRecord", + req->u.request->startRecord); + l = append_extra_arg_int(o, l, "maximumRecords", + req->u.request->maximumRecords); + if (version2) + { + l = append_extra_arg(o, l, "recordXMLEscaping", + req->u.request->recordPacking); + l = append_extra_arg(o, l, "recordPacking", + req->u.request->packing); + } + else + l = append_extra_arg(o, l, "recordPacking", + req->u.request->recordPacking); + l = append_extra_arg(o, l, "recordSchema", + req->u.request->recordSchema); + if (req->u.request->sort_type == Z_SRW_sort_type_sort) + l = append_extra_arg(o, l, "sortKeys", + req->u.request->sort.sortKeys); + l = append_extra_arg(o, l, "stylesheet", req->u.request->stylesheet); + } + if (req->which == Z_SRW_explain_request && + which == Z_SRW_explain_response) + { + if (version2) + { + l = append_extra_arg(o, l, "recordXMLEscaping", + req->u.explain_request->recordPacking); + l = append_extra_arg(o, l, "recordPacking", + req->u.explain_request->packing); + } + else + l = append_extra_arg(o, l, "recordPacking", + req->u.explain_request->recordPacking); + l = append_extra_arg(o, l, "stylesheet", + req->u.explain_request->stylesheet); + } + for (ea = req->extra_args; ea; ea = ea->next) + l = append_extra_arg(o, l, ea->name, ea->value); + return res; +} + Z_SRW_PDU *yaz_srw_get_pdu(ODR o, int which, const char *version) { Z_SRW_PDU *sr = yaz_srw_get_core_ver(o, version); sr->which = which; - switch(which) + switch (which) { case Z_SRW_searchRetrieve_request: sr->u.request = (Z_SRW_searchRetrieveRequest *) @@ -884,19 +954,19 @@ static int yaz_get_sru_parms(const Z_SRW_PDU *srw_pdu, ODR encode, int version2 = strcmp(srw_pdu->srw_version, "2.") > 0; int i = 0; char *queryType; - if (!version2) - yaz_add_name_value_str(encode, name, value, &i, "version", srw_pdu->srw_version); + yaz_add_name_value_str(encode, name, value, &i, "version", + srw_pdu->srw_version); name[i] = "operation"; switch (srw_pdu->which) { case Z_SRW_searchRetrieve_request: - if (!version2) - value[i++] = "searchRetrieve"; + value[i++] = "searchRetrieve"; queryType = srw_pdu->u.request->queryType; if (version2) { - yaz_add_name_value_str(encode, name, value, &i, "queryType", - queryType); + if (queryType && strcmp(queryType, "cql")) + yaz_add_name_value_str(encode, name, value, &i, "queryType", + queryType); yaz_add_name_value_str(encode, name, value, &i, "query", srw_pdu->u.request->query); } @@ -949,6 +1019,19 @@ static int yaz_get_sru_parms(const Z_SRW_PDU *srw_pdu, ODR encode, srw_pdu->u.request->stylesheet); yaz_add_name_value_int(encode, name, value, &i, "resultSetTTL", srw_pdu->u.request->resultSetTTL); + { + const char *facetLimit = 0; + const char *facetStart = 0; + const char *facetSort = 0; + yaz_sru_facet_request(encode, &srw_pdu->u.request->facetList, + &facetLimit, &facetStart, &facetSort); + yaz_add_name_value_str(encode, name, value, &i, "facetLimit", + (char *) facetLimit); + yaz_add_name_value_str(encode, name, value, &i, "facetStart", + (char *) facetStart); + yaz_add_name_value_str(encode, name, value, &i, "facetSort", + (char *) facetSort); + } break; case Z_SRW_explain_request: value[i++] = "explain"; @@ -1024,6 +1107,7 @@ int yaz_sru_get_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu, char *name[MAX_SRU_PARAMETERS], *value[MAX_SRU_PARAMETERS]; /* definite upper limit for SRU params */ char *uri_args; char *path; + char *cp; z_HTTP_header_add_basic_auth(encode, &hreq->headers, srw_pdu->username, srw_pdu->password); @@ -1033,11 +1117,15 @@ int yaz_sru_get_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu, hreq->method = "GET"; + cp = strchr(hreq->path, '#'); + if (cp) + *cp = '\0'; + path = (char *) odr_malloc(encode, strlen(hreq->path) + strlen(uri_args) + 4); - sprintf(path, "%s?%s", hreq->path, uri_args); - yaz_log(YLOG_DEBUG, "SRU HTTP Get Request %s", path); + sprintf(path, "%s%c%s", hreq->path, strchr(hreq->path, '?') ? '&' : '?', + uri_args); hreq->path = path; z_HTTP_header_add_content_type(encode, &hreq->headers, @@ -1085,7 +1173,7 @@ int yaz_sru_soap_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu, srw_pdu->username, srw_pdu->password); z_HTTP_header_add_content_type(odr, &hreq->headers, - "text/xml", charset); + "text/xml", 0 /* no charset in MIME */); z_HTTP_header_add(odr, &hreq->headers, "SOAPAction", "\"\""); @@ -1156,14 +1244,10 @@ void yaz_encode_sru_extra(Z_SRW_PDU *sr, ODR odr, const char *extra_args) while (*name) { - *ea = (Z_SRW_extra_arg *) odr_malloc(odr, sizeof(**ea)); - (*ea)->name = *name; - (*ea)->value = *val; - ea = &(*ea)->next; + ea = append_extra_arg(odr, ea, *name, *val); val++; name++; } - *ea = 0; } }