Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
Http2Session.hpp
Go to the documentation of this file.
1#pragma once
3
9
10#include <nghttp2/nghttp2.h>
11#include <string>
12#include <unordered_map>
13#include <memory>
14#include <vector>
15#include <mutex>
16#include <string_view>
17
18namespace orbit::server {
19 class Connection;
20}
21namespace orbit::network {
22 class Proactor;
23}
24
25namespace orbit::http {
26namespace h2 {
27
28namespace detail {
29
39 std::vector<std::string> storage;
40 std::vector<nghttp2_nv> nvs;
41};
42
54
61bool parse_method(std::string_view value, http::HttpMethod& out);
62
67bool is_connection_specific_header(std::string_view name);
68
76bool apply_request_target(std::string_view target, http::HttpRequest& req);
77
81void parse_cookies(std::string_view cookies, http::HttpRequest& req);
82
83} // namespace detail
84
96
97class Http2Session : public std::enable_shared_from_this<Http2Session> {
98public:
99 Http2Session(std::weak_ptr<server::Connection> connection, network::Proactor& proactor,
101 std::string client_ip, size_t max_body_size);
103
104 void process_data(const uint8_t* data, size_t len);
106
110 bool idle();
113
121 void submit_response(int32_t stream_id, http::HttpResponse& response, bool has_body, bool streaming = false);
123 void submit_data(int32_t stream_id, std::string_view chunk);
125 void end_stream(int32_t stream_id);
126
129
130private:
131 std::weak_ptr<server::Connection> connection_;
135 std::string client_ip_;
137
138 nghttp2_session* session_{nullptr};
139 std::mutex session_mutex_;
140
142 int32_t stream_id;
144
145 // Backing storage for string_views
146 std::string backing_uri;
147 std::string backing_body;
148 std::vector<std::pair<std::string, std::string>> backing_headers;
149
150 // For writing response bodies
151 std::string response_body;
153 bool streaming{false}; // Body produced by write_chunk() until end()
154 bool stream_ended{false};
155 std::string backing_cookie; // All cookie fields joined (HTTP/2 may split them)
156
157 // For sendfile
158 int file_fd{-1};
159 off_t file_size{0};
160 off_t file_offset{0};
161
162 bool body_too_large{false};
163
164 // For is_open() / on_close() once the stream closes (#197).
165 std::weak_ptr<Http2ResponseWriter> writer;
166 };
167
168 std::unordered_map<int32_t, std::shared_ptr<StreamContext>> streams_;
169
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);
176
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);
178
179 void dispatch_request(std::shared_ptr<StreamContext> stream_ctx);
180 void submit_status_locked(int32_t stream_id, http::HttpStatus status);
181};
182
187public:
188 Http2ResponseWriter(std::weak_ptr<Http2Session> session, int32_t stream_id, bool suppress_body = false);
189
190 void add_interceptor(Interceptor interceptor) override;
191 void set_header(const std::string& key, const std::string& value) override;
194
195 void send(http::HttpResponse&& response) override;
196 void send_headers(http::HttpResponse& response) override;
197 void write_chunk(std::string_view chunk) override;
198 void end() override;
199 void send_sse_event(std::string_view data, std::string_view event = "", std::string_view id = "") override;
200 void upgrade_to_raw_stream(std::function<void(std::string_view)> on_data, std::function<void()> on_close) override;
201 void read_body_stream(std::function<void(std::string_view)> on_data, std::function<void()> on_end) override;
202
203private:
204 std::shared_ptr<Http2Session> session_or_throw();
205
206 std::weak_ptr<Http2Session> session_;
207 int32_t stream_id_;
208 std::unordered_map<std::string, std::string> default_headers_;
209 std::vector<Interceptor> interceptors_;
210 bool headers_sent_{false};
211 bool suppress_body_{false}; // HEAD request: headers only
212
213 // The request body, kept alive by owner, for read_body_stream().
214 std::shared_ptr<void> body_owner_;
215 std::string_view body_;
216
218
219 friend class Http2Session;
220};
221
222} // namespace h2
223} // namespace http
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.
Definition Task.hpp:10
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
Owns the header name/value strings backing an nghttp2_nv list.
Definition Http2Session.hpp:38
std::vector< nghttp2_nv > nvs
Definition Http2Session.hpp:40
std::vector< std::string > storage
Definition Http2Session.hpp:39