From 81afb524fb5c2d761d37853f4182fca4567cd2fd Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Tue, 11 Mar 2003 11:09:17 +0000 Subject: [PATCH] SRW character set handling --- include/yaz/soap.h | 5 ++++- server/seshigh.c | 40 ++++++++++++++++++++++++++++++++++------ zoom/Makefile.am | 4 ++-- zutil/Makefile.am | 4 ++-- zutil/soap.c | 15 +++++++++++++-- zutil/zgdu.c | 7 +++++-- zutil/zoom-c.c | 37 ++++++++++++++++++++----------------- 7 files changed, 80 insertions(+), 32 deletions(-) diff --git a/include/yaz/soap.h b/include/yaz/soap.h index 0123828..bdbb098 100644 --- a/include/yaz/soap.h +++ b/include/yaz/soap.h @@ -2,7 +2,7 @@ * Copyright (c) 2002-2003, Index Data. * See the file LICENSE for details. * - * $Id: soap.h,v 1.2 2003-02-14 18:49:23 adam Exp $ + * $Id: soap.h,v 1.3 2003-03-11 11:09:17 adam Exp $ */ #ifndef YAZ_SOAP_H @@ -46,5 +46,8 @@ typedef struct { YAZ_EXPORT int z_soap_codec(ODR o, Z_SOAP **pp, char **content_buf, int *content_len, Z_SOAP_Handler *handlers); +YAZ_EXPORT int z_soap_codec_enc(ODR o, Z_SOAP **pp, + char **content_buf, int *content_len, + Z_SOAP_Handler *handlers, const char *encoding); #endif diff --git a/server/seshigh.c b/server/seshigh.c index 4d2ecf0..e8c6439 100644 --- a/server/seshigh.c +++ b/server/seshigh.c @@ -2,7 +2,7 @@ * Copyright (c) 1995-2003, Index Data * See the file LICENSE for details. * - * $Id: seshigh.c,v 1.147 2003-02-23 20:39:31 adam Exp $ + * $Id: seshigh.c,v 1.148 2003-03-11 11:09:17 adam Exp $ */ /* @@ -100,6 +100,7 @@ association *create_association(IOCHAN channel, COMSTACK link) if (!(anew = (association *)xmalloc(sizeof(*anew)))) return 0; anew->init = 0; + anew->version = 0; anew->client_chan = channel; anew->client_link = link; anew->cs_get_mask = 0; @@ -444,6 +445,8 @@ static void assoc_init_reset(association *assoc) static int srw_bend_init(association *assoc) { + const char *encoding = "UTF-8"; + Z_External *ce; bend_initresult *binitres; statserv_options_block *cb = statserv_getcontrol(); @@ -451,7 +454,10 @@ static int srw_bend_init(association *assoc) assoc->maximumRecordSize = 3000000; assoc->preferredMessageSize = 3000000; - +#if 1 + ce = yaz_set_proposal_charneg(assoc->decode, &encoding, 1, 0, 0, 1); + assoc->init->charneg_request = ce->u.charNeg3; +#endif if (!(binitres = (*cb->bend_init)(assoc->init))) { yaz_log(LOG_WARN, "Bad response from backend."); @@ -791,6 +797,8 @@ static void process_http_request(association *assoc, request *req) Z_SOAP *soap_package = 0; int ret = -1; int http_code = 500; + const char *charset_p = 0; + char *charset = 0; static Z_SOAP_Handler soap_handlers[2] = { #if HAVE_XML2 @@ -799,6 +807,18 @@ static void process_http_request(association *assoc, request *req) #endif {0, 0, 0} }; + if ((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 = odr_malloc(assoc->encode, i+1); + memcpy(charset, charset_p, i); + charset[i] = '\0'; + yaz_log(LOG_LOG, "SOAP encoding %s", charset); + } ret = z_soap_codec(assoc->decode, &soap_package, &hreq->content_buf, &hreq->content_len, soap_handlers); @@ -844,11 +864,19 @@ static void process_http_request(association *assoc, request *req) #endif p = z_get_HTTP_Response(o, 200); hres = p->u.HTTP_Response; - ret = z_soap_codec(assoc->encode, &soap_package, - &hres->content_buf, &hres->content_len, - soap_handlers); + ret = z_soap_codec_enc(assoc->encode, &soap_package, + &hres->content_buf, &hres->content_len, + soap_handlers, charset); hres->code = http_code; - z_HTTP_header_add(o, &hres->headers, "Content-Type", "text/xml"); + if (!charset) + z_HTTP_header_add(o, &hres->headers, "Content-Type", "text/xml"); + else + { + char ctype[60]; + strcpy(ctype, "text/xml; charset="); + strcat(ctype, charset); + z_HTTP_header_add(o, &hres->headers, "Content-Type", ctype); + } } if (!p) /* still no response ? */ p = z_get_HTTP_Response(o, 500); diff --git a/zoom/Makefile.am b/zoom/Makefile.am index be36a81..7943906 100644 --- a/zoom/Makefile.am +++ b/zoom/Makefile.am @@ -1,9 +1,9 @@ -## $Id: Makefile.am,v 1.10 2003-02-18 10:37:08 adam Exp $ +## $Id: Makefile.am,v 1.11 2003-03-11 11:09:17 adam Exp $ ## Copyright (C) 2001, Index Data AM_CPPFLAGS = -I$(top_srcdir)/include -noinst_PROGRAMS = zoomtst1 zoomtst2 zoomtst3 zoomtst4 zoomtst5 zoomtst6 zoomtst7 zoomtst8 +EXTRA_PROGRAMS = zoomtst1 zoomtst2 zoomtst3 zoomtst4 zoomtst5 zoomtst6 zoomtst7 zoomtst8 bin_PROGRAMS = zoomsh LDADD = ../lib/libyazmalloc.la ../lib/libyaz.la $(READLINE_LIBS) diff --git a/zutil/Makefile.am b/zutil/Makefile.am index 0a65a31..366a731 100644 --- a/zutil/Makefile.am +++ b/zutil/Makefile.am @@ -1,4 +1,4 @@ -## $Id: Makefile.am,v 1.18 2003-03-02 15:34:51 adam Exp $ +## $Id: Makefile.am,v 1.19 2003-03-11 11:09:17 adam Exp $ noinst_LTLIBRARIES = libzutil.la AM_CPPFLAGS=-I$(top_srcdir)/include $(XML2_CFLAGS) @@ -10,4 +10,4 @@ libzutil_la_SOURCES = zget.c yaz-ccl.c diagbib1.c logrpn.c \ srwtst_LDADD = ../odr/libodr.la libzutil.la ../util/libutil.la srwtst_SOURCES = srwtst.c -noinst_PROGRAMS = srwtst +EXTRA_PROGRAMS = srwtst diff --git a/zutil/soap.c b/zutil/soap.c index 2b416b3..d848cf9 100644 --- a/zutil/soap.c +++ b/zutil/soap.c @@ -2,7 +2,7 @@ * Copyright (c) 2002-2003, Index Data. * See the file LICENSE for details. * - * $Id: soap.c,v 1.5 2003-02-18 14:28:53 adam Exp $ + * $Id: soap.c,v 1.6 2003-03-11 11:09:17 adam Exp $ */ #include @@ -34,6 +34,14 @@ int z_soap_codec(ODR o, Z_SOAP **pp, char **content_buf, int *content_len, Z_SOAP_Handler *handlers) { + return z_soap_codec_enc(o, pp, content_buf, content_len, handlers, 0); +} + +int z_soap_codec_enc(ODR o, Z_SOAP **pp, + char **content_buf, int *content_len, + Z_SOAP_Handler *handlers, + const char *encoding) +{ if (o->direction == ODR_DECODE) { Z_SOAP *p; @@ -207,7 +215,10 @@ int z_soap_codec(ODR o, Z_SOAP **pp, if (ret) return ret; } - xmlDocDumpMemory(doc, &buf_out, &len_out); + if (encoding) + xmlDocDumpMemoryEnc(doc, &buf_out, &len_out, encoding); + else + xmlDocDumpMemory(doc, &buf_out, &len_out); *content_buf = (char *) odr_malloc(o, len_out); *content_len = len_out; memcpy(*content_buf, buf_out, len_out); diff --git a/zutil/zgdu.c b/zutil/zgdu.c index ab99984..dbd921e 100644 --- a/zutil/zgdu.c +++ b/zutil/zgdu.c @@ -2,7 +2,7 @@ * Copyright (c) 2002-2003, Index Data. * See the file LICENSE for details. * - * $Id: zgdu.c,v 1.8 2003-02-21 12:08:59 adam Exp $ + * $Id: zgdu.c,v 1.9 2003-03-11 11:09:17 adam Exp $ */ #include @@ -279,6 +279,7 @@ int z_GDU (ODR o, Z_GDU **p, int opt, const char *name) } else /* ENCODE or PRINT */ { + int top0 = o->top; char sbuf[80]; Z_HTTP_Header *h; switch((*p)->which) @@ -312,7 +313,9 @@ int z_GDU (ODR o, Z_GDU **p, int opt, const char *name) (*p)->u.HTTP_Response->content_len); if (o->direction == ODR_PRINT) { - fprintf(o->print, "-- HTTP response:\n%.*s\n", o->top, o->buf); + fprintf(o->print, "-- HTTP response:\n%.*s\n", o->top - top0, + o->buf + top0); + fprintf(o->print, "-- \n"); } break; case Z_GDU_HTTP_Request: diff --git a/zutil/zoom-c.c b/zutil/zoom-c.c index 82c7c3e..2f636e2 100644 --- a/zutil/zoom-c.c +++ b/zutil/zoom-c.c @@ -2,7 +2,7 @@ * Copyright (c) 2000-2003, Index Data * See the file LICENSE for details. * - * $Id: zoom-c.c,v 1.28 2003-02-23 14:26:58 adam Exp $ + * $Id: zoom-c.c,v 1.29 2003-03-11 11:09:17 adam Exp $ * * ZOOM layer for C, connections, result sets, queries. */ @@ -712,29 +712,23 @@ static zoom_ret do_connect (ZOOM_connection c) yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host); assert (!c->cs); + c->cs = cs_create_host (effective_host, 0, &add); - if (memcmp(c->host_port, "http:", 5) == 0) + if (c->cs && c->cs->protocol == PROTO_HTTP) { #if HAVE_XML2 - const char *path; + const char *path = 0; + c->proto = PROTO_HTTP; - effective_host = c->host_port + 5; - if (*effective_host == '/') - effective_host++; - if (*effective_host == '/') - effective_host++; - if (!(path = strchr(effective_host, '/'))) - path = "/"; + cs_get_host_args(c->host_port, &path); xfree(c->path); c->path = xstrdup(path); #else - c->state = STATE_IDLE; set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, "SRW"); + do_close(c); return zoom_complete; #endif } - c->cs = cs_create_host (effective_host, 0, &add); - if (c->cs) { int ret = cs_connect (c->cs, add); @@ -981,6 +975,7 @@ static zoom_ret ZOOM_connection_send_init (ZOOM_connection c) #if HAVE_XML2 static zoom_ret send_srw (ZOOM_connection c, Z_SRW_PDU *sr) { + char ctype[50]; Z_SOAP_Handler h[2] = { {"http://www.loc.gov/zing/srw/v1.0/", 0, (Z_SOAP_fun) yaz_srw_codec}, {0, 0, 0} @@ -993,8 +988,15 @@ static zoom_ret send_srw (ZOOM_connection c, Z_SRW_PDU *sr) gdu = z_get_HTTP_Request(c->odr_out); gdu->u.HTTP_Request->path = c->path; + + strcpy(ctype, "text/xml"); + if (c->charset && strlen(c->charset) < 20) + { + strcat(ctype, "; charset="); + strcat(ctype, c->charset); + } z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers, - "Content-Type", "text/xml"); + "Content-Type", ctype); z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers, "SOAPAction", "\"\""); p->which = Z_SOAP_generic; @@ -1004,9 +1006,10 @@ static zoom_ret send_srw (ZOOM_connection c, Z_SRW_PDU *sr) p->u.generic->p = sr; p->ns = "http://schemas.xmlsoap.org/soap/envelope/"; - ret = z_soap_codec(o, &p, - &gdu->u.HTTP_Request->content_buf, - &gdu->u.HTTP_Request->content_len, h); + ret = z_soap_codec_enc(o, &p, + &gdu->u.HTTP_Request->content_buf, + &gdu->u.HTTP_Request->content_len, h, + c->charset); if (!z_GDU(c->odr_out, &gdu, 0, 0)) return zoom_complete; -- 1.7.10.4