10#include <nghttp2/nghttp2.h>
12#include <unordered_map>
40 std::vector<nghttp2_nv>
nvs;
97class Http2Session :
public std::enable_shared_from_this<Http2Session> {
101 std::string client_ip,
size_t max_body_size);
165 std::weak_ptr<Http2ResponseWriter>
writer;
168 std::unordered_map<int32_t, std::shared_ptr<StreamContext>>
streams_;
170 static int on_begin_headers(nghttp2_session* session,
const nghttp2_frame* frame,
void* user_data);
171 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);
172 static int on_frame_recv(nghttp2_session* session,
const nghttp2_frame* frame,
void* user_data);
173 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);
174 static int on_stream_close(nghttp2_session* session, int32_t stream_id, uint32_t error_code,
void* user_data);
175 static ssize_t
send_callback(nghttp2_session* session,
const uint8_t* data,
size_t length,
int flags,
void* user_data);
177 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);
191 void set_header(
const std::string& key,
const std::string& value)
override;
199 void send_sse_event(std::string_view data, std::string_view event =
"", std::string_view
id =
"")
override;
201 void read_body_stream(std::function<
void(std::string_view)> on_data, std::function<
void()> on_end)
override;
Definition ThreadPool.hpp:13
Represents an HTTP response to be sent to a client.
Definition HttpResponse.hpp:107
Abstract interface for writing HTTP responses.
Definition ResponseWriter.hpp:23
void on_close(std::function< void()> callback)
Runs callback once when the writer closes (see is_open()), or right away if it already has.
std::function< void(HttpResponse &)> Interceptor
Definition ResponseWriter.hpp:27
ResponseWriter implementation for HTTP/2 streams.
Definition Http2Session.hpp:186
std::string_view body_
Definition Http2Session.hpp:215
std::unordered_map< std::string, std::string > default_headers_
Definition Http2Session.hpp:208
void send_sse_event(std::string_view data, std::string_view event="", std::string_view id="") override
Sends a Server-Sent Events (SSE) message.
void send(http::HttpResponse &&response) override
Sends a complete HTTP response.
network::Proactor & proactor() override
Gets the underlying Proactor to dispatch async operations.
std::shared_ptr< Http2Session > session_or_throw()
void set_header(const std::string &key, const std::string &value) override
Adds a default header that will be included in the final response.
concurrency::ThreadPool & thread_pool() override
Gets the thread pool to offload blocking tasks.
int32_t stream_id_
Definition Http2Session.hpp:207
void add_interceptor(Interceptor interceptor) override
Adds an interceptor to modify the response before it is sent.
Http2ResponseWriter(std::weak_ptr< Http2Session > session, int32_t stream_id, bool suppress_body=false)
std::weak_ptr< Http2Session > session_
Definition Http2Session.hpp:206
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.
void write_chunk(std::string_view chunk) override
Streams a chunk of data (for Chunked Transfer Encoding).
void send_headers(http::HttpResponse &response) override
Sends only the HTTP headers.
bool suppress_body_
Definition Http2Session.hpp:211
std::vector< Interceptor > interceptors_
Definition Http2Session.hpp:209
void end() override
Ends a chunked response stream.
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.
std::shared_ptr< void > body_owner_
Definition Http2Session.hpp:214
bool headers_sent_
Definition Http2Session.hpp:210
void apply_response_hooks(http::HttpResponse &response)
Definition Http2Session.hpp:97
network::Proactor & proactor_
Definition Http2Session.hpp:132
void close_streams()
The connection is gone: every open stream's writer reports closed.
concurrency::ThreadPool & thread_pool_
Definition Http2Session.hpp:134
void end_stream(int32_t stream_id)
Ends a streaming response.
void submit_response(int32_t stream_id, http::HttpResponse &response, bool has_body, bool streaming=false)
Thread-safe API to submit an HTTP/2 response.
std::string client_ip_
Definition Http2Session.hpp:135
bool idle()
True if no stream is open.
std::weak_ptr< server::Connection > connection_
Definition Http2Session.hpp:131
Http2Session(std::weak_ptr< server::Connection > connection, network::Proactor &proactor, const routing::Router &router, concurrency::ThreadPool &thread_pool, std::string client_ip, size_t max_body_size)
static int on_stream_close(nghttp2_session *session, int32_t stream_id, uint32_t error_code, void *user_data)
concurrency::ThreadPool & thread_pool()
Definition Http2Session.hpp:128
const routing::Router & router_
Definition Http2Session.hpp:133
static ssize_t send_callback(nghttp2_session *session, const uint8_t *data, size_t length, int flags, void *user_data)
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)
static int on_frame_recv(nghttp2_session *session, const nghttp2_frame *frame, void *user_data)
void begin_shutdown()
Sends GOAWAY: the client opens no new streams; open ones may finish.
void submit_data(int32_t stream_id, std::string_view chunk)
Appends chunk to a streaming response (see Http2ResponseWriter::send_headers).
void dispatch_request(std::shared_ptr< StreamContext > stream_ctx)
nghttp2_session * session_
Definition Http2Session.hpp:138
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)
void submit_status_locked(int32_t stream_id, http::HttpStatus status)
static int on_begin_headers(nghttp2_session *session, const nghttp2_frame *frame, void *user_data)
size_t max_body_size_
Definition Http2Session.hpp:136
std::unordered_map< int32_t, std::shared_ptr< StreamContext > > streams_
Definition Http2Session.hpp:168
network::Proactor & proactor()
Definition Http2Session.hpp:127
std::mutex session_mutex_
Definition Http2Session.hpp:139
void process_data(const uint8_t *data, size_t len)
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)
Definition Proactor.hpp:9
Manages routing of HTTP requests to their appropriate handlers.
Definition Router.hpp:46
Represents an active client connection, handling request parsing and response writing.
Definition Connection.hpp:67
void parse_cookies(std::string_view cookies, http::HttpRequest &req)
Adds the name=value pairs of a Cookie header value to req.cookies.
bool parse_method(std::string_view value, http::HttpMethod &out)
Maps an HTTP/2 :method pseudo-header value to an HttpMethod.
bool apply_request_target(std::string_view target, http::HttpRequest &req)
Fills req.uri (the decoded path), req.target and req.query from a request target such as an HTTP/2 or...
bool is_connection_specific_header(std::string_view name)
True if a header is forbidden in HTTP/2 and must not be forwarded.
HeaderBlock build_response_headers(const http::HttpResponse &response)
Builds the HTTP/2 response header list for a response.
HttpMethod
Represents standard HTTP methods.
Definition HttpRequest.hpp:28
HttpStatus
Standard HTTP status codes.
Definition HttpResponse.hpp:17
Definition Http2Session.hpp:21
Definition Http2Session.hpp:18
Represents an incoming HTTP request.
Definition HttpRequest.hpp:54
Definition Http2Session.hpp:141
std::string backing_cookie
Definition Http2Session.hpp:155
bool streaming
Definition Http2Session.hpp:153
off_t file_size
Definition Http2Session.hpp:159
std::weak_ptr< Http2ResponseWriter > writer
Definition Http2Session.hpp:165
http::HttpRequest request
Definition Http2Session.hpp:143
std::string backing_body
Definition Http2Session.hpp:147
std::vector< std::pair< std::string, std::string > > backing_headers
Definition Http2Session.hpp:148
off_t file_offset
Definition Http2Session.hpp:160
int file_fd
Definition Http2Session.hpp:158
int32_t stream_id
Definition Http2Session.hpp:142
bool body_too_large
Definition Http2Session.hpp:162
std::string response_body
Definition Http2Session.hpp:151
bool stream_ended
Definition Http2Session.hpp:154
size_t response_offset
Definition Http2Session.hpp:152
std::string backing_uri
Definition Http2Session.hpp:146