First go at returning start+offset
[yaz-moved-to-github.git] / src / url.c
index 7bf6f4e..97e8247 100644 (file)
--- a/src/url.c
+++ b/src/url.c
@@ -1,5 +1,5 @@
 /* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2012 Index Data
+ * Copyright (C) 1995-2013 Index Data
  * See the file LICENSE for details.
  */
 /**
@@ -60,7 +60,7 @@ static void extract_user_pass(NMEM nmem,
     if (cp1 && cp1 > uri)
     {
         cp1--;
-        
+
         if (!strncmp(cp1, "://", 3))
         {
             const char *cp3 = 0;
@@ -76,7 +76,7 @@ static void extract_user_pass(NMEM nmem,
                 *uri_lean = nmem_malloc(nmem, strlen(uri) + 1);
                 memcpy(*uri_lean, uri, cp1 + 3 - uri);
                 strcpy(*uri_lean + (cp1 + 3 - uri), cp2 + 1);
-               
+
                 *http_user = nmem_strdupn(nmem, cp1 + 3, cp3 - (cp1 + 3));
                 *http_pass = nmem_strdupn(nmem, cp3 + 1, cp2 - (cp3 + 1));
             }
@@ -88,7 +88,7 @@ static void extract_user_pass(NMEM nmem,
 
 Z_HTTP_Response *yaz_url_exec(yaz_url_t p, const char *uri,
                               const char *method,
-                              Z_HTTP_Header *headers,
+                              Z_HTTP_Header *user_headers,
                               const char *buf, size_t len)
 {
     Z_HTTP_Response *res = 0;
@@ -99,7 +99,6 @@ Z_HTTP_Response *yaz_url_exec(yaz_url_t p, const char *uri,
         void *add;
         COMSTACK conn = 0;
         int code;
-        struct Z_HTTP_Header **last_header_entry;
         const char *location = 0;
         char *http_user = 0;
         char *http_pass = 0;
@@ -112,17 +111,25 @@ Z_HTTP_Response *yaz_url_exec(yaz_url_t p, const char *uri,
         gdu = z_get_HTTP_Request_uri(p->odr_out, uri_lean, 0, p->proxy ? 1 : 0);
         gdu->u.HTTP_Request->method = odr_strdup(p->odr_out, method);
 
+        for ( ; user_headers; user_headers = user_headers->next)
+        {
+            /* prefer new Host over user-supplied Host */
+            if (!strcmp(user_headers->name, "Host"))
+                ;
+            /* prefer user-supplied User-Agent over YAZ' own */
+            else if (!strcmp(user_headers->name, "User-Agent"))
+                z_HTTP_header_set(p->odr_out, &gdu->u.HTTP_Request->headers,
+                                  user_headers->name, user_headers->value);
+            else
+                z_HTTP_header_add(p->odr_out, &gdu->u.HTTP_Request->headers,
+                                  user_headers->name, user_headers->value);
+        }
         if (http_user && http_pass)
             z_HTTP_header_add_basic_auth(p->odr_out,
                                          &gdu->u.HTTP_Request->headers,
                                          http_user, http_pass);
 
         res = 0;
-        last_header_entry = &gdu->u.HTTP_Request->headers;
-        while (*last_header_entry)
-            last_header_entry = &(*last_header_entry)->next;
-        *last_header_entry = headers; /* attach user headers */
-
         if (buf && len)
         {
             gdu->u.HTTP_Request->content_buf = (char *) buf;
@@ -146,7 +153,7 @@ Z_HTTP_Response *yaz_url_exec(yaz_url_t p, const char *uri,
         {
             int len;
             char *buf = odr_getbuf(p->odr_out, &len, 0);
-            
+
             if (cs_put(conn, buf, len) < 0)
                 yaz_log(YLOG_WARN, "cs_put failed URL: %s", uri);
             else