diff --git a/examples/h09server.cc b/examples/h09server.cc index eb87eee4f11a4b945bde4fc1fe536055972b93e7..be8fbcd13804d16e040c2c9628a71f655ef052c5 100644 --- a/examples/h09server.cc +++ b/examples/h09server.cc @@ -233,6 +233,11 @@ Request request_path(const std::string_view &uri) { req.path = "/index.html"; } + req.path = util::normalize_path(req.path); + if (req.path == "/") { + req.path = "/index.html"; + } + if (u.field_set & (1 << UF_QUERY)) { static constexpr char push_prefix[] = "push="; static constexpr char urgency_prefix[] = "u="; @@ -293,13 +298,6 @@ Request request_path(const std::string_view &uri) { } } // namespace -namespace { -std::string resolve_path(const std::string &req_path) { - auto path = util::normalize_path(req_path); - return config.htdocs + path; -} -} // namespace - enum FileEntryFlag { FILE_ENTRY_TYPE_DIR = 0x1, }; @@ -381,11 +379,7 @@ int Stream::start_response() { return send_status_response(400); } - auto path = resolve_path(req.path); - if (path.empty()) { - send_status_response(404); - return 0; - } + auto path = config.htdocs + req.path; auto [fe, rv] = open_file(path); if (rv != 0) { send_status_response(404); diff --git a/examples/server.cc b/examples/server.cc index 9d57476b74e5d212d8268451bbdf72ece64a6721..d79250227bb65eb91ca86ba939d5b450e79d45d0 100644 --- a/examples/server.cc +++ b/examples/server.cc @@ -242,6 +242,11 @@ Request request_path(const std::string_view &uri, bool is_connect) { req.path = "/index.html"; } + req.path = util::normalize_path(req.path); + if (req.path == "/") { + req.path = "/index.html"; + } + if (u.field_set & (1 << UF_QUERY)) { static constexpr char push_prefix[] = "push="; static constexpr char urgency_prefix[] = "u="; @@ -302,13 +307,6 @@ Request request_path(const std::string_view &uri, bool is_connect) { } } // namespace -namespace { -std::string resolve_path(const std::string &req_path) { - auto path = util::normalize_path(req_path); - return config.htdocs + path; -} -} // namespace - enum FileEntryFlag { FILE_ENTRY_TYPE_DIR = 0x1, }; @@ -370,6 +368,10 @@ void Stream::map_file(const FileEntry &fe) { int64_t Stream::find_dyn_length(const std::string_view &path) { assert(path[0] == '/'); + if (path.size() == 1) { + return -1; + } + uint64_t n = 0; for (auto it = std::begin(path) + 1; it != std::end(path); ++it) { @@ -537,11 +539,7 @@ int Stream::start_response(nghttp3_conn *httpconn) { std::string content_type = "text/plain"; if (dyn_len == -1) { - auto path = resolve_path(req.path); - if (path.empty()) { - send_status_response(httpconn, 404); - return 0; - } + auto path = config.htdocs + req.path; auto [fe, rv] = open_file(path); if (rv != 0) { send_status_response(httpconn, 404);