libidav/methods.c

changeset 170
cf054cded046
parent 166
8911170d5e78
child 171
254326cbc1bc
--- a/libidav/methods.c	Thu Oct 15 15:01:50 2015 +0200
+++ b/libidav/methods.c	Thu Oct 15 15:36:26 2015 +0200
@@ -54,6 +54,9 @@
     curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 0L);
     const int maxredirect = 16;
     
+    // always try to get information about possible children
+    int depth = 1;
+    
     struct curl_slist *headers = NULL;
     CURLcode ret = 0;
     
@@ -64,6 +67,8 @@
     
     curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, ucx_buffer_write);
     curl_easy_setopt(handle, CURLOPT_WRITEDATA, response);
+    UcxMap *respheaders = ucx_map_new(32);
+    util_capture_header(handle, respheaders);
     
     for(int i=0;i<=maxredirect;i++) {
         char *url = NULL;
@@ -71,7 +76,7 @@
         if(url) {
             size_t ulen = strlen(url);
             if(ulen > 0) {
-                if(url[ulen-1] == '/') {
+                if (depth == 1) {
                     headers = curl_slist_append(headers, "Depth: 1");
                 } else {
                     headers = curl_slist_append(headers, "Depth: 0");
@@ -92,17 +97,35 @@
         curl_slist_free_all(headers);
         headers = NULL;
         
-        // continue work with URL we get from the server!
+        /*
+         * Handle three cases:
+         * 1. We get a Location header (it's a redirect)
+         *    => follow the URL we get from the server
+         * 2. We communicate with IIS and get a X-MSDAVEXT_Error: 589831
+         *    => try with depth 0 next time, it's not a collection
+         * 3. Other cases
+         *    => the server handled our request and we can stop requesting
+         */
         char *location = NULL;
+        char *msdavexterror;
         curl_easy_getinfo(handle, CURLINFO_REDIRECT_URL, &location);
+        msdavexterror = ucx_map_cstr_get(respheaders, "x-msdavext_error");
+        int iishack =  depth == 1 &&
+            msdavexterror && !strncmp(msdavexterror, "589831;", 7);
+        
         if(location) {
             // redirect
             curl_easy_setopt(handle, CURLOPT_URL, location);
+        } else if(iishack) {
+            depth = 0;
         } else {
             break;
         }
     }
     
+    ucx_map_free_content(respheaders, free);
+    ucx_map_free(respheaders);
+    
     // reset followlocation option
     curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, follow);
     

mercurial