Ensure sessions with different auth IDs are not shared.
[pazpar2-moved-to-github.git] / src / connection.c
index 77bd8e7..b9964be 100644 (file)
@@ -71,7 +71,6 @@ struct connection {
     struct client *client;
     char *ibuf;
     int ibufsize;
-    char *authentication; // Empty string or authentication string if set
     char *zproxy;
     enum {
         Conn_Resolving,
@@ -86,12 +85,11 @@ static struct connection *connection_freelist = 0;
 static int connection_is_idle(struct connection *co)
 {
     ZOOM_connection link = co->link;
-    int event = ZOOM_connection_peek_event(link);
+    int event;
 
-    if (co->state != Conn_Open)
+    if (co->state != Conn_Open || !link)
         return 0;
 
-    link = co->link;
     event = ZOOM_connection_peek_event(link);
     if (event == ZOOM_EVENT_NONE ||
                 event == ZOOM_EVENT_END)
@@ -135,10 +133,8 @@ static void remove_connection_from_host(struct connection *con)
 
 void connection_continue(struct connection *co)
 {
-#if 1
     yaz_log(YLOG_LOG, "connection_continue");
     iochan_setevent(co->iochan, EVENT_OUTPUT);
-#endif
 }
 
 // Close connection and recycle structure
@@ -167,7 +163,7 @@ void connection_destroy(struct connection *co)
 
 // Creates a new connection for client, associated with the host of 
 // client's database
-struct connection *connection_create(struct client *cl)
+static struct connection *connection_create(struct client *cl)
 {
     struct connection *new;
     struct host *host = client_get_host(cl);
@@ -184,7 +180,6 @@ struct connection *connection_create(struct client *cl)
     new->next = new->host->connections;
     new->host->connections = new;
     new->client = cl;
-    new->authentication = "";
     new->zproxy = 0;
     client_set_connection(cl, new);
     new->link = 0;
@@ -320,7 +315,7 @@ void connect_resolver_host(struct host *host)
     }
 }
 
-struct host *connection_get_host(struct connection *con)
+static struct host *connection_get_host(struct connection *con)
 {
     return con->host;
 }
@@ -352,7 +347,10 @@ int connection_connect(struct connection *con)
     ZOOM_connection link = 0;
     struct host *host = connection_get_host(con);
     ZOOM_options zoptions = ZOOM_options_create();
-    char *auth;
+    const char *auth;
+    const char *sru;
+    const char *sru_version = 0;
+    char ipport[512] = "";
 
     struct session_database *sdb = client_get_database(con->client);
     const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
@@ -374,8 +372,13 @@ int connection_connect(struct connection *con)
     if (apdulog && *apdulog)
         ZOOM_options_set(zoptions, "apdulog", apdulog);
 
-    if ((auth = (char*) session_setting_oneval(sdb, PZ_AUTHENTICATION)))
+    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 (!(link = ZOOM_connection_create(zoptions)))
     {
@@ -383,7 +386,21 @@ int connection_connect(struct connection *con)
         ZOOM_options_destroy(zoptions);
         return -1;
     }
-    ZOOM_connection_connect(link, host->ipport, 0);
+
+    if (sru && *sru)
+        strcpy(ipport, "http://");
+    strcat(ipport, host->ipport);
+    /* deal with SRU path here because databaseName option is not read in
+       ZOOM in SRU mode */
+    if (sru && *sru)
+    {
+        if (*sdb->database->databases[0])
+        {
+            strcat(ipport, "/");
+            strcat(ipport, sdb->database->databases[0]);
+        }
+    }
+    ZOOM_connection_connect(link, ipport, 0);
     
     con->link = link;
     con->iochan = iochan_create(0, connection_handler, 0);
@@ -407,11 +424,6 @@ const char *connection_get_url(struct connection *co)
     return client_get_url(co->client);
 }
 
-void connection_set_authentication(struct connection *co, char *auth)
-{
-    co->authentication = auth;
-}
-
 // Ensure that client has a connection associated
 int client_prep_connection(struct client *cl)
 {
@@ -435,9 +447,9 @@ int client_prep_connection(struct client *cl)
         for (co = host->connections; co; co = co->next)
             if (connection_is_idle(co) &&
                 (!co->client || client_get_session(co->client) != se) &&
-                !strcmp(co->authentication,
-                    session_setting_oneval(client_get_database(cl),
-                    PZ_AUTHENTICATION)))
+                !strcmp(ZOOM_connection_option_get(co->link, "user"),
+                        session_setting_oneval(client_get_database(cl),
+                                               PZ_AUTHENTICATION)))
             {
                 if (zproxy == 0 && co->zproxy == 0)
                     break;
@@ -460,13 +472,6 @@ int client_prep_connection(struct client *cl)
         return 0;
 }
 
-// DELETEME
-
-
-int connection_send_apdu(struct connection *co, Z_APDU *a){return 10;}
-
-
-
 /*
  * Local variables:
  * c-basic-offset: 4