Orbit Framework
An ultra-modern, asynchronous, cross-platform C++ Web Framework.
Loading...
Searching...
No Matches
Http2Session.hpp
Go to the documentation of this file.
1#pragma once
2
8
9#include <nghttp2/nghttp2.h>
10#include <string>
11#include <unordered_map>
12#include <memory>
13#include <vector>
14#include <mutex>
15#include <string_view>
16
17namespace server {
18 class Connection;
19}
20
21namespace http {
22namespace h2 {
23
24namespace detail {
25
35 std::vector<std::string> storage;
36 std::vector<nghttp2_nv> nvs;
37};
38
50
57bool parse_method(std::string_view value, http::HttpMethod& out);
58
63bool is_connection_specific_header(std::string_view name);
64
65} // namespace detail
66
71public:
72 Http2Session(server::Connection& connection, const routing::Router& router, concurrency::ThreadPool& thread_pool);
74
75 void process_data(const uint8_t* data, size_t len);
76 void send_pending();
77
84 void submit_response(int32_t stream_id, const http::HttpResponse& response, bool has_body);
85 void submit_data(int32_t stream_id);
86 void end_stream(int32_t stream_id);
87
88 server::Connection& get_connection() { return connection_; }
89
90private:
91 server::Connection& connection_;
92 const routing::Router& router_;
93 concurrency::ThreadPool& thread_pool_;
94
95 nghttp2_session* session_{nullptr};
96 std::mutex session_mutex_;
97
98 struct StreamContext {
99 int32_t stream_id;
100 http::HttpRequest request;
101
102 // Backing storage for string_views
103 std::string backing_uri;
104 std::string backing_body;
105 std::vector<std::pair<std::string, std::string>> backing_headers;
106
107 // For writing response bodies
108 std::string response_body;
109 size_t response_offset{0};
110
111 // For sendfile
112 int file_fd{-1};
113 off_t file_size{0};
114 off_t file_offset{0};
115
116 Http2Session* session;
117 };
118
119 std::unordered_map<int32_t, std::shared_ptr<StreamContext>> streams_;
120
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);
127
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);
129
130 void dispatch_request(std::shared_ptr<StreamContext> stream_ctx);
131};
132
137public:
138 Http2ResponseWriter(Http2Session* session, int32_t stream_id);
139
140 void add_interceptor(Interceptor interceptor) override;
141 void set_header(const std::string& key, const std::string& value) override;
142 network::Proactor& proactor() override;
144
145 void send(http::HttpResponse&& response) override;
146 void send_headers(http::HttpResponse& response) override;
147 void write_chunk(std::string_view chunk) override;
148 void end() 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;
152
153private:
154 Http2Session* session_;
155 int32_t stream_id_;
156 std::unordered_map<std::string, std::string> default_headers_;
157 std::vector<Interceptor> interceptors_;
158 bool headers_sent_{false};
159};
160
161} // namespace h2
162} // namespace http
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
Owns the header name/value strings backing an nghttp2_nv list.
Definition Http2Session.hpp:34
std::vector< nghttp2_nv > nvs
Definition Http2Session.hpp:36
std::vector< std::string > storage
Definition Http2Session.hpp:35