Version 5.0.11
[yaz-moved-to-github.git] / src / zoom-c.c
index 5f52d7c..93fb070 100644 (file)
@@ -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;
 }
@@ -542,6 +543,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);
@@ -561,8 +565,6 @@ ZOOM_API(void)
 
 ZOOM_API(void) ZOOM_resultset_release(ZOOM_resultset r)
 {
-#if ZOOM_RESULT_LISTS
-#else
     if (r->connection)
     {
         /* remove ourselves from the resultsets in connection */
@@ -579,36 +581,20 @@ ZOOM_API(void) ZOOM_resultset_release(ZOOM_resultset r)
         }
         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(c->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);
@@ -637,6 +623,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);
 }
@@ -731,9 +718,6 @@ ZOOM_API(ZOOM_resultset)
     const char *cp;
     int start, count;
     const char *syntax, *elementSetName, *schema;
-#if ZOOM_RESULT_LISTS
-    ZOOM_resultsets set;
-#endif
 
     yaz_log(c->log_api, "%p ZOOM_connection_search set %p query %p", c, r, q);
     r->r_sort_spec = ZOOM_query_get_sortspec(q);
@@ -758,18 +742,8 @@ ZOOM_API(ZOOM_resultset)
                                          r->odr);
 
     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)
@@ -1509,43 +1483,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,
@@ -1595,6 +1540,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")))
     {
@@ -1609,9 +1555,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;
         }
     }