ZOOM HTTP: refactor and simplify redirect/proxy code
authorAdam Dickmeiss <adam@indexdata.dk>
Wed, 20 Jul 2011 12:16:35 +0000 (14:16 +0200)
committerAdam Dickmeiss <adam@indexdata.dk>
Wed, 20 Jul 2011 12:16:35 +0000 (14:16 +0200)
include/yaz/zgdu.h
src/http.c
src/zoom-c.c
src/zoom-p.h
src/zoom-sru.c

index d0f8f03..ff8cb64 100644 (file)
@@ -92,7 +92,9 @@ YAZ_EXPORT Z_GDU *z_get_HTTP_Request(ODR o);
 YAZ_EXPORT Z_GDU *z_get_HTTP_Request_host_path(ODR odr,
                                                const char *host,
                                                const char *path);
-
+YAZ_EXPORT Z_GDU *z_get_HTTP_Request_uri(ODR odr, const char *uri,
+                                         const char *args,
+                                         int use_full_uri);
 YAZ_EXPORT int yaz_decode_http_request(ODR o, Z_HTTP_Request **hr_p);
 YAZ_EXPORT int yaz_decode_http_response(ODR o, Z_HTTP_Response **hr_p);
 YAZ_EXPORT int yaz_encode_http_response(ODR o, Z_HTTP_Response *hr);
index 3f8fb53..c05dc02 100644 (file)
@@ -317,6 +317,48 @@ Z_GDU *z_get_HTTP_Request_host_path(ODR odr,
     return p;
 }
 
+Z_GDU *z_get_HTTP_Request_uri(ODR odr, const char *uri, const char *args,
+                              int use_full_uri)
+{
+    Z_GDU *p = z_get_HTTP_Request(odr);
+    const char *cp0 = strstr(uri, "://");
+    const char *cp1 = 0;
+    if (cp0)
+        cp0 = cp0+3;
+    else
+        cp0 = uri;
+    
+    cp1 = strchr(cp0, '/');
+    if (!cp1)
+        cp1 = cp0+strlen(cp0);
+    
+    if (cp0 && cp1)
+    {
+        char *h = (char*) odr_malloc(odr, cp1 - cp0 + 1);
+        memcpy (h, cp0, cp1 - cp0);
+        h[cp1-cp0] = '\0';
+        z_HTTP_header_add(odr, &p->u.HTTP_Request->headers,
+                          "Host", h);
+    }
+
+    if (!args)
+    {
+        if (*cp1)
+            args = cp1 + 1;
+        else
+            args = "";
+    }
+    p->u.HTTP_Request->path = odr_malloc(odr, cp1 - uri + strlen(args) + 2);
+    if (use_full_uri)
+    {
+        memcpy(p->u.HTTP_Request->path, uri, cp1 - uri);
+        strcpy(p->u.HTTP_Request->path + (cp1 - uri), "/");
+    }
+    else
+        strcpy(p->u.HTTP_Request->path, "/");
+    strcat(p->u.HTTP_Request->path, args);
+    return p;
+}
 
 Z_GDU *z_get_HTTP_Response(ODR o, int code)
 {
index d6821d6..742b956 100644 (file)
@@ -265,7 +265,6 @@ ZOOM_API(ZOOM_connection)
     c->options = ZOOM_options_create_with_parent(options);
 
     c->host_port = 0;
-    c->path = 0;
     c->proxy = 0;
     
     c->charset = c->lang = 0;
@@ -583,7 +582,6 @@ ZOOM_API(void)
     ZOOM_connection_remove_tasks(c);
     ZOOM_connection_remove_events(c);
     xfree(c->host_port);
-    xfree(c->path);
     xfree(c->proxy);
     xfree(c->charset);
     xfree(c->lang);
@@ -1045,20 +1043,6 @@ static zoom_ret do_connect_host(ZOOM_connection c, const char *logical_url)
     {
 #if YAZ_HAVE_XML2
         c->proto = PROTO_HTTP;
-        xfree(c->path);
-        if (c->proxy)
-        {
-            c->path = xstrdup(logical_url);
-        }
-        else
-        {
-            const char *db = 0;
-            
-            cs_get_host_args(logical_url, &db);
-            
-            c->path = xmalloc(strlen(db) * 3 + 2);
-            yaz_encode_sru_dbpath_buf(c->path, db);
-        }
 #else
         ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, "SRW");
         ZOOM_connection_close(c);
@@ -1476,39 +1460,14 @@ ZOOM_API(int)
 }
 
 #if YAZ_HAVE_XML2
-static Z_GDU *get_HTTP_Request_url(ODR odr, const char *url)
-{
-    Z_GDU *p = z_get_HTTP_Request(odr);
-    const char *host = url;
-    const char *cp0 = strstr(host, "://");
-    const char *cp1 = 0;
-    if (cp0)
-        cp0 = cp0+3;
-    else
-        cp0 = host;
-    
-    cp1 = strchr(cp0, '/');
-    if (!cp1)
-        cp1 = cp0 + strlen(cp0);
-    
-    if (cp0 && cp1)
-    {
-        char *h = (char*) odr_malloc(odr, cp1 - cp0 + 1);
-        memcpy (h, cp0, cp1 - cp0);
-        h[cp1-cp0] = '\0';
-        z_HTTP_header_add(odr, &p->u.HTTP_Request->headers, "Host", h);
-    }
-    p->u.HTTP_Request->path = odr_strdup(odr, *cp1 ? cp1 : "/");
-    return p;
-}
 
 static zoom_ret send_HTTP_redirect(ZOOM_connection c, const char *uri,
                                   Z_HTTP_Response *cookie_hres)
 {
     struct Z_HTTP_Header *h;
-    Z_GDU *gdu = get_HTTP_Request_url(c->odr_out, uri);
     char *combined_cookies = 0;
     int combined_cookies_len = 0;
+    Z_GDU *gdu = z_get_HTTP_Request_uri(c->odr_out, uri, 0, c->proxy ? 1 : 0);
 
     gdu->u.HTTP_Request->method = odr_strdup(c->odr_out, "GET");
     z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers, "Accept",
@@ -1574,7 +1533,7 @@ static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
 
     ZOOM_connection_set_mask(c, 0);
     yaz_log(c->log_details, "%p handle_http", c);
-    
+
     if ((hres->code == 301 || hres->code == 302) && c->sru_mode == zoom_sru_get
         && (location = z_HTTP_header_lookup(hres->headers, "Location")))
     {
@@ -1592,8 +1551,7 @@ static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
             do_connect_host(c, location);
             send_HTTP_redirect(c, location, hres);
             /* we're OK for now. Operation is not really complete */
-            ret = 0;
-            cret = zoom_pending;
+            return;
         }
     }
     else 
index 3041928..3c7b7ea 100644 (file)
@@ -64,7 +64,6 @@ struct ZOOM_connection_p {
     enum oid_proto proto;
     COMSTACK cs;
     char *host_port;
-    char *path;
     int error;
     char *addinfo;
     char *diagset;
index d6ce28d..105970f 100644 (file)
@@ -36,12 +36,10 @@ static zoom_ret send_srw(ZOOM_connection c, Z_SRW_PDU *sr)
     Z_GDU *gdu;
     ZOOM_Event event;
     const char *database =  ZOOM_options_get(c->options, "databaseName");
-    char *fdatabase = 0;
-    
-    if (database)
-        fdatabase = yaz_encode_sru_dbpath_odr(c->odr_out, database);
-    gdu = z_get_HTTP_Request_host_path(c->odr_out, c->host_port,
-                                       fdatabase ? fdatabase : c->path);
+
+    gdu = z_get_HTTP_Request_uri(c->odr_out, c->host_port,
+                                 database,
+                                 c->proxy ? 1 : 0);
 
     if (c->sru_mode == zoom_sru_get)
     {