X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Fzoom-c.c;h=56d631f435715028e351fc385698e95d57a87e6e;hp=bedb9955b1560ad8d37c0ce8de46b0c4eada5135;hb=3d536728dae01844f9a2b89337eb3b3fdb759382;hpb=ad4b64e5b3ab9ebc4b2e8184a3782b10962f7d3d diff --git a/src/zoom-c.c b/src/zoom-c.c index bedb995..56d631f 100644 --- a/src/zoom-c.c +++ b/src/zoom-c.c @@ -28,6 +28,12 @@ #include #include +#include + +#if SHPTR +YAZ_SHPTR_TYPE(WRBUF) +#endif + static int log_api = 0; static int log_details = 0; @@ -787,17 +793,56 @@ ZOOM_API(int) static zoom_ret do_write(ZOOM_connection c); +ZOOM_API(void) ZOOM_resultset_release(ZOOM_resultset r) +{ +#if ZOOM_RESULT_LISTS +#else + if (r->connection) + { + /* remove ourselves from the resultsets in connection */ + ZOOM_resultset *rp = &r->connection->resultsets; + while (1) + { + assert(*rp); /* we must be in this list!! */ + if (*rp == r) + { /* OK, we're here - take us out of it */ + *rp = (*rp)->next; + break; + } + rp = &(*rp)->next; + } + r->connection = 0; + } +#endif +} + ZOOM_API(void) ZOOM_connection_destroy(ZOOM_connection c) { +#if ZOOM_RESULT_LISTS + ZOOM_resultsets list; +#else ZOOM_resultset r; +#endif if (!c) return; yaz_log(log_api, "%p ZOOM_connection_destroy", c); if (c->cs) cs_close(c->cs); + +#if ZOOM_RESULT_LISTS + // Remove the connection's usage of resultsets + list = c->resultsets; + while (list) { + ZOOM_resultsets removed = list; + ZOOM_resultset_destroy(list->resultset); + list = list->next; + xfree(removed); + } +#else for (r = c->resultsets; r; r = r->next) r->connection = 0; +#endif xfree(c->buf_in); xfree(c->addinfo); @@ -831,9 +876,11 @@ void ZOOM_resultset_addref(ZOOM_resultset r) { if (r) { + yaz_mutex_enter(r->mutex); (r->refcount)++; yaz_log(log_details, "%p ZOOM_resultset_addref count=%d", r, r->refcount); + yaz_mutex_leave(r->mutex); } } @@ -857,9 +904,16 @@ ZOOM_resultset ZOOM_resultset_create(void) r->r_sort_spec = 0; r->query = 0; r->connection = 0; - r->next = 0; r->databaseNames = 0; r->num_databaseNames = 0; + r->mutex = 0; + yaz_mutex_create(&r->mutex); +#if SHPTR + { + WRBUF w = wrbuf_alloc(); + YAZ_SHPTR_INIT(r->record_wrbuf, w); + } +#endif return r; } @@ -884,6 +938,9 @@ ZOOM_API(ZOOM_resultset) const char *cp; int start, count; const char *syntax, *elementSetName; +#if ZOOM_RESULT_LISTS + ZOOM_resultsets set; +#endif yaz_log(log_api, "%p ZOOM_connection_search set %p query %p", c, r, q); r->r_sort_spec = q->sort_spec; @@ -912,11 +969,17 @@ ZOOM_API(ZOOM_resultset) r->connection = c; +#if ZOOM_RESULT_LISTS + yaz_log(log_details, "%p ZOOM_connection_search: Adding new resultset (%p) to resultsets (%p) ", c, r, c->resultsets); + set = xmalloc(sizeof(*set)); + ZOOM_resultset_addref(r); + set->resultset = r; + set->next = c->resultsets; + c->resultsets = set; +#else r->next = c->resultsets; c->resultsets = r; - - - +#endif if (c->host_port && c->proto == PROTO_HTTP) { if (!c->cs) @@ -1014,12 +1077,15 @@ static void ZOOM_record_release(ZOOM_record rec) { if (!rec) return; + +#if SHPTR + if (rec->record_wrbuf) + YAZ_SHPTR_DEC(rec->record_wrbuf, wrbuf_destroy); +#else if (rec->wrbuf) wrbuf_destroy(rec->wrbuf); -#if YAZ_HAVE_XML2 - if (rec->xml_mem) - xmlFree(rec->xml_mem); #endif + if (rec->odr) odr_destroy(rec->odr); } @@ -1049,35 +1115,30 @@ static void resultset_destroy(ZOOM_resultset r) { if (!r) return; + yaz_mutex_enter(r->mutex); (r->refcount)--; yaz_log(log_details, "%p ZOOM_resultset_destroy r=%p count=%d", r, r, r->refcount); if (r->refcount == 0) { - ZOOM_resultset_cache_reset(r); + yaz_mutex_leave(r->mutex); - if (r->connection) - { - /* remove ourselves from the resultsets in connection */ - ZOOM_resultset *rp = &r->connection->resultsets; - while (1) - { - assert(*rp); /* we must be in this list!! */ - if (*rp == r) - { /* OK, we're here - take us out of it */ - *rp = (*rp)->next; - break; - } - rp = &(*rp)->next; - } - } + yaz_log(log_details, "%p ZOOM_connection resultset_destroy: Deleting resultset (%p) ", r->connection, r); + ZOOM_resultset_cache_reset(r); + ZOOM_resultset_release(r); ZOOM_query_destroy(r->query); ZOOM_options_destroy(r->options); odr_destroy(r->odr); xfree(r->setname); xfree(r->schema); + yaz_mutex_destroy(&r->mutex); +#if SHPTR + YAZ_SHPTR_DEC(r->record_wrbuf, wrbuf_destroy); +#endif xfree(r); } + else + yaz_mutex_leave(r->mutex); } ZOOM_API(size_t) @@ -1736,6 +1797,15 @@ static zoom_ret ZOOM_connection_send_search(ZOOM_connection c) result sets on the server. */ for (ord = 1; ; ord++) { +#if ZOOM_RESULT_LISTS + ZOOM_resultsets rsp; + sprintf(setname, "%d", ord); + for (rsp = c->resultsets; rsp; rsp = rsp->next) + if (rsp->resultset->setname && !strcmp(rsp->resultset->setname, setname)) + break; + if (!rsp) + break; +#else ZOOM_resultset rp; sprintf(setname, "%d", ord); for (rp = c->resultsets; rp; rp = rp->next) @@ -1743,6 +1813,8 @@ static zoom_ret ZOOM_connection_send_search(ZOOM_connection c) break; if (!rp) break; +#endif + } r->setname = xstrdup(setname); yaz_log(log_details, "%p ZOOM_connection_send_search: allocating " @@ -1803,11 +1875,12 @@ ZOOM_API(ZOOM_record) buf = odr_getbuf(odr_enc, &size, 0); nrec = (ZOOM_record) xmalloc(sizeof(*nrec)); + yaz_log(log_details, "ZOOM_record create"); nrec->odr = odr_createmem(ODR_DECODE); +#if SHPTR + nrec->record_wrbuf = 0; +#else nrec->wrbuf = 0; -#if YAZ_HAVE_XML2 - nrec->xml_mem = 0; - nrec->xml_size = 0; #endif odr_setbuf(nrec->odr, buf, size, 0); z_NamePlusRecord(nrec->odr, &nrec->npr, 0, 0); @@ -1857,6 +1930,7 @@ ZOOM_API(void) ZOOM_record_destroy(ZOOM_record rec) { ZOOM_record_release(rec); + yaz_log(log_details, "ZOOM_record destroy"); xfree(rec); } @@ -1892,7 +1966,8 @@ static yaz_iconv_t iconv_create_charset(const char *record_charset) return cd; } -static const char *return_marc_record(ZOOM_record rec, int marc_type, +static const char *return_marc_record(WRBUF wrbuf, + int marc_type, int *len, const char *buf, int sz, const char *record_charset) @@ -1904,14 +1979,11 @@ static const char *return_marc_record(ZOOM_record rec, int marc_type, if (cd) yaz_marc_iconv(mt, cd); yaz_marc_xml(mt, marc_type); - if (!rec->wrbuf) - rec->wrbuf = wrbuf_alloc(); - wrbuf_rewind(rec->wrbuf); - if (yaz_marc_decode_wrbuf(mt, buf, sz, rec->wrbuf) > 0) + if (yaz_marc_decode_wrbuf(mt, buf, sz, wrbuf) > 0) { if (len) - *len = wrbuf_len(rec->wrbuf); - ret_string = wrbuf_cstr(rec->wrbuf); + *len = wrbuf_len(wrbuf); + ret_string = wrbuf_cstr(wrbuf); } yaz_marc_destroy(mt); if (cd) @@ -1919,7 +1991,8 @@ static const char *return_marc_record(ZOOM_record rec, int marc_type, return ret_string; } -static const char *return_opac_record(ZOOM_record rec, int marc_type, +static const char *return_opac_record(WRBUF wrbuf, + int marc_type, int *len, Z_OPACRecord *opac_rec, const char *record_charset) @@ -1931,21 +2004,18 @@ static const char *return_opac_record(ZOOM_record rec, int marc_type, yaz_marc_iconv(mt, cd); yaz_marc_xml(mt, marc_type); - if (!rec->wrbuf) - rec->wrbuf = wrbuf_alloc(); - wrbuf_rewind(rec->wrbuf); - - yaz_opac_decode_wrbuf(mt, opac_rec, rec->wrbuf); + yaz_opac_decode_wrbuf(mt, opac_rec, wrbuf); yaz_marc_destroy(mt); if (cd) yaz_iconv_close(cd); if (len) - *len = wrbuf_len(rec->wrbuf); - return wrbuf_cstr(rec->wrbuf); + *len = wrbuf_len(wrbuf); + return wrbuf_cstr(wrbuf); } -static const char *return_string_record(ZOOM_record rec, int *len, +static const char *return_string_record(WRBUF wrbuf, + int *len, const char *buf, int sz, const char *record_charset) { @@ -1953,16 +2023,11 @@ static const char *return_string_record(ZOOM_record rec, int *len, if (cd) { - if (!rec->wrbuf) - rec->wrbuf = wrbuf_alloc(); + wrbuf_iconv_write(wrbuf, cd, buf, sz); + wrbuf_iconv_reset(wrbuf, cd); - wrbuf_rewind(rec->wrbuf); - - wrbuf_iconv_write(rec->wrbuf, cd, buf, sz); - wrbuf_iconv_reset(rec->wrbuf, cd); - - buf = wrbuf_cstr(rec->wrbuf); - sz = wrbuf_len(rec->wrbuf); + buf = wrbuf_cstr(wrbuf); + sz = wrbuf_len(wrbuf); yaz_iconv_close(cd); } if (len) @@ -1970,21 +2035,22 @@ static const char *return_string_record(ZOOM_record rec, int *len, return buf; } -static const char *return_record(ZOOM_record rec, int *len, - Z_NamePlusRecord *npr, - int marctype, const char *charset) +static const char *return_record_wrbuf(WRBUF wrbuf, int *len, + Z_NamePlusRecord *npr, + int marctype, const char *charset) { Z_External *r = (Z_External *) npr->u.databaseRecord; const Odr_oid *oid = r->direct_reference; - + + wrbuf_rewind(wrbuf); /* render bibliographic record .. */ if (r->which == Z_External_OPAC) { - return return_opac_record(rec, marctype, len, + return return_opac_record(wrbuf, marctype, len, r->u.opac, charset); } if (r->which == Z_External_sutrs) - return return_string_record(rec, len, + return return_string_record(wrbuf, len, (char*) r->u.sutrs->buf, r->u.sutrs->len, charset); @@ -1993,7 +2059,7 @@ static const char *return_record(ZOOM_record rec, int *len, if (yaz_oid_is_iso2709(oid)) { const char *ret_buf = return_marc_record( - rec, marctype, len, + wrbuf, marctype, len, (const char *) r->u.octet_aligned->buf, r->u.octet_aligned->len, charset); @@ -2003,26 +2069,22 @@ static const char *return_record(ZOOM_record rec, int *len, if (marctype != YAZ_MARC_ISO2709) return 0; } - return return_string_record(rec, len, + return return_string_record(wrbuf, len, (const char *) r->u.octet_aligned->buf, r->u.octet_aligned->len, charset); } else if (r->which == Z_External_grs1) { - if (!rec->wrbuf) - rec->wrbuf = wrbuf_alloc(); - wrbuf_rewind(rec->wrbuf); - yaz_display_grs1(rec->wrbuf, r->u.grs1, 0); - return return_string_record(rec, len, - wrbuf_buf(rec->wrbuf), - wrbuf_len(rec->wrbuf), + yaz_display_grs1(wrbuf, r->u.grs1, 0); + return return_string_record(wrbuf, len, + wrbuf_buf(wrbuf), + wrbuf_len(wrbuf), charset); } return 0; } - ZOOM_API(int) ZOOM_record_error(ZOOM_record rec, const char **cp, const char **addinfo, const char **diagset) @@ -2083,12 +2145,12 @@ ZOOM_API(int) return 0; } -static const char *get_record_format(ZOOM_record rec, int *len, +static const char *get_record_format(WRBUF wrbuf, int *len, Z_NamePlusRecord *npr, int marctype, const char *charset, const char *format) { - const char *res = return_record(rec, len, npr, marctype, charset); + const char *res = return_record_wrbuf(wrbuf, len, npr, marctype, charset); #if YAZ_HAVE_XML2 if (*format == '1' && len) { @@ -2098,12 +2160,15 @@ static const char *get_record_format(ZOOM_record rec, int *len, doc = xmlParseMemory(res, *len); if (doc) { - if (rec->xml_mem) - xmlFree(rec->xml_mem); - xmlDocDumpFormatMemory(doc, &rec->xml_mem, &rec->xml_size, 1); + xmlChar *xml_mem; + int xml_size; + xmlDocDumpFormatMemory(doc, &xml_mem, &xml_size, 1); + wrbuf_rewind(wrbuf); + wrbuf_write(wrbuf, (const char *) xml_mem, xml_size); + xmlFree(xml_mem); xmlFreeDoc(doc); - res = (char *) rec->xml_mem; - *len = rec->xml_size; + res = wrbuf_cstr(wrbuf); + *len = wrbuf_len(wrbuf); } } #endif @@ -2111,26 +2176,16 @@ static const char *get_record_format(ZOOM_record rec, int *len, } -ZOOM_API(const char *) - ZOOM_record_get(ZOOM_record rec, const char *type_spec, int *len) +static const char *npr_format(Z_NamePlusRecord *npr, const char *schema, + WRBUF wrbuf, + const char *type_spec, int *len) { + size_t i; char type[40]; char charset[40]; char format[3]; - const char *cp; - size_t i; - Z_NamePlusRecord *npr; - - if (len) - *len = 0; /* default return */ - - if (!rec) - return 0; - npr = rec->npr; - if (!npr) - return 0; + const char *cp = type_spec; - cp = type_spec; for (i = 0; cp[i] && cp[i] != ';' && cp[i] != ' ' && i < sizeof(type)-1; i++) type[i] = cp[i]; @@ -2178,8 +2233,8 @@ ZOOM_API(const char *) else if (!strcmp(type, "schema")) { if (len) - *len = rec->schema ? strlen(rec->schema) : 0; - return rec->schema; + *len = schema ? strlen(schema) : 0; + return schema; } else if (!strcmp(type, "syntax")) { @@ -2201,21 +2256,21 @@ ZOOM_API(const char *) /* from now on - we have a database record .. */ if (!strcmp(type, "render")) { - return get_record_format(rec, len, npr, YAZ_MARC_LINE, charset, format); + return get_record_format(wrbuf, len, npr, YAZ_MARC_LINE, charset, format); } else if (!strcmp(type, "xml")) { - return get_record_format(rec, len, npr, YAZ_MARC_MARCXML, charset, + return get_record_format(wrbuf, len, npr, YAZ_MARC_MARCXML, charset, format); } else if (!strcmp(type, "txml")) { - return get_record_format(rec, len, npr, YAZ_MARC_TURBOMARC, charset, + return get_record_format(wrbuf, len, npr, YAZ_MARC_TURBOMARC, charset, format); } else if (!strcmp(type, "raw")) { - return get_record_format(rec, len, npr, YAZ_MARC_ISO2709, charset, + return get_record_format(wrbuf, len, npr, YAZ_MARC_ISO2709, charset, format); } else if (!strcmp(type, "ext")) @@ -2226,12 +2281,38 @@ ZOOM_API(const char *) else if (!strcmp(type, "opac")) { if (npr->u.databaseRecord->which == Z_External_OPAC) - return get_record_format(rec, len, npr, YAZ_MARC_MARCXML, charset, - format); + return get_record_format(wrbuf, len, npr, YAZ_MARC_MARCXML, charset, + format); } return 0; } +ZOOM_API(const char *) + ZOOM_record_get(ZOOM_record rec, const char *type_spec, int *len) +{ + WRBUF wrbuf; + + if (len) + *len = 0; /* default return */ + + if (!rec || !rec->npr) + return 0; + +#if SHPTR + if (!rec->record_wrbuf) + { + WRBUF w = wrbuf_alloc(); + YAZ_SHPTR_INIT(rec->record_wrbuf, w); + } + wrbuf = rec->record_wrbuf->ptr; +#else + if (!rec->wrbuf) + rec->wrbuf = wrbuf_alloc(); + wrbuf = rec->wrbuf; +#endif + return npr_format(rec->npr, rec->schema, wrbuf, type_spec, len); +} + static int strcmp_null(const char *v1, const char *v2) { if (!v1 && !v2) @@ -2271,9 +2352,11 @@ static void record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr, { rc = (ZOOM_record_cache) odr_malloc(r->odr, sizeof(*rc)); rc->rec.odr = 0; +#if SHPTR + YAZ_SHPTR_INC(r->record_wrbuf); + rc->rec.record_wrbuf = r->record_wrbuf; +#else rc->rec.wrbuf = 0; -#if YAZ_HAVE_XML2 - rc->rec.xml_mem = 0; #endif rc->elementSetName = odr_strdup_null(r->odr, elementSetName); @@ -2903,7 +2986,7 @@ static zoom_ret ZOOM_connection_send_scan(ZOOM_connection c) } *req->numberOfTermsRequested = - ZOOM_options_get_int(scan->options, "number", 10); + ZOOM_options_get_int(scan->options, "number", 20); req->preferredPositionInResponse = odr_intdup(c->odr_out, @@ -4158,17 +4241,38 @@ static zoom_ret send_SRW_redirect(ZOOM_connection c, const char *uri, { struct Z_HTTP_Header *h; Z_GDU *gdu = get_HTTP_Request_url(c->odr_out, uri); + char *combined_cookies; + int combined_cookies_len = 0; gdu->u.HTTP_Request->method = odr_strdup(c->odr_out, "GET"); z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers, "Accept", "text/xml"); - + for (h = cookie_hres->headers; h; h = h->next) { if (!strcmp(h->name, "Set-Cookie")) - z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers, - "Cookie", h->value); + { + char *cp; + + if (!(cp = strchr(h->value, ';'))) + cp = h->value + strlen(h->value); + if (cp - h->value >= 1) { + combined_cookies = xrealloc(combined_cookies, combined_cookies_len + cp - h->value + 3); + memcpy(combined_cookies+combined_cookies_len, h->value, cp - h->value); + combined_cookies[combined_cookies_len + cp - h->value] = '\0'; + strcat(combined_cookies,"; "); + combined_cookies_len = strlen(combined_cookies); + } + } + } + + if (combined_cookies_len) + { + z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers, + "Cookie", combined_cookies); + xfree(combined_cookies); } + if (c->user && c->password) { z_HTTP_header_add_basic_auth(c->odr_out, &gdu->u.HTTP_Request->headers, @@ -4273,18 +4377,34 @@ static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres) do_close(c); } if (cret == zoom_complete) - ZOOM_connection_remove_task(c); - if (!strcmp(hres->version, "1.0")) { - /* HTTP 1.0: only if Keep-Alive we stay alive.. */ - if (!connection_head || strcmp(connection_head, "Keep-Alive")) - do_close(c); + yaz_log(YLOG_LOG, "removing tasks in handle_http"); + ZOOM_connection_remove_task(c); } - else { - /* HTTP 1.1: only if no close we stay alive .. */ - if (connection_head && !strcmp(connection_head, "close")) + int must_close = 0; + if (!strcmp(hres->version, "1.0")) + { + /* HTTP 1.0: only if Keep-Alive we stay alive.. */ + if (!connection_head || strcmp(connection_head, "Keep-Alive")) + must_close = 1; + } + else + { + /* HTTP 1.1: only if no close we stay alive.. */ + if (connection_head && !strcmp(connection_head, "close")) + must_close = 1; + } + if (must_close) + { do_close(c); + if (c->tasks) + { + c->tasks->running = 0; + ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT); + c->reconnect_ok = 0; + } + } } } #endif