From b2f370a727cbb8be4973bdd1eb8c7af743b2f7c4 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 12 Jan 2020 00:49:53 +0100 Subject: [PATCH] Add escape_url() and unescape_url() to CURLWrapper. --- include/curl_wrapper.hpp | 42 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/include/curl_wrapper.hpp b/include/curl_wrapper.hpp index 9e7ee48..118dfcb 100644 --- a/include/curl_wrapper.hpp +++ b/include/curl_wrapper.hpp @@ -157,6 +157,48 @@ public: */ void set_proxy(string_view proxy); + /*! + * @brief URL encodes the given string. + * + * For more information consult [curl_easy_escape(3)] + * (https://curl.haxx.se/libcurl/c/curl_easy_escape.html). + * + * @param url String to escape. + * + * @return The escaped string or {} if it failed. + * + * @since 0.3.0 + */ + inline string escape_url(const string_view url) const + { + char *cbuf{curl_easy_escape(_connection, url.data(), + static_cast(url.size()))}; + string sbuf{cbuf}; + curl_free(cbuf); + return sbuf; + } + + /*! + * @brief URL decodes the given string . + * + * For more information consult [curl_easy_unescape(3)] + * (https://curl.haxx.se/libcurl/c/curl_easy_unescape.html). + * + * @param url String to unescape. + * + * @return The unescaped string or {} if it failed. + * + * @since 0.3.0 + */ + inline string unescape_url(const string_view url) const + { + char *cbuf{curl_easy_unescape(_connection, url.data(), + static_cast(url.size()), nullptr)}; + string sbuf{cbuf}; + curl_free(cbuf); + return sbuf; + } + protected: /*! * @brief Mutex for #get_buffer a.k.a. _curl_buffer_body.