207 CURLcode ret = do_propfind_request(handle, rqbuf, rpbuf); |
208 CURLcode ret = do_propfind_request(handle, rqbuf, rpbuf); |
208 int status = 0; |
209 int status = 0; |
209 curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status); |
210 curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status); |
210 if(ret == CURLE_OK && status == 207) { |
211 if(ret == CURLE_OK && status == 207) { |
211 //printf("response\n%s\n", rpbuf->space); |
212 //printf("response\n%s\n", rpbuf->space); |
212 resource = parse_propfind_response(sn, rpbuf); |
213 resource = parse_propfind_response(sn, NULL, rpbuf); |
213 sn->error = DAV_OK; |
214 sn->error = DAV_OK; |
214 } else { |
215 } else { |
215 session_set_error(sn, ret, status); |
216 session_set_error(sn, ret, status); |
|
217 } |
|
218 return resource; |
|
219 } |
|
220 |
|
221 DavResource* dav_propfind(DavSession *sn, DavResource *root, UcxBuffer *rqbuf, char *path) { |
|
222 char *url = util_concat_path(sn->base_url, path); |
|
223 CURL *handle = sn->handle; |
|
224 curl_easy_setopt(handle, CURLOPT_URL, url); |
|
225 free(url); |
|
226 |
|
227 UcxBuffer *rpbuf = ucx_buffer_new(NULL, 4096, UCX_BUFFER_AUTOEXTEND); |
|
228 DavResource *resource = root; |
|
229 CURLcode ret = do_propfind_request(handle, rqbuf, rpbuf); |
|
230 int status = 0; |
|
231 curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status); |
|
232 if(ret == CURLE_OK && status == 207) { |
|
233 //printf("response\n%s\n", rpbuf->space); |
|
234 resource = parse_propfind_response(sn, resource, rpbuf); |
|
235 sn->error = DAV_OK; |
|
236 } else { |
|
237 session_set_error(sn, ret, status); |
|
238 resource = NULL; |
|
239 } |
|
240 ucx_buffer_free(rpbuf); |
|
241 return resource; |
|
242 } |
|
243 |
|
244 UcxList* propfind_stack_push(UcxList *stack, DavResource *children) { |
|
245 while(children) { |
|
246 if(children->iscollection) { |
|
247 stack = ucx_list_prepend(stack, children); |
|
248 } |
|
249 children = children->next; |
|
250 } |
|
251 return stack; |
|
252 } |
|
253 |
|
254 DavResource* dav_get2(DavSession *sn, DavGetQuery *query) { |
|
255 char *path; |
|
256 int depth = 0; |
|
257 if(parse_path_query(query->from, &path, &depth)) { |
|
258 sn->error = DAV_ERROR; |
|
259 return NULL; |
|
260 } |
|
261 |
|
262 sstr_t ps = query->properties; |
|
263 UcxBuffer *rqbuf; |
|
264 if(!sstrcmp(ps, S("*"))) { |
|
265 rqbuf = create_allprop_propfind_request(); |
|
266 } else if(!sstrcmp(ps, S("-"))) { |
|
267 rqbuf = create_propfind_request(NULL); |
|
268 } else { |
|
269 UcxList *proplist = parse_properties_string(sn->context, ps); |
|
270 rqbuf = create_propfind_request(proplist); |
|
271 } |
|
272 |
|
273 //fwrite(rqbuf->space, 1, rqbuf->size, stdout); |
|
274 //printf("\n"); |
|
275 |
|
276 DavResource *resource = dav_propfind(sn, NULL, rqbuf, path); |
|
277 free(path); |
|
278 int error = 0; |
|
279 if(resource && depth == -1) { |
|
280 UcxList *stack = NULL; // stack with davResource* elements |
|
281 stack = propfind_stack_push(stack, resource->children); |
|
282 while(stack) { |
|
283 DavResource *sr = stack->data; // get first element from the stack |
|
284 stack = ucx_list_remove(stack, stack); // remove first element |
|
285 // do propfind request for sr |
|
286 sr = dav_propfind(sn, sr, rqbuf, sr->path); |
|
287 if(!sr) { |
|
288 error = 1; |
|
289 printf("subrequest failed\n"); |
|
290 break; |
|
291 } |
|
292 stack = propfind_stack_push(stack, sr->children); // add children |
|
293 } |
216 } |
294 } |
217 return resource; |
295 return resource; |
218 } |
296 } |
219 |
297 |
220 UcxList* parse_properties_string(DavContext *context, sstr_t str) { |
298 UcxList* parse_properties_string(DavContext *context, sstr_t str) { |