diff --git a/src/shrpx.cc b/src/shrpx.cc index 15d11867..ed7ff18f 100644 --- a/src/shrpx.cc +++ b/src/shrpx.cc @@ -3479,9 +3479,12 @@ int process_options(Config *config, return -1; } + std::array hostport_buf; + auto &proxy = config->downstream_http_proxy; if (!proxy.host.empty()) { - auto hostport = util::make_hostport(StringRef{proxy.host}, proxy.port); + auto hostport = util::make_hostport(std::begin(hostport_buf), + StringRef{proxy.host}, proxy.port); if (resolve_hostname(&proxy.addr, proxy.host.c_str(), proxy.port, AF_UNSPEC) == -1) { LOG(FATAL) << "Resolving backend HTTP proxy address failed: " << hostport; @@ -3494,7 +3497,8 @@ int process_options(Config *config, { auto &memcachedconf = tlsconf.session_cache.memcached; if (!memcachedconf.host.empty()) { - auto hostport = util::make_hostport(StringRef{memcachedconf.host}, + auto hostport = util::make_hostport(std::begin(hostport_buf), + StringRef{memcachedconf.host}, memcachedconf.port); if (resolve_hostname(&memcachedconf.addr, memcachedconf.host.c_str(), memcachedconf.port, memcachedconf.family) == -1) { @@ -3515,7 +3519,8 @@ int process_options(Config *config, { auto &memcachedconf = tlsconf.ticket.memcached; if (!memcachedconf.host.empty()) { - auto hostport = util::make_hostport(StringRef{memcachedconf.host}, + auto hostport = util::make_hostport(std::begin(hostport_buf), + StringRef{memcachedconf.host}, memcachedconf.port); if (resolve_hostname(&memcachedconf.addr, memcachedconf.host.c_str(), memcachedconf.port, memcachedconf.family) == -1) { diff --git a/src/shrpx_config.cc b/src/shrpx_config.cc index f07e48dd..a9886119 100644 --- a/src/shrpx_config.cc +++ b/src/shrpx_config.cc @@ -4332,6 +4332,8 @@ int configure_downstream_group(Config *config, bool http2_proxy, auto resolve_flags = numeric_addr_only ? AI_NUMERICHOST | AI_NUMERICSERV : 0; + std::array hostport_buf; + for (auto &g : addr_groups) { std::unordered_map wgchk; for (auto &addr : g.addrs) { @@ -4377,7 +4379,7 @@ int configure_downstream_group(Config *config, bool http2_proxy, util::make_http_hostport(downstreamconf.balloc, addr.host, addr.port); auto hostport = - util::make_hostport(downstreamconf.balloc, addr.host, addr.port); + util::make_hostport(std::begin(hostport_buf), addr.host, addr.port); if (!addr.dns) { if (resolve_hostname(&addr.addr, addr.host.c_str(), addr.port, diff --git a/src/util.cc b/src/util.cc index 97af8d79..4491a07d 100644 --- a/src/util.cc +++ b/src/util.cc @@ -1329,18 +1329,6 @@ StringRef make_http_hostport(BlockAllocator &balloc, const StringRef &host, return make_http_hostport(iov.base, host, port); } -std::string make_hostport(const StringRef &host, uint16_t port) { - std::string hostport; - // I'm not sure we can write \0 at the position std::string::size(), - // so allocate an extra byte. - hostport.resize(host.size() + 2 + 1 + 5 + 1); - - auto s = make_hostport(std::begin(hostport), host, port); - hostport.resize(s.size()); - - return hostport; -} - StringRef make_hostport(BlockAllocator &balloc, const StringRef &host, uint16_t port) { auto iov = make_byte_ref(balloc, host.size() + 2 + 1 + 5 + 1); diff --git a/src/util.h b/src/util.h index d4990e03..9c34d9e6 100644 --- a/src/util.h +++ b/src/util.h @@ -764,8 +764,6 @@ std::string format_duration(double t); // Just like make_http_hostport(), but doesn't treat 80 and 443 // specially. -std::string make_hostport(const StringRef &host, uint16_t port); - StringRef make_hostport(BlockAllocator &balloc, const StringRef &host, uint16_t port); diff --git a/src/util_test.cc b/src/util_test.cc index d5ff942f..17d640d5 100644 --- a/src/util_test.cc +++ b/src/util_test.cc @@ -540,22 +540,19 @@ void test_util_make_http_hostport(void) { } void test_util_make_hostport(void) { + std::array hostport_buf; CU_ASSERT("localhost:80" == - util::make_hostport(StringRef::from_lit("localhost"), 80)); - CU_ASSERT("[::1]:443" == - util::make_hostport(StringRef::from_lit("::1"), 443)); + util::make_hostport(std::begin(hostport_buf), + StringRef::from_lit("localhost"), 80)); + CU_ASSERT("[::1]:443" == util::make_hostport(std::begin(hostport_buf), + 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)); - - // Check std::string version - CU_ASSERT( - "abcdefghijklmnopqrstuvwxyz0123456789:65535" == - util::make_hostport( - StringRef::from_lit("abcdefghijklmnopqrstuvwxyz0123456789"), 65535)); } void test_util_strifind(void) {