Skip to content

Commit

Permalink
Simplify enterprise HTTP handlers (#87)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti authored Jan 7, 2025
1 parent 47bbcd8 commit dccb68b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
18 changes: 2 additions & 16 deletions src/enterprise/enterprise_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,18 @@
#include <sourcemeta/hydra/httpserver.h>

#include <filesystem> // std::filesystem
#include <sstream> // 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;
Expand All @@ -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);
}
Expand Down
10 changes: 10 additions & 0 deletions src/server/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit dccb68b

Please sign in to comment.