From dccb68bafee933ece025df3ca41d29b97211cd92 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Tue, 7 Jan 2025 10:00:30 -0400 Subject: [PATCH] Simplify enterprise HTTP handlers (#87) Signed-off-by: Juan Cruz Viotti --- src/enterprise/enterprise_server.h | 18 ++---------------- src/server/server.cc | 10 ++++++++++ 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/enterprise/enterprise_server.h b/src/enterprise/enterprise_server.h index b1af400..e502a11 100644 --- a/src/enterprise/enterprise_server.h +++ b/src/enterprise/enterprise_server.h @@ -4,31 +4,18 @@ #include #include // std::filesystem -#include // std::ostringstream namespace sourcemeta::registry::enterprise { -auto on_index(const sourcemeta::hydra::http::ServerLogger &, - const sourcemeta::hydra::http::ServerRequest &request, - sourcemeta::hydra::http::ServerResponse &response) -> void { - sourcemeta::hydra::http::serve_file( - *(__global_data) / "generated" / "index.html", request, response); -} - auto on_request(const sourcemeta::hydra::http::ServerLogger &logger, const sourcemeta::hydra::http::ServerRequest &request, sourcemeta::hydra::http::ServerResponse &response) -> void { const auto &request_path{request.path()}; - const auto extension{std::filesystem::path{request_path}.extension()}; - if (extension == ".json") { + if (request_path.ends_with(".json")) { return ::on_request(logger, request, response); } - // TODO: Prevent relative paths that can let a client - // serve a file outside of the static asset directory - std::ostringstream asset_path_stream; - asset_path_stream << SOURCEMETA_REGISTRY_ENTERPRISE_STATIC << request_path; - const std::string asset_path{asset_path_stream.str()}; + const auto asset_path{SOURCEMETA_REGISTRY_ENTERPRISE_STATIC + request_path}; if (std::filesystem::exists(asset_path)) { sourcemeta::hydra::http::serve_file(asset_path, request, response); return; @@ -49,7 +36,6 @@ auto on_request(const sourcemeta::hydra::http::ServerLogger &logger, } auto attach(sourcemeta::hydra::http::Server &server) -> void { - server.route(sourcemeta::hydra::http::Method::GET, "/", on_index); server.route(sourcemeta::hydra::http::Method::GET, "/*", on_request); server.route(sourcemeta::hydra::http::Method::HEAD, "/*", on_request); } diff --git a/src/server/server.cc b/src/server/server.cc index fdf13c6..356e9dd 100644 --- a/src/server/server.cc +++ b/src/server/server.cc @@ -52,6 +52,15 @@ static auto json_error(const sourcemeta::hydra::http::ServerLogger &logger, response.end(std::move(object)); } +#ifdef SOURCEMETA_REGISTRY_ENTERPRISE +auto on_index(const sourcemeta::hydra::http::ServerLogger &, + const sourcemeta::hydra::http::ServerRequest &request, + sourcemeta::hydra::http::ServerResponse &response) -> void { + sourcemeta::hydra::http::serve_file( + *(__global_data) / "generated" / "index.html", request, response); +} +#endif + static auto on_request(const sourcemeta::hydra::http::ServerLogger &logger, const sourcemeta::hydra::http::ServerRequest &request, sourcemeta::hydra::http::ServerResponse &response) @@ -174,6 +183,7 @@ auto main(int argc, char *argv[]) noexcept -> int { sourcemeta::hydra::http::Server server; #ifdef SOURCEMETA_REGISTRY_ENTERPRISE sourcemeta::registry::enterprise::attach(server); + server.route(sourcemeta::hydra::http::Method::GET, "/", on_index); #else server.route(sourcemeta::hydra::http::Method::GET, "/*", on_request); server.route(sourcemeta::hydra::http::Method::HEAD, "/*", on_request);