add util_url_encode_s/util_url_decode_s cxstring variants

Sun, 20 Apr 2025 13:17:14 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 20 Apr 2025 13:17:14 +0200
changeset 862
e58dd7fb1a97
parent 861
879d4bf93ef4
child 863
46275296d606

add util_url_encode_s/util_url_decode_s cxstring variants

libidav/utils.c file | annotate | diff | comparison | revisions
libidav/utils.h file | annotate | diff | comparison | revisions
--- a/libidav/utils.c	Sun Apr 20 13:09:03 2025 +0200
+++ b/libidav/utils.c	Sun Apr 20 13:17:14 2025 +0200
@@ -394,6 +394,38 @@
     return ret;
 }
 
+cxmutstr util_url_encode_s(const CxAllocator *a, cxstring url) {
+    CURL *handle = NULL;
+#if LIBCURL_VERSION_NUM < 0x075200
+    handle = curl_easy_init();
+#endif
+    
+    char *esc = curl_easy_escape(handle, url.ptr, url.length);
+    cxmutstr ret = esc ? cx_strdup_a(a, cx_str(esc)) : (cxmutstr){NULL, 0};
+    curl_free(esc);
+    
+#if LIBCURL_VERSION_NUM < 0x075200
+    curl_easy_cleanup(handle);
+#endif
+    return ret;
+}
+
+cxmutstr util_url_decode_s(const CxAllocator *a, cxstring url) {
+    CURL *handle = NULL;
+#if LIBCURL_VERSION_NUM < 0x075200
+    handle = curl_easy_init();
+#endif
+    
+    char *unesc = curl_easy_unescape(handle, url.ptr, url.length, NULL);
+    cxmutstr ret = unesc ? cx_strdup_a(a, cx_str(unesc)) : (cxmutstr){NULL, 0};
+    curl_free(unesc);
+    
+#if LIBCURL_VERSION_NUM < 0x075200
+    curl_easy_cleanup(handle);
+#endif
+    return ret;
+}
+
 static size_t util_header_callback(char *buffer, size_t size,
         size_t nitems, void *data) {
     
--- a/libidav/utils.h	Sun Apr 20 13:09:03 2025 +0200
+++ b/libidav/utils.h	Sun Apr 20 13:17:14 2025 +0200
@@ -78,6 +78,8 @@
 cxstring util_url_path_s(cxstring url);
 char* util_url_encode(DavSession *sn, const char *url);
 char* util_url_decode(DavSession *sn, const char *url);
+cxmutstr util_url_encode_s(const CxAllocator *a, cxstring url);
+cxmutstr util_url_decode_s(const CxAllocator *a, cxstring url);
 const char* util_resource_name(const char *url);
 const char* util_resource_name_c(const char *url, char pathseparator);
 const char* util_path_file_name(const char *url);

mercurial