X-Git-Url: http://lists.indexdata.com/cgi-bin?a=blobdiff_plain;f=src%2Fzoom-c.c;h=c3d7f0c945b8f785c093912dd46e23f21033bfc2;hb=d913827c1f2e7c3ace54eb0d8b4c99daed376c2e;hp=2877a16554fda14b17a8c4701afafe7a3e66c1e8;hpb=30b3c16a904efc6f5e0c4326eaa7e13589921572;p=yaz-moved-to-github.git diff --git a/src/zoom-c.c b/src/zoom-c.c index 2877a16..c3d7f0c 100644 --- a/src/zoom-c.c +++ b/src/zoom-c.c @@ -1,5 +1,5 @@ /* This file is part of the YAZ toolkit. - * Copyright (C) 1995-2013 Index Data + * Copyright (C) Index Data * See the file LICENSE for details. */ /** @@ -306,6 +306,7 @@ ZOOM_API(ZOOM_connection) c->sru_version = 0; c->no_redirects = 0; + c->cookies = 0; c->saveAPDU_wrbuf = 0; return c; } @@ -369,6 +370,7 @@ ZOOM_API(void) const char *host, int portnum) { const char *val; + const char *http_lead; initlog(); @@ -435,17 +437,25 @@ ZOOM_API(void) else c->lang = 0; + val = ZOOM_options_get(c->options, "sru"); + if (val && *val && !strstr(host, "://")) + http_lead = "http://"; + else + http_lead = ""; + c->sru_mode = get_sru_mode_from_string(val); + if (host) { + char hostn[128]; xfree(c->host_port); if (portnum) { - char hostn[128]; sprintf(hostn, "%.80s:%d", host, portnum); - c->host_port = xstrdup(hostn); + host = hostn; } - else - c->host_port = xstrdup(host); + c->host_port = xmalloc(strlen(host) + strlen(http_lead) + 1); + strcpy(c->host_port, http_lead); + strcat(c->host_port, host); } { @@ -481,9 +491,6 @@ ZOOM_API(void) } } - val = ZOOM_options_get(c->options, "sru"); - c->sru_mode = get_sru_mode_from_string(val); - xfree(c->sru_version); val = ZOOM_options_get(c->options, "sru_version"); c->sru_version = xstrdup(val ? val : "1.2"); @@ -542,6 +549,9 @@ ZOOM_API(void) c->async = ZOOM_options_get_bool(c->options, "async", 0); + yaz_cookies_destroy(c->cookies); + c->cookies = yaz_cookies_create(); + if (c->sru_mode == zoom_sru_error) { ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, val); @@ -619,6 +629,7 @@ ZOOM_API(void) xfree(c->group); xfree(c->password); xfree(c->sru_version); + yaz_cookies_destroy(c->cookies); wrbuf_destroy(c->saveAPDU_wrbuf); xfree(c); } @@ -1478,43 +1489,14 @@ ZOOM_API(int) #if YAZ_HAVE_XML2 -static zoom_ret send_HTTP_redirect(ZOOM_connection c, const char *uri, - Z_HTTP_Response *cookie_hres) +static zoom_ret send_HTTP_redirect(ZOOM_connection c, const char *uri) { - struct Z_HTTP_Header *h; - char *combined_cookies = 0; - int combined_cookies_len = 0; Z_GDU *gdu = z_get_HTTP_Request_uri(c->odr_out, uri, 0, c->proxy ? 1 : 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")) - { - 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); - } - + yaz_cookies_request(c->cookies, c->odr_out, gdu->u.HTTP_Request); if (c->user && c->password) { z_HTTP_header_add_basic_auth(c->odr_out, &gdu->u.HTTP_Request->headers, @@ -1564,6 +1546,7 @@ static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres) ZOOM_connection_set_mask(c, 0); yaz_log(c->log_details, "%p handle_http", c); + yaz_cookies_response(c->cookies, hres); if ((hres->code == 301 || hres->code == 302) && c->sru_mode == zoom_sru_get && (location = z_HTTP_header_lookup(hres->headers, "Location"))) { @@ -1578,9 +1561,13 @@ static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres) { /* since redirect may change host we just reconnect. A smarter implementation might check whether it's the same server */ - do_connect_host(c, location); - send_HTTP_redirect(c, location, hres); - /* we're OK for now. Operation is not really complete */ + + int host_change = 0; + location = yaz_check_location(c->odr_in, c->host_port, + location, &host_change); + if (do_connect_host(c, location) == zoom_complete) + return; /* connect failed.. */ + send_HTTP_redirect(c, location); return; } }