Fix ZOOM: crash extended services diagnostics YAZ-846
[yaz-moved-to-github.git] / src / zoom-c.c
index b162b6d..b6cc643 100644 (file)
@@ -49,7 +49,7 @@ 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,
                          const char *dset,
@@ -83,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);
     }
 }
 
@@ -96,8 +95,6 @@ int ZOOM_uri_to_code(const char *uri)
     return code;
 }
 
-
-
 void ZOOM_set_error(ZOOM_connection c, int error, const char *addinfo)
 {
     ZOOM_set_dset_error(c, error, "ZOOM", addinfo, 0);
@@ -136,9 +133,6 @@ void ZOOM_connection_show_task(ZOOM_task task)
     case ZOOM_TASK_SEARCH:
         yaz_log(YLOG_LOG, "search p=%p", task);
         break;
-    case ZOOM_TASK_RETRIEVE:
-        yaz_log(YLOG_LOG, "retrieve p=%p", task);
-        break;
     case ZOOM_TASK_CONNECT:
         yaz_log(YLOG_LOG, "connect p=%p", task);
         break;
@@ -193,6 +187,8 @@ void ZOOM_connection_remove_task(ZOOM_connection c)
     if (task)
     {
         c->tasks = task->next;
+        yaz_log(YLOG_LOG, "remove task p=%d type=%d next=%p",
+                task, (int) task->which, c->tasks);
         switch (task->which)
         {
         case ZOOM_TASK_SEARCH:
@@ -201,12 +197,6 @@ void ZOOM_connection_remove_task(ZOOM_connection c)
             xfree(task->u.search.elementSetName);
             xfree(task->u.search.schema);
             break;
-        case ZOOM_TASK_RETRIEVE:
-            resultset_destroy(task->u.retrieve.resultset);
-            xfree(task->u.retrieve.syntax);
-            xfree(task->u.retrieve.elementSetName);
-            xfree(task->u.retrieve.schema);
-            break;
         case ZOOM_TASK_CONNECT:
             break;
         case ZOOM_TASK_SCAN:
@@ -276,6 +266,7 @@ ZOOM_API(ZOOM_connection)
     c->host_port = 0;
     c->proxy = 0;
     c->tproxy = 0;
+    c->proxy_mode = 0;
 
     c->charset = c->lang = 0;
 
@@ -307,7 +298,10 @@ ZOOM_API(ZOOM_connection)
     c->sru_version = 0;
     c->no_redirects = 0;
     c->cookies = 0;
+    c->location = 0;
     c->saveAPDU_wrbuf = 0;
+
+    ZOOM_memcached_init(c);
     return c;
 }
 
@@ -379,19 +373,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);
@@ -438,15 +419,25 @@ ZOOM_API(void)
 
     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)
         {
-            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);
     }
 
     {
@@ -482,9 +473,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");
@@ -546,6 +534,11 @@ ZOOM_API(void)
     yaz_cookies_destroy(c->cookies);
     c->cookies = yaz_cookies_create();
 
+    if (ZOOM_memcached_configure(c))
+    {
+        ZOOM_connection_remove_tasks(c);
+        return;
+    }
     if (c->sru_mode == zoom_sru_error)
     {
         ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, val);
@@ -553,6 +546,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);
 
@@ -590,6 +610,8 @@ ZOOM_API(void)
     if (!c)
         return;
     yaz_log(c->log_api, "%p ZOOM_connection_destroy", c);
+
+    ZOOM_memcached_destroy(c);
     if (c->cs)
         cs_close(c->cs);
 
@@ -604,10 +626,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);
@@ -623,6 +642,7 @@ ZOOM_API(void)
     xfree(c->group);
     xfree(c->password);
     xfree(c->sru_version);
+    xfree(c->location);
     yaz_cookies_destroy(c->cookies);
     wrbuf_destroy(c->saveAPDU_wrbuf);
     xfree(c);
@@ -671,7 +691,7 @@ ZOOM_resultset ZOOM_resultset_create(void)
     yaz_log(log_details0, "%p ZOOM_resultset_create", r);
     r->refcount = 1;
     r->size = 0;
-    r->odr = odr_createmem(ODR_ENCODE);
+    r->odr = odr_createmem(ODR_DECODE);
     r->piggyback = 1;
     r->setname = 0;
     r->step = 0;
@@ -682,8 +702,9 @@ ZOOM_resultset ZOOM_resultset_create(void)
     r->connection = 0;
     r->databaseNames = 0;
     r->num_databaseNames = 0;
-    r->facets = 0;
-    r->num_facets = 0;
+    r->req_facets = 0;
+    r->res_facets = 0;
+    r->num_res_facets = 0;
     r->facets_names = 0;
     r->mutex = 0;
     yaz_mutex_create(&r->mutex);
@@ -694,6 +715,8 @@ ZOOM_resultset ZOOM_resultset_create(void)
     }
 #endif
     resultset_use(1);
+    r->mc_key = 0;
+    r->live_set = 0;
     return r;
 }
 
@@ -715,16 +738,17 @@ ZOOM_API(ZOOM_resultset)
 {
     ZOOM_resultset r = ZOOM_resultset_create();
     ZOOM_task task;
-    const char *cp;
     int start, count;
     const char *syntax, *elementSetName, *schema;
-
     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);
     r->query = q;
+    ZOOM_query_addref(q);
 
     r->options = ZOOM_options_create_with_parent(c->options);
 
+    r->req_facets = odr_strdup_null(r->odr,
+                                    ZOOM_options_get(r->options, "facets"));
     start = ZOOM_options_get_int(r->options, "start", 0);
     count = ZOOM_options_get_int(r->options, "count", 0);
     {
@@ -734,16 +758,17 @@ ZOOM_API(ZOOM_resultset)
                                        (cp != 0 ? "presentChunk": "step"), 0);
     }
     r->piggyback = ZOOM_options_get_bool(r->options, "piggyback", 1);
-    cp = ZOOM_options_get(r->options, "setname");
-    if (cp)
-        r->setname = xstrdup(cp);
-
-    r->databaseNames = ZOOM_connection_get_databases(c, c->options, &r->num_databaseNames,
-                                         r->odr);
-
+    r->setname = odr_strdup_null(r->odr,
+                                 ZOOM_options_get(r->options, "setname"));
+    r->databaseNames = ZOOM_connection_get_databases(c, c->options,
+                                                     &r->num_databaseNames,
+                                                     r->odr);
     r->connection = c;
     r->next = c->resultsets;
     c->resultsets = r;
+
+    ZOOM_memcached_resultset(r, q);
+
     if (c->host_port && c->proto == PROTO_HTTP)
     {
         if (!c->cs)
@@ -762,7 +787,6 @@ ZOOM_API(ZOOM_resultset)
     task->u.search.resultset = r;
     task->u.search.start = start;
     task->u.search.count = count;
-    task->u.search.recv_search_fired = 0;
 
     syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
     task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
@@ -774,8 +798,6 @@ ZOOM_API(ZOOM_resultset)
 
     ZOOM_resultset_addref(r);
 
-    ZOOM_query_addref(q);
-
     if (!c->async)
     {
         while (ZOOM_event(1, &c))
@@ -863,11 +885,11 @@ static void resultset_destroy(ZOOM_resultset r)
         ZOOM_query_destroy(r->query);
         ZOOM_options_destroy(r->options);
         odr_destroy(r->odr);
-        xfree(r->setname);
         yaz_mutex_destroy(&r->mutex);
 #if SHPTR
         YAZ_SHPTR_DEC(r->record_wrbuf, wrbuf_destroy);
 #endif
+        wrbuf_destroy(r->mc_key);
         resultset_use(-1);
         xfree(r);
     }
@@ -928,19 +950,19 @@ static void ZOOM_resultset_retrieve(ZOOM_resultset r,
             c->reconnect_ok = 1;
         }
     }
-    task = ZOOM_connection_add_task(c, ZOOM_TASK_RETRIEVE);
-    task->u.retrieve.resultset = r;
-    task->u.retrieve.start = start;
-    task->u.retrieve.count = count;
+    task = ZOOM_connection_add_task(c, ZOOM_TASK_SEARCH);
+    task->u.search.resultset = r;
+    task->u.search.start = start;
+    task->u.search.count = count;
 
     syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
-    task->u.retrieve.syntax = syntax ? xstrdup(syntax) : 0;
+    task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
     elementSetName = ZOOM_options_get(r->options, "elementSetName");
-    task->u.retrieve.elementSetName = elementSetName
+    task->u.search.elementSetName = elementSetName
         ? xstrdup(elementSetName) : 0;
 
     cp = ZOOM_options_get(r->options, "schema");
-    task->u.retrieve.schema = cp ? xstrdup(cp) : 0;
+    task->u.search.schema = cp ? xstrdup(cp) : 0;
 
     ZOOM_resultset_addref(r);
 
@@ -971,37 +993,37 @@ ZOOM_API(void)
 }
 
 ZOOM_API(size_t)
-    ZOOM_resultset_facets_size(ZOOM_resultset r) {
-    return r->num_facets;
+    ZOOM_resultset_facets_size(ZOOM_resultset r)
+{
+    return r->num_res_facets;
 }
 
 ZOOM_API(ZOOM_facet_field)
-    ZOOM_resultset_get_facet_field(ZOOM_resultset r, const char *name) {
-    int num = r->num_facets;
-    ZOOM_facet_field *facets = r->facets;
-    int index;
-    for (index = 0; index < num; index++) {
-        if (!strcmp(facets[index]->facet_name, name)) {
-            return facets[index];
-        }
-    }
+    ZOOM_resultset_get_facet_field(ZOOM_resultset r, const char *name)
+{
+    int num = r->num_res_facets;
+    ZOOM_facet_field *facets = r->res_facets;
+    int i;
+    for (i = 0; i < num; i++)
+        if (!strcmp(facets[i]->facet_name, name))
+            return facets[i];
     return 0;
 }
 
 ZOOM_API(ZOOM_facet_field)
-    ZOOM_resultset_get_facet_field_by_index(ZOOM_resultset r, int index) {
-    int num = r->num_facets;
-    ZOOM_facet_field *facets = r->facets;
-    if (index >= 0 && index < num) {
-        return facets[index];
-    }
+    ZOOM_resultset_get_facet_field_by_index(ZOOM_resultset r, int idx)
+{
+    int num = r->num_res_facets;
+    ZOOM_facet_field *facets = r->res_facets;
+    if (idx >= 0 && idx < num)
+        return facets[idx];
     return 0;
 }
 
 ZOOM_API(ZOOM_facet_field *)
     ZOOM_resultset_facets(ZOOM_resultset r)
 {
-    return r->facets;
+    return r->res_facets;
 }
 
 ZOOM_API(const char**)
@@ -1023,7 +1045,8 @@ ZOOM_API(size_t)
 }
 
 ZOOM_API(const char*)
-    ZOOM_facet_field_get_term(ZOOM_facet_field field, size_t idx, int *freq) {
+    ZOOM_facet_field_get_term(ZOOM_facet_field field, size_t idx, int *freq)
+{
     *freq = field->facet_terms[idx].frequency;
     return field->facet_terms[idx].term;
 }
@@ -1056,8 +1079,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, 0, &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)
     {
@@ -1085,7 +1111,11 @@ static zoom_ret do_connect_host(ZOOM_connection c, const char *logical_url)
                 assert(c->tasks->which == ZOOM_TASK_CONNECT);
                 ZOOM_connection_remove_task(c);
                 ZOOM_connection_set_mask(c, 0);
-                ZOOM_connection_exec_task(c);
+
+                if (c->cs && c->location)
+                    send_HTTP_redirect(c, c->location);
+                else
+                    ZOOM_connection_exec_task(c);
             }
             c->state = STATE_ESTABLISHED;
             return zoom_pending;
@@ -1121,7 +1151,7 @@ ZOOM_API(ZOOM_record)
     const char *schema =
         ZOOM_options_get(s->options, "schema");
 
-    return ZOOM_record_cache_lookup(s, pos, syntax, elementSetName, schema);
+    return ZOOM_record_cache_lookup_i(s, pos, syntax, elementSetName, schema);
 }
 
 ZOOM_API(ZOOM_record)
@@ -1436,13 +1466,7 @@ ZOOM_API(int)
             if (c->proto == PROTO_HTTP)
                 ret = ZOOM_connection_srw_send_search(c);
             else
-                ret = ZOOM_connection_Z3950_send_search(c);
-            break;
-        case ZOOM_TASK_RETRIEVE:
-            if (c->proto == PROTO_HTTP)
-                ret = ZOOM_connection_srw_send_search(c);
-            else
-                ret = send_Z3950_present(c);
+                ret = ZOOM_connection_Z3950_search(c);
             break;
         case ZOOM_TASK_CONNECT:
             ret = do_connect(c);
@@ -1481,7 +1505,6 @@ ZOOM_API(int)
     return 1;
 }
 
-#if YAZ_HAVE_XML2
 
 static zoom_ret send_HTTP_redirect(ZOOM_connection c, const char *uri)
 {
@@ -1496,9 +1519,12 @@ static zoom_ret send_HTTP_redirect(ZOOM_connection c, const char *uri)
         z_HTTP_header_add_basic_auth(c->odr_out, &gdu->u.HTTP_Request->headers,
                                      c->user, c->password);
     }
+    xfree(c->location);
+    c->location = 0;
     return ZOOM_send_GDU(c, gdu);
 }
 
+
 zoom_ret ZOOM_send_GDU(ZOOM_connection c, Z_GDU *gdu)
 {
     ZOOM_Event event;
@@ -1519,15 +1545,13 @@ zoom_ret ZOOM_send_GDU(ZOOM_connection c, Z_GDU *gdu)
     return ZOOM_send_buf(c);
 }
 
-#if YAZ_HAVE_XML2
 void ZOOM_set_HTTP_error(ZOOM_connection c, int error,
                          const char *addinfo, const char *addinfo2)
 {
     ZOOM_set_dset_error(c, error, "HTTP", addinfo, addinfo2);
 }
-#endif
-
 
+#if YAZ_HAVE_XML2
 static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
 {
     zoom_ret cret = zoom_complete;
@@ -1555,26 +1579,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 */
-            if (*location != '/')
-            {
-                /* full header */
-                do_connect_host(c, location);
-                send_HTTP_redirect(c, location);
-            }
-            else
-            {  /* relative header - same host */
-                char *args = 0;
-                char *nlocation = odr_malloc(c->odr_in, strlen(location)
-                                             + strlen(c->host_port) + 3);
-                strcpy(nlocation, c->host_port);
-                cs_get_host_args(nlocation, (const char **) &args);
-                if (!args || !*args)
-                    args = nlocation + strlen(nlocation);
-                else
-                    args--;
-                strcpy(args, location);
-                send_HTTP_redirect(c, nlocation);
-            }
+
+            int host_change = 0;
+            location = yaz_check_location(c->odr_in, c->host_port,
+                                          location, &host_change);
+            xfree(c->location);
+            c->location = xstrdup(location);
+            do_connect_host(c, location);
             return;
         }
     }
@@ -1668,22 +1679,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
         {
@@ -1905,6 +1920,8 @@ ZOOM_API(const char *)
         return "Extended Service. invalid version";
     case ZOOM_ERROR_ES_INVALID_SYNTAX:
         return "Extended Service. invalid syntax";
+    case ZOOM_ERROR_MEMCACHED:
+        return "Memcached";
     default:
         return diagbib1_str(error);
     }
@@ -1969,7 +1986,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)
@@ -1982,10 +1999,15 @@ 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);
-                ZOOM_connection_exec_task(c);
+                if (c->tasks->which == ZOOM_TASK_CONNECT)
+                {
+                    ZOOM_connection_remove_task(c);
+                    ZOOM_connection_set_mask(c, 0);
+                }
+                if (c->cs && c->location)
+                    send_HTTP_redirect(c, c->location);
+                else
+                    ZOOM_connection_exec_task(c);
             }
             c->state = STATE_ESTABLISHED;
         }