Separate out api url parser

This commit is contained in:
2025-09-04 16:36:10 -04:00
parent 55069c0c79
commit 2278694f4f
7 changed files with 417 additions and 219 deletions

View File

@@ -8,6 +8,7 @@
#include <llhttp.h>
#include "api_url_parser.hpp"
#include "arena_allocator.hpp"
#include "connection.hpp"
#include "connection_handler.hpp"
@@ -20,29 +21,14 @@
// Forward declarations
struct CommitRequest;
struct JsonCommitRequestParser;
/**
* HTTP routes supported by WeaselDB server.
* Using enum for efficient switch-based routing.
*/
enum class HttpRoute {
GET_version,
POST_commit,
GET_subscribe,
GET_status,
PUT_retention,
GET_retention,
DELETE_retention,
GET_metrics,
GET_ok,
NotFound
};
struct RouteMatch;
/**
* HTTP connection state stored in Connection::user_data.
* Manages llhttp parser state and request data.
*/
struct HttpConnectionState {
ArenaAllocator &arena;
llhttp_t parser;
llhttp_settings_t settings;
@@ -70,8 +56,8 @@ struct HttpConnectionState {
0; // X-Request-Id header value (for tracing/logging)
// Streaming parser for POST requests
std::unique_ptr<JsonCommitRequestParser> commit_parser;
std::unique_ptr<CommitRequest> commit_request;
ArenaAllocator::Ptr<JsonCommitRequestParser> commit_parser;
ArenaAllocator::Ptr<CommitRequest> commit_request;
bool parsing_commit = false;
bool basic_validation_passed =
false; // Set to true if basic validation passes
@@ -154,9 +140,6 @@ struct HttpHandler : ConnectionHandler {
void on_batch_complete(
std::span<std::unique_ptr<Connection>> /*batch*/) override;
// Route parsing (public for testing)
static HttpRoute parseRoute(std::string_view method, std::string_view url);
// llhttp callbacks (public for HttpConnectionState access)
static int onUrl(llhttp_t *parser, const char *at, size_t length);
static int onHeaderField(llhttp_t *parser, const char *at, size_t length);
@@ -208,17 +191,21 @@ private:
bool process_release_batch(BatchType &batch);
// Route handlers
void handleGetVersion(Connection &conn, const HttpConnectionState &state);
void handlePostCommit(Connection &conn, const HttpConnectionState &state);
void handleGetSubscribe(Connection &conn, const HttpConnectionState &state);
void handleGetStatus(Connection &conn, HttpConnectionState &state);
void handlePutRetention(Connection &conn, const HttpConnectionState &state);
void handleGetRetention(Connection &conn, const HttpConnectionState &state);
void handleDeleteRetention(Connection &conn,
const HttpConnectionState &state);
void handleGetMetrics(Connection &conn, const HttpConnectionState &state);
void handleGetOk(Connection &conn, const HttpConnectionState &state);
void handleNotFound(Connection &conn, const HttpConnectionState &state);
void handle_get_version(Connection &conn, const HttpConnectionState &state);
void handle_post_commit(Connection &conn, const HttpConnectionState &state);
void handle_get_subscribe(Connection &conn, const HttpConnectionState &state);
void handle_get_status(Connection &conn, HttpConnectionState &state,
const RouteMatch &route_match);
void handle_put_retention(Connection &conn, const HttpConnectionState &state,
const RouteMatch &route_match);
void handle_get_retention(Connection &conn, const HttpConnectionState &state,
const RouteMatch &route_match);
void handle_delete_retention(Connection &conn,
const HttpConnectionState &state,
const RouteMatch &route_match);
void handle_get_metrics(Connection &conn, const HttpConnectionState &state);
void handle_get_ok(Connection &conn, const HttpConnectionState &state);
void handle_not_found(Connection &conn, const HttpConnectionState &state);
// HTTP utilities
static void sendResponse(Connection &conn, int status_code,