From dc0f7919a40a602e459eca8704689b24f0a542a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Hern=C3=A1ndez=20Cordero?= Date: Mon, 17 Jun 2024 14:57:21 +0200 Subject: [PATCH] Allow spaces (#100) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Alejandro Hernández Cordero --- resource_retriever/src/retriever.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/resource_retriever/src/retriever.cpp b/resource_retriever/src/retriever.cpp index 242ae3e..3ca5371 100644 --- a/resource_retriever/src/retriever.cpp +++ b/resource_retriever/src/retriever.cpp @@ -111,6 +111,25 @@ size_t curlWriteFunc(void * buffer, size_t size, size_t nmemb, void * userp) return size * nmemb; } +static std::string escape_spaces(const std::string & url) +{ + std::string new_mod_url; + new_mod_url.reserve(url.length()); + + std::string::size_type last_pos = 0; + std::string::size_type find_pos; + + while (std::string::npos != (find_pos = url.find(" ", last_pos))) { + new_mod_url.append(url, last_pos, find_pos - last_pos); + new_mod_url += "%20"; + last_pos = find_pos + std::string(" ").length(); + } + + // Take care for the rest after last occurrence + new_mod_url.append(url, last_pos, url.length() - last_pos); + return new_mod_url; +} + MemoryResource Retriever::get(const std::string & url) { std::string mod_url = url; @@ -136,6 +155,9 @@ MemoryResource Retriever::get(const std::string & url) mod_url = "file://" + package_path + mod_url; } + // newer versions of curl do not accept spaces in URLs + mod_url = escape_spaces(mod_url); + curl_easy_setopt(curl_handle_, CURLOPT_URL, mod_url.c_str()); curl_easy_setopt(curl_handle_, CURLOPT_WRITEFUNCTION, curlWriteFunc);