Allow unix sockets.. Not well tested yet!
[pazpar2-moved-to-github.git] / src / connection.c
index d5b5be2..9a56b16 100644 (file)
@@ -375,7 +375,6 @@ void connect_resolver_host(struct host *host, iochan_man_t iochan_man)
                 if (sdb)
                 {
                     yaz_mutex_leave(host->mutex);
-                    connection_connect(con, iochan_man);
                     client_start_search(con->client);
                 }
                 else
@@ -391,7 +390,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,6 +409,8 @@ 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);
@@ -440,13 +440,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 +485,17 @@ static int connection_connect(struct connection *con, iochan_man_t iochan_man)
         return -1;
     }
 
-    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, '#'))
+    w = wrbuf_alloc();
+    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 +504,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;
 }
 
@@ -497,7 +520,7 @@ int client_prep_connection(struct client *cl,
     const char *url = session_setting_oneval(sdb, PZ_URL);
     const char *sru = session_setting_oneval(sdb, PZ_SRU);
     struct host *host = 0;
-    int default_port = *sru ? 80 : 210;
+    WRBUF url_w = wrbuf_alloc();
 
     if (zproxy && zproxy[0] == '\0')
         zproxy = 0;
@@ -505,20 +528,27 @@ int client_prep_connection(struct client *cl,
     if (!url || !*url)
         url = sdb->database->id;
 
+    if (sru && *sru && !strstr(url, "://"))
+        wrbuf_puts(url_w, "http://");
+    wrbuf_puts(url_w, url);
+
     host = find_host(client_get_session(cl)->service->server->database_hosts,
-                     url, zproxy, default_port, iochan_man);
+                     wrbuf_cstr(url_w), zproxy, iochan_man);
 
     yaz_log(YLOG_DEBUG, "client_prep_connection: target=%s url=%s",
-            client_get_id(cl), url);
+            client_get_id(cl), wrbuf_cstr(url_w));
     if (!host)
+    {
+        wrbuf_destroy(url_w);
         return 0;
-
+    }
     co = client_get_connection(cl);
     if (co)
     {
         assert(co->host);
         if (co->host == host && client_get_state(cl) == Client_Idle)
         {
+            wrbuf_destroy(url_w);
             return 2;
         }
         connection_release(co);
@@ -551,7 +581,7 @@ int client_prep_connection(struct client *cl,
                 for (co = host->connections; co; co = co->next)
                 {
                     if (connection_is_idle(co) &&
-                        !strcmp(url, co->url) &&
+                        !strcmp(wrbuf_cstr(url_w), co->url) &&
                         (!co->client || client_get_state(co->client) == Client_Idle) &&
                         !strcmp(ZOOM_connection_option_get(co->link, "user"),
                                 session_setting_oneval(client_get_database(cl),
@@ -582,6 +612,7 @@ int client_prep_connection(struct client *cl,
                 yaz_log(YLOG_LOG, "out of connections %s", client_get_id(cl));
                 client_set_state(cl, Client_Error);
                 yaz_mutex_leave(host->mutex);
+                wrbuf_destroy(url_w);
                 return 0;
             }
         }
@@ -604,13 +635,13 @@ int client_prep_connection(struct client *cl,
         else
         {
             yaz_mutex_leave(host->mutex);
-            co = connection_create(cl, url, host,
+            co = connection_create(cl, wrbuf_cstr(url_w), host,
                                    operation_timeout, session_timeout,
                                    iochan_man);
         }
         assert(co->host);
     }
-
+    wrbuf_destroy(url_w);
     if (co && co->link)
         return 1;
     else