libidav/utils.c

changeset 113
dde28a806552
parent 103
6606616eca9f
equal deleted inserted replaced
112:c3f2f16fa4b8 113:dde28a806552
32 #include <string.h> 32 #include <string.h>
33 #include <errno.h> 33 #include <errno.h>
34 #include <ctype.h> 34 #include <ctype.h>
35 #include <cx/string.h> 35 #include <cx/string.h>
36 #include <cx/buffer.h> 36 #include <cx/buffer.h>
37 #include <cx/utils.h>
38 #include <cx/printf.h> 37 #include <cx/printf.h>
39 #include <libxml/tree.h> 38 #include <libxml/tree.h>
40 #include <curl/curl.h> 39 #include <curl/curl.h>
41 40
42 #ifdef _WIN32 41 #ifdef _WIN32
346 } 345 }
347 } 346 }
348 return path; 347 return path;
349 } 348 }
350 349
350 char* util_url_encode(DavSession *sn, const char *url) {
351 CURL *handle = sn ? sn->handle : NULL;
352 #if LIBCURL_VERSION_NUM < 0x075200
353 int cleanup_handle = 0;
354 if(!handle) {
355 handle = curl_easy_init();
356 cleanup_handle = 1;
357 }
358 #endif
359
360 char *esc = curl_easy_escape(handle, url, strlen(url));
361 char *ret = esc ? strdup(esc) : NULL;
362 curl_free(esc);
363
364 #if LIBCURL_VERSION_NUM < 0x075200
365 if(cleanup_handle) {
366 curl_easy_cleanup(handle);
367 }
368 #endif
369
370 return ret;
371 }
372
351 char* util_url_decode(DavSession *sn, const char *url) { 373 char* util_url_decode(DavSession *sn, const char *url) {
352 char *unesc = curl_easy_unescape(sn->handle, url, strlen(url), NULL); 374 CURL *handle = sn ? sn->handle : NULL;
353 char *ret = strdup(unesc); 375 #if LIBCURL_VERSION_NUM < 0x075200
376 int cleanup_handle = 0;
377 if(!handle) {
378 handle = curl_easy_init();
379 cleanup_handle = 1;
380 }
381 #endif
382
383 char *unesc = curl_easy_unescape(handle, url, strlen(url), NULL);
384 char *ret = unesc ? strdup(unesc) : NULL;
354 curl_free(unesc); 385 curl_free(unesc);
386
387 #if LIBCURL_VERSION_NUM < 0x075200
388 if(cleanup_handle) {
389 curl_easy_cleanup(handle);
390 }
391 #endif
392
393 return ret;
394 }
395
396 cxmutstr util_url_encode_s(const CxAllocator *a, cxstring url) {
397 CURL *handle = NULL;
398 #if LIBCURL_VERSION_NUM < 0x075200
399 handle = curl_easy_init();
400 #endif
401
402 char *esc = curl_easy_escape(handle, url.ptr, url.length);
403 cxmutstr ret = esc ? cx_strdup_a(a, cx_str(esc)) : (cxmutstr){NULL, 0};
404 curl_free(esc);
405
406 #if LIBCURL_VERSION_NUM < 0x075200
407 curl_easy_cleanup(handle);
408 #endif
409 return ret;
410 }
411
412 cxmutstr util_url_decode_s(const CxAllocator *a, cxstring url) {
413 CURL *handle = NULL;
414 #if LIBCURL_VERSION_NUM < 0x075200
415 handle = curl_easy_init();
416 #endif
417
418 char *unesc = curl_easy_unescape(handle, url.ptr, url.length, NULL);
419 cxmutstr ret = unesc ? cx_strdup_a(a, cx_str(unesc)) : (cxmutstr){NULL, 0};
420 curl_free(unesc);
421
422 #if LIBCURL_VERSION_NUM < 0x075200
423 curl_easy_cleanup(handle);
424 #endif
355 return ret; 425 return ret;
356 } 426 }
357 427
358 static size_t util_header_callback(char *buffer, size_t size, 428 static size_t util_header_callback(char *buffer, size_t size,
359 size_t nitems, void *data) { 429 size_t nitems, void *data) {

mercurial