From 4b5179a5448221d870223e920246717a2d00e805 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 2 Oct 2016 22:41:14 +0900 Subject: [PATCH] nghttpx: Fix bug in util::make_hostport --- src/util.cc | 27 +-------------------------- src/util.h | 2 -- src/util_test.cc | 19 ++++++++++++++----- 3 files changed, 15 insertions(+), 33 deletions(-) diff --git a/src/util.cc b/src/util.cc index c6515c25..3e21628d 100644 --- a/src/util.cc +++ b/src/util.cc @@ -1147,31 +1147,6 @@ std::string dtos(double n) { return utos(static_cast(n)) + "." + (f.size() == 1 ? "0" : "") + f; } -std::string make_http_hostport(const StringRef &host, uint16_t port) { - if (port != 80 && port != 443) { - return make_hostport(host, port); - } - - auto ipv6 = ipv6_numeric_addr(host.c_str()); - - std::string hostport; - hostport.resize(host.size() + (ipv6 ? 2 : 0)); - - auto p = &hostport[0]; - - if (ipv6) { - *p++ = '['; - } - - p = std::copy_n(host.c_str(), host.size(), p); - - if (ipv6) { - *p++ = ']'; - } - - return hostport; -} - StringRef make_http_hostport(BlockAllocator &balloc, const StringRef &host, uint16_t port) { if (port != 80 && port != 443) { @@ -1244,7 +1219,7 @@ StringRef make_hostport(BlockAllocator &balloc, const StringRef &host, *p++ = ':'; - std::copy(std::begin(serv), std::end(serv), p); + p = std::copy(std::begin(serv), std::end(serv), p); *p = '\0'; diff --git a/src/util.h b/src/util.h index 95239ab8..40d95348 100644 --- a/src/util.h +++ b/src/util.h @@ -633,8 +633,6 @@ std::string format_duration(double t); // Creates "host:port" string using given |host| and |port|. If // |host| is numeric IPv6 address (e.g., ::1), it is enclosed by "[" // and "]". If |port| is 80 or 443, port part is omitted. -std::string make_http_hostport(const StringRef &host, uint16_t port); - StringRef make_http_hostport(BlockAllocator &balloc, const StringRef &host, uint16_t port); diff --git a/src/util_test.cc b/src/util_test.cc index d9bce306..c8934402 100644 --- a/src/util_test.cc +++ b/src/util_test.cc @@ -502,12 +502,15 @@ void test_util_parse_config_str_list(void) { } void test_util_make_http_hostport(void) { - CU_ASSERT("localhost" == - util::make_http_hostport(StringRef::from_lit("localhost"), 80)); + BlockAllocator balloc(4096, 4096); + + CU_ASSERT("localhost" == util::make_http_hostport( + balloc, StringRef::from_lit("localhost"), 80)); CU_ASSERT("[::1]" == - util::make_http_hostport(StringRef::from_lit("::1"), 443)); - CU_ASSERT("localhost:3000" == - util::make_http_hostport(StringRef::from_lit("localhost"), 3000)); + util::make_http_hostport(balloc, StringRef::from_lit("::1"), 443)); + CU_ASSERT( + "localhost:3000" == + util::make_http_hostport(balloc, StringRef::from_lit("localhost"), 3000)); } void test_util_make_hostport(void) { @@ -515,6 +518,12 @@ void test_util_make_hostport(void) { util::make_hostport(StringRef::from_lit("localhost"), 80)); CU_ASSERT("[::1]:443" == util::make_hostport(StringRef::from_lit("::1"), 443)); + + BlockAllocator balloc(4096, 4096); + CU_ASSERT("localhost:80" == + util::make_hostport(balloc, StringRef::from_lit("localhost"), 80)); + CU_ASSERT("[::1]:443" == + util::make_hostport(balloc, StringRef::from_lit("::1"), 443)); } void test_util_strifind(void) {