X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Fsrwutil.c;h=c7ae1c22c7092b154f707198608401efd7a600c8;hp=45b578598598c1aebc9074030438142a99d7ff73;hb=62fd62f25a6893d042c4c87f8c770929fd8cf95d;hpb=d58b3c28b5efc7106f6ed65cd52c8ad56de7b6b9 diff --git a/src/srwutil.c b/src/srwutil.c index 45b5785..c7ae1c2 100644 --- a/src/srwutil.c +++ b/src/srwutil.c @@ -1,8 +1,8 @@ /* - * Copyright (C) 1995-2005, Index Data ApS + * Copyright (C) 1995-2007, Index Data ApS * See the file LICENSE for details. * - * $Id: srwutil.c,v 1.41 2006-06-02 13:08:27 adam Exp $ + * $Id: srwutil.c,v 1.55 2007-01-03 08:42:15 adam Exp $ */ /** * \file srwutil.c @@ -41,9 +41,10 @@ void encode_uri_char(char *dst, char ch) } } -void yaz_array_to_uri(char **path, ODR o, char **name, char **value) +static void yaz_array_to_uri_ex(char **path, ODR o, char **name, char **value, + const char *extra_args) { - size_t i, szp = 0, sz = 0; + size_t i, szp = 0, sz = extra_args ? 1+strlen(extra_args) : 1; for(i = 0; name[i]; i++) sz += strlen(name[i]) + 3 + strlen(value[i]) * 3; *path = odr_malloc(o, sz); @@ -67,9 +68,21 @@ void yaz_array_to_uri(char **path, ODR o, char **name, char **value) szp += vlen; } } + if (extra_args) + { + if (i) + (*path)[szp++] = '&'; + memcpy(*path + szp, extra_args, strlen(extra_args)); + szp += strlen(extra_args); + } (*path)[szp] = '\0'; } +void yaz_array_to_uri(char **path, ODR o, char **name, char **value) +{ + yaz_array_to_uri_ex(path, o, name, value, 0); +} + int yaz_uri_array(const char *path, ODR o, char ***name, char ***val) { int no = 2; @@ -226,8 +239,8 @@ static int yaz_base64decode(const char *in, char *out) * Look for authentication tokens in HTTP Basic parameters or in x-username/x-password * parameters. Added by SH. */ -static void yaz_srw_decodeauth(Z_SRW_PDU *sr, Z_HTTP_Request *hreq, char *username, - char *password, ODR decode) +static void yaz_srw_decodeauth(Z_SRW_PDU *sr, Z_HTTP_Request *hreq, + char *username, char *password, ODR decode) { const char *basic = z_HTTP_header_lookup(hreq->headers, "Authorization"); @@ -315,6 +328,36 @@ void yaz_add_srw_diagnostic(ODR o, Z_SRW_diagnostic **d, yaz_add_srw_diagnostic_uri(o, d, num, uri, 0, addinfo); } + +void yaz_add_sru_update_diagnostic(ODR o, Z_SRW_diagnostic **d, + int *num, int code, const char *addinfo) +{ + char uri[40]; + + sprintf(uri, "info:srw/diagnostic/12/%d", code); + yaz_add_srw_diagnostic_uri(o, d, num, uri, 0, addinfo); +} + + +static void grab_charset(ODR o, const char *content_type, char **charset) +{ + if (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 yaz_srw_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu, Z_SOAP **soap_package, ODR decode, char **charset) { @@ -330,16 +373,12 @@ int yaz_srw_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu, char *db = "Default"; const char *p0 = hreq->path, *p1; int ret = -1; - const char *charset_p = 0; static Z_SOAP_Handler soap_handlers[4] = { -#if HAVE_XML2 - {"http://www.loc.gov/zing/srw/", 0, - (Z_SOAP_fun) yaz_srw_codec}, - {"http://www.loc.gov/zing/srw/v1.0/", 0, - (Z_SOAP_fun) yaz_srw_codec}, - {"http://www.loc.gov/zing/srw/update/", 0, - (Z_SOAP_fun) yaz_ucp_codec}, +#if YAZ_HAVE_XML2 + { YAZ_XMLNS_SRU_v1_1, 0, (Z_SOAP_fun) yaz_srw_codec }, + { YAZ_XMLNS_SRU_v1_0, 0, (Z_SOAP_fun) yaz_srw_codec }, + { YAZ_XMLNS_UPDATE_v0_9, 0, (Z_SOAP_fun) yaz_ucp_codec }, #endif {0, 0, 0} }; @@ -356,17 +395,8 @@ int yaz_srw_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu, db[p1 - p0] = '\0'; } - if (charset && (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(decode, i+1); - memcpy(*charset, charset_p, i); - (*charset)[i] = '\0'; - } + grab_charset(decode, content_type, charset); + ret = z_soap_codec(decode, soap_package, &hreq->content_buf, &hreq->content_len, soap_handlers); @@ -398,6 +428,30 @@ int yaz_srw_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu, return 2; } +static int yaz_sru_decode_integer(ODR odr, const char *pname, + const char *valstr, int **valp, + Z_SRW_diagnostic **diag, int *num_diag, + int min_value) +{ + int ival; + if (!valstr) + return 0; + if (sscanf(valstr, "%d", &ival) != 1) + { + yaz_add_srw_diagnostic(odr, diag, num_diag, + YAZ_SRW_UNSUPP_PARAMETER_VALUE, pname); + return 0; + } + if (min_value >= 0 && ival < min_value) + { + yaz_add_srw_diagnostic(odr, diag, num_diag, + YAZ_SRW_UNSUPP_PARAMETER_VALUE, pname); + return 0; + } + *valp = odr_intdup(odr, ival); + return 1; +} + /** http://www.loc.gov/z3950/agency/zing/srw/service.html */ @@ -405,32 +459,29 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu, Z_SOAP **soap_package, ODR decode, char **charset, Z_SRW_diagnostic **diag, int *num_diag) { -#if HAVE_XML2 +#if YAZ_HAVE_XML2 static Z_SOAP_Handler soap_handlers[2] = { - {"http://www.loc.gov/zing/srw/", 0, - (Z_SOAP_fun) yaz_srw_codec}, + {YAZ_XMLNS_SRU_v1_1, 0, (Z_SOAP_fun) yaz_srw_codec}, {0, 0, 0} }; #endif const char *content_type = z_HTTP_header_lookup(hreq->headers, - "Content-Type"); + "Content-Type"); + /* - SRU GET: allow any content type. + SRU GET: ignore content type. SRU POST: we support "application/x-www-form-urlencoded"; not "multipart/form-data" . */ - if (!strcmp(hreq->method, "GET") - || - (!strcmp(hreq->method, "POST") - && content_type && - !yaz_strcmp_del("application/x-www-form-urlencoded", - content_type, "; ") - ) - ) + if (!strcmp(hreq->method, "GET") + || + (!strcmp(hreq->method, "POST") && content_type && + !yaz_strcmp_del("application/x-www-form-urlencoded", + content_type, "; "))) { char *db = "Default"; const char *p0 = hreq->path, *p1; -#if HAVE_XML2 +#if YAZ_HAVE_XML2 const char *operation = 0; char *version = 0; char *query = 0; @@ -453,8 +504,10 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu, char **uri_name; char **uri_val; - if (charset) - *charset = 0; + grab_charset(decode, content_type, charset); + if (charset && *charset == 0 && !strcmp(hreq->method, "GET")) + *charset = "UTF-8"; + if (*p0 == '/') p0++; p1 = strchr(p0, '?'); @@ -469,7 +522,7 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu, if (!strcmp(hreq->method, "POST")) p1 = hreq->content_buf; yaz_uri_array(p1, decode, &uri_name, &uri_val); -#if HAVE_XML2 +#if YAZ_HAVE_XML2 if (uri_name) { int i; @@ -514,21 +567,27 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu, else if (!strcmp(n, "extraRequestData")) extraRequestData = v; else - yaz_add_srw_diagnostic(decode, diag, num_diag, 8, n); + yaz_add_srw_diagnostic(decode, diag, num_diag, + YAZ_SRW_UNSUPP_PARAMETER, n); } } if (!version) { if (uri_name) - yaz_add_srw_diagnostic(decode, diag, num_diag, 7, "version"); + yaz_add_srw_diagnostic( + decode, diag, num_diag, + YAZ_SRW_MANDATORY_PARAMETER_NOT_SUPPLIED, "version"); version = "1.1"; } if (strcmp(version, "1.1")) - yaz_add_srw_diagnostic(decode, diag, num_diag, 5, "1.1"); + yaz_add_srw_diagnostic(decode, diag, num_diag, + YAZ_SRW_UNSUPP_VERSION, "1.1"); if (!operation) { if (uri_name) - yaz_add_srw_diagnostic(decode, diag, num_diag, 7, "operation"); + yaz_add_srw_diagnostic( + decode, diag, num_diag, + YAZ_SRW_MANDATORY_PARAMETER_NOT_SUPPLIED, "operation"); operation = "explain"; } if (!strcmp(operation, "searchRetrieve")) @@ -549,7 +608,9 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu, sr->u.request->query.pqf = pQuery; } else - yaz_add_srw_diagnostic(decode, diag, num_diag, 7, "query"); + yaz_add_srw_diagnostic( + decode, diag, num_diag, + YAZ_SRW_MANDATORY_PARAMETER_NOT_SUPPLIED, "query"); if (sortKeys) { @@ -561,12 +622,13 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu, 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)); + yaz_sru_decode_integer(decode, "maximumRecords", maximumRecords, + &sr->u.request->maximumRecords, + diag, num_diag, 0); + + yaz_sru_decode_integer(decode, "startRecord", startRecord, + &sr->u.request->startRecord, + diag, num_diag, 1); sr->u.request->database = db; @@ -633,16 +695,20 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu, sr->u.scan_request->scanClause.pqf = pScanClause; } else - yaz_add_srw_diagnostic(decode, diag, num_diag, 7, - "scanClause"); + yaz_add_srw_diagnostic( + decode, diag, num_diag, + YAZ_SRW_MANDATORY_PARAMETER_NOT_SUPPLIED, "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)); + + yaz_sru_decode_integer(decode, "maximumTerms", + maximumTerms, + &sr->u.scan_request->maximumTerms, + diag, num_diag, 0); + + yaz_sru_decode_integer(decode, "responsePosition", + responsePosition, + &sr->u.scan_request->responsePosition, + diag, num_diag, 0); sr->u.scan_request->stylesheet = stylesheet; @@ -686,7 +752,8 @@ 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); + yaz_add_srw_diagnostic(decode, diag, num_diag, + YAZ_SRW_UNSUPP_OPERATION, operation); return 0; } #endif @@ -699,23 +766,48 @@ Z_SRW_extra_record *yaz_srw_get_extra_record(ODR o) { Z_SRW_extra_record *res = (Z_SRW_extra_record *) odr_malloc(o, sizeof(*res)); - res->type = 1; - res->recordReviewCode = 0; - res->recordReviewNote = 0; - res->recordId = 0; - res->nonDupRecordId = 0; - res->recordLockStatus = 0; - res->recordOldVersion = 0; + + res->extraRecordData_buf = 0; + res->extraRecordData_len = 0; + res->recordIdentifier = 0; return res; } -Z_SRW_PDU *yaz_srw_get(ODR o, int which) + +Z_SRW_record *yaz_srw_get_records(ODR o, int n) { - Z_SRW_PDU *sr = (Z_SRW_PDU *) odr_malloc(o, sizeof(*o)); + Z_SRW_record *res = (Z_SRW_record *) odr_malloc(o, n * sizeof(*res)); + int i; - sr->username = 0; - sr->password = 0; - sr->srw_version = odr_strdup(o, "1.1"); + for (i = 0; isrw_version = odr_strdup(o, "1.1"); + p->username = 0; + p->password = 0; + p->extra_args = 0; + return p; +} + +Z_SRW_PDU *yaz_srw_get(ODR o, int which) +{ + Z_SRW_PDU *sr = yaz_srw_get_core_v_1_1(o); sr->which = which; switch(which) { @@ -791,15 +883,13 @@ Z_SRW_PDU *yaz_srw_get(ODR o, int which) odr_malloc(o, sizeof(*sr->u.update_request)); sr->u.update_request->database = 0; sr->u.update_request->stylesheet = 0; - sr->u.update_request->record.recordSchema = 0; - sr->u.update_request->record.recordPacking = Z_SRW_recordPacking_XML; + sr->u.update_request->record = 0; sr->u.update_request->recordId = 0; - sr->u.update_request->recordVersion = 0; - sr->u.update_request->recordOldVersion = 0; - sr->u.update_request->record.recordData_buf = 0; - sr->u.update_request->record.recordData_len = 0; + sr->u.update_request->recordVersions = 0; + sr->u.update_request->num_recordVersions = 0; sr->u.update_request->extra_record = 0; - sr->u.update_request->extraRequestData = 0; + sr->u.update_request->extraRequestData_buf = 0; + sr->u.update_request->extraRequestData_len = 0; sr->u.request->database = 0; break; case Z_SRW_update_response: @@ -807,15 +897,12 @@ Z_SRW_PDU *yaz_srw_get(ODR o, int which) odr_malloc(o, sizeof(*sr->u.update_response)); sr->u.update_response->operationStatus = 0; sr->u.update_response->recordId = 0; - sr->u.update_response->recordVersion = 0; - sr->u.update_response->recordChecksum = 0; - sr->u.update_response->record.recordData_buf = 0; - sr->u.update_response->record.recordData_len = 0; - sr->u.update_response->record.recordSchema = 0; - sr->u.update_response->record.recordPacking = - Z_SRW_recordPacking_XML; + sr->u.update_response->recordVersions = 0; + sr->u.update_response->num_recordVersions = 0; + sr->u.update_response->record = 0; sr->u.update_response->extra_record = 0; - sr->u.update_response->extraResponseData = 0; + sr->u.update_response->extraResponseData_buf = 0; + sr->u.update_response->extraResponseData_len = 0; sr->u.update_response->diagnostics = 0; sr->u.update_response->num_diagnostics = 0; } @@ -1137,11 +1224,13 @@ int yaz_sru_get_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu, if (yaz_get_sru_parms(srw_pdu, encode, name, value)) return -1; - yaz_array_to_uri(&uri_args, encode, name, value); + yaz_array_to_uri_ex(&uri_args, encode, name, value, srw_pdu->extra_args); hreq->method = "GET"; - path = odr_malloc(encode, strlen(hreq->path) + strlen(uri_args) + 3); + path = odr_malloc(encode, strlen(hreq->path) + strlen(uri_args) + 4 + +(srw_pdu->extra_args ? strlen(srw_pdu->extra_args) : 0) + ); sprintf(path, "%s?%s", hreq->path, uri_args); hreq->path = path; @@ -1159,7 +1248,7 @@ int yaz_sru_post_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu, if (yaz_get_sru_parms(srw_pdu, encode, name, value)) return -1; - yaz_array_to_uri(&uri_args, encode, name, value); + yaz_array_to_uri_ex(&uri_args, encode, name, value, srw_pdu->extra_args); hreq->method = "POST"; @@ -1175,11 +1264,15 @@ int yaz_sru_post_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu, int yaz_sru_soap_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu, ODR odr, const char *charset) { - Z_SOAP_Handler handlers[2] = { - {"http://www.loc.gov/zing/srw/", 0, (Z_SOAP_fun) yaz_srw_codec}, + Z_SOAP_Handler handlers[3] = { +#if YAZ_HAVE_XML2 + {YAZ_XMLNS_SRU_v1_1, 0, (Z_SOAP_fun) yaz_srw_codec}, + {YAZ_XMLNS_UPDATE_v0_9, 0, (Z_SOAP_fun) yaz_ucp_codec}, +#endif {0, 0, 0} }; Z_SOAP *p = (Z_SOAP*) odr_malloc(odr, sizeof(*p)); + z_HTTP_header_add_content_type(odr, &hreq->headers, "text/xml", charset); @@ -1192,13 +1285,55 @@ int yaz_sru_soap_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu, p->u.generic->ns = 0; p->u.generic->p = srw_pdu; p->ns = "http://schemas.xmlsoap.org/soap/envelope/"; - + +#if YAZ_HAVE_XML2 + if (srw_pdu->which == Z_SRW_update_request || + srw_pdu->which == Z_SRW_update_response) + p->u.generic->no = 1; /* second handler */ +#endif return z_soap_codec_enc(odr, &p, &hreq->content_buf, &hreq->content_len, handlers, charset); } +Z_SRW_recordVersion *yaz_srw_get_record_versions(ODR odr, int num ) +{ + Z_SRW_recordVersion *ver + = (Z_SRW_recordVersion *) odr_malloc( odr, num * sizeof(*ver) ); + int i; + for ( i=0; i < num; ++i ){ + ver[i].versionType = 0; + ver[i].versionValue = 0; + } + return ver; +} + +const char *yaz_srw_pack_to_str(int pack) +{ + switch(pack) + { + case Z_SRW_recordPacking_string: + return "string"; + case Z_SRW_recordPacking_XML: + return "xml"; + case Z_SRW_recordPacking_URL: + return "url"; + } + return 0; +} + +int yaz_srw_str_to_pack(const char *str) +{ + if (!strcmp(str, "string")) + return Z_SRW_recordPacking_string; + if (!strcmp(str, "xml")) + return Z_SRW_recordPacking_XML; + if (!strcmp(str, "url")) + return Z_SRW_recordPacking_URL; + return -1; +} + /* * Local variables: * c-basic-offset: 4