Add yaz_check_location
authorAdam Dickmeiss <adam@indexdata.dk>
Fri, 10 Jan 2014 12:58:36 +0000 (13:58 +0100)
committerAdam Dickmeiss <adam@indexdata.dk>
Fri, 10 Jan 2014 12:58:36 +0000 (13:58 +0100)
This function gets an absolute URI from Location (HTTP redirect).

include/yaz/zgdu.h
src/http.c

index a92a3b1..3c2ad75 100644 (file)
@@ -110,6 +110,8 @@ 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);
 YAZ_EXPORT int yaz_encode_http_request(ODR o, Z_HTTP_Request *hr);
 
 YAZ_EXPORT int yaz_encode_http_response(ODR o, Z_HTTP_Response *hr);
 YAZ_EXPORT int yaz_encode_http_request(ODR o, Z_HTTP_Request *hr);
 
+YAZ_EXPORT const char *yaz_check_location(ODR odr, const char *uri,
+                                    const char *location, int *host_change);
 YAZ_END_CDECL
 
 #endif
 YAZ_END_CDECL
 
 #endif
index d79fd10..0f17df5 100644 (file)
@@ -16,6 +16,7 @@
 #include <yaz/matchstr.h>
 #include <yaz/zgdu.h>
 #include <yaz/base64.h>
 #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)
 
 static int decode_headers_content(ODR o, int off, Z_HTTP_Header **headers,
                                   char **content_buf, int *content_len)
@@ -645,6 +646,32 @@ int yaz_encode_http_request(ODR o, Z_HTTP_Request *hr)
     return 1;
 }
 
     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
 /*
  * Local variables:
  * c-basic-offset: 4