From: Joel Rosdahl Date: Mon, 19 Jul 2021 10:50:20 +0000 (+0200) Subject: HTTP storage: Fix crash when not specifying port X-Git-Tag: v4.4~111 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d286f658fc396945004860259fee3511a5b671d0;p=thirdparty%2Fccache.git HTTP storage: Fix crash when not specifying port 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. --- 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)