reimplemented redirects for propfind requests

Wed, 14 Oct 2015 14:37:08 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 14 Oct 2015 14:37:08 +0200
changeset 163
c6d05f646b59
parent 162
25374afa5616
child 164
4c204e40ef52

reimplemented redirects for propfind requests

libidav/methods.c file | annotate | diff | comparison | revisions
--- a/libidav/methods.c	Wed Oct 14 11:49:39 2015 +0200
+++ b/libidav/methods.c	Wed Oct 14 14:37:08 2015 +0200
@@ -48,28 +48,12 @@
 {
     curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "PROPFIND");
     
-    struct curl_slist *headers = NULL;
+    long follow = 0;
+    curl_easy_getinfo(handle, CURLOPT_FOLLOWLOCATION, &follow);
+    int maxredirect = 16;
     
-    char *url = NULL;
-    curl_easy_getinfo(handle, CURLINFO_EFFECTIVE_URL, &url);
-    if(url) {
-        size_t ulen = strlen(url);
-        if(ulen > 0) {
-            if(url[ulen-1] == '/') {
-                headers = curl_slist_append(headers, "Depth: 1");
-            } else {
-                headers = curl_slist_append(headers, "Depth: 0");
-            }
-        } else {
-            return CURLE_URL_MALFORMAT;
-        }
-    } else {
-        return CURLE_URL_MALFORMAT;
-    }
-    
-    headers = curl_slist_append(headers, "Content-Type: text/xml");
-    
-    curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
+    struct curl_slist *headers = NULL;
+    CURLcode ret = 0;
     
     curl_easy_setopt(handle, CURLOPT_UPLOAD, 1); 
     curl_easy_setopt(handle, CURLOPT_READFUNCTION, ucx_buffer_read);
@@ -79,9 +63,46 @@
     curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, ucx_buffer_write);
     curl_easy_setopt(handle, CURLOPT_WRITEDATA, response);
     
-    ucx_buffer_seek(request, 0, SEEK_SET);
-    CURLcode ret = curl_easy_perform(handle);
-    curl_slist_free_all(headers);
+    curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 0L);
+    for(int i=0;i<=maxredirect;i++) {
+        char *url = NULL;
+        curl_easy_getinfo(handle, CURLINFO_EFFECTIVE_URL, &url);
+        if(url) {
+            size_t ulen = strlen(url);
+            if(ulen > 0) {
+                if(url[ulen-1] == '/') {
+                    headers = curl_slist_append(headers, "Depth: 1");
+                } else {
+                    headers = curl_slist_append(headers, "Depth: 0");
+                }
+            } else {
+                return CURLE_URL_MALFORMAT;
+            }
+        } else {
+            return CURLE_URL_MALFORMAT;
+        }
+        headers = curl_slist_append(headers, "Content-Type: text/xml");
+        curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
+        
+        ucx_buffer_seek(request, 0, SEEK_SET);
+        ucx_buffer_seek(response, 0 , SEEK_SET);
+        ret = curl_easy_perform(handle);
+        curl_slist_free_all(headers);
+        headers = NULL;
+        
+        char *location = NULL;
+        curl_easy_getinfo(handle, CURLINFO_REDIRECT_URL, &location);
+        if(location) {
+            // redirect
+            curl_easy_setopt(handle, CURLOPT_URL, location);
+        } else {
+            break;
+        }
+    }
+    
+    // reset followlocation option
+    curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, follow);
+    
     return ret;
 }
 

mercurial