Add yaz_check_location
[yaz-moved-to-github.git] / src / http.c
index 378d145..0f17df5 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2013 Index Data
+ * Copyright (C) Index Data
  * See the file LICENSE for details.
  */
 /**
@@ -16,6 +16,7 @@
 #include <yaz/matchstr.h>
 #include <yaz/zgdu.h>
 #include <yaz/base64.h>
+#include <yaz/comstack.h>
 
 static int decode_headers_content(ODR o, int off, Z_HTTP_Header **headers,
                                   char **content_buf, int *content_len)
@@ -507,9 +508,9 @@ int yaz_decode_http_request(ODR o, Z_HTTP_Request **hr_p)
     Z_HTTP_Request *hr = (Z_HTTP_Request *) odr_malloc(o, sizeof(*hr));
     const char *buf = o->op->buf;
     int size = o->op->size;
+    int lspace = 0;
 
     *hr_p = hr;
-
     /* method .. */
     for (i = 0; buf[i] != ' '; i++)
         if (i >= size-5 || i > 30)
@@ -518,28 +519,21 @@ int yaz_decode_http_request(ODR o, Z_HTTP_Request **hr_p)
             return 0;
         }
     hr->method = odr_strdupn(o, buf, i);
-    /* path */
-    po = i+1;
-    for (i = po; buf[i] != ' '; i++)
-        if (i >= size-5)
-        {
-            o->error = OHTTP;
-            return 0;
-        }
-    hr->path = odr_strdupn(o, buf + po, i - po);
-    /* HTTP version */
-    i++;
-    if (i > size-5 || memcmp(buf+i, "HTTP/", 5))
+    po = ++i;
+    while (i < size && !strchr("\r\n", buf[i]))
+    {
+        if (buf[i] == ' ')
+            lspace = i;
+        i++;
+    }
+    if (!lspace || i >= size || lspace >= size - 5 ||
+        memcmp(buf + lspace + 1, "HTTP/", 5))
     {
         o->error = OHTTP;
         return 0;
     }
-    i+= 5;
-    po = i;
-    while (i < size && !strchr("\r\n", buf[i]))
-        i++;
-    hr->version = odr_strdupn(o, buf + po, i - po);
-    /* headers */
+    hr->path = odr_strdupn(o, buf + po, lspace - po);
+    hr->version = odr_strdupn(o, buf + lspace + 6, i - (lspace + 6));
     if (i < size-1 && buf[i] == '\r')
         i++;
     if (buf[i] != '\n')
@@ -547,29 +541,30 @@ int yaz_decode_http_request(ODR o, Z_HTTP_Request **hr_p)
         o->error = OHTTP;
         return 0;
     }
+    /* headers */
     return decode_headers_content(o, i, &hr->headers,
                                   &hr->content_buf, &hr->content_len);
 }
 
 static void dump_http_package(ODR o, const char *buf, size_t len)
 {
-    int i;
+    int i, limit = 8192;
     for (i = 0; ; i++)
     {
         if (i == len)
         {
-            odr_printf(o, "%.*s\n", i, buf);
+            o->op->stream_write(o, o->op->print, ODR_VISIBLESTRING, buf, i);
             break;
         }
-        else if (i > 8192)
+        else if (i >= limit)
         {
-            odr_printf(o, "%.*s\n", i, buf);
-            odr_printf(o, "(truncated\n", (long) len);
+            o->op->stream_write(o, o->op->print, ODR_VISIBLESTRING, buf, i);
+            odr_printf(o, "(truncated from %ld to %d\n", (long) len, i);
             break;
         }
         else if (buf[i] == 0)
         {
-            odr_printf(o, "%.*s\n", i, buf);
+            o->op->stream_write(o, o->op->print, ODR_VISIBLESTRING, buf, i);
             odr_printf(o, "(binary data)\n", (long) len);
             break;
         }
@@ -651,6 +646,32 @@ int yaz_encode_http_request(ODR o, Z_HTTP_Request *hr)
     return 1;
 }
 
+const char *yaz_check_location(ODR odr, const char *uri, const char *location,
+                               int *host_change)
+{
+    if (*location == '/')
+    {  /* relative location */
+        char *args = 0;
+        char *nlocation = (char *) odr_malloc(odr, strlen(location)
+                                              + strlen(uri) + 3);
+        strcpy(nlocation, uri);
+        cs_get_host_args(nlocation, (const char **) &args);
+        if (!args || !*args)
+            args = nlocation + strlen(nlocation);
+        else
+            args--;
+        strcpy(args, location);
+        *host_change = 0;
+        return nlocation;
+    }
+    else
+    {
+        /* we don't check if host is the same as before - yet */
+        *host_change = 1;
+        return location;
+    }
+}
+
 /*
  * Local variables:
  * c-basic-offset: 4