Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
Connection.hpp
Go to the documentation of this file.
1#pragma once
11#include <vector>
12#include <chrono>
13#include <string_view>
14#include <memory>
15#include <mutex>
16
17namespace orbit::http::websocket { class WebSocketConnection; }
18namespace orbit::http::h2 { class Http2Session; }
19
20namespace orbit::server {
21
22class ConnectionManager; // Forward declaration
23
31
41
49 size_t max_header_bytes = 8192;
50 size_t max_request_line = 4096;
51 size_t max_headers = 100;
52 size_t websocket_max_message_size = 16 * 1024 * 1024;
54 bool h2c = false;
55};
56
58 std::chrono::milliseconds header{10000};
59 std::chrono::milliseconds keep_alive{10000};
60 std::chrono::milliseconds idle{30000};
61 std::chrono::milliseconds websocket_idle{0};
62};
63
67class Connection : public std::enable_shared_from_this<Connection>, public http::ResponseWriter {
68public:
81 Connection(network::Socket socket, const std::string& client_ip, network::Proactor& proactor, const routing::Router& router, ConnectionManager& manager, concurrency::ThreadPool& thread_pool, TimerManager& timer_manager, size_t max_body_size, network::TlsContext* tls_context = nullptr);
83
84 Connection(const Connection&) = delete;
85 Connection& operator=(const Connection&) = delete;
86
90 void start();
91
97 void on_removed(bool notify_now = false);
98
102 void set_timeouts(const ConnectionTimeouts& timeouts) { timeouts_ = timeouts; }
103 void set_limits(const ConnectionLimits& limits) { limits_ = limits; }
104
107
112 void write_raw(const std::vector<char>& data);
113
118
123
128 void upgrade_to_websocket(std::unique_ptr<http::websocket::WebSocketConnection> ws_conn);
129
134 const std::string& client_ip() const { return client_ip_; }
135
136 // ResponseWriter Implementation
137 void add_interceptor(std::function<void(http::HttpResponse&)> interceptor) override;
138 void set_header(const std::string& key, const std::string& value) override;
139 network::Proactor& proactor() override { return proactor_; }
141 void send(http::HttpResponse&& response) override;
142 void send_headers(http::HttpResponse& response) override;
143 void write_chunk(std::string_view chunk) override;
144 void end() override;
145 void send_sse_event(std::string_view data, std::string_view event = "", std::string_view id = "") override;
146 void upgrade_to_raw_stream(std::function<void(std::string_view)> on_data, std::function<void()> on_close) override;
147 void read_body_stream(std::function<void(std::string_view)> on_data, std::function<void()> on_end) override;
148
149private:
151 // Queues bytes for writing; with close_after, closes once they are sent.
152 void send_data(std::string_view data, bool close_after = false);
155 void arm_timer(std::chrono::milliseconds timeout);
157 // WebSocket or raw stream: long-lived, timed out by peer inactivity only.
158 bool is_message_stream() const;
159 void send_error(http::HttpStatus status, const std::string& message);
160
162 void on_read_complete(ssize_t bytes_read);
164 void on_write_complete(ssize_t bytes_written);
165 void on_sendfile_complete(ssize_t bytes_written);
167
169 std::string client_ip_;
175
176 std::vector<char> read_buffer_;
177 mutable std::mutex read_mutex_;
178 std::vector<char> write_buffer_;
179 std::vector<char> active_write_buffer_;
180 mutable std::mutex write_mutex_;
181
182 char async_read_buf_[16384]; // Buffer for kernel to write into asynchronously
183
184 std::unordered_map<std::string, std::string> default_headers_; // Populated by middlewares; guarded by write_mutex_
185 std::unordered_map<std::string, std::string> default_headers_snapshot();
186 std::atomic<bool> is_reading_{false};
187
188 // Runs submit() (one proactor_.async_* call) unless the connection has
189 // been removed. A worker thread can finish a response after the event
190 // loop already removed the connection; registering I/O then would keep
191 // the socket (held by the callback's shared_ptr) open until the peer
192 // gave up. Returns false if nothing was submitted.
193 template <typename Submit>
194 bool submit_io(Submit&& submit) {
195 std::lock_guard<std::mutex> lock(io_mutex_);
196 if (removed_) return false;
197 submit();
198 return true;
199 }
200 std::mutex io_mutex_;
201 bool removed_ = false; // guarded by io_mutex_
202 std::atomic<bool> is_writing_{false};
203 // Separate flags: a handler may start a chunked response while a
204 // chunked request body is still being streamed in.
205 bool response_chunked_{false}; // encoding the outgoing body (handler threads)
206 bool is_head_request_{false}; // Responses to HEAD carry headers only
207
209 // HTTP/1.1 request parsing. Guarded by read_mutex_; fed only while no
210 // handler is using the request it holds (except a streamed body).
211 std::unique_ptr<http::Http1Parser> parser_;
212 bool message_started_ = false; // bytes of the current request were parsed
213 bool headers_parsed_ = false; // its headers are complete, a body follows
214 bool request_complete_ = false; // it is whole, waiting to be handled
215 bool streaming_request_ = false; // a stream route: the handler reads the body
216 std::atomic<bool> should_close_{false};
217 bool h2c_decided_ = false; // event-loop thread: the connection's first bytes were checked
218 // Expect: 100-continue on the request being read, and whether the 100
219 // has been sent for it.
220 std::atomic<bool> expect_continue_{false};
221 std::atomic<bool> continue_sent_{false};
222 // Set (under write_mutex_) together with the final response's bytes;
223 // the connection closes when everything queued has been written.
225 // Set (under write_mutex_) while a file response is being sent; the
226 // next pipelined request waits for it.
228 // Written by handler threads (upgrades, streaming) and read by the event loop.
229 std::atomic<ConnectionState> state_{ConnectionState::HTTP};
230 uint64_t current_timer_id_{0};
231 std::mutex timer_mutex_;
234
235 int file_fd_{-1};
236 off_t file_size_{0};
237 off_t file_offset_{0};
238
240
241 SSL* ssl_{nullptr};
242 BIO* rbio_{nullptr};
243 BIO* wbio_{nullptr};
245 std::vector<char> tls_write_buffer_; // For holding ciphertext before sending
246 // OpenSSL objects are not thread-safe: SSL_read runs on the event loop and
247 // SSL_write on whichever thread sends. tls_mutex_ guards ssl_, the BIOs and
248 // tls_write_buffer_ (encrypted bytes waiting to be written).
249 std::mutex tls_mutex_;
250 // Encrypted bytes handed to the proactor. Only the thread holding
251 // is_writing_ touches it, so it is never reallocated during a write.
252 std::vector<char> tls_inflight_;
254 // True if anything is queued that trigger_write() would send. Also
255 // reports, consistently with the queue, whether a close was requested
256 // and (when nothing is pending) whether a finished file response is
257 // waiting to resume the request pipeline.
258 bool has_pending_output(bool& close_requested, bool& resume);
260
261 std::atomic<bool> is_processing_request_{false};
262 bool shutdown_notified_ = false; // event-loop thread only
263
264 std::unique_ptr<http::websocket::WebSocketConnection> ws_connection_;
265 std::shared_ptr<http::h2::Http2Session> h2_session_;
266 std::function<void(std::string_view)> raw_stream_on_data_;
267 std::function<void()> raw_stream_on_close_;
268 std::function<void(std::string_view)> body_stream_on_data_;
269 std::function<void()> body_stream_on_end_;
270 std::vector<std::function<void(http::HttpResponse&)>> interceptors_;
271};
272
273} // namespace server
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.
Definition Proactor.hpp:9
Definition Socket.hpp:7
Server TLS settings: certificates (with SNI) and ALPN.
Definition TlsContext.hpp:22
Manages routing of HTTP requests to their appropriate handlers.
Definition Router.hpp:46
Manages active HTTP connections.
Definition ConnectionManager.hpp:20
Represents an active client connection, handling request parsing and response writing.
Definition Connection.hpp:67
bool is_head_request_
Definition Connection.hpp:206
std::atomic< bool > is_writing_
Definition Connection.hpp:202
std::mutex write_mutex_
Definition Connection.hpp:180
bool streaming_request_
Definition Connection.hpp:215
std::unique_ptr< http::websocket::WebSocketConnection > ws_connection_
Definition Connection.hpp:264
bool is_tls_handshake_complete_
Definition Connection.hpp:244
BIO * wbio_
Definition Connection.hpp:243
std::vector< std::function< void(http::HttpResponse &)> > interceptors_
Definition Connection.hpp:270
void add_interceptor(std::function< void(http::HttpResponse &)> interceptor) override
std::unordered_map< std::string, std::string > default_headers_
Definition Connection.hpp:184
void write_raw(const std::vector< char > &data)
Writes raw data to the connection.
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.
TimerManager & timer_manager_
Definition Connection.hpp:174
bool request_complete_
Definition Connection.hpp:214
std::shared_ptr< http::h2::Http2Session > h2_session_
Definition Connection.hpp:265
std::atomic< ConnectionState > state_
Definition Connection.hpp:229
void on_write_complete(ssize_t bytes_written)
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.
bool resume_after_write_
Definition Connection.hpp:227
std::vector< char > active_write_buffer_
Definition Connection.hpp:179
void arm_timer(std::chrono::milliseconds timeout)
std::vector< char > read_buffer_
Definition Connection.hpp:176
void end() override
Ends a chunked response stream.
bool has_pending_output(bool &close_requested, bool &resume)
Connection(const Connection &)=delete
bool close_after_write_
Definition Connection.hpp:224
void ping_if_websocket()
Event-loop thread: sends a WebSocket ping if this is a WebSocket.
std::vector< char > tls_write_buffer_
Definition Connection.hpp:245
std::atomic< bool > is_processing_request_
Definition Connection.hpp:261
void upgrade_to_websocket(std::unique_ptr< http::websocket::WebSocketConnection > ws_conn)
Upgrades the connection to a WebSocket.
void set_timeouts(const ConnectionTimeouts &timeouts)
Sets the timeouts used for this connection. Call before start().
Definition Connection.hpp:102
void send_continue_if_expected()
Sends "100 Continue" once per request to a client that asked for it.
network::Socket socket_
Definition Connection.hpp:168
bool h2c_decided_
Definition Connection.hpp:217
std::mutex io_mutex_
Definition Connection.hpp:200
concurrency::ThreadPool & thread_pool() override
Gets the thread pool to offload blocking tasks.
Definition Connection.hpp:140
std::string client_ip_
Definition Connection.hpp:169
off_t file_size_
Definition Connection.hpp:236
bool headers_parsed_
Definition Connection.hpp:213
std::function< void()> raw_stream_on_close_
Definition Connection.hpp:267
void on_removed(bool notify_now=false)
std::vector< char > write_buffer_
Definition Connection.hpp:178
void mark_for_close()
Marks the connection to be closed after writing completes.
const routing::Router & router_
Definition Connection.hpp:171
RequestState check_request_state()
std::vector< char > tls_inflight_
Definition Connection.hpp:252
uint64_t current_timer_id_
Definition Connection.hpp:230
void send_data(std::string_view data, bool close_after=false)
std::unique_ptr< http::Http1Parser > parser_
Definition Connection.hpp:211
void send_error(http::HttpStatus status, const std::string &message)
std::mutex timer_mutex_
Definition Connection.hpp:231
size_t max_body_size_
Definition Connection.hpp:239
bool message_started_
Definition Connection.hpp:212
std::atomic< bool > should_close_
Definition Connection.hpp:216
void start()
Starts processing the connection.
void send_sse_event(std::string_view data, std::string_view event="", std::string_view id="") override
Sends a Server-Sent Events (SSE) message.
std::atomic< bool > continue_sent_
Definition Connection.hpp:221
network::Proactor & proactor() override
Gets the underlying Proactor to dispatch async operations.
Definition Connection.hpp:139
ConnectionTimeouts timeouts_
Definition Connection.hpp:232
Connection & operator=(const Connection &)=delete
const std::string & client_ip() const
Gets the client's IP address.
Definition Connection.hpp:134
void set_limits(const ConnectionLimits &limits)
Definition Connection.hpp:103
void set_header(const std::string &key, const std::string &value) override
Adds a default header that will be included in the final response.
void write_chunk(std::string_view chunk) override
Streams a chunk of data (for Chunked Transfer Encoding).
std::function< void(std::string_view)> raw_stream_on_data_
Definition Connection.hpp:266
void on_read_complete(ssize_t bytes_read)
std::function< void(std::string_view)> body_stream_on_data_
Definition Connection.hpp:268
int file_fd_
Definition Connection.hpp:235
ConnectionLimits limits_
Definition Connection.hpp:233
SSL * ssl_
Definition Connection.hpp:241
concurrency::ThreadPool & thread_pool_
Definition Connection.hpp:173
bool response_chunked_
Definition Connection.hpp:205
bool removed_
Definition Connection.hpp:201
std::unordered_map< std::string, std::string > default_headers_snapshot()
std::mutex read_mutex_
Definition Connection.hpp:177
ConnectionManager & manager_
Definition Connection.hpp:172
network::Proactor & proactor_
Definition Connection.hpp:170
void on_sendfile_complete(ssize_t bytes_written)
BIO * rbio_
Definition Connection.hpp:242
std::function< void()> body_stream_on_end_
Definition Connection.hpp:269
std::atomic< bool > expect_continue_
Definition Connection.hpp:220
std::atomic< bool > is_reading_
Definition Connection.hpp:186
void send(http::HttpResponse &&response) override
Sends a complete HTTP response.
off_t file_offset_
Definition Connection.hpp:237
bool shutdown_notified_
Definition Connection.hpp:262
std::mutex tls_mutex_
Definition Connection.hpp:249
void send_headers(http::HttpResponse &response) override
Sends only the HTTP headers.
bool submit_io(Submit &&submit)
Definition Connection.hpp:194
bool is_message_stream() const
char async_read_buf_[16384]
Definition Connection.hpp:182
Connection(network::Socket socket, const std::string &client_ip, network::Proactor &proactor, const routing::Router &router, ConnectionManager &manager, concurrency::ThreadPool &thread_pool, TimerManager &timer_manager, size_t max_body_size, network::TlsContext *tls_context=nullptr)
Constructs a new Connection.
Manages timers for connection timeouts.
Definition TimerManager.hpp:29
Definition Http2Session.hpp:26
Definition WebSocket.hpp:9
HttpStatus
Standard HTTP status codes.
Definition HttpResponse.hpp:17
Definition Http2Session.hpp:18
ConnectionState
Definition Connection.hpp:24
RequestState
Definition Connection.hpp:32
Timeouts applied to a connection, in the phases described on ServerConfig.
Definition Connection.hpp:48
size_t max_header_bytes
Request line plus headers.
Definition Connection.hpp:49
size_t websocket_max_message_size
Definition Connection.hpp:52
bool h2c
Accept HTTP/2 with prior knowledge (h2c) on plaintext connections.
Definition Connection.hpp:54
size_t max_headers
Header fields per request.
Definition Connection.hpp:51
size_t max_request_line
Definition Connection.hpp:50
Definition Connection.hpp:57
std::chrono::milliseconds keep_alive
Definition Connection.hpp:59
std::chrono::milliseconds websocket_idle
Definition Connection.hpp:61
std::chrono::milliseconds header
Definition Connection.hpp:58
std::chrono::milliseconds idle
Definition Connection.hpp:60