diff options
Diffstat (limited to '')
-rw-r--r-- | src/HttpServer.cc | 124 |
1 files changed, 63 insertions, 61 deletions
diff --git a/src/HttpServer.cc b/src/HttpServer.cc index 6b28d1b..fb7b1db 100644 --- a/src/HttpServer.cc +++ b/src/HttpServer.cc @@ -78,9 +78,8 @@ namespace nghttp2 { namespace { // TODO could be constexpr -constexpr auto DEFAULT_HTML = StringRef::from_lit("index.html"); -constexpr auto NGHTTPD_SERVER = - StringRef::from_lit("nghttpd nghttp2/" NGHTTP2_VERSION); +constexpr auto DEFAULT_HTML = "index.html"_sr; +constexpr auto NGHTTPD_SERVER = "nghttpd nghttp2/" NGHTTP2_VERSION ""_sr; } // namespace namespace { @@ -925,28 +924,32 @@ int Http2Handler::submit_file_response(const StringRef &status, Stream *stream, const std::string *content_type, nghttp2_data_provider2 *data_prd) { std::string last_modified_str; - auto nva = make_array(http2::make_nv_ls_nocopy(":status", status), - http2::make_nv_ls_nocopy("server", NGHTTPD_SERVER), - http2::make_nv_ll("cache-control", "max-age=3600"), - http2::make_nv_ls("date", sessions_->get_cached_date()), - http2::make_nv_ll("", ""), http2::make_nv_ll("", ""), - http2::make_nv_ll("", ""), http2::make_nv_ll("", "")); + auto nva = std::to_array({ + http2::make_field(":status"_sr, status), + http2::make_field("server"_sr, NGHTTPD_SERVER), + http2::make_field("cache-control"_sr, "max-age=3600"_sr), + http2::make_field_v("date"_sr, sessions_->get_cached_date()), + {}, + {}, + {}, + {}, + }); size_t nvlen = 4; if (!get_config()->no_content_length) { - nva[nvlen++] = http2::make_nv_ls_nocopy( - "content-length", + nva[nvlen++] = http2::make_field( + "content-length"_sr, util::make_string_ref_uint(stream->balloc, file_length)); } if (last_modified != 0) { last_modified_str = util::http_date(last_modified); - nva[nvlen++] = http2::make_nv_ls("last-modified", last_modified_str); + nva[nvlen++] = http2::make_field_v("last-modified"_sr, last_modified_str); } if (content_type) { - nva[nvlen++] = http2::make_nv_ls("content-type", *content_type); + nva[nvlen++] = http2::make_field_v("content-type"_sr, *content_type); } auto &trailer_names = get_config()->trailer_names; if (!trailer_names.empty()) { - nva[nvlen++] = http2::make_nv_ls_nocopy("trailer", trailer_names); + nva[nvlen++] = http2::make_field("trailer"_sr, trailer_names); } return nghttp2_submit_response2(session_, stream->stream_id, nva.data(), nvlen, data_prd); @@ -957,19 +960,20 @@ int Http2Handler::submit_response(const StringRef &status, int32_t stream_id, nghttp2_data_provider2 *data_prd) { auto nva = std::vector<nghttp2_nv>(); nva.reserve(4 + headers.size()); - nva.push_back(http2::make_nv_ls_nocopy(":status", status)); - nva.push_back(http2::make_nv_ls_nocopy("server", NGHTTPD_SERVER)); - nva.push_back(http2::make_nv_ls("date", sessions_->get_cached_date())); + nva.push_back(http2::make_field(":status"_sr, status)); + nva.push_back(http2::make_field("server"_sr, NGHTTPD_SERVER)); + nva.push_back(http2::make_field_v("date"_sr, sessions_->get_cached_date())); if (data_prd) { auto &trailer_names = get_config()->trailer_names; if (!trailer_names.empty()) { - nva.push_back(http2::make_nv_ls_nocopy("trailer", trailer_names)); + nva.push_back(http2::make_field("trailer"_sr, trailer_names)); } } for (auto &nv : headers) { - nva.push_back(http2::make_nv_nocopy(nv.name, nv.value, nv.no_index)); + nva.push_back( + http2::make_field(nv.name, nv.value, http2::no_index(nv.no_index))); } int r = nghttp2_submit_response2(session_, stream_id, nva.data(), nva.size(), data_prd); @@ -978,16 +982,18 @@ int Http2Handler::submit_response(const StringRef &status, int32_t stream_id, int Http2Handler::submit_response(const StringRef &status, int32_t stream_id, nghttp2_data_provider2 *data_prd) { - auto nva = make_array(http2::make_nv_ls_nocopy(":status", status), - http2::make_nv_ls_nocopy("server", NGHTTPD_SERVER), - http2::make_nv_ls("date", sessions_->get_cached_date()), - http2::make_nv_ll("", "")); + auto nva = std::to_array({ + http2::make_field(":status"_sr, status), + http2::make_field("server"_sr, NGHTTPD_SERVER), + http2::make_field_v("date"_sr, sessions_->get_cached_date()), + {}, + }); size_t nvlen = 3; if (data_prd) { auto &trailer_names = get_config()->trailer_names; if (!trailer_names.empty()) { - nva[nvlen++] = http2::make_nv_ls_nocopy("trailer", trailer_names); + nva[nvlen++] = http2::make_field("trailer"_sr, trailer_names); } } @@ -997,7 +1003,7 @@ int Http2Handler::submit_response(const StringRef &status, int32_t stream_id, int Http2Handler::submit_non_final_response(const std::string &status, int32_t stream_id) { - auto nva = make_array(http2::make_nv_ls(":status", status)); + auto nva = std::to_array({http2::make_field_v(":status"_sr, status)}); return nghttp2_submit_headers(session_, NGHTTP2_FLAG_NONE, stream_id, nullptr, nva.data(), nva.size(), nullptr); } @@ -1010,13 +1016,12 @@ int Http2Handler::submit_push_promise(Stream *stream, authority = stream->header.host; } - auto scheme = get_config()->no_tls ? StringRef::from_lit("http") - : StringRef::from_lit("https"); + auto scheme = get_config()->no_tls ? "http"_sr : "https"_sr; - auto nva = make_array(http2::make_nv_ll(":method", "GET"), - http2::make_nv_ls_nocopy(":path", push_path), - http2::make_nv_ls_nocopy(":scheme", scheme), - http2::make_nv_ls_nocopy(":authority", authority)); + auto nva = std::to_array({http2::make_field(":method"_sr, "GET"_sr), + http2::make_field(":path"_sr, push_path), + http2::make_field(":scheme"_sr, scheme), + http2::make_field(":authority"_sr, authority)}); auto promised_stream_id = nghttp2_submit_push_promise( session_, NGHTTP2_FLAG_END_HEADERS, stream->stream_id, nva.data(), @@ -1029,7 +1034,7 @@ int Http2Handler::submit_push_promise(Stream *stream, auto promised_stream = std::make_unique<Stream>(this, promised_stream_id); auto &promised_header = promised_stream->header; - promised_header.method = StringRef::from_lit("GET"); + promised_header.method = "GET"_sr; promised_header.path = push_path; promised_header.scheme = scheme; promised_header.authority = @@ -1103,7 +1108,8 @@ nghttp2_ssize file_read_callback(nghttp2_session *session, int32_t stream_id, std::vector<nghttp2_nv> 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) { @@ -1140,10 +1146,9 @@ void prepare_status_response(Stream *stream, Http2Handler *hd, int status) { HeaderRefs headers; headers.reserve(2); - headers.emplace_back(StringRef::from_lit("content-type"), - StringRef::from_lit("text/html; charset=UTF-8")); + headers.emplace_back("content-type"_sr, "text/html; charset=UTF-8"_sr); headers.emplace_back( - StringRef::from_lit("content-length"), + "content-length"_sr, util::make_string_ref_uint(stream->balloc, file_ent->length)); hd->submit_response(StringRef{status_page->status}, stream->stream_id, headers, &data_prd); @@ -1167,15 +1172,13 @@ void prepare_echo_response(Stream *stream, Http2Handler *hd) { data_prd.read_callback = file_read_callback; HeaderRefs headers; - headers.emplace_back(StringRef::from_lit("nghttpd-response"), - StringRef::from_lit("echo")); + headers.emplace_back("nghttpd-response"_sr, "echo"_sr); if (!hd->get_config()->no_content_length) { - headers.emplace_back(StringRef::from_lit("content-length"), + headers.emplace_back("content-length"_sr, util::make_string_ref_uint(stream->balloc, length)); } - hd->submit_response(StringRef::from_lit("200"), stream->stream_id, headers, - &data_prd); + hd->submit_response("200"_sr, stream->stream_id, headers, &data_prd); } } // namespace @@ -1209,10 +1212,10 @@ void prepare_redirect_response(Stream *stream, Http2Handler *hd, authority = stream->header.host; } - auto location = concat_string_ref( - stream->balloc, scheme, StringRef::from_lit("://"), authority, path); + auto location = + concat_string_ref(stream->balloc, scheme, "://"_sr, authority, path); - auto headers = HeaderRefs{{StringRef::from_lit("location"), location}}; + auto headers = HeaderRefs{{"location"_sr, location}}; auto sessions = hd->get_sessions(); auto status_page = sessions->get_server()->get_status_page(status); @@ -1245,8 +1248,8 @@ void prepare_response(Stream *stream, Http2Handler *hd, auto query_pos = std::find(std::begin(reqpath), std::end(reqpath), '?'); if (query_pos != std::end(reqpath)) { // Do not response to this request to allow clients to test timeouts. - if (util::streq_l("nghttpd_do_not_respond_to_req=yes", - StringRef{query_pos, std::end(reqpath)})) { + if ("nghttpd_do_not_respond_to_req=yes"_sr == + StringRef{query_pos, std::end(reqpath)}) { return; } raw_path = StringRef{std::begin(reqpath), query_pos}; @@ -1278,7 +1281,7 @@ void prepare_response(Stream *stream, Http2Handler *hd, } if (!hd->get_config()->push.empty()) { - auto push_itr = hd->get_config()->push.find(path.str()); + auto push_itr = hd->get_config()->push.find(std::string{path}); if (allow_push && push_itr != std::end(hd->get_config()->push)) { for (auto &push_path : (*push_itr).second) { rv = hd->submit_push_promise(stream, StringRef{push_path}); @@ -1339,8 +1342,8 @@ void prepare_response(Stream *stream, Http2Handler *hd, if (buf.st_mode & S_IFDIR) { close(file); - auto reqpath = concat_string_ref(stream->balloc, raw_path, - StringRef::from_lit("/"), raw_query); + auto reqpath = + concat_string_ref(stream->balloc, raw_path, "/"_sr, raw_query); prepare_redirect_response(stream, hd, reqpath, 301); @@ -1370,16 +1373,15 @@ void prepare_response(Stream *stream, Http2Handler *hd, stream->file_ent = file_ent; if (last_mod_found && file_ent->mtime <= last_mod) { - hd->submit_response(StringRef::from_lit("304"), stream->stream_id, nullptr); + hd->submit_response("304"_sr, stream->stream_id, nullptr); return; } auto method = stream->header.method; - if (method == StringRef::from_lit("HEAD")) { - hd->submit_file_response(StringRef::from_lit("200"), stream, - file_ent->mtime, file_ent->length, - file_ent->content_type, nullptr); + if (method == "HEAD"_sr) { + hd->submit_file_response("200"_sr, stream, file_ent->mtime, + file_ent->length, file_ent->content_type, nullptr); return; } @@ -1390,8 +1392,8 @@ void prepare_response(Stream *stream, Http2Handler *hd, data_prd.source.fd = file_ent->fd; data_prd.read_callback = file_read_callback; - hd->submit_file_response(StringRef::from_lit("200"), stream, file_ent->mtime, - file_ent->length, file_ent->content_type, &data_prd); + hd->submit_file_response("200"_sr, stream, file_ent->mtime, file_ent->length, + file_ent->content_type, &data_prd); } } // namespace @@ -1425,7 +1427,7 @@ int on_header_callback2(nghttp2_session *session, const nghttp2_frame *frame, stream->header_buffer_size += namebuf.len + valuebuf.len; - auto token = http2::lookup_token(namebuf.base, namebuf.len); + auto token = http2::lookup_token(StringRef{namebuf.base, namebuf.len}); auto &header = stream->header; @@ -1528,14 +1530,13 @@ int hd_on_frame_recv_callback(nghttp2_session *session, auto expect100 = stream->header.expect; - if (util::strieq_l("100-continue", expect100)) { + if (util::strieq("100-continue"_sr, expect100)) { hd->submit_non_final_response("100", frame->hd.stream_id); } auto method = stream->header.method; if (hd->get_config()->echo_upload && - (method == StringRef::from_lit("POST") || - method == StringRef::from_lit("PUT"))) { + (method == "POST"_sr || method == "PUT"_sr)) { if (!prepare_upload_temp_store(stream, hd)) { hd->submit_rst_stream(stream, NGHTTP2_INTERNAL_ERROR); return 0; @@ -2129,7 +2130,8 @@ int HttpServer::run() { return -1; } - 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 << ERR_error_string(ERR_get_error(), nullptr) << std::endl; return -1; } |