Orbit Framework
An ultra-modern, asynchronous, cross-platform C++ Web Framework.
Loading...
Searching...
No Matches
Connection.hpp
Go to the documentation of this file.
1#pragma once
9#include <vector>
10#include <string_view>
11#include <memory>
12#include <mutex>
13
14namespace http::websocket { class WebSocketConnection; }
15namespace http::h2 { class Http2Session; }
16
17namespace server {
18
19class ConnectionManager; // Forward declaration
20
28
36
40class Connection : public std::enable_shared_from_this<Connection>, public http::ResponseWriter {
41public:
54 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);
56
57 Connection(const Connection&) = delete;
58 Connection& operator=(const Connection&) = delete;
59
63 void start();
64
69 void write_raw(const std::vector<char>& data);
70
74 void mark_for_close();
75
80 void upgrade_to_websocket(std::unique_ptr<http::websocket::WebSocketConnection> ws_conn);
81
86 const std::string& client_ip() const { return client_ip_; }
87
88 // ResponseWriter Implementation
89 void add_interceptor(std::function<void(http::HttpResponse&)> interceptor) override;
90 void set_header(const std::string& key, const std::string& value) override;
91 network::Proactor& proactor() override { return proactor_; }
92 concurrency::ThreadPool& thread_pool() override { return thread_pool_; }
93 void send(http::HttpResponse&& response) override;
94 void send_headers(http::HttpResponse& response) override;
95 void write_chunk(std::string_view chunk) override;
96 void end() override;
97 void send_sse_event(std::string_view data, std::string_view event = "", std::string_view id = "") override;
98 void upgrade_to_raw_stream(std::function<void(std::string_view)> on_data, std::function<void()> on_close) override;
99 void read_body_stream(std::function<void(std::string_view)> on_data, std::function<void()> on_end) override;
100
101private:
102 void process_request();
103 void send_data(std::string_view data);
104 void reset_timer();
105 void send_error(http::HttpStatus status, const std::string& message);
106
107 void trigger_read();
108 void on_read_complete(ssize_t bytes_read);
109 void trigger_write();
110 void on_write_complete(ssize_t bytes_written);
111 void on_sendfile_complete(ssize_t bytes_written);
112 void process_streaming_data();
113
114 network::Socket socket_;
115 std::string client_ip_;
116 network::Proactor& proactor_;
117 const routing::Router& router_;
118 ConnectionManager& manager_;
119 concurrency::ThreadPool& thread_pool_;
120 TimerManager& timer_manager_;
121
122 std::vector<char> read_buffer_;
123 mutable std::mutex read_mutex_;
124 std::vector<char> write_buffer_;
125 std::vector<char> active_write_buffer_;
126 mutable std::mutex write_mutex_;
127
128 char async_read_buf_[16384]; // Buffer for kernel to write into asynchronously
129
130 std::unordered_map<std::string, std::string> default_headers_; // Populated by middlewares
131 std::string current_request_buffer_; // Holds the request data for string_views during async processing
132 std::atomic<bool> is_reading_{false};
133 std::atomic<bool> is_writing_{false};
134 bool is_chunked_{false};
135 bool is_chunk_header_mode_{true};
136 size_t chunk_bytes_remaining_{0};
137 size_t content_length_remaining_{0};
138
139 RequestState check_request_state();
140 bool should_close_{false};
142 uint64_t current_timer_id_{0};
143
144 int file_fd_{-1};
145 off_t file_size_{0};
146 off_t file_offset_{0};
147
148 size_t max_body_size_;
149
150 SSL* ssl_{nullptr};
151 BIO* rbio_{nullptr};
152 BIO* wbio_{nullptr};
153 bool is_tls_handshake_complete_{false};
154 std::vector<char> tls_write_buffer_; // For holding ciphertext before sending
155
156 std::atomic<bool> is_processing_request_{false};
157
158 std::unique_ptr<http::websocket::WebSocketConnection> ws_connection_;
159 std::shared_ptr<http::h2::Http2Session> h2_session_;
160 std::function<void(std::string_view)> raw_stream_on_data_;
161 std::function<void()> raw_stream_on_close_;
162 std::function<void(std::string_view)> body_stream_on_data_;
163 std::function<void()> body_stream_on_end_;
164 std::vector<std::function<void(http::HttpResponse&)>> interceptors_;
165};
166
167} // namespace server
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
Definition Proactor.hpp:8
Definition Socket.hpp:6
Definition TlsContext.hpp:10
Manages routing of HTTP requests to their appropriate handlers.
Definition Router.hpp:30
Manages active HTTP connections.
Definition ConnectionManager.hpp:17
Represents an active client connection, handling request parsing and response writing.
Definition Connection.hpp:40
const std::string & client_ip() const
Gets the client's IP address.
Definition Connection.hpp:86
void add_interceptor(std::function< void(http::HttpResponse &)> interceptor) override
Definition Connection.cpp:379
~Connection()
Definition Connection.cpp:38
concurrency::ThreadPool & thread_pool() override
Gets the thread pool to offload blocking tasks.
Definition Connection.hpp:92
Connection(const Connection &)=delete
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 Connection.cpp:374
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 Connection.cpp:696
Connection & operator=(const Connection &)=delete
void send_headers(http::HttpResponse &response) override
Sends only the HTTP headers.
Definition Connection.cpp:384
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 Connection.cpp:517
network::Proactor & proactor() override
Gets the underlying Proactor to dispatch async operations.
Definition Connection.hpp:91
void upgrade_to_websocket(std::unique_ptr< http::websocket::WebSocketConnection > ws_conn)
Upgrades the connection to a WebSocket.
Definition Connection.cpp:691
void write_chunk(std::string_view chunk) override
Streams a chunk of data (for Chunked Transfer Encoding).
Definition Connection.cpp:472
void end() override
Ends a chunked response stream.
Definition Connection.cpp:486
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 Connection.cpp:711
void start()
Starts processing the connection.
Definition Connection.cpp:57
void mark_for_close()
Marks the connection to be closed after writing completes.
Definition Connection.cpp:686
void send(http::HttpResponse &&response) override
Sends a complete HTTP response.
Definition Connection.cpp:409
void write_raw(const std::vector< char > &data)
Writes raw data to the connection.
Definition Connection.cpp:678
Manages timers for connection timeouts.
Definition TimerManager.hpp:29
Definition Http2Session.hpp:22
Definition WebSocket.hpp:7
HttpStatus
Standard HTTP status codes.
Definition HttpResponse.hpp:14
Definition Http2Session.hpp:17
RequestState
Definition Connection.hpp:29
ConnectionState
Definition Connection.hpp:21