From d286f658fc396945004860259fee3511a5b671d0 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Mon, 19 Jul 2021 12:50:20 +0200 Subject: [PATCH] HTTP storage: Fix crash when not specifying port MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Url::port(const std::string&) apparently doesn’t accept an empty string as the port (even though Url::port() can return an empty string), so work around that. --- src/storage/secondary/HttpStorage.cpp | 29 +++++++++++++++------------ 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/storage/secondary/HttpStorage.cpp b/src/storage/secondary/HttpStorage.cpp index 891fa251c..c0259a51b 100644 --- a/src/storage/secondary/HttpStorage.cpp +++ b/src/storage/secondary/HttpStorage.cpp @@ -103,19 +103,26 @@ get_url_path(const Url& url) return path; } +Url +get_partial_url(const Url& from_url) +{ + Url url; + url.host(from_url.host(), from_url.ip_version()); + if (!from_url.port().empty()) { + url.port(from_url.port()); + } + return url; +} + std::string get_host_header_value(const Url& url) { // We need to construct an HTTP Host header that follows the same IPv6 - // escaping rules like a URL. To avoid code duplication we re-use the - // Url class to render that string. - - Url host_and_port_only; - host_and_port_only.host(url.host(), url.ip_version()).port(url.port()); + // escaping rules like a URL. + const auto rendered_value = get_partial_url(url).str(); - // The rendered_value now contains a string like '//[::1]:8080'. The trailing + // The rendered_value now contains a string like "//[::1]:8080". The leading // slashes must be stripped. - const auto rendered_value = host_and_port_only.str(); const auto prefix = nonstd::string_view{"//"}; if (!util::starts_with(rendered_value, prefix)) { throw Fatal(R"(Expected partial URL "{}" to start with "{}")", @@ -132,12 +139,8 @@ get_url(const Url& url) throw Fatal("A host is required in HTTP storage URL \"{}\"", url.str()); } - // httplib requires a partial URL with just scheme, host and port. - Url destination; - destination.scheme(url.scheme()) - .host(url.host(), url.ip_version()) - .port(url.port()); - return destination.str(); + // httplib requires a partial URL with just scheme, host and port. + return get_partial_url(url).scheme(url.scheme()).str(); } HttpStorageBackend::HttpStorageBackend(const Params& params) -- 2.47.3