nghttpx: Refactor split_host_port
This commit is contained in:
parent
144ae3af9d
commit
44ee67ff71
|
@ -78,31 +78,30 @@ TicketKeys::~TicketKeys() {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
int split_host_port(char *host, size_t hostlen, uint16_t *port_ptr,
|
int split_host_port(char *host, size_t hostlen, uint16_t *port_ptr,
|
||||||
const char *hostport, size_t hostportlen) {
|
const StringRef &hostport, const char *opt) {
|
||||||
// host and port in |hostport| is separated by single ','.
|
// host and port in |hostport| is separated by single ','.
|
||||||
const char *p = strchr(hostport, ',');
|
auto sep = std::find(std::begin(hostport), std::end(hostport), ',');
|
||||||
if (!p) {
|
if (sep == std::end(hostport)) {
|
||||||
LOG(ERROR) << "Invalid host, port: " << hostport;
|
LOG(ERROR) << opt << ": Invalid host, port: " << hostport;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
size_t len = p - hostport;
|
size_t len = sep - std::begin(hostport);
|
||||||
if (hostlen < len + 1) {
|
if (hostlen < len + 1) {
|
||||||
LOG(ERROR) << "Hostname too long: " << hostport;
|
LOG(ERROR) << opt << ": Hostname too long: " << hostport;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
memcpy(host, hostport, len);
|
std::copy(std::begin(hostport), sep, host);
|
||||||
host[len] = '\0';
|
host[len] = '\0';
|
||||||
|
|
||||||
errno = 0;
|
auto portstr = StringRef{sep + 1, std::end(hostport)};
|
||||||
auto portlen = hostportlen - len - 1;
|
auto d = util::parse_uint(portstr);
|
||||||
auto d = util::parse_uint(reinterpret_cast<const uint8_t *>(p + 1), portlen);
|
|
||||||
if (1 <= d && d <= std::numeric_limits<uint16_t>::max()) {
|
if (1 <= d && d <= std::numeric_limits<uint16_t>::max()) {
|
||||||
*port_ptr = d;
|
*port_ptr = d;
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
|
||||||
LOG(ERROR) << "Port is invalid: " << std::string(p + 1, portlen);
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LOG(ERROR) << opt << ": Port is invalid: " << portstr;
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
@ -1753,8 +1752,8 @@ int parse_config(const char *opt, const char *optarg,
|
||||||
addr.host = ImmutableString(path, addr_end);
|
addr.host = ImmutableString(path, addr_end);
|
||||||
addr.host_unix = true;
|
addr.host_unix = true;
|
||||||
} else {
|
} else {
|
||||||
if (split_host_port(host, sizeof(host), &port, src.c_str(),
|
if (split_host_port(host, sizeof(host), &port,
|
||||||
addr_end - std::begin(src)) == -1) {
|
StringRef{std::begin(src), addr_end}, opt) == -1) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1802,8 +1801,8 @@ int parse_config(const char *opt, const char *optarg,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (split_host_port(host, sizeof(host), &port, src.c_str(),
|
if (split_host_port(host, sizeof(host), &port,
|
||||||
addr_end - std::begin(src)) == -1) {
|
StringRef{std::begin(src), addr_end}, opt) == -1) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2451,8 +2450,8 @@ int parse_config(const char *opt, const char *optarg,
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (split_host_port(host, sizeof(host), &port, src.c_str(),
|
if (split_host_port(host, sizeof(host), &port,
|
||||||
addr_end - std::begin(src)) == -1) {
|
StringRef{std::begin(src), addr_end}, opt) == -1) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue