X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fsrwutil.c;h=5cc58e65aa9c031f8ea17b13ea3afef78e41c2e4;hb=138fbf3c4582f2738e5b30e440994c9b6d76e194;hp=f15e4cae4e35616a8120a60fc41b496c2dc8bb2a;hpb=669aa0722ab651e114f8226014e7957d5d2b92fd;p=yaz-moved-to-github.git diff --git a/src/srwutil.c b/src/srwutil.c index f15e4ca..5cc58e6 100644 --- a/src/srwutil.c +++ b/src/srwutil.c @@ -1,8 +1,6 @@ -/* - * Copyright (C) 1995-2007, Index Data ApS +/* This file is part of the YAZ toolkit. + * Copyright (C) 1995-2008 Index Data * See the file LICENSE for details. - * - * $Id: srwutil.c,v 1.62 2007-08-24 13:26:03 adam Exp $ */ /** * \file srwutil.c @@ -42,10 +40,9 @@ void encode_uri_char(char *dst, char ch) } } -static void yaz_array_to_uri_ex(char **path, ODR o, char **name, char **value, - const char *extra_args) +static void yaz_array_to_uri(char **path, ODR o, char **name, char **value) { - size_t i, szp = 0, sz = extra_args ? 1+strlen(extra_args) : 1; + size_t i, szp = 0, sz = 1; for(i = 0; name[i]; i++) sz += strlen(name[i]) + 3 + strlen(value[i]) * 3; *path = (char *) odr_malloc(o, sz); @@ -69,22 +66,10 @@ static void yaz_array_to_uri_ex(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 yaz_uri_to_array(const char *path, ODR o, char ***name, char ***val) { int no = 2; const char *cp; @@ -354,6 +339,34 @@ void yaz_add_sru_update_diagnostic(ODR o, Z_SRW_diagnostic **d, } +void yaz_mk_sru_surrogate(ODR o, Z_SRW_record *record, int pos, + int code, const char *details) +{ + const char *message = yaz_diag_srw_str(code); + int len = 200; + if (message) + len += strlen(message); + if (details) + len += strlen(details); + + record->recordData_buf = (char *) odr_malloc(o, len); + + sprintf(record->recordData_buf, "\n" + " info:srw/diagnostic/1/%d\n", code); + if (details) + sprintf(record->recordData_buf + strlen(record->recordData_buf), + "
%s
\n", details); + if (message) + sprintf(record->recordData_buf + strlen(record->recordData_buf), + " %s\n", message); + sprintf(record->recordData_buf + strlen(record->recordData_buf), + "
\n"); + record->recordData_len = strlen(record->recordData_buf); + record->recordPosition = odr_intdup(o, pos); + record->recordSchema = "info:srw/schema/1/diagnostics-v1.1"; +} + static void grab_charset(ODR o, const char *content_type, char **charset) { if (charset) @@ -515,6 +528,7 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu, char *maximumTerms = 0; char *responsePosition = 0; char *extraRequestData = 0; + Z_SRW_extra_arg *extra_args = 0; #endif char **uri_name; char **uri_val; @@ -536,7 +550,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); + yaz_uri_to_array(p1, decode, &uri_name, &uri_val); #if YAZ_HAVE_XML2 if (uri_name) { @@ -581,6 +595,16 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu, responsePosition = v; else if (!strcmp(n, "extraRequestData")) extraRequestData = v; + else if (n[0] == 'x' && n[1] == '-') + { + Z_SRW_extra_arg **l = &extra_args; + while (*l) + l = &(*l)->next; + *l = odr_malloc(decode, sizeof(**l)); + (*l)->name = odr_strdup(decode, n); + (*l)->value = odr_strdup(decode, v); + (*l)->next = 0; + } else yaz_add_srw_diagnostic(decode, diag, num_diag, YAZ_SRW_UNSUPP_PARAMETER, n); @@ -617,6 +641,7 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu, Z_SRW_PDU *sr = yaz_srw_get(decode, Z_SRW_searchRetrieve_request); sr->srw_version = version; + sr->extra_args = extra_args; *srw_pdu = sr; yaz_srw_decodeauth(sr, hreq, username, password, decode); if (query) @@ -676,6 +701,7 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu, Z_SRW_PDU *sr = yaz_srw_get(decode, Z_SRW_explain_request); sr->srw_version = version; + sr->extra_args = extra_args; yaz_srw_decodeauth(sr, hreq, username, password, decode); *srw_pdu = sr; sr->u.explain_request->recordPacking = recordPacking; @@ -705,6 +731,7 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu, Z_SRW_PDU *sr = yaz_srw_get(decode, Z_SRW_scan_request); sr->srw_version = version; + sr->extra_args = extra_args; *srw_pdu = sr; yaz_srw_decodeauth(sr, hreq, username, password, decode); @@ -947,7 +974,7 @@ Z_SRW_PDU *yaz_srw_get_pdu(ODR o, int which, const char *version) } /* bib1:srw */ -static int srw_bib1_map[] = { +static int bib1_srw_map[] = { 1, 1, 2, 2, 3, 11, @@ -1116,9 +1143,21 @@ static int srw_bib1_map[] = { 0 }; +/* + * This array contains overrides for when the first occurrence of a + * particular SRW error in the array above does not correspond with + * the best back-translation of that SRW error. + */ +static int srw_bib1_map[] = { + 66, 238, + /* No doubt there are many more */ + 0 +}; + + int yaz_diag_bib1_to_srw (int code) { - const int *p = srw_bib1_map; + const int *p = bib1_srw_map; while (*p) { if (code == p[0]) @@ -1130,9 +1169,19 @@ int yaz_diag_bib1_to_srw (int code) int yaz_diag_srw_to_bib1(int code) { + /* Check explicit reverse-map first */ const int *p = srw_bib1_map; while (*p) { + if (code == p[0]) + return p[1]; + p += 2; + } + + /* Fall back on reverse lookup in main map */ + p = bib1_srw_map; + while (*p) + { if (code == p[1]) return p[0]; p += 2; @@ -1164,7 +1213,7 @@ static void add_val_str(ODR o, char **name, char **value, int *i, } static int yaz_get_sru_parms(const Z_SRW_PDU *srw_pdu, ODR encode, - char **name, char **value) + char **name, char **value, int max_names) { int i = 0; add_val_str(encode, name, value, &i, "version", srw_pdu->srw_version); @@ -1248,7 +1297,18 @@ static int yaz_get_sru_parms(const Z_SRW_PDU *srw_pdu, ODR encode, default: return -1; } + if (srw_pdu->extra_args) + { + Z_SRW_extra_arg *ea = srw_pdu->extra_args; + for (; ea && i < max_names-1; ea = ea->next) + { + name[i] = ea->name; + value[i] = ea->value; + i++; + } + } name[i++] = 0; + return 0; } @@ -1259,15 +1319,16 @@ int yaz_sru_get_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu, char *uri_args; char *path; - if (yaz_get_sru_parms(srw_pdu, encode, name, value)) + z_HTTP_header_add_basic_auth(encode, &hreq->headers, + srw_pdu->username, srw_pdu->password); + if (yaz_get_sru_parms(srw_pdu, encode, name, value, 30)) return -1; - yaz_array_to_uri_ex(&uri_args, encode, name, value, srw_pdu->extra_args); + yaz_array_to_uri(&uri_args, encode, name, value); hreq->method = "GET"; path = (char *) - odr_malloc(encode, strlen(hreq->path) + strlen(uri_args) + 4 - +(srw_pdu->extra_args ? strlen(srw_pdu->extra_args) : 0)); + odr_malloc(encode, strlen(hreq->path) + strlen(uri_args) + 4); sprintf(path, "%s?%s", hreq->path, uri_args); hreq->path = path; @@ -1283,10 +1344,12 @@ int yaz_sru_post_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu, char *name[30], *value[30]; /* definite upper limit for SRU params */ char *uri_args; - if (yaz_get_sru_parms(srw_pdu, encode, name, value)) + z_HTTP_header_add_basic_auth(encode, &hreq->headers, + srw_pdu->username, srw_pdu->password); + if (yaz_get_sru_parms(srw_pdu, encode, name, value, 30)) return -1; - yaz_array_to_uri_ex(&uri_args, encode, name, value, srw_pdu->extra_args); + yaz_array_to_uri(&uri_args, encode, name, value); hreq->method = "POST"; @@ -1311,6 +1374,8 @@ int yaz_sru_soap_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu, }; Z_SOAP *p = (Z_SOAP*) odr_malloc(odr, sizeof(*p)); + z_HTTP_header_add_basic_auth(odr, &hreq->headers, + srw_pdu->username, srw_pdu->password); z_HTTP_header_add_content_type(odr, &hreq->headers, "text/xml", charset); @@ -1372,6 +1437,30 @@ int yaz_srw_str_to_pack(const char *str) return -1; } +void yaz_encode_sru_extra(Z_SRW_PDU *sr, ODR odr, const char *extra_args) +{ + if (extra_args) + { + char **name; + char **val; + Z_SRW_extra_arg **ea = &sr->extra_args; + yaz_uri_to_array(extra_args, odr, &name, &val); + + while (*name) + { + *ea = odr_malloc(odr, sizeof(**ea)); + (*ea)->name = *name; + (*ea)->value = *val; + ea = &(*ea)->next; + val++; + name++; + } + *ea = 0; + } +} + + + /* * Local variables: * c-basic-offset: 4