/*
- * Copyright (C) 1995-2005, Index Data ApS
+ * Copyright (C) 1995-2006, Index Data ApS
* See the file LICENSE for details.
*
- * $Id: srwutil.c,v 1.38 2006-04-20 00:01:01 adam Exp $
+ * $Id: srwutil.c,v 1.53 2006-10-27 11:22:09 adam Exp $
*/
/**
* \file srwutil.c
}
}
-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);
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;
return 0;
}
+static int yaz_base64decode(const char *in, char *out)
+{
+ const char *map = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz0123456789+/";
+ int olen = 0;
+ int len = strlen(in);
+
+ while (len >= 4)
+ {
+ char i0, i1, i2, i3;
+ char *p;
+
+ if (!(p = strchr(map, in[0])))
+ return 0;
+ i0 = p - map;
+ len--;
+ if (!(p = strchr(map, in[1])))
+ return 0;
+ i1 = p - map;
+ len--;
+ *(out++) = i0 << 2 | i1 >> 4;
+ olen++;
+ if (in[2] == '=')
+ break;
+ if (!(p = strchr(map, in[2])))
+ return 0;
+ i2 = p - map;
+ len--;
+ *(out++) = i1 << 4 | i2 >> 2;
+ olen++;
+ if (in[3] == '=')
+ break;
+ if (!(p = strchr(map, in[3])))
+ return 0;
+ i3 = p - map;
+ len--;
+ *(out++) = i2 << 6 | i3;
+ olen++;
+
+ in += 4;
+ }
+
+ *out = '\0';
+ return olen;
+}
+
+/**
+ * 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)
+{
+ const char *basic = z_HTTP_header_lookup(hreq->headers, "Authorization");
+
+ if (username)
+ sr->username = username;
+ if (password)
+ sr->password = password;
+
+ if (basic) {
+ int len, olen;
+ char out[256];
+ char ubuf[256] = "", pbuf[256] = "", *p;
+ if (strncmp(basic, "Basic ", 6))
+ return;
+ basic += 6;
+ len = strlen(basic);
+ if (!len || len > 256)
+ return;
+ olen = yaz_base64decode(basic, out);
+ /* Format of out should be username:password at this point */
+ strcpy(ubuf, out);
+ if ((p = strchr(ubuf, ':'))) {
+ *(p++) = '\0';
+ if (*p)
+ strcpy(pbuf, p);
+ }
+ if (*ubuf)
+ sr->username = odr_strdup(decode, ubuf);
+ if (*pbuf)
+ sr->password = odr_strdup(decode, pbuf);
+ }
+}
+
void yaz_uri_val_int(const char *path, const char *name, ODR o, int **intp)
{
const char *v = yaz_uri_val(path, name, o);
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)
{
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
+#if YAZ_HAVE_XML2
{"http://www.loc.gov/zing/srw/", 0,
(Z_SOAP_fun) yaz_srw_codec},
{"http://www.loc.gov/zing/srw/v1.0/", 0,
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);
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
*/
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},
};
#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;
char *pQuery = 0;
+ char *username = 0;
+ char *password = 0;
char *sortKeys = 0;
char *stylesheet = 0;
char *scanClause = 0;
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, '?');
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;
query = v;
else if (!strcmp(n, "x-pquery"))
pQuery = v;
+ else if (!strcmp(n, "x-username"))
+ username = v;
+ else if (!strcmp(n, "x-password"))
+ password = v;
else if (!strcmp(n, "operation"))
operation = v;
else if (!strcmp(n, "stylesheet"))
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"))
sr->srw_version = version;
*srw_pdu = sr;
+ yaz_srw_decodeauth(sr, hreq, username, password, decode);
if (query)
{
sr->u.request->query_type = Z_SRW_query_type_cql;
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)
{
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;
Z_SRW_PDU *sr = yaz_srw_get(decode, Z_SRW_explain_request);
sr->srw_version = version;
+ yaz_srw_decodeauth(sr, hreq, username, password, decode);
*srw_pdu = sr;
sr->u.explain_request->recordPacking = recordPacking;
sr->u.explain_request->database = db;
sr->srw_version = version;
*srw_pdu = sr;
+ yaz_srw_decodeauth(sr, hreq, username, password, decode);
if (scanClause)
{
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;
(*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
return res;
}
-Z_SRW_PDU *yaz_srw_get(ODR o, int which)
+Z_SRW_PDU *yaz_srw_get_core_v_1_1(ODR o)
{
- Z_SRW_PDU *sr = (Z_SRW_PDU *) odr_malloc(o, sizeof(*o));
+ Z_SRW_PDU *p = (Z_SRW_PDU *) odr_malloc(o, sizeof(*p));
+ p->srw_version = odr_strdup(o, "1.1");
+ p->username = 0;
+ p->password = 0;
+ p->extra_args = 0;
+ return p;
+}
- sr->srw_version = odr_strdup(o, "1.1");
+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)
{
}
int yaz_sru_get_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
- ODR encode, char *charset)
+ ODR encode, const char *charset)
{
char *name[30], *value[30]; /* definite upper limit for SRU params */
char *uri_args;
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;
}
int yaz_sru_post_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
- ODR encode, char *charset)
+ ODR encode, const char *charset)
{
char *name[30], *value[30]; /* definite upper limit for SRU params */
char *uri_args;
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";
return 0;
}
+int yaz_sru_soap_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
+ ODR odr, const char *charset)
+{
+ Z_SOAP_Handler handlers[2] = {
+#if YAZ_HAVE_XML2
+ {"http://www.loc.gov/zing/srw/", 0, (Z_SOAP_fun) yaz_srw_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);
+
+ z_HTTP_header_add(odr, &hreq->headers,
+ "SOAPAction", "\"\"");
+ p->which = Z_SOAP_generic;
+ p->u.generic = (Z_SOAP_Generic *) odr_malloc(odr, sizeof(*p->u.generic));
+ p->u.generic->no = 0;
+ p->u.generic->ns = 0;
+ p->u.generic->p = srw_pdu;
+ p->ns = "http://schemas.xmlsoap.org/soap/envelope/";
+
+ return z_soap_codec_enc(odr, &p,
+ &hreq->content_buf,
+ &hreq->content_len, handlers,
+ charset);
+}
+
/*
* Local variables:
* c-basic-offset: 4