9#include <nghttp2/nghttp2.h>
11#include <unordered_map>
36 std::vector<nghttp2_nv>
nvs;
95 nghttp2_session* session_{
nullptr};
96 std::mutex session_mutex_;
98 struct StreamContext {
103 std::string backing_uri;
104 std::string backing_body;
105 std::vector<std::pair<std::string, std::string>> backing_headers;
108 std::string response_body;
109 size_t response_offset{0};
114 off_t file_offset{0};
119 std::unordered_map<int32_t, std::shared_ptr<StreamContext>> streams_;
121 static int on_begin_headers(nghttp2_session* session,
const nghttp2_frame* frame,
void* user_data);
122 static int on_header(nghttp2_session* session,
const nghttp2_frame* frame,
const uint8_t* name,
size_t namelen,
const uint8_t* value,
size_t valuelen, uint8_t flags,
void* user_data);
123 static int on_frame_recv(nghttp2_session* session,
const nghttp2_frame* frame,
void* user_data);
124 static int on_data_chunk_recv(nghttp2_session* session, uint8_t flags, int32_t stream_id,
const uint8_t* data,
size_t len,
void* user_data);
125 static int on_stream_close(nghttp2_session* session, int32_t stream_id, uint32_t error_code,
void* user_data);
126 static ssize_t send_callback(nghttp2_session* session,
const uint8_t* data,
size_t length,
int flags,
void* user_data);
128 static ssize_t data_provider_read(nghttp2_session *session, int32_t stream_id, uint8_t *buf,
size_t length, uint32_t *data_flags, nghttp2_data_source *source,
void *user_data);
130 void dispatch_request(std::shared_ptr<StreamContext> stream_ctx);
141 void set_header(
const std::string& key,
const std::string& value)
override;
149 void send_sse_event(std::string_view data, std::string_view event =
"", std::string_view
id =
"")
override;
150 void upgrade_to_raw_stream(std::function<
void(std::string_view)> on_data, std::function<
void()> on_close)
override;
151 void read_body_stream(std::function<
void(std::string_view)> on_data, std::function<
void()> on_end)
override;
156 std::unordered_map<std::string, std::string> default_headers_;
157 std::vector<Interceptor> interceptors_;
158 bool headers_sent_{
false};
Definition ThreadPool.hpp:12
Represents an HTTP response to be sent to a client.
Definition HttpResponse.hpp:50
Abstract interface for writing HTTP responses.
Definition ResponseWriter.hpp:16
std::function< void(HttpResponse &)> Interceptor
Definition ResponseWriter.hpp:20
ResponseWriter implementation for HTTP/2 streams.
Definition Http2Session.hpp:136
void send_sse_event(std::string_view data, std::string_view event="", std::string_view id="") override
Sends a Server-Sent Events (SSE) message.
Definition Http2Session.cpp:387
void send(http::HttpResponse &&response) override
Sends a complete HTTP response.
Definition Http2Session.cpp:354
void set_header(const std::string &key, const std::string &value) override
Adds a default header that will be included in the final response.
Definition Http2Session.cpp:342
void add_interceptor(Interceptor interceptor) override
Adds an interceptor to modify the response before it is sent.
Definition Http2Session.cpp:338
concurrency::ThreadPool & thread_pool() override
Gets the thread pool to offload blocking tasks.
Definition Http2Session.cpp:350
void upgrade_to_raw_stream(std::function< void(std::string_view)> on_data, std::function< void()> on_close) override
Upgrades the connection to a raw bi-directional byte stream.
Definition Http2Session.cpp:394
network::Proactor & proactor() override
Gets the underlying Proactor to dispatch async operations.
Definition Http2Session.cpp:346
void send_headers(http::HttpResponse &response) override
Sends only the HTTP headers.
Definition Http2Session.cpp:366
void read_body_stream(std::function< void(std::string_view)> on_data, std::function< void()> on_end) override
Asynchronously streams the incoming HTTP request body.
Definition Http2Session.cpp:398
void write_chunk(std::string_view chunk) override
Streams a chunk of data (for Chunked Transfer Encoding).
Definition Http2Session.cpp:377
void end() override
Ends a chunked response stream.
Definition Http2Session.cpp:383
Manages an HTTP/2 session over a connection.
Definition Http2Session.hpp:70
void end_stream(int32_t stream_id)
Definition Http2Session.cpp:328
void send_pending()
Definition Http2Session.cpp:147
~Http2Session()
Definition Http2Session.cpp:131
void submit_data(int32_t stream_id)
Definition Http2Session.cpp:323
server::Connection & get_connection()
Definition Http2Session.hpp:88
void process_data(const uint8_t *data, size_t len)
Definition Http2Session.cpp:137
void submit_response(int32_t stream_id, const http::HttpResponse &response, bool has_body)
Thread-safe API to submit an HTTP/2 response.
Definition Http2Session.cpp:293
Definition Proactor.hpp:8
Manages routing of HTTP requests to their appropriate handlers.
Definition Router.hpp:30
Represents an active client connection, handling request parsing and response writing.
Definition Connection.hpp:40
auto session(const std::string &redis_host, int redis_port)
Helper to create a session management middleware handler.
Definition SessionManager.hpp:48
bool parse_method(std::string_view value, http::HttpMethod &out)
Maps an HTTP/2 :method pseudo-header value to an HttpMethod.
Definition Http2Session.cpp:47
bool is_connection_specific_header(std::string_view name)
True if a header is forbidden in HTTP/2 and must not be forwarded.
Definition Http2Session.cpp:30
HeaderBlock build_response_headers(const http::HttpResponse &response)
Builds the HTTP/2 response header list for a response.
Definition Http2Session.cpp:58
Definition Http2Session.hpp:21
HttpMethod
Represents standard HTTP methods.
Definition HttpRequest.hpp:17
::ssize_t ssize_t
Definition PlatformSocket.hpp:41
Definition Http2Session.hpp:17
Represents an incoming HTTP request.
Definition HttpRequest.hpp:22