diff options
Diffstat (limited to 'src/shrpx_https_upstream.cc')
-rw-r--r-- | src/shrpx_https_upstream.cc | 70 |
1 files changed, 37 insertions, 33 deletions
diff --git a/src/shrpx_https_upstream.cc b/src/shrpx_https_upstream.cc index 0412384..aa54eb9 100644 --- a/src/shrpx_https_upstream.cc +++ b/src/shrpx_https_upstream.cc @@ -258,7 +258,7 @@ void rewrite_request_host_path_from_uri(BlockAllocator &balloc, Request &req, // As per https://tools.ietf.org/html/rfc7230#section-5.4, we // rewrite host header field with authority component. - auto authority = util::get_uri_field(uri.c_str(), u, UF_HOST); + auto authority = util::get_uri_field(uri.data(), u, UF_HOST); // TODO properly check IPv6 numeric address auto ipv6 = std::find(std::begin(authority), std::end(authority), ':') != std::end(authority); @@ -271,7 +271,7 @@ void rewrite_request_host_path_from_uri(BlockAllocator &balloc, Request &req, } if (authoritylen > authority.size()) { auto iovec = make_byte_ref(balloc, authoritylen + 1); - auto p = iovec.base; + auto p = std::begin(iovec); if (ipv6) { *p++ = '['; } @@ -286,16 +286,16 @@ void rewrite_request_host_path_from_uri(BlockAllocator &balloc, Request &req, } *p = '\0'; - req.authority = StringRef{iovec.base, p}; + req.authority = StringRef{std::span{std::begin(iovec), p}}; } else { req.authority = authority; } - req.scheme = util::get_uri_field(uri.c_str(), u, UF_SCHEMA); + req.scheme = util::get_uri_field(uri.data(), u, UF_SCHEMA); StringRef path; if (u.field_set & (1 << UF_PATH)) { - path = util::get_uri_field(uri.c_str(), u, UF_PATH); + path = util::get_uri_field(uri.data(), u, UF_PATH); } else if (req.method == HTTP_OPTIONS) { // Server-wide OPTIONS takes following form in proxy request: // @@ -303,21 +303,21 @@ void rewrite_request_host_path_from_uri(BlockAllocator &balloc, Request &req, // // Notice that no slash after authority. See // http://tools.ietf.org/html/rfc7230#section-5.3.4 - req.path = StringRef::from_lit(""); + req.path = ""_sr; // we ignore query component here return; } else { - path = StringRef::from_lit("/"); + path = "/"_sr; } if (u.field_set & (1 << UF_QUERY)) { auto &fdata = u.field_data[UF_QUERY]; if (u.field_set & (1 << UF_PATH)) { - auto q = util::get_uri_field(uri.c_str(), u, UF_QUERY); + auto q = util::get_uri_field(uri.data(), u, UF_QUERY); path = StringRef{std::begin(path), std::end(q)}; } else { - path = concat_string_ref(balloc, path, StringRef::from_lit("?"), + path = concat_string_ref(balloc, path, "?"_sr, StringRef{&uri[fdata.off], fdata.len}); } } @@ -369,7 +369,7 @@ int htp_hdrs_completecb(llhttp_t *htp) { << "HTTP/" << req.http_major << "." << req.http_minor << "\n"; for (const auto &kv : req.fs.headers()) { - if (kv.name == "authorization") { + if (kv.name == "authorization"_sr) { ss << TTY_HTTP_HD << kv.name << TTY_RST << ": <redacted>\n"; continue; } @@ -431,7 +431,7 @@ int htp_hdrs_completecb(llhttp_t *htp) { if (method != HTTP_CONNECT) { http_parser_url u{}; - rv = http_parser_parse_url(req.path.c_str(), req.path.size(), 0, &u); + rv = http_parser_parse_url(req.path.data(), req.path.size(), 0, &u); if (rv != 0) { // Expect to respond with 400 bad request return -1; @@ -440,7 +440,7 @@ int htp_hdrs_completecb(llhttp_t *htp) { if (!(u.field_set & (1 << UF_SCHEMA)) || !(u.field_set & (1 << UF_HOST))) { req.no_authority = true; - if (method == HTTP_OPTIONS && req.path == StringRef::from_lit("*")) { + if (method == HTTP_OPTIONS && req.path == "*"_sr) { req.path = StringRef{}; } else { req.path = http2::rewrite_clean_path(balloc, req.path); @@ -451,9 +451,9 @@ int htp_hdrs_completecb(llhttp_t *htp) { } if (handler->get_ssl()) { - req.scheme = StringRef::from_lit("https"); + req.scheme = "https"_sr; } else { - req.scheme = StringRef::from_lit("http"); + req.scheme = "http"_sr; } } else { rewrite_request_host_path_from_uri(balloc, req, req.path, u); @@ -544,7 +544,7 @@ int htp_hdrs_completecb(llhttp_t *htp) { // Continue here to make the client happy. if (downstream->get_expect_100_continue()) { auto output = downstream->get_response_buf(); - constexpr auto res = StringRef::from_lit("HTTP/1.1 100 Continue\r\n\r\n"); + constexpr auto res = "HTTP/1.1 100 Continue\r\n\r\n"_sr; output->append(res); handler->signal_write(); } @@ -982,8 +982,7 @@ int HttpsUpstream::send_reply(Downstream *downstream, const uint8_t *body, if (httpconf.max_requests <= num_requests_ || worker->get_graceful_shutdown()) { - resp.fs.add_header_token(StringRef::from_lit("connection"), - StringRef::from_lit("close"), false, + resp.fs.add_header_token("connection"_sr, "close"_sr, false, http2::HD_CONNECTION); connection_close = true; } else if (req.http_major <= 0 || @@ -991,7 +990,7 @@ int HttpsUpstream::send_reply(Downstream *downstream, const uint8_t *body, connection_close = true; } else { auto c = resp.fs.header(http2::HD_CONNECTION); - if (c && util::strieq_l("close", c->value)) { + if (c && util::strieq("close"_sr, c->value)) { connection_close = true; } } @@ -1034,9 +1033,12 @@ int HttpsUpstream::send_reply(Downstream *downstream, const uint8_t *body, output->append("\r\n"); - output->append(body, bodylen); + if (req.method != HTTP_HEAD) { + output->append(body, bodylen); + + downstream->response_sent_body_length += bodylen; + } - downstream->response_sent_body_length += bodylen; downstream->set_response_state(DownstreamState::MSG_COMPLETE); return 0; @@ -1071,7 +1073,7 @@ void HttpsUpstream::error_reply(unsigned int status_code) { output->append("\r\nServer: "); output->append(get_config()->http.server_name); output->append("\r\nContent-Length: "); - std::array<uint8_t, NGHTTP2_MAX_UINT64_DIGITS> intbuf; + std::array<char, NGHTTP2_MAX_UINT64_DIGITS> intbuf; output->append(StringRef{std::begin(intbuf), util::utos(std::begin(intbuf), html.size())}); output->append("\r\nDate: "); @@ -1080,9 +1082,15 @@ void HttpsUpstream::error_reply(unsigned int status_code) { output->append(lgconf->tstamp->time_http); output->append("\r\nContent-Type: text/html; " "charset=UTF-8\r\nConnection: close\r\n\r\n"); - output->append(html); - downstream->response_sent_body_length += html.size(); + const auto &req = downstream->request(); + + if (req.method != HTTP_HEAD) { + output->append(html); + + downstream->response_sent_body_length += html.size(); + } + downstream->set_response_state(DownstreamState::MSG_COMPLETE); } @@ -1403,7 +1411,7 @@ int HttpsUpstream::on_downstream_abort_request_with_https_redirect( int HttpsUpstream::redirect_to_https(Downstream *downstream) { auto &req = downstream->request(); - if (req.method == HTTP_CONNECT || req.scheme != "http" || + if (req.method == HTTP_CONNECT || req.scheme != "http"_sr || req.authority.empty()) { error_reply(400); return 0; @@ -1420,21 +1428,17 @@ int HttpsUpstream::redirect_to_https(Downstream *downstream) { auto &httpconf = config->http; StringRef loc; - if (httpconf.redirect_https_port == StringRef::from_lit("443")) { - loc = concat_string_ref(balloc, StringRef::from_lit("https://"), authority, - req.path); + if (httpconf.redirect_https_port == "443"_sr) { + loc = concat_string_ref(balloc, "https://"_sr, authority, req.path); } else { - loc = concat_string_ref(balloc, StringRef::from_lit("https://"), authority, - StringRef::from_lit(":"), + loc = concat_string_ref(balloc, "https://"_sr, authority, ":"_sr, httpconf.redirect_https_port, req.path); } auto &resp = downstream->response(); resp.http_status = 308; - resp.fs.add_header_token(StringRef::from_lit("location"), loc, false, - http2::HD_LOCATION); - resp.fs.add_header_token(StringRef::from_lit("connection"), - StringRef::from_lit("close"), false, + resp.fs.add_header_token("location"_sr, loc, false, http2::HD_LOCATION); + resp.fs.add_header_token("connection"_sr, "close"_sr, false, http2::HD_CONNECTION); return send_reply(downstream, nullptr, 0); |