From 652210e901f5e1e9bf8e25d35423348de8e50c1a Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 28 Nov 2018 13:30:13 -0500 Subject: cmSystemTools: Add EncodeURL helper Factor a URL encoding implementation out of CTest. Add an option to not escape slashes. Suggested-by: Daniel Pfeifer --- Source/CTest/cmCTestSubmitHandler.cxx | 21 +-------------------- Source/cmSystemTools.cxx | 29 +++++++++++++++++++++++++++++ Source/cmSystemTools.h | 4 ++++ 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/Source/CTest/cmCTestSubmitHandler.cxx b/Source/CTest/cmCTestSubmitHandler.cxx index 061c8ef..cc3a105 100644 --- a/Source/CTest/cmCTestSubmitHandler.cxx +++ b/Source/CTest/cmCTestSubmitHandler.cxx @@ -407,26 +407,7 @@ bool cmCTestSubmitHandler::SubmitUsingHTTP( *this->LogFile << "\tUpload file: " << local_file << " to " << remote_file << std::endl; - std::string ofile; - for (char c : remote_file) { - char hexCh[4] = { 0, 0, 0, 0 }; - hexCh[0] = c; - switch (c) { - case '+': - case '?': - case '/': - case '\\': - case '&': - case ' ': - case '=': - case '%': - sprintf(hexCh, "%%%02X", static_cast(c)); - ofile.append(hexCh); - break; - default: - ofile.append(hexCh); - } - } + std::string ofile = cmSystemTools::EncodeURL(remote_file); std::string upload_as = url + ((url.find('?') == std::string::npos) ? '?' : '&') + "FileName=" + ofile; diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index 28aa57c..6fbe482 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -3009,6 +3009,35 @@ bool cmSystemTools::StringToULong(const char* str, unsigned long* value) return (*endp == '\0') && (endp != str) && (errno == 0); } +std::string cmSystemTools::EncodeURL(std::string const& in, bool escapeSlashes) +{ + std::string out; + for (char c : in) { + char hexCh[4] = { 0, 0, 0, 0 }; + hexCh[0] = c; + switch (c) { + case '+': + case '?': + case '\\': + case '&': + case ' ': + case '=': + case '%': + sprintf(hexCh, "%%%02X", static_cast(c)); + break; + case '/': + if (escapeSlashes) { + strcpy(hexCh, "%2F"); + } + break; + default: + break; + } + out.append(hexCh); + } + return out; +} + bool cmSystemTools::CreateSymlink(const std::string& origName, const std::string& newName) { diff --git a/Source/cmSystemTools.h b/Source/cmSystemTools.h index 98300eb..832c1ca 100644 --- a/Source/cmSystemTools.h +++ b/Source/cmSystemTools.h @@ -495,6 +495,10 @@ public: static bool StringToLong(const char* str, long* value); static bool StringToULong(const char* str, unsigned long* value); + /** Encode a string as a URL. */ + static std::string EncodeURL(std::string const& in, + bool escapeSlashes = true); + #ifdef _WIN32 struct WindowsFileRetry { -- cgit v0.12 From 7954ba9bc196b34ced6c3359c464afbd0678c2e0 Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 28 Nov 2018 13:34:45 -0500 Subject: productbuild: escape pkg-ref urls Inspired-by: James Goruk Fixes: #18645 --- Source/CPack/cmCPackPKGGenerator.cxx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Source/CPack/cmCPackPKGGenerator.cxx b/Source/CPack/cmCPackPKGGenerator.cxx index bdda386..9401bca 100644 --- a/Source/CPack/cmCPackPKGGenerator.cxx +++ b/Source/CPack/cmCPackPKGGenerator.cxx @@ -225,7 +225,8 @@ void cmCPackPKGGenerator::CreateChoice(const cmCPackComponent& component, xout.Content(this->GetPackageName(component)); } else { xout.Content("file:./"); - xout.Content(relativePackageLocation); + xout.Content(cmSystemTools::EncodeURL(relativePackageLocation, + /*escapeSlashes=*/false)); } xout.EndElement(); // pkg-ref } -- cgit v0.12