From e0d38508fc8b6bc3915b2235a85a068eacfb87bf Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Tue, 11 Jun 2024 18:46:31 +0200 Subject: Merging upstream version 1.62.1. Signed-off-by: Daniel Baumann --- src/nghttp.cc | 116 +++++++++++++++++++++++++++++++--------------------------- 1 file changed, 62 insertions(+), 54 deletions(-) (limited to 'src/nghttp.cc') diff --git a/src/nghttp.cc b/src/nghttp.cc index 6684374..8ca37bb 100644 --- a/src/nghttp.cc +++ b/src/nghttp.cc @@ -89,13 +89,13 @@ enum { }; namespace { -constexpr auto anchors = std::array{{ +constexpr auto anchors = std::to_array({ {3, 0, 201}, {5, 0, 101}, {7, 0, 1}, {9, 7, 1}, {11, 3, 1}, -}}; +}); } // namespace Config::Config() @@ -201,9 +201,9 @@ StringRef Request::get_real_host() const { uint16_t Request::get_real_port() const { auto scheme = get_real_scheme(); return config.host_override.empty() ? util::has_uri_field(u, UF_PORT) ? u.port - : scheme == "https" ? 443 + : scheme == "https"_sr ? 443 : 80 - : config.port_override == 0 ? scheme == "https" ? 443 : 80 + : config.port_override == 0 ? scheme == "https"_sr ? 443 : 80 : config.port_override; } @@ -215,7 +215,7 @@ void Request::init_html_parser() { auto ipv6_lit = std::find(std::begin(host), std::end(host), ':') != std::end(host); - auto base_uri = scheme.str(); + auto base_uri = std::string{scheme}; base_uri += "://"; if (ipv6_lit) { base_uri += '['; @@ -224,8 +224,8 @@ void Request::init_html_parser() { if (ipv6_lit) { base_uri += ']'; } - if (!((scheme == "https" && port == 443) || - (scheme == "http" && port == 80))) { + if (!((scheme == "https"_sr && port == 443) || + (scheme == "http"_sr && port == 80))) { base_uri += ':'; base_uri += util::utos(port); } @@ -247,9 +247,9 @@ int Request::update_html_parser(const uint8_t *data, size_t len, int fin) { } std::string Request::make_reqpath() const { - std::string path = util::has_uri_field(u, UF_PATH) - ? util::get_uri_field(uri.c_str(), u, UF_PATH).str() - : "/"; + auto path = util::has_uri_field(u, UF_PATH) + ? std::string{util::get_uri_field(uri.c_str(), u, UF_PATH)} + : "/"s; if (util::has_uri_field(u, UF_QUERY)) { path += '?'; path.append(uri.c_str() + u.field_data[UF_QUERY].off, @@ -266,15 +266,15 @@ std::string decode_host(const StringRef &host) { if (zone_start == std::end(host) || !util::ipv6_numeric_addr( std::string(std::begin(host), zone_start).c_str())) { - return host.str(); + return std::string{host}; } // case: ::1% if (zone_start + 1 == std::end(host)) { - return StringRef{host.c_str(), host.size() - 1}.str(); + return {host.data(), host.size() - 1}; } // case: ::1%12 or ::1%1 if (zone_start + 3 >= std::end(host)) { - return host.str(); + return std::string{host}; } // If we see "%25", followed by more characters, then decode %25 as // '%'. @@ -454,7 +454,7 @@ int submit_request(HttpClient *client, const Headers &headers, Request *req) { auto scheme = util::get_uri_field(req->uri.c_str(), req->u, UF_SCHEMA); auto build_headers = Headers{{":method", req->data_prd ? "POST" : "GET"}, {":path", req->make_reqpath()}, - {":scheme", scheme.str()}, + {":scheme", std::string{scheme}}, {":authority", client->hostport}, {"accept", "*/*"}, {"accept-encoding", "gzip, deflate"}, @@ -500,7 +500,8 @@ int submit_request(HttpClient *client, const Headers &headers, Request *req) { nva.reserve(build_headers.size()); for (auto &kv : build_headers) { - nva.push_back(http2::make_nv(kv.name, kv.value, kv.no_index)); + nva.push_back( + http2::make_field_nv(kv.name, kv.value, http2::no_index(kv.no_index))); } auto method = http2::get_header(build_headers, ":method"); @@ -515,7 +516,7 @@ int submit_request(HttpClient *client, const Headers &headers, Request *req) { trailer_names += ", "; trailer_names += config.trailer[i].name; } - nva.push_back(http2::make_nv_ls("trailer", trailer_names)); + nva.push_back(http2::make_field_v("trailer"_sr, trailer_names)); } int32_t stream_id; @@ -975,9 +976,9 @@ int HttpClient::on_upgrade_connect() { // If the request contains upload data, use OPTIONS * to upgrade req = "OPTIONS *"; } else { - auto meth = std::find_if( - std::begin(config.headers), std::end(config.headers), - [](const Header &kv) { return util::streq_l(":method", kv.name); }); + auto meth = + std::find_if(std::begin(config.headers), std::end(config.headers), + [](const auto &kv) { return ":method"_sr == kv.name; }); if (meth == std::end(config.headers)) { req = "GET "; @@ -1424,8 +1425,7 @@ void HttpClient::update_hostport() { if (reqvec.empty()) { return; } - scheme = util::get_uri_field(reqvec[0]->uri.c_str(), reqvec[0]->u, UF_SCHEMA) - .str(); + scheme = util::get_uri_field(reqvec[0]->uri.c_str(), reqvec[0]->u, UF_SCHEMA); std::stringstream ss; if (reqvec[0]->is_ipv6_literal_addr()) { // we may have zone ID, which must start with "%25", or "%". RFC @@ -1435,7 +1435,7 @@ void HttpClient::update_hostport() { util::get_uri_field(reqvec[0]->uri.c_str(), reqvec[0]->u, UF_HOST); auto end = std::find(std::begin(host), std::end(host), '%'); ss << "["; - ss.write(host.c_str(), end - std::begin(host)); + ss.write(host.data(), end - std::begin(host)); ss << "]"; } else { util::write_uri_field(ss, reqvec[0]->uri.c_str(), reqvec[0]->u, UF_HOST); @@ -1688,7 +1688,7 @@ void update_html_parser(HttpClient *client, Request *req, const uint8_t *data, } auto link_port = util::has_uri_field(u, UF_PORT) ? u.port - : scheme == "https" ? 443 + : scheme == "https"_sr ? 443 : 80; if (port != link_port) { @@ -1800,8 +1800,8 @@ void check_response_header(nghttp2_session *session, Request *req) { for (auto &nv : req->res_nva) { if ("content-encoding" == nv.name) { - gzip = util::strieq_l("gzip", nv.value) || - util::strieq_l("deflate", nv.value); + gzip = util::strieq("gzip"_sr, nv.value) || + util::strieq("deflate"_sr, nv.value); continue; } } @@ -1915,10 +1915,12 @@ int on_header_callback(nghttp2_session *session, const nghttp2_frame *frame, req->header_buffer_size += namelen + valuelen; - auto token = http2::lookup_token(name, namelen); + auto nameref = StringRef{name, namelen}; + auto valueref = StringRef{value, valuelen}; + auto token = http2::lookup_token(nameref); http2::index_header(req->res_hdidx, token, req->res_nva.size()); - http2::add_header(req->res_nva, name, namelen, value, valuelen, + http2::add_header(req->res_nva, nameref, valueref, flags & NGHTTP2_NV_FLAG_NO_INDEX, token); break; } @@ -1939,10 +1941,12 @@ int on_header_callback(nghttp2_session *session, const nghttp2_frame *frame, req->header_buffer_size += namelen + valuelen; - auto token = http2::lookup_token(name, namelen); + auto nameref = StringRef{name, namelen}; + auto valueref = StringRef{value, valuelen}; + auto token = http2::lookup_token(nameref); http2::index_header(req->req_hdidx, token, req->req_nva.size()); - http2::add_header(req->req_nva, name, namelen, value, valuelen, + http2::add_header(req->req_nva, nameref, valueref, flags & NGHTTP2_NV_FLAG_NO_INDEX, token); break; } @@ -2283,7 +2287,8 @@ int communicate( goto fin; } - if (SSL_CTX_set_cipher_list(ssl_ctx, tls::DEFAULT_CIPHER_LIST) == 0) { + if (SSL_CTX_set_cipher_list(ssl_ctx, tls::DEFAULT_CIPHER_LIST.data()) == + 0) { std::cerr << "[ERROR] " << ERR_error_string(ERR_get_error(), nullptr) << std::endl; result = -1; @@ -2437,7 +2442,8 @@ nghttp2_ssize file_read_callback(nghttp2_session *session, int32_t stream_id, std::vector nva; nva.reserve(config.trailer.size()); for (auto &kv : config.trailer) { - nva.push_back(http2::make_nv(kv.name, kv.value, kv.no_index)); + nva.push_back(http2::make_field_nv(kv.name, kv.value, + http2::no_index(kv.no_index))); } rv = nghttp2_submit_trailer(session, stream_id, nva.data(), nva.size()); if (rv != 0) { @@ -2609,7 +2615,7 @@ int run(char **uris, int n) { } requests.clear(); } - prev_scheme = util::get_uri_field(uri.c_str(), u, UF_SCHEMA).str(); + prev_scheme = util::get_uri_field(uri.c_str(), u, UF_SCHEMA); prev_host = std::move(host); prev_port = port; } @@ -2818,11 +2824,11 @@ int main(int argc, char **argv) { case 'M': { // peer-max-concurrent-streams option auto n = util::parse_uint(optarg); - if (n == -1) { + if (!n) { std::cerr << "-M: Bad option value: " << optarg << std::endl; exit(EXIT_FAILURE); } - config.peer_max_concurrent_streams = n; + config.peer_max_concurrent_streams = *n; break; } case 'O': @@ -2833,11 +2839,11 @@ int main(int argc, char **argv) { exit(EXIT_SUCCESS); case 'b': { auto n = util::parse_uint(optarg); - if (n == -1) { + if (!n) { std::cerr << "-b: Bad option value: " << optarg << std::endl; exit(EXIT_FAILURE); } - config.padding = n; + config.padding = *n; break; } case 'n': @@ -2845,13 +2851,13 @@ int main(int argc, char **argv) { break; case 'p': { auto n = util::parse_uint(optarg); - if (n == -1 || NGHTTP2_MIN_WEIGHT > n || n > NGHTTP2_MAX_WEIGHT) { + if (!n || NGHTTP2_MIN_WEIGHT > n || n > NGHTTP2_MAX_WEIGHT) { std::cerr << "-p: specify the integer in the range [" << NGHTTP2_MIN_WEIGHT << ", " << NGHTTP2_MAX_WEIGHT << "], inclusive" << std::endl; exit(EXIT_FAILURE); } - config.weight.push_back(n); + config.weight.push_back(*n); break; } case 'r': @@ -2865,29 +2871,31 @@ int main(int argc, char **argv) { case 'v': ++config.verbose; break; - case 't': - config.timeout = util::parse_duration_with_unit(optarg); - if (config.timeout == std::numeric_limits::infinity()) { + case 't': { + auto d = util::parse_duration_with_unit(optarg); + if (!d) { std::cerr << "-t: bad timeout value: " << optarg << std::endl; exit(EXIT_FAILURE); } + config.timeout = *d; break; + } case 'u': config.upgrade = true; break; case 'w': case 'W': { auto n = util::parse_uint(optarg); - if (n == -1 || n > 30) { + if (!n || n > 30) { std::cerr << "-" << static_cast(c) << ": specify the integer in the range [0, 30], inclusive" << std::endl; exit(EXIT_FAILURE); } if (c == 'w') { - config.window_bits = n; + config.window_bits = *n; } else { - config.connection_window_bits = n; + config.connection_window_bits = *n; } break; } @@ -2931,16 +2939,16 @@ int main(int argc, char **argv) { break; case 'm': { auto n = util::parse_uint(optarg); - if (n == -1) { + if (!n) { std::cerr << "-m: Bad option value: " << optarg << std::endl; exit(EXIT_FAILURE); } - config.multiply = n; + config.multiply = *n; break; } case 'c': { auto n = util::parse_uint_with_unit(optarg); - if (n == -1) { + if (!n) { std::cerr << "-c: Bad option value: " << optarg << std::endl; exit(EXIT_FAILURE); } @@ -2949,8 +2957,8 @@ int main(int argc, char **argv) { << std::numeric_limits::max() << std::endl; exit(EXIT_FAILURE); } - config.header_table_size = n; - config.min_header_table_size = std::min(config.min_header_table_size, n); + config.header_table_size = *n; + config.min_header_table_size = std::min(config.min_header_table_size, *n); break; } case 'y': @@ -3024,12 +3032,12 @@ int main(int argc, char **argv) { case 12: { // max-concurrent-streams option auto n = util::parse_uint(optarg); - if (n == -1) { + if (!n) { std::cerr << "--max-concurrent-streams: Bad option value: " << optarg << std::endl; exit(EXIT_FAILURE); } - config.max_concurrent_streams = n; + config.max_concurrent_streams = *n; break; } case 13: @@ -3039,7 +3047,7 @@ int main(int argc, char **argv) { case 14: { // encoder-header-table-size option auto n = util::parse_uint_with_unit(optarg); - if (n == -1) { + if (!n) { std::cerr << "--encoder-header-table-size: Bad option value: " << optarg << std::endl; exit(EXIT_FAILURE); @@ -3050,7 +3058,7 @@ int main(int argc, char **argv) { << std::numeric_limits::max() << std::endl; exit(EXIT_FAILURE); } - config.encoder_header_table_size = n; + config.encoder_header_table_size = *n; break; } case 15: @@ -3105,7 +3113,7 @@ int main(int argc, char **argv) { exit(EXIT_FAILURE); } - config.host_override = util::get_uri_field(uri.c_str(), u, UF_HOST).str(); + config.host_override = util::get_uri_field(uri.c_str(), u, UF_HOST); if (util::has_uri_field(u, UF_PORT)) { config.port_override = u.port; } -- cgit v1.2.3