From 38eed2cd90e933f18993007d4a2117daf267ca84 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> Date: Mon, 17 Aug 2020 01:04:50 +0900 Subject: [PATCH] Tweak path normalization --- examples/h09server.cc | 18 ++++++------------ examples/server.cc | 22 ++++++++++------------ 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/examples/h09server.cc b/examples/h09server.cc index eb87eee4..be8fbcd1 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 9d57476b..d7925022 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); -- GitLab