Remove util::make_hostport returning std::string

This commit is contained in:
Tatsuhiro Tsujikawa 2021-09-12 18:06:55 +09:00
parent 00f65afe20
commit 525d59fdf6
5 changed files with 17 additions and 27 deletions

View File

@ -3479,9 +3479,12 @@ int process_options(Config *config,
return -1; return -1;
} }
std::array<char, util::max_hostport> hostport_buf;
auto &proxy = config->downstream_http_proxy; auto &proxy = config->downstream_http_proxy;
if (!proxy.host.empty()) { 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, if (resolve_hostname(&proxy.addr, proxy.host.c_str(), proxy.port,
AF_UNSPEC) == -1) { AF_UNSPEC) == -1) {
LOG(FATAL) << "Resolving backend HTTP proxy address failed: " << hostport; LOG(FATAL) << "Resolving backend HTTP proxy address failed: " << hostport;
@ -3494,7 +3497,8 @@ int process_options(Config *config,
{ {
auto &memcachedconf = tlsconf.session_cache.memcached; auto &memcachedconf = tlsconf.session_cache.memcached;
if (!memcachedconf.host.empty()) { 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); memcachedconf.port);
if (resolve_hostname(&memcachedconf.addr, memcachedconf.host.c_str(), if (resolve_hostname(&memcachedconf.addr, memcachedconf.host.c_str(),
memcachedconf.port, memcachedconf.family) == -1) { memcachedconf.port, memcachedconf.family) == -1) {
@ -3515,7 +3519,8 @@ int process_options(Config *config,
{ {
auto &memcachedconf = tlsconf.ticket.memcached; auto &memcachedconf = tlsconf.ticket.memcached;
if (!memcachedconf.host.empty()) { 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); memcachedconf.port);
if (resolve_hostname(&memcachedconf.addr, memcachedconf.host.c_str(), if (resolve_hostname(&memcachedconf.addr, memcachedconf.host.c_str(),
memcachedconf.port, memcachedconf.family) == -1) { memcachedconf.port, memcachedconf.family) == -1) {

View File

@ -4332,6 +4332,8 @@ int configure_downstream_group(Config *config, bool http2_proxy,
auto resolve_flags = numeric_addr_only ? AI_NUMERICHOST | AI_NUMERICSERV : 0; auto resolve_flags = numeric_addr_only ? AI_NUMERICHOST | AI_NUMERICSERV : 0;
std::array<char, util::max_hostport> hostport_buf;
for (auto &g : addr_groups) { for (auto &g : addr_groups) {
std::unordered_map<StringRef, uint32_t> wgchk; std::unordered_map<StringRef, uint32_t> wgchk;
for (auto &addr : g.addrs) { 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); util::make_http_hostport(downstreamconf.balloc, addr.host, addr.port);
auto hostport = 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 (!addr.dns) {
if (resolve_hostname(&addr.addr, addr.host.c_str(), addr.port, if (resolve_hostname(&addr.addr, addr.host.c_str(), addr.port,

View File

@ -1329,18 +1329,6 @@ StringRef make_http_hostport(BlockAllocator &balloc, const StringRef &host,
return make_http_hostport(iov.base, host, port); 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, StringRef make_hostport(BlockAllocator &balloc, const StringRef &host,
uint16_t port) { uint16_t port) {
auto iov = make_byte_ref(balloc, host.size() + 2 + 1 + 5 + 1); auto iov = make_byte_ref(balloc, host.size() + 2 + 1 + 5 + 1);

View File

@ -764,8 +764,6 @@ std::string format_duration(double t);
// Just like make_http_hostport(), but doesn't treat 80 and 443 // Just like make_http_hostport(), but doesn't treat 80 and 443
// specially. // specially.
std::string make_hostport(const StringRef &host, uint16_t port);
StringRef make_hostport(BlockAllocator &balloc, const StringRef &host, StringRef make_hostport(BlockAllocator &balloc, const StringRef &host,
uint16_t port); uint16_t port);

View File

@ -540,22 +540,19 @@ void test_util_make_http_hostport(void) {
} }
void test_util_make_hostport(void) { void test_util_make_hostport(void) {
std::array<char, util::max_hostport> hostport_buf;
CU_ASSERT("localhost:80" == CU_ASSERT("localhost:80" ==
util::make_hostport(StringRef::from_lit("localhost"), 80)); util::make_hostport(std::begin(hostport_buf),
CU_ASSERT("[::1]:443" == StringRef::from_lit("localhost"), 80));
util::make_hostport(StringRef::from_lit("::1"), 443)); CU_ASSERT("[::1]:443" == util::make_hostport(std::begin(hostport_buf),
StringRef::from_lit("::1"),
443));
BlockAllocator balloc(4096, 4096); BlockAllocator balloc(4096, 4096);
CU_ASSERT("localhost:80" == CU_ASSERT("localhost:80" ==
util::make_hostport(balloc, StringRef::from_lit("localhost"), 80)); util::make_hostport(balloc, StringRef::from_lit("localhost"), 80));
CU_ASSERT("[::1]:443" == CU_ASSERT("[::1]:443" ==
util::make_hostport(balloc, StringRef::from_lit("::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) { void test_util_strifind(void) {