# HG changeset patch # User Olaf Wintermann # Date 1444826228 -7200 # Node ID c6d05f646b5900752c0e6b5b74cd016ff4bb270d # Parent 25374afa56162328cd843d4173dcc9295b233d57 reimplemented redirects for propfind requests diff -r 25374afa5616 -r c6d05f646b59 libidav/methods.c --- 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; }