Version 1.11.0
[pazpar2-moved-to-github.git] / src / connection.c
index d5b5be2..a25d840 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of Pazpar2.
-   Copyright (C) 2006-2013 Index Data
+   Copyright (C) Index Data
 
 Pazpar2 is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free
@@ -228,8 +228,10 @@ static void non_block_events(struct connection *co)
                 int err;
                 if ((err = ZOOM_connection_error(link, &error, &addinfo)))
                 {
-                    yaz_log(YLOG_LOG, "Error %s from %s",
-                            error, client_get_id(cl));
+                    struct session *se = client_get_session(cl);
+
+                    session_log(se, YLOG_WARN, "%s: Error %s (%s)",
+                                client_get_id(cl), error, addinfo);
                     client_set_diagnostic(cl, err, error, addinfo);
                     client_set_state(cl, Client_Error);
                 }
@@ -254,7 +256,6 @@ static void non_block_events(struct connection *co)
         case ZOOM_EVENT_RECV_APDU:
             break;
         case ZOOM_EVENT_CONNECT:
-            yaz_log(YLOG_LOG, "Connected to %s", client_get_id(cl));
             co->state = Conn_Open;
             break;
         case ZOOM_EVENT_RECV_SEARCH:
@@ -273,10 +274,7 @@ static void non_block_events(struct connection *co)
     {
         struct client *cl = co->client;
         if (cl)
-        {
-            client_check_preferred_watch(cl);
             client_got_records(cl);
-        }
     }
 }
 
@@ -284,7 +282,13 @@ void connection_continue(struct connection *co)
 {
     int r = ZOOM_connection_exec_task(co->link);
     if (!r)
+    {
+        struct client *cl = co->client;
+
+        client_lock(cl);
         non_block_events(co);
+        client_unlock(cl);
+    }
     else
     {
         iochan_setflags(co->iochan, ZOOM_connection_get_mask(co->link));
@@ -353,6 +357,11 @@ static void connection_release(struct connection *co)
     co->client = 0;
 }
 
+void connection_release2(struct connection *co)
+{
+    co->client = 0;
+}
+
 void connect_resolver_host(struct host *host, iochan_man_t iochan_man)
 {
     struct connection *con;
@@ -363,7 +372,8 @@ void connect_resolver_host(struct host *host, iochan_man_t iochan_man)
     {
         if (con->state == Conn_Closed)
         {
-            if (!host->ipport || !con->client) /* unresolved or no client */
+            struct client *cl = con->client;
+            if (!host->ipport || !cl) /* unresolved or no client */
             {
                 remove_connection_from_host(con);
                 yaz_mutex_leave(host->mutex);
@@ -371,12 +381,12 @@ void connect_resolver_host(struct host *host, iochan_man_t iochan_man)
             }
             else
             {
-                struct session_database *sdb = client_get_database(con->client);
-                if (sdb)
+                struct session_database *sdb = client_get_database(cl);
+                struct session *se = client_get_session(cl);
+                if (sdb && se)
                 {
                     yaz_mutex_leave(host->mutex);
-                    connection_connect(con, iochan_man);
-                    client_start_search(con->client);
+                    client_start_search(cl);
                 }
                 else
                 {
@@ -391,7 +401,6 @@ void connect_resolver_host(struct host *host, iochan_man_t iochan_man)
         }
         else
         {
-            yaz_log(YLOG_LOG, "connect_resolver_host: state=%d", con->state);
             con = con->next;
         }
     }
@@ -411,9 +420,13 @@ static int connection_connect(struct connection *con, iochan_man_t iochan_man)
     const char *charset;
     const char *sru;
     const char *sru_version = 0;
+    const char *value;
+    WRBUF w;
 
     struct session_database *sdb = client_get_database(con->client);
     const char *apdulog = session_setting_oneval(sdb, PZ_APDULOG);
+    const char *memcached = session_setting_oneval(sdb, PZ_MEMCACHED);
+    const char *redis = session_setting_oneval(sdb, PZ_REDIS);
 
     assert(con);
 
@@ -423,6 +436,10 @@ static int connection_connect(struct connection *con, iochan_man_t iochan_man)
 
     if ((charset = session_setting_oneval(sdb, PZ_NEGOTIATION_CHARSET)))
         ZOOM_options_set(zoptions, "charset", charset);
+    if (memcached && *memcached)
+        ZOOM_options_set(zoptions, "memcached", memcached);
+    if (redis && *redis)
+        ZOOM_options_set(zoptions, "redis", redis);
 
     assert(host->ipport);
     if (host->proxy)
@@ -440,13 +457,44 @@ static int connection_connect(struct connection *con, iochan_man_t iochan_man)
     if (apdulog && *apdulog)
         ZOOM_options_set(zoptions, "apdulog", apdulog);
 
-    if ((auth = session_setting_oneval(sdb, PZ_AUTHENTICATION)))
-        ZOOM_options_set(zoptions, "user", auth);
+
     if ((sru = session_setting_oneval(sdb, PZ_SRU)) && *sru)
         ZOOM_options_set(zoptions, "sru", sru);
     if ((sru_version = session_setting_oneval(sdb, PZ_SRU_VERSION))
         && *sru_version)
         ZOOM_options_set(zoptions, "sru_version", sru_version);
+
+    if ((auth = session_setting_oneval(sdb, PZ_AUTHENTICATION)))
+    {
+        /* allow splitting user and reset with a blank always */
+        const char *cp1 = strchr(auth, ' ');
+        if (!cp1 && sru && *sru)
+            cp1 =  strchr(auth, '/');
+        if (!cp1)
+        {
+            /* Z39.50 user/password style, or no password for SRU */
+            ZOOM_options_set(zoptions, "user", auth);
+        }
+        else
+        {
+            /* now consider group as well */
+            const char *cp2 = strchr(cp1 + 1, ' ');
+
+            ZOOM_options_setl(zoptions, "user", auth, cp1 - auth);
+            if (!cp2)
+                ZOOM_options_set(zoptions, "password", cp1 + 1);
+            else
+            {
+                ZOOM_options_setl(zoptions, "group", cp1 + 1, cp2 - cp1 - 1);
+                ZOOM_options_set(zoptions, "password", cp2 + 1);
+            }
+        }
+    }
+
+    value = session_setting_oneval(sdb, PZ_AUTHENTICATION_MODE);
+    if (value && *value)
+        ZOOM_options_set(zoptions, "authenticationMode", value);
+
     if (!(con->link = ZOOM_connection_create(zoptions)))
     {
         yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create ZOOM Connection");
@@ -454,26 +502,19 @@ static int connection_connect(struct connection *con, iochan_man_t iochan_man)
         return -1;
     }
 
+    w = wrbuf_alloc();
     if (sru && *sru && !strstr(con->url, "://"))
-    {
-        WRBUF w = wrbuf_alloc();
         wrbuf_puts(w, "http://");
-        wrbuf_puts(w, con->url);
-        ZOOM_connection_connect(con->link, wrbuf_cstr(w), 0);
-        wrbuf_destroy(w);
-    }
-    else if (strchr(con->url, '#'))
+    if (strchr(con->url, '#'))
     {
         const char *cp = strchr(con->url, '#');
-        WRBUF w = wrbuf_alloc();
         wrbuf_write(w, con->url, cp - con->url);
-        ZOOM_connection_connect(con->link, wrbuf_cstr(w), 0);
-        wrbuf_destroy(w);
     }
     else
-    {
-        ZOOM_connection_connect(con->link, con->url, 0);
-    }
+        wrbuf_puts(w, con->url);
+
+    ZOOM_connection_connect(con->link, wrbuf_cstr(w), 0);
+
     con->iochan = iochan_create(-1, connection_handler, 0, "connection_socket");
     con->state = Conn_Connecting;
     iochan_settimeout(con->iochan, con->operation_timeout);
@@ -482,6 +523,7 @@ static int connection_connect(struct connection *con, iochan_man_t iochan_man)
 
     client_set_state(con->client, Client_Connecting);
     ZOOM_options_destroy(zoptions);
+    wrbuf_destroy(w);
     return 0;
 }