Initial http implementation
This commit is contained in:
92
src/http_handler.hpp
Normal file
92
src/http_handler.hpp
Normal file
@@ -0,0 +1,92 @@
|
||||
#pragma once
|
||||
|
||||
#include "connection.hpp"
|
||||
#include "connection_handler.hpp"
|
||||
#include <llhttp.h>
|
||||
#include <memory>
|
||||
#include <string_view>
|
||||
|
||||
/**
|
||||
* 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,
|
||||
NotFound
|
||||
};
|
||||
|
||||
/**
|
||||
* HTTP connection state stored in Connection::user_data.
|
||||
* Manages llhttp parser state and request data.
|
||||
*/
|
||||
struct HttpConnectionState {
|
||||
llhttp_t parser;
|
||||
llhttp_settings_t settings;
|
||||
|
||||
// Current request data (arena-allocated)
|
||||
std::string_view method;
|
||||
std::string_view url;
|
||||
std::string_view body;
|
||||
|
||||
// Parse state
|
||||
bool headers_complete = false;
|
||||
bool message_complete = false;
|
||||
HttpRoute route = HttpRoute::NotFound;
|
||||
|
||||
explicit HttpConnectionState(ArenaAllocator &arena);
|
||||
};
|
||||
|
||||
/**
|
||||
* HTTP/1.1 server implementation using llhttp for parsing.
|
||||
* Supports the WeaselDB REST API endpoints with enum-based routing.
|
||||
*/
|
||||
class HttpHandler : public ConnectionHandler {
|
||||
public:
|
||||
HttpHandler() = default;
|
||||
|
||||
void on_connection_established(Connection &conn) override;
|
||||
void on_connection_closed(Connection &conn) override;
|
||||
void on_data_arrived(std::string_view data,
|
||||
std::unique_ptr<Connection> &conn_ptr) override;
|
||||
void on_write_progress(std::unique_ptr<Connection> &conn_ptr) 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);
|
||||
static int onHeaderValue(llhttp_t *parser, const char *at, size_t length);
|
||||
static int onHeadersComplete(llhttp_t *parser);
|
||||
static int onBody(llhttp_t *parser, const char *at, size_t length);
|
||||
static int onMessageComplete(llhttp_t *parser);
|
||||
|
||||
private:
|
||||
// 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, const 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 handleNotFound(Connection &conn, const HttpConnectionState &state);
|
||||
|
||||
// HTTP utilities
|
||||
static void sendResponse(Connection &conn, int status_code,
|
||||
std::string_view content_type,
|
||||
std::string_view body);
|
||||
static void sendJsonResponse(Connection &conn, int status_code,
|
||||
std::string_view json);
|
||||
static void sendErrorResponse(Connection &conn, int status_code,
|
||||
std::string_view message);
|
||||
};
|
||||
Reference in New Issue
Block a user