Fix segv in ZOOM_connection_fire_event_socket YAZ-858
[yaz-moved-to-github.git] / src / zoom-c.c
index f97123d..3e5d647 100644 (file)
@@ -49,7 +49,6 @@ static void initlog(void)
     }
 }
 
-void ZOOM_connection_remove_tasks(ZOOM_connection c);
 static zoom_ret send_HTTP_redirect(ZOOM_connection c, const char *uri);
 
 void ZOOM_set_dset_error(ZOOM_connection c, int error,
@@ -84,7 +83,6 @@ void ZOOM_set_dset_error(ZOOM_connection c, int error,
                 c, c->host_port ? c->host_port : "<>", dset, error,
                 addinfo ? addinfo : "",
                 addinfo2 ? addinfo2 : "");
-        ZOOM_connection_remove_tasks(c);
     }
 }
 
@@ -266,6 +264,7 @@ ZOOM_API(ZOOM_connection)
     c->host_port = 0;
     c->proxy = 0;
     c->tproxy = 0;
+    c->proxy_mode = 0;
 
     c->charset = c->lang = 0;
 
@@ -1078,8 +1077,11 @@ static zoom_ret do_connect_host(ZOOM_connection c, const char *logical_url)
 
     if (c->cs)
         cs_close(c->cs);
-    c->cs = cs_create_host_proxy(logical_url, CS_FLAGS_DNS_NO_BLOCK, &add,
-                                 c->tproxy ? c->tproxy : c->proxy);
+    c->cs = cs_create_host2(logical_url, CS_FLAGS_DNS_NO_BLOCK, &add,
+                            c->tproxy ? c->tproxy : c->proxy,
+                            &c->proxy_mode);
+    if (!c->proxy)
+        c->proxy_mode = 0;
 
     if (c->cs && c->cs->protocol == PROTO_HTTP)
     {
@@ -1555,11 +1557,23 @@ static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
     char *addinfo = 0;
     const char *connection_head = z_HTTP_header_lookup(hres->headers,
                                                        "Connection");
+    int must_close = 0;
     const char *location;
 
     ZOOM_connection_set_mask(c, 0);
     yaz_log(c->log_details, "%p handle_http", 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"))
+             must_close = 1;
+    }
+    else
+    {
+        /* HTTP 1.1: only if no close we stay alive.. */
+        if (connection_head && !strcmp(connection_head, "close"))
+             must_close = 1;
+    }
     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")))
@@ -1612,33 +1626,18 @@ static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
         yaz_log(c->log_details, "removing tasks in handle_http");
         ZOOM_connection_remove_task(c);
     }
+    if (must_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)
+        ZOOM_connection_close(c);
+        if (c->tasks)
         {
-            ZOOM_connection_close(c);
-            if (c->tasks)
-            {
-                c->tasks->running = 0;
-                ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
-                c->reconnect_ok = 0;
-            }
+            c->tasks->running = 0;
+            ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
+            c->reconnect_ok = 0;
         }
-        else
-            c->reconnect_ok = 1; /* if the server closes anyway */
     }
+    else
+        c->reconnect_ok = 1; /* if the server closes anyway */
 }
 #endif
 
@@ -1675,22 +1674,26 @@ static int do_read(ZOOM_connection c)
 
         if (!z_GDU(c->odr_in, &gdu, 0, 0))
         {
-            int x;
-            int err = odr_geterrorx(c->odr_in, &x);
-            char msg[100];
-            const char *element = odr_getelement(c->odr_in);
-            yaz_snprintf(msg, sizeof(msg),
-                    "ODR code %d:%d element=%s offset=%d",
-                    err, x, element ? element : "<unknown>",
-                    odr_offset(c->odr_in));
-            ZOOM_set_error(c, ZOOM_ERROR_DECODE, msg);
-            if (c->log_api)
+            /* even on failures we try to re-connect (HTTP) */
+            if (!ZOOM_test_reconnect(c))
             {
-                FILE *ber_file = yaz_log_file();
-                if (ber_file)
-                    odr_dumpBER(ber_file, c->buf_in, r);
+                int x;
+                int err = odr_geterrorx(c->odr_in, &x);
+                char msg[100];
+                const char *element = odr_getelement(c->odr_in);
+                yaz_snprintf(msg, sizeof(msg),
+                             "ODR code %d:%d element=%s offset=%d",
+                             err, x, element ? element : "<unknown>",
+                             odr_offset(c->odr_in));
+                ZOOM_set_error(c, ZOOM_ERROR_DECODE, msg);
+                if (c->log_api)
+                {
+                    FILE *ber_file = yaz_log_file();
+                    if (ber_file)
+                        odr_dumpBER(ber_file, c->buf_in, r);
+                }
+                ZOOM_connection_close(c);
             }
-            ZOOM_connection_close(c);
         }
         else
         {