ZOOM: new setting apdufile
[yaz-moved-to-github.git] / src / zoom-c.c
index b1f5aca..264e2eb 100644 (file)
@@ -361,7 +361,6 @@ ZOOM_API(void)
                             const char *host, int portnum)
 {
     const char *val;
-    const char *http_lead;
 
     initlog();
 
@@ -371,19 +370,6 @@ ZOOM_API(void)
     ZOOM_set_error(c, ZOOM_ERROR_NONE, 0);
     ZOOM_connection_remove_tasks(c);
 
-    if (c->odr_print)
-    {
-        odr_setprint(c->odr_print, 0); /* prevent destroy from fclose'ing */
-        odr_destroy(c->odr_print);
-    }
-    if (ZOOM_options_get_bool(c->options, "apdulog", 0))
-    {
-        c->odr_print = odr_createmem(ODR_PRINT);
-        odr_setprint(c->odr_print, yaz_log_file());
-    }
-    else
-        c->odr_print = 0;
-
     if (c->cs)
     {
         yaz_log(c->log_details, "%p ZOOM_connection_connect reconnect ok", c);
@@ -428,16 +414,18 @@ 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];
+        const char *http_lead;
+
+        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);
+
         xfree(c->host_port);
         if (portnum)
         {
@@ -555,6 +543,33 @@ ZOOM_API(void)
         return;
     }
 
+    if (c->odr_print)
+        odr_destroy(c->odr_print);
+    c->odr_print = 0;
+    val = ZOOM_options_get(c->options, "apdufile");
+    if (val)
+    {
+        c->odr_print = odr_createmem(ODR_PRINT);
+        if (strcmp(val, "-"))
+        {
+            FILE *f = fopen(val, "a");
+            if (!f)
+            {
+                WRBUF w = wrbuf_alloc();
+                wrbuf_printf(w, "fopen: %s", val);
+                ZOOM_set_error(c, ZOOM_ERROR_INTERNAL, wrbuf_cstr(w));
+                wrbuf_destroy(w);
+                return;
+            }
+            odr_setprint(c->odr_print, f);
+        }
+    }
+    else if (ZOOM_options_get_bool(c->options, "apdulog", 0))
+    {
+        c->odr_print = odr_createmem(ODR_PRINT);
+        odr_setprint_noclose(c->odr_print, yaz_log_file());
+    }
+
     yaz_log(c->log_details, "%p ZOOM_connection_connect async=%d", c, c->async);
     ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
 
@@ -608,10 +623,7 @@ ZOOM_API(void)
     if (c->odr_save)
         odr_destroy(c->odr_save);
     if (c->odr_print)
-    {
-        odr_setprint(c->odr_print, 0); /* prevent destroy from fclose'ing */
         odr_destroy(c->odr_print);
-    }
     ZOOM_options_destroy(c->options);
     ZOOM_connection_remove_tasks(c);
     ZOOM_connection_remove_events(c);
@@ -1063,7 +1075,7 @@ 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, 0, &add,
+    c->cs = cs_create_host_proxy(logical_url, CS_FLAGS_DNS_NO_BLOCK, &add,
                                  c->tproxy ? c->tproxy : c->proxy);
 
     if (c->cs && c->cs->protocol == PROTO_HTTP)
@@ -1562,6 +1574,7 @@ static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
                                           location, &host_change);
             if (do_connect_host(c, location) == zoom_complete)
                 return;  /* connect failed.. */
+            cs_rcvconnect(c->cs);
             send_HTTP_redirect(c, location);
             return;
         }
@@ -1959,7 +1972,7 @@ static void ZOOM_connection_do_io(ZOOM_connection c, int mask)
             if (c->cs->io_pending & CS_WANT_READ)
                 mask += ZOOM_SELECT_READ;
             ZOOM_connection_set_mask(c, mask);
-            event = ZOOM_Event_create(ZOOM_EVENT_NONE);
+            event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
             ZOOM_connection_put_event(c, event);
         }
         else if (ret == 0)
@@ -1972,9 +1985,11 @@ static void ZOOM_connection_do_io(ZOOM_connection c, int mask)
             else
             {
                 /* no init request for SRW .. */
-                assert(c->tasks->which == ZOOM_TASK_CONNECT);
-                ZOOM_connection_remove_task(c);
-                ZOOM_connection_set_mask(c, 0);
+                if (c->tasks->which == ZOOM_TASK_CONNECT)
+                {
+                    ZOOM_connection_remove_task(c);
+                    ZOOM_connection_set_mask(c, 0);
+                }
                 ZOOM_connection_exec_task(c);
             }
             c->state = STATE_ESTABLISHED;