Merge pull request #2418 from nghttp2/utos-require-unsigned

Make util::utos require std::unsigned_integral
This commit is contained in:
Tatsuhiro Tsujikawa
2025-05-24 17:58:49 +09:00
committed by GitHub
10 changed files with 78 additions and 64 deletions

View File

@@ -946,7 +946,7 @@ int Http2Handler::submit_file_response(const StringRef &status, Stream *stream,
if (!get_config()->no_content_length) {
nva[nvlen++] = http2::make_field(
"content-length"_sr,
util::make_string_ref_uint(stream->balloc, file_length));
util::make_string_ref_uint(stream->balloc, as_unsigned(file_length)));
}
if (last_modified != 0) {
last_modified_str = util::format_http_date(
@@ -1158,7 +1158,7 @@ void prepare_status_response(Stream *stream, Http2Handler *hd, int status) {
headers.emplace_back("content-type"_sr, "text/html; charset=UTF-8"_sr);
headers.emplace_back(
"content-length"_sr,
util::make_string_ref_uint(stream->balloc, file_ent->length));
util::make_string_ref_uint(stream->balloc, as_unsigned(file_ent->length)));
hd->submit_response(StringRef{status_page->status}, stream->stream_id,
headers, &data_prd);
}
@@ -1183,8 +1183,9 @@ void prepare_echo_response(Stream *stream, Http2Handler *hd) {
HeaderRefs headers;
headers.emplace_back("nghttpd-response"_sr, "echo"_sr);
if (!hd->get_config()->no_content_length) {
headers.emplace_back("content-length"_sr,
util::make_string_ref_uint(stream->balloc, length));
headers.emplace_back(
"content-length"_sr,
util::make_string_ref_uint(stream->balloc, as_unsigned(length)));
}
hd->submit_response("200"_sr, stream->stream_id, headers, &data_prd);
@@ -1978,7 +1979,7 @@ FileEntry make_status_body(int status, uint16_t port) {
assert(0);
}
return FileEntry(util::utos(status), nwrite, 0, fd, nullptr, {});
return FileEntry(util::utos(as_unsigned(status)), nwrite, 0, fd, nullptr, {});
}
} // namespace

View File

@@ -1058,16 +1058,16 @@ void Client::on_stream_close(int32_t stream_id, bool success, bool final) {
std::array<uint8_t, 256> buf;
auto p = std::begin(buf);
p = util::utos(start.count(), p);
p = util::utos(as_unsigned(start.count()), p);
*p++ = '\t';
if (success) {
p = util::utos(req_stat->status, p);
p = util::utos(as_unsigned(req_stat->status), p);
} else {
*p++ = '-';
*p++ = '1';
}
*p++ = '\t';
p = util::utos(delta.count(), p);
p = util::utos(as_unsigned(delta.count()), p);
*p++ = '\n';
auto nwrite = static_cast<size_t>(std::distance(std::begin(buf), p));
@@ -3101,7 +3101,7 @@ int main(int argc, char **argv) {
std::string content_length_str;
if (config.data_fd != -1) {
content_length_str = util::utos(config.data_length);
content_length_str = util::utos(as_unsigned(config.data_length));
}
auto method_it =
@@ -3334,7 +3334,7 @@ int main(int argc, char **argv) {
std::cout << std::fixed << std::setprecision(2) << R"(
finished in )"
<< util::format_duration(duration) << ", " << rps << " req/s, "
<< util::utos_funit(bps) << R"(B/s
<< util::utos_funit(as_unsigned(bps)) << R"(B/s
requests: )" << stats.req_todo
<< " total, " << stats.req_started << " started, " << stats.req_done
<< " done, " << stats.req_status_success << " succeeded, "
@@ -3343,11 +3343,12 @@ requests: )" << stats.req_todo
status codes: )"
<< stats.status[2] << " 2xx, " << stats.status[3] << " 3xx, "
<< stats.status[4] << " 4xx, " << stats.status[5] << R"( 5xx
traffic: )" << util::utos_funit(stats.bytes_total)
traffic: )" << util::utos_funit(as_unsigned(stats.bytes_total))
<< "B (" << stats.bytes_total << ") total, "
<< util::utos_funit(stats.bytes_head) << "B (" << stats.bytes_head
<< ") headers (space savings " << header_space_savings * 100
<< "%), " << util::utos_funit(stats.bytes_body) << "B ("
<< util::utos_funit(as_unsigned(stats.bytes_head)) << "B ("
<< stats.bytes_head << ") headers (space savings "
<< header_space_savings * 100 << "%), "
<< util::utos_funit(as_unsigned(stats.bytes_body)) << "B ("
<< stats.bytes_body << R"() data)" << std::endl;
#ifdef ENABLE_HTTP3
if (config.is_quic()) {

View File

@@ -438,7 +438,7 @@ int submit_request(HttpClient *client, const Headers &headers, Request *req) {
if (req->data_prd) {
if (!config.no_content_length) {
build_headers.emplace_back("content-length",
util::utos(req->data_length));
util::utos(as_unsigned(req->data_length)));
}
if (config.expect_continue) {
expect_continue = true;
@@ -1574,8 +1574,9 @@ void HttpClient::output_har(FILE *outfile) {
json_object_set_new(timings, "receive", json_real(receive_delta));
json_object_set_new(entry, "pageref", json_string(PAGE_ID));
json_object_set_new(entry, "connection",
json_string(util::utos(req->stream_id).c_str()));
json_object_set_new(
entry, "connection",
json_string(util::utos(as_unsigned(req->stream_id)).c_str()));
}
json_dumpf(root, outfile, JSON_PRESERVE_ORDER | JSON_INDENT(2));
@@ -2156,7 +2157,7 @@ id responseEnd requestStart process code size request path)"
<< ("+" + util::format_duration(request_start)) << " "
<< std::setw(8) << util::format_duration(total) << " "
<< std::setw(4) << req->status << " " << std::setw(4)
<< util::utos_unit(req->response_len) << " "
<< util::utos_unit(as_unsigned(req->response_len)) << " "
<< req->make_reqpath() << std::endl;
}
}

View File

@@ -389,7 +389,7 @@ int save_pid() {
return -1;
}
auto content = util::utos(config->pid) + '\n';
auto content = util::utos(as_unsigned(config->pid)) + '\n';
if (write(fd, content.c_str(), content.size()) == -1) {
auto error = errno;
@@ -535,7 +535,7 @@ void exec_binary() {
auto s = std::string{ENV_ACCEPT_PREFIX};
s += util::utos(i + 1);
s += "=unix,";
s += util::utos(addr.fd);
s += util::utos(as_unsigned(addr.fd));
s += ',';
s += addr.host;
@@ -545,7 +545,7 @@ void exec_binary() {
auto ipc_fd_str = std::string{ENV_ORIG_PID};
ipc_fd_str += '=';
ipc_fd_str += util::utos(config->pid);
ipc_fd_str += util::utos(as_unsigned(config->pid));
envp[envidx++] = const_cast<char *>(ipc_fd_str.c_str());
#ifdef ENABLE_HTTP3
@@ -555,7 +555,7 @@ void exec_binary() {
auto s = std::string{ENV_QUIC_WORKER_PROCESS_PREFIX};
s += util::utos(i + 1);
s += '=';
s += util::utos(wp->quic_ipc_fd);
s += util::utos(as_unsigned(wp->quic_ipc_fd));
for (auto &wid : wp->worker_ids) {
s += ',';
s += util::format_hex(as_uint8_span(std::span{&wid, 1}));
@@ -3205,12 +3205,14 @@ HTTP/3 and QUIC:
Sets the per-stream initial window size of HTTP/3
frontend connection.
Default: )"
<< util::utos_unit(config->http3.upstream.window_size) << R"(
<< util::utos_unit(as_unsigned(config->http3.upstream.window_size)) << R"(
--frontend-http3-connection-window-size=<SIZE>
Sets the per-connection window size of HTTP/3 frontend
connection.
Default: )"
<< util::utos_unit(config->http3.upstream.connection_window_size) << R"(
<< util::utos_unit(
as_unsigned(config->http3.upstream.connection_window_size))
<< R"(
--frontend-http3-max-window-size=<SIZE>
Sets the maximum per-stream window size of HTTP/3
frontend connection. The window size is adjusted based
@@ -3218,7 +3220,8 @@ HTTP/3 and QUIC:
is the value specified by --frontend-http3-window-size
and the window size grows up to <SIZE> bytes.
Default: )"
<< util::utos_unit(config->http3.upstream.max_window_size) << R"(
<< util::utos_unit(as_unsigned(config->http3.upstream.max_window_size))
<< R"(
--frontend-http3-max-connection-window-size=<SIZE>
Sets the maximum per-connection window size of HTTP/3
frontend connection. The window size is adjusted based
@@ -3227,7 +3230,8 @@ HTTP/3 and QUIC:
--frontend-http3-connection-window-size and the window
size grows up to <SIZE> bytes.
Default: )"
<< util::utos_unit(config->http3.upstream.max_connection_window_size)
<< util::utos_unit(
as_unsigned(config->http3.upstream.max_connection_window_size))
<< R"(
--frontend-http3-max-concurrent-streams=<N>
Set the maximum number of the concurrent streams in one

View File

@@ -403,7 +403,8 @@ std::span<char> copy_hex_low(std::span<const uint8_t> src,
} // namespace
namespace {
template <std::integral T> std::span<char> copy(T n, std::span<char> dest) {
template <std::unsigned_integral T>
std::span<char> copy(T n, std::span<char> dest) {
if (dest.size() < NGHTTP2_MAX_UINT64_DIGITS) {
return dest.first(0);
}
@@ -892,7 +893,7 @@ void log_chld(pid_t pid, int rstatus, const char *msg) {
} else {
signalstr += "UNKNOWN(";
}
signalstr += util::utos(sig);
signalstr += util::utos(as_unsigned(sig));
signalstr += ')';
}

View File

@@ -239,7 +239,8 @@ mrb_value response_return(mrb_state *mrb, mrb_value self) {
resp.fs.content_length = -1;
} else {
auto content_length = util::make_string_ref_uint(balloc, vallen);
auto content_length =
util::make_string_ref_uint(balloc, as_unsigned(vallen));
if (cl) {
cl->value = content_length;

View File

@@ -505,6 +505,11 @@ requires(sizeof(std::iter_value_t<I>) == sizeof(StringRef::value_type))
reinterpret_cast<StringRef::const_pointer>(std::to_address(first)), n};
}
template <std::integral T>
[[nodiscard]] constexpr auto as_unsigned(T n) noexcept {
return static_cast<std::make_unsigned_t<T>>(n);
}
inline int run_app(std::function<int(int, char **)> app, int argc,
char **argv) {
try {

View File

@@ -1373,11 +1373,11 @@ std::string duration_str(double t) {
if (t == 0.) {
return "0";
}
auto frac = static_cast<int64_t>(t * 1000) % 1000;
auto frac = static_cast<uint64_t>(t * 1000) % 1000;
if (frac > 0) {
return utos(static_cast<int64_t>(t * 1000)) + "ms";
return utos(static_cast<uint64_t>(t * 1000)) + "ms";
}
auto v = static_cast<int64_t>(t);
auto v = static_cast<uint64_t>(t);
if (v % 60) {
return utos(v) + "s";
}
@@ -1392,7 +1392,7 @@ std::string duration_str(double t) {
std::string format_duration(const std::chrono::microseconds &u) {
const char *unit = "us";
int d = 0;
auto t = u.count();
auto t = as_unsigned(u.count());
if (t >= 1000000) {
d = 1000000;
unit = "s";
@@ -1414,13 +1414,13 @@ std::string format_duration(double t) {
unit = "ms";
} else {
t *= 1000000.;
return utos(static_cast<int64_t>(t)) + unit;
return utos(static_cast<uint64_t>(t)) + unit;
}
return dtos(t) + unit;
}
std::string dtos(double n) {
auto m = llround(100. * n);
auto m = as_unsigned(llround(100. * n));
auto f = utos(m % 100);
return utos(m / 100) + "." + (f.size() == 1 ? "0" : "") + f;
}

View File

@@ -731,7 +731,7 @@ constinit const auto utos_digits = []() {
return a;
}();
template <std::integral T, std::weakly_incrementable O>
template <std::unsigned_integral T, std::weakly_incrementable O>
requires(std::indirectly_writable<O, char>)
constexpr O utos(T n, O result) {
if (n < 10) {
@@ -764,7 +764,7 @@ constexpr O utos(T n, O result) {
return result;
}
template <std::integral T> constexpr std::string utos(T n) {
template <std::unsigned_integral T> constexpr std::string utos(T n) {
using namespace std::literals;
if (n == 0) {
@@ -780,7 +780,7 @@ template <std::integral T> constexpr std::string utos(T n) {
return res;
}
template <std::integral T>
template <std::unsigned_integral T>
StringRef make_string_ref_uint(BlockAllocator &balloc, T n) {
auto iov = make_byte_ref(
balloc, count_digit(static_cast<std::make_unsigned_t<T>>(n)) + 1);
@@ -792,7 +792,7 @@ StringRef make_string_ref_uint(BlockAllocator &balloc, T n) {
return as_string_ref(std::ranges::begin(iov), p);
}
template <std::integral T> constexpr std::string utos_unit(T n) {
template <std::unsigned_integral T> constexpr std::string utos_unit(T n) {
char u;
if (n >= (1 << 30)) {
@@ -812,7 +812,7 @@ template <std::integral T> constexpr std::string utos_unit(T n) {
}
// Like utos_unit(), but 2 digits fraction part is followed.
template <std::integral T> constexpr std::string utos_funit(T n) {
template <std::unsigned_integral T> constexpr std::string utos_funit(T n) {
char u;
int b;

View File

@@ -395,16 +395,16 @@ void test_util_count_digit(void) {
void test_util_utos(void) {
char buf[32];
assert_stdstring_equal("123"s, (std::string{buf, util::utos(123, buf)}));
assert_stdstring_equal("123"s, (std::string{buf, util::utos(123u, buf)}));
assert_stdstring_equal("0"s, util::utos(0));
assert_stdstring_equal("123"s, util::utos(123));
assert_stdstring_equal("0"s, util::utos(0u));
assert_stdstring_equal("123"s, util::utos(123u));
assert_stdstring_equal("123"s, util::utos(static_cast<uint8_t>(123)));
assert_stdstring_equal("123"s, util::utos(static_cast<uint16_t>(123)));
assert_stdstring_equal("18446744073709551615"s,
util::utos(18'446'744'073'709'551'615u));
assert_stdsv_equal("0"sv, (std::string_view{buf, util::utos(0, buf)}));
assert_stdsv_equal("0"sv, (std::string_view{buf, util::utos(0u, buf)}));
assert_stdsv_equal("1"sv, (std::string_view{buf, util::utos(1u, buf)}));
assert_stdsv_equal("9"sv, (std::string_view{buf, util::utos(9u, buf)}));
assert_stdsv_equal("10"sv, (std::string_view{buf, util::utos(10u, buf)}));
@@ -503,36 +503,36 @@ void test_util_utos(void) {
void test_util_make_string_ref_uint(void) {
BlockAllocator balloc(1024, 1024);
assert_stdsv_equal("0"sv, util::make_string_ref_uint(balloc, 0));
assert_stdsv_equal("123"sv, util::make_string_ref_uint(balloc, 123));
assert_stdsv_equal("0"sv, util::make_string_ref_uint(balloc, 0u));
assert_stdsv_equal("123"sv, util::make_string_ref_uint(balloc, 123u));
assert_stdsv_equal(
"18446744073709551615"sv,
util::make_string_ref_uint(balloc, 18446744073709551615ULL));
}
void test_util_utos_unit(void) {
assert_stdstring_equal("0", util::utos_unit(0));
assert_stdstring_equal("1023", util::utos_unit(1023));
assert_stdstring_equal("1K", util::utos_unit(1024));
assert_stdstring_equal("1K", util::utos_unit(1025));
assert_stdstring_equal("1M", util::utos_unit(1 << 20));
assert_stdstring_equal("1G", util::utos_unit(1 << 30));
assert_stdstring_equal("1024G", util::utos_unit(1LL << 40));
assert_stdstring_equal("0", util::utos_unit(0u));
assert_stdstring_equal("1023", util::utos_unit(1023u));
assert_stdstring_equal("1K", util::utos_unit(1024u));
assert_stdstring_equal("1K", util::utos_unit(1025u));
assert_stdstring_equal("1M", util::utos_unit(1u << 20));
assert_stdstring_equal("1G", util::utos_unit(1u << 30));
assert_stdstring_equal("1024G", util::utos_unit(1ULL << 40));
}
void test_util_utos_funit(void) {
assert_stdstring_equal("0", util::utos_funit(0));
assert_stdstring_equal("1023", util::utos_funit(1023));
assert_stdstring_equal("1.00K", util::utos_funit(1024));
assert_stdstring_equal("1.00K", util::utos_funit(1025));
assert_stdstring_equal("1.09K", util::utos_funit(1119));
assert_stdstring_equal("1.27K", util::utos_funit(1300));
assert_stdstring_equal("1.00M", util::utos_funit(1 << 20));
assert_stdstring_equal("1.18M", util::utos_funit(1234567));
assert_stdstring_equal("1.00G", util::utos_funit(1 << 30));
assert_stdstring_equal("0", util::utos_funit(0u));
assert_stdstring_equal("1023", util::utos_funit(1023u));
assert_stdstring_equal("1.00K", util::utos_funit(1024u));
assert_stdstring_equal("1.00K", util::utos_funit(1025u));
assert_stdstring_equal("1.09K", util::utos_funit(1119u));
assert_stdstring_equal("1.27K", util::utos_funit(1300u));
assert_stdstring_equal("1.00M", util::utos_funit(1u << 20));
assert_stdstring_equal("1.18M", util::utos_funit(1234567u));
assert_stdstring_equal("1.00G", util::utos_funit(1u << 30));
assert_stdstring_equal("4492450797.23G",
util::utos_funit(4823732313248234343LL));
assert_stdstring_equal("1024.00G", util::utos_funit(1LL << 40));
util::utos_funit(4823732313248234343ULL));
assert_stdstring_equal("1024.00G", util::utos_funit(1ULL << 40));
}
void test_util_parse_uint_with_unit(void) {