From 39a36357055b29eb41cfb1ef0cc82f2826d2df64 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Mon, 8 Oct 2007 13:19:22 +0000 Subject: [PATCH] Added support for retrieval of records in binary. --- NEWS | 2 ++ doc/pazpar2_protocol.xml | 14 +++++++++++++- src/client.c | 22 ++++++++++++++++++++-- src/client.h | 5 +++-- src/http.c | 7 ++++--- src/http.h | 3 ++- src/http_command.c | 33 +++++++++++++++++++++++++++++---- test/test_http_24.res | 1 + test/test_http_urls | 1 + 9 files changed, 75 insertions(+), 13 deletions(-) create mode 100644 test/test_http_24.res diff --git a/NEWS b/NEWS index e5cda69..e723291 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,5 @@ +Added support for retrieval of records in binary. + --- 1.0.4 2007/09/28 JavaScript library pzw2.js throws error if WS response (from Pazpar2 or diff --git a/doc/pazpar2_protocol.xml b/doc/pazpar2_protocol.xml index 969093c..006aab1 100644 --- a/doc/pazpar2_protocol.xml +++ b/doc/pazpar2_protocol.xml @@ -9,7 +9,7 @@ %idcommon; ]> - + Pazpar2 @@ -389,6 +389,18 @@ search.pz2?session=2044502273&command=show&start=0&num=2&sort=title:1 + + binary + + + This optional parameter enables "binary" response for retrieval + of a raw record (i.e. when offset is specified). For binary + responses the record is not converted to + XML and the HTTP content type is application/octet-stream. + + + + diff --git a/src/client.c b/src/client.c index ca8b067..a3b5eff 100644 --- a/src/client.c +++ b/src/client.c @@ -1,4 +1,4 @@ -/* $Id: client.c,v 1.28 2007-10-02 12:11:14 adam Exp $ +/* $Id: client.c,v 1.29 2007-10-08 13:19:23 adam Exp $ Copyright (c) 2006-2007, Index Data. This file is part of Pazpar2. @@ -84,6 +84,7 @@ struct client { struct show_raw { int active; // whether this request has been sent to the server int position; + int binary; char *syntax; char *esn; void (*error_handler)(void *data, const char *addinfo); @@ -238,7 +239,8 @@ int client_show_raw_begin(struct client *cl, int position, void (*error_handler)(void *data, const char *addinfo), void (*record_handler)(void *data, const char *buf, size_t sz), - void **data2) + void **data2, + int binary) { struct show_raw *rr, **rrp; if (!cl->connection) @@ -252,6 +254,7 @@ int client_show_raw_begin(struct client *cl, int position, rr->data = data; rr->error_handler = error_handler; rr->record_handler = record_handler; + rr->binary = binary; if (syntax) rr->syntax = xstrdup(syntax); else @@ -564,6 +567,21 @@ static void ingest_raw_records(struct client *cl, Z_Records *r) return; } + if (cl->show_raw && cl->show_raw->binary) + { + Z_External *rec = npr->u.databaseRecord; + if (rec->which == Z_External_octet) + { + cl->show_raw->record_handler(cl->show_raw->data, + (const char *) + rec->u.octet_aligned->buf, + rec->u.octet_aligned->len); + client_show_raw_dequeue(cl); + } + else + client_show_raw_error(cl, "no records"); + } + doc = record_to_xml(client_get_database(cl), npr->u.databaseRecord); if (!doc) { diff --git a/src/client.h b/src/client.h index 7af4630..fef4e6a 100644 --- a/src/client.h +++ b/src/client.h @@ -1,4 +1,4 @@ -/* $Id: client.h,v 1.5 2007-10-02 12:11:14 adam Exp $ +/* $Id: client.h,v 1.6 2007-10-08 13:19:23 adam Exp $ Copyright (c) 2006-2007, Index Data. This file is part of Pazpar2. @@ -50,7 +50,8 @@ int client_show_raw_begin(struct client *cl, int position, void (*error_handler)(void *data, const char *addinfo), void (*record_handler)(void *data, const char *buf, size_t sz), - void **data2); + void **data2, + int binary); void client_show_raw_remove(struct client *cl, void *rr); const char *client_get_state_str(struct client *cl); diff --git a/src/http.c b/src/http.c index 9a142d7..c23b63d 100644 --- a/src/http.c +++ b/src/http.c @@ -1,4 +1,4 @@ -/* $Id: http.c,v 1.41 2007-10-02 12:11:14 adam Exp $ +/* $Id: http.c,v 1.42 2007-10-08 13:19:23 adam Exp $ Copyright (c) 2006-2007, Index Data. This file is part of Pazpar2. @@ -293,6 +293,7 @@ struct http_response *http_create_response(struct http_channel *c) r->channel = c; r->headers = 0; r->payload = 0; + r->content_type = "text/xml"; return r; } @@ -604,8 +605,8 @@ static struct http_buf *http_serialize_response(struct http_channel *c, { wrbuf_printf(c->wrbuf, "Content-Length: %d\r\n", r->payload ? (int) strlen(r->payload) : 0); - wrbuf_printf(c->wrbuf, "Content-Type: text/xml\r\n"); - if (1) + wrbuf_printf(c->wrbuf, "Content-Type: %s\r\n", r->content_type); + if (!strcmp(r->content_type, "text/xml")) { xmlDoc *doc = xmlParseMemory(r->payload, strlen(r->payload)); if (doc) diff --git a/src/http.h b/src/http.h index ffb1ca9..b4cdd9e 100644 --- a/src/http.h +++ b/src/http.h @@ -1,4 +1,4 @@ -/* $Id: http.h,v 1.12 2007-10-02 12:11:14 adam Exp $ +/* $Id: http.h,v 1.13 2007-10-08 13:19:23 adam Exp $ Copyright (c) 2006-2007, Index Data. This file is part of Pazpar2. @@ -98,6 +98,7 @@ struct http_response struct http_channel *channel; struct http_header *headers; char *payload; + char *content_type; }; void http_set_proxyaddr(char *url, char *baseurl); diff --git a/src/http_command.c b/src/http_command.c index 7be4e07..158bfaa 100644 --- a/src/http_command.c +++ b/src/http_command.c @@ -1,4 +1,4 @@ -/* $Id: http_command.c,v 1.64 2007-10-02 12:11:14 adam Exp $ +/* $Id: http_command.c,v 1.65 2007-10-08 13:19:23 adam Exp $ Copyright (c) 2006-2007, Index Data. This file is part of Pazpar2. @@ -20,7 +20,7 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA */ /* - * $Id: http_command.c,v 1.64 2007-10-02 12:11:14 adam Exp $ + * $Id: http_command.c,v 1.65 2007-10-08 13:19:23 adam Exp $ */ #include @@ -513,6 +513,23 @@ static void show_raw_record_ok(void *data, const char *buf, size_t sz) http_send_response(c); } + +static void show_raw_record_ok_binary(void *data, const char *buf, size_t sz) +{ + http_channel_observer_t obs = data; + struct http_channel *c = http_channel_observer_chan(obs); + struct http_response *rs = c->response; + + http_remove_observer(obs); + + wrbuf_write(c->wrbuf, buf, sz); + rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf)); + + rs->content_type = "application/octet-stream"; + http_send_response(c); +} + + void show_raw_reset(void *data, struct http_channel *c, void *data2) { struct client *client = data; @@ -531,6 +548,7 @@ static void cmd_record(struct http_channel *c) struct conf_service *service = global_parameters.server->service; const char *idstr = http_argbyname(rq, "id"); const char *offsetstr = http_argbyname(rq, "offset"); + const char *binarystr = http_argbyname(rq, "binary"); if (!s) return; @@ -556,6 +574,10 @@ static void cmd_record(struct http_channel *c) const char *esn = http_argbyname(rq, "esn"); int i; struct record*r = rec->records; + int binary = 0; + + if (binarystr && *binarystr != '0') + binary = 1; for (i = 0; i < offset && r; r = r->next, i++) ; @@ -573,8 +595,11 @@ static void cmd_record(struct http_channel *c) client_show_raw_begin(r->client, r->position, syntax, esn, obs /* data */, show_raw_record_error, - show_raw_record_ok, - &data2); + (binary ? + show_raw_record_ok_binary : + show_raw_record_ok), + &data2, + (binary ? 1 : 0)); if (ret == -1) { http_remove_observer(obs); diff --git a/test/test_http_24.res b/test/test_http_24.res new file mode 100644 index 0000000..a2c38de --- /dev/null +++ b/test/test_http_24.res @@ -0,0 +1 @@ +00362nam 22001698a 4504001001300000003000400013005001700017008004100034010001300075040001300088050001200101100001700113245003000130260001200160263000900172300001100181 11224467 DLC00000000000000.0910710c19910701nju 00010 eng  a11224467 aDLCcDLC00a123-xyz10aJack Collins10aHow to program a computer1 aPenguin a8710 ap. cm. \ No newline at end of file diff --git a/test/test_http_urls b/test/test_http_urls index 86080c9..bc84f81 100644 --- a/test/test_http_urls +++ b/test/test_http_urls @@ -24,3 +24,4 @@ http://localhost:9763/search.pz2?session=2&command=search&query=kubiak%20sts%C5% http://localhost:9763/search.pz2?session=2&command=bytarget http://localhost:9763/search.pz2?session=1&command=search&query=computer http://localhost:9763/search.pz2?session=1&command=record&id=title+how+to+program+a+computer+author+jack+collins+medium+book +http://localhost:9763/search.pz2?session=1&command=record&id=title+how+to+program+a+computer+author+jack+collins+medium+book&offset=0&binary=1 -- 1.7.10.4