Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions include/http/http_callbacks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#pragma once

#include "common.h"
// <functional>, <memory>, <string>, <cstdint> provided by common.h

// Forward declarations
class HttpConnectionHandler;
class ConnectionHandler;
class WebSocketConnection;
struct HttpRequest;
class HttpResponse;

namespace HTTP_CALLBACKS_NAMESPACE {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The namespace HTTP_CALLBACKS_NAMESPACE is quite verbose. Consider using a shorter, more conventional name like HttpCallbacks or HttpWsCallbacks to improve readability and reduce verbosity in qualified names.


// ---- HttpConnectionHandler callbacks ------------------------------------
using HttpConnRequestCallback = std::function<void(
std::shared_ptr<HttpConnectionHandler> self,
const HttpRequest& request,
HttpResponse& response
)>;
using HttpConnRouteCheckCallback = std::function<bool(const std::string& path)>;
using HttpConnMiddlewareCallback = std::function<bool(
const HttpRequest& request,
HttpResponse& response
)>;
using HttpConnUpgradeCallback = std::function<void(
std::shared_ptr<HttpConnectionHandler> self,
const HttpRequest& request
)>;

struct HttpConnCallbacks {
HttpConnRequestCallback request_callback = nullptr;
HttpConnRouteCheckCallback route_check_callback = nullptr;
HttpConnMiddlewareCallback middleware_callback = nullptr;
HttpConnUpgradeCallback upgrade_callback = nullptr;
};

// ---- WebSocketConnection callbacks --------------------------------------
using WsMessageCallback = std::function<void(
WebSocketConnection& ws, const std::string& message, bool is_binary
)>;
using WsCloseCallback = std::function<void(
WebSocketConnection& ws, uint16_t code, const std::string& reason
)>;
using WsPingCallback = std::function<void(
WebSocketConnection& ws, const std::string& payload
)>;
using WsErrorCallback = std::function<void(
WebSocketConnection& ws, const std::string& error
)>;

struct WsCallbacks {
WsMessageCallback message_callback = nullptr;
WsCloseCallback close_callback = nullptr;
WsPingCallback ping_callback = nullptr;
WsErrorCallback error_callback = nullptr;
};

} // namespace HTTP_CALLBACKS_NAMESPACE
41 changes: 16 additions & 25 deletions include/http/http_connection_handler.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "http/http_callbacks.h"
#include "http/http_parser.h"
#include "http/http_request.h"
#include "http/http_response.h"
Expand All @@ -13,30 +14,15 @@ class HttpConnectionHandler : public std::enable_shared_from_this<HttpConnection
public:
explicit HttpConnectionHandler(std::shared_ptr<ConnectionHandler> conn);

// Handler for complete HTTP requests
using RequestCallback = std::function<void(
std::shared_ptr<HttpConnectionHandler> self,
const HttpRequest& request,
HttpResponse& response
)>;
void SetRequestCallback(RequestCallback callback);
// Public type aliases for backward compatibility with SetupHandlers() callers
using RequestCallback = HTTP_CALLBACKS_NAMESPACE::HttpConnRequestCallback;
using RouteCheckCallback = HTTP_CALLBACKS_NAMESPACE::HttpConnRouteCheckCallback;
using MiddlewareCallback = HTTP_CALLBACKS_NAMESPACE::HttpConnMiddlewareCallback;
using UpgradeCallback = HTTP_CALLBACKS_NAMESPACE::HttpConnUpgradeCallback;

// Check if a WebSocket route exists for the given path.
// Returns true if upgrade should proceed, false to reject.
using RouteCheckCallback = std::function<bool(const std::string& path)>;
void SetRequestCallback(RequestCallback callback);
void SetRouteCheckCallback(RouteCheckCallback callback);

// Run middleware chain before WebSocket upgrade.
// Returns true if all middleware passed, false if any short-circuited (response is set).
using MiddlewareCallback = std::function<bool(const HttpRequest& request, HttpResponse& response)>;
void SetMiddlewareCallback(MiddlewareCallback callback);

// Handler called ONCE after WebSocket upgrade is complete and ws_conn_ exists.
// Wires application-level OnMessage/OnClose callbacks on the WebSocketConnection.
using UpgradeCallback = std::function<void(
std::shared_ptr<HttpConnectionHandler> self,
const HttpRequest& request
)>;
void SetUpgradeCallback(UpgradeCallback callback);

// Send an HTTP response
Expand Down Expand Up @@ -83,12 +69,17 @@ class HttpConnectionHandler : public std::enable_shared_from_this<HttpConnection

// Close the underlying connection (send response then close)
void CloseConnection();

// Internal phases of OnRawData -- split for readability
void HandleUpgradedData(const std::string& data);
void HandleParseError();
// Returns true to continue pipelining loop, false to stop processing
bool HandleCompleteRequest(const char*& buf, size_t& remaining, size_t consumed);
void HandleIncompleteRequest();

std::shared_ptr<ConnectionHandler> conn_;
HttpParser parser_;
RequestCallback request_callback_;
RouteCheckCallback route_check_callback_;
MiddlewareCallback middleware_callback_;
UpgradeCallback upgrade_callback_;
HTTP_CALLBACKS_NAMESPACE::HttpConnCallbacks callbacks_;
bool upgraded_ = false;
std::unique_ptr<WebSocketConnection> ws_conn_;
};
3 changes: 3 additions & 0 deletions include/http/http_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class HttpServer {
void WireNetServerCallbacks();
// Compute the pre-read input buffer cap from configured limits.
size_t ComputeInputCap() const;
// Safe WS transport-close notification: null-check, exception-safe, log errors.
// Must be called OUTSIDE conn_mtx_ to prevent deadlock.
void SafeNotifyWsClose(const std::shared_ptr<HttpConnectionHandler>& http_conn);

std::shared_ptr<TlsContext> tls_ctx_; // Shared with NetServer for safe lifetime

Expand Down
16 changes: 7 additions & 9 deletions include/ws/websocket_connection.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "http/http_callbacks.h"
#include "ws/websocket_parser.h"
#include "ws/websocket_frame.h"
#include "connection_handler.h"
Expand All @@ -10,11 +11,11 @@ class WebSocketConnection {
public:
explicit WebSocketConnection(std::shared_ptr<ConnectionHandler> conn);

// Message-level callbacks
using MessageCallback = std::function<void(WebSocketConnection& ws, const std::string& message, bool is_binary)>;
using CloseCallback = std::function<void(WebSocketConnection& ws, uint16_t code, const std::string& reason)>;
using PingCallback = std::function<void(WebSocketConnection& ws, const std::string& payload)>;
using ErrorCallback = std::function<void(WebSocketConnection& ws, const std::string& error)>;
// Public type aliases for backward compatibility
using MessageCallback = HTTP_CALLBACKS_NAMESPACE::WsMessageCallback;
using CloseCallback = HTTP_CALLBACKS_NAMESPACE::WsCloseCallback;
using PingCallback = HTTP_CALLBACKS_NAMESPACE::WsPingCallback;
using ErrorCallback = HTTP_CALLBACKS_NAMESPACE::WsErrorCallback;

void OnMessage(MessageCallback callback);
void OnClose(CloseCallback callback);
Expand Down Expand Up @@ -58,10 +59,7 @@ class WebSocketConnection {
uint16_t sent_close_code_ = 0; // Close code we sent (for NotifyTransportClose)
std::string sent_close_reason_; // Close reason we sent

MessageCallback message_callback_;
CloseCallback close_callback_;
PingCallback ping_callback_;
ErrorCallback error_callback_;
HTTP_CALLBACKS_NAMESPACE::WsCallbacks callbacks_;

// Fragmentation reassembly
std::string fragment_buffer_;
Expand Down
Loading