Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
orbit::server::Connection Class Reference

Represents an active client connection, handling request parsing and response writing. More...

#include <Connection.hpp>

Inheritance diagram for orbit::server::Connection:
Collaboration diagram for orbit::server::Connection:

Public Member Functions

 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.
 
 ~Connection ()
 
 Connection (const Connection &)=delete
 
Connection & operator= (const Connection &)=delete
 
void start ()
 Starts processing the connection.
 
void on_removed (bool notify_now=false)
 
void set_timeouts (const ConnectionTimeouts &timeouts)
 Sets the timeouts used for this connection. Call before start().
 
void set_limits (const ConnectionLimits &limits)
 
void ping_if_websocket ()
 Event-loop thread: sends a WebSocket ping if this is a WebSocket.
 
void write_raw (const std::vector< char > &data)
 Writes raw data to the connection.
 
void mark_for_close ()
 Marks the connection to be closed after writing completes.
 
void on_server_shutdown ()
 
void upgrade_to_websocket (std::unique_ptr< http::websocket::WebSocketConnection > ws_conn)
 Upgrades the connection to a WebSocket.
 
const std::string & client_ip () const
 Gets the client's IP address.
 
void add_interceptor (std::function< void(http::HttpResponse &)> interceptor) override
 
void set_header (const std::string &key, const std::string &value) override
 Adds a default header that will be included in the final response.
 
network::Proactor & proactor () override
 Gets the underlying Proactor to dispatch async operations.
 
concurrency::ThreadPool & thread_pool () override
 Gets the thread pool to offload blocking tasks.
 
void send (http::HttpResponse &&response) override
 Sends a complete HTTP response.
 
void send_headers (http::HttpResponse &response) override
 Sends only the HTTP headers.
 
void write_chunk (std::string_view chunk) override
 Streams a chunk of data (for Chunked Transfer Encoding).
 
void end () override
 Ends a chunked response stream.
 
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 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 read_body_stream (std::function< void(std::string_view)> on_data, std::function< void()> on_end) override
 Asynchronously streams the incoming HTTP request body.
 
- Public Member Functions inherited from orbit::http::ResponseWriter
virtual ~ResponseWriter ()=default
 
virtual void add_interceptor (Interceptor interceptor)=0
 Adds an interceptor to modify the response before it is sent.
 
bool has_responded () const
 True once anything of a response to the current request has been sent: a full response, its headers, a chunk or an event.
 
void set_error_sink (ErrorSink sink)
 
uint64_t request_generation () const
 
bool is_open () const
 False once the client can no longer receive anything: the connection (HTTP/1.1) or stream (HTTP/2, HTTP/3) closed, because the client left, a timeout fired or the server shut down.
 
void on_close (std::function< void()> callback)
 Runs callback once when the writer closes (see is_open()), or right away if it already has.
 

Private Member Functions

void process_request ()
 
void send_data (std::string_view data, bool close_after=false)
 
void send_continue_if_expected ()
 Sends "100 Continue" once per request to a client that asked for it.
 
void arm_timer (std::chrono::milliseconds timeout)
 
void arm_timer_for_current_phase ()
 
bool is_message_stream () const
 
void send_error (http::HttpStatus status, const std::string &message)
 
void trigger_read ()
 
void on_read_complete (ssize_t bytes_read)
 
void trigger_write ()
 
void on_write_complete (ssize_t bytes_written)
 
void on_sendfile_complete (ssize_t bytes_written)
 
void process_streaming_data ()
 
std::unordered_map< std::string, std::string > default_headers_snapshot ()
 
template<typename Submit >
bool submit_io (Submit &&submit)
 
RequestState check_request_state ()
 
void drain_tls_output_locked ()
 
bool has_pending_output (bool &close_requested, bool &resume)
 
void continue_after_response ()
 

Private Attributes

network::Socket socket_
 
std::string client_ip_
 
network::Proactor & proactor_
 
const routing::Router & router_
 
ConnectionManager & manager_
 
concurrency::ThreadPool & thread_pool_
 
TimerManager & timer_manager_
 
std::vector< char > read_buffer_
 
std::mutex read_mutex_
 
std::vector< char > write_buffer_
 
std::vector< char > active_write_buffer_
 
std::mutex write_mutex_
 
char async_read_buf_ [16384]
 
std::unordered_map< std::string, std::string > default_headers_
 
std::atomic< bool > is_reading_ {false}
 
std::mutex io_mutex_
 
bool removed_ = false
 
std::atomic< bool > is_writing_ {false}
 
bool response_chunked_ {false}
 
bool is_head_request_ {false}
 
std::unique_ptr< http::Http1Parser > parser_
 
bool message_started_ = false
 
bool headers_parsed_ = false
 
bool request_complete_ = false
 
bool streaming_request_ = false
 
std::atomic< bool > should_close_ {false}
 
bool h2c_decided_ = false
 
std::atomic< bool > expect_continue_ {false}
 
std::atomic< bool > continue_sent_ {false}
 
bool close_after_write_ {false}
 
bool resume_after_write_ {false}
 
std::atomic< ConnectionState > state_ {ConnectionState::HTTP}
 
uint64_t current_timer_id_ {0}
 
std::mutex timer_mutex_
 
ConnectionTimeouts timeouts_
 
ConnectionLimits limits_
 
int file_fd_ {-1}
 
off_t file_size_ {0}
 
off_t file_offset_ {0}
 
size_t max_body_size_
 
SSL * ssl_ {nullptr}
 
BIO * rbio_ {nullptr}
 
BIO * wbio_ {nullptr}
 
bool is_tls_handshake_complete_ {false}
 
std::vector< char > tls_write_buffer_
 
std::mutex tls_mutex_
 
std::vector< char > tls_inflight_
 
std::atomic< bool > is_processing_request_ {false}
 
bool shutdown_notified_ = false
 
std::unique_ptr< http::websocket::WebSocketConnection > ws_connection_
 
std::shared_ptr< http::h2::Http2Session > h2_session_
 
std::function< void(std::string_view)> raw_stream_on_data_
 
std::function< void()> raw_stream_on_close_
 
std::function< void(std::string_view)> body_stream_on_data_
 
std::function< void()> body_stream_on_end_
 
std::vector< std::function< void(http::HttpResponse &)> > interceptors_
 

Additional Inherited Members

- Public Types inherited from orbit::http::ResponseWriter
using Interceptor = std::function< void(HttpResponse &)>
 
using ErrorSink = std::function< void(std::exception_ptr, std::shared_ptr< ResponseWriter >)>
 Receives an exception that escaped asynchronous handler code, such as a coroutine handler that threw after a co_await.
 
- Static Public Member Functions inherited from orbit::http::ResponseWriter
static void report_async_exception (const std::shared_ptr< ResponseWriter > &writer, uint64_t generation, std::exception_ptr error) noexcept
 Reports an exception from asynchronous handler code.
 
- Protected Member Functions inherited from orbit::http::ResponseWriter
void mark_responded ()
 Implementations call this whenever they write part of a response.
 
void reset_responded ()
 
void mark_closed () noexcept
 

Detailed Description

Represents an active client connection, handling request parsing and response writing.

Constructor & Destructor Documentation

◆ Connection() [1/2]

orbit::server::Connection::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.

Parameters
socketThe connection socket.
client_ipThe client's IP address.
proactorThe Proactor for async I/O.
routerThe application router.
managerThe connection manager.
thread_poolThe application thread pool.
timer_managerThe timer manager for timeouts.
max_body_sizeMaximum allowed request body size.
tls_contextThe TLS context, if applicable.

◆ ~Connection()

orbit::server::Connection::~Connection ( )

◆ Connection() [2/2]

orbit::server::Connection::Connection ( const Connection &  )
delete

Member Function Documentation

◆ add_interceptor()

void orbit::server::Connection::add_interceptor ( std::function< void(http::HttpResponse &)>  interceptor)
override

◆ arm_timer()

void orbit::server::Connection::arm_timer ( std::chrono::milliseconds  timeout)
private

◆ arm_timer_for_current_phase()

void orbit::server::Connection::arm_timer_for_current_phase ( )
private

◆ check_request_state()

RequestState orbit::server::Connection::check_request_state ( )
private

◆ client_ip()

const std::string & orbit::server::Connection::client_ip ( ) const
inline

Gets the client's IP address.

Returns
The IP address string.

◆ continue_after_response()

void orbit::server::Connection::continue_after_response ( )
private

◆ default_headers_snapshot()

std::unordered_map< std::string, std::string > orbit::server::Connection::default_headers_snapshot ( )
private

◆ drain_tls_output_locked()

void orbit::server::Connection::drain_tls_output_locked ( )
private

◆ end()

void orbit::server::Connection::end ( )
overridevirtual

Ends a chunked response stream.

Implements orbit::http::ResponseWriter.

◆ has_pending_output()

bool orbit::server::Connection::has_pending_output ( bool &  close_requested,
bool &  resume 
)
private

◆ is_message_stream()

bool orbit::server::Connection::is_message_stream ( ) const
private

◆ mark_for_close()

void orbit::server::Connection::mark_for_close ( )

Marks the connection to be closed after writing completes.

◆ on_read_complete()

void orbit::server::Connection::on_read_complete ( ssize_t  bytes_read)
private

◆ on_removed()

void orbit::server::Connection::on_removed ( bool  notify_now = false)

Called by ConnectionManager when the connection is dropped, before its pending I/O is cancelled. No new I/O is submitted afterwards. Writers held elsewhere then report closed (on_close callbacks): on a pool thread, or right away when notify_now (the server is going away and the pool with it).

◆ on_sendfile_complete()

void orbit::server::Connection::on_sendfile_complete ( ssize_t  bytes_written)
private

◆ on_server_shutdown()

void orbit::server::Connection::on_server_shutdown ( )

Event-loop thread, repeatedly while the server drains: closes the connection if it is idle, otherwise makes its current work the last (Connection: close, HTTP/2 GOAWAY, WebSocket close 1001).

◆ on_write_complete()

void orbit::server::Connection::on_write_complete ( ssize_t  bytes_written)
private

◆ operator=()

Connection & orbit::server::Connection::operator= ( const Connection &  )
delete

◆ ping_if_websocket()

void orbit::server::Connection::ping_if_websocket ( )

Event-loop thread: sends a WebSocket ping if this is a WebSocket.

◆ proactor()

network::Proactor & orbit::server::Connection::proactor ( )
inlineoverridevirtual

Gets the underlying Proactor to dispatch async operations.

Returns
A reference to the network::Proactor.

Implements orbit::http::ResponseWriter.

◆ process_request()

void orbit::server::Connection::process_request ( )
private

◆ process_streaming_data()

void orbit::server::Connection::process_streaming_data ( )
private

◆ read_body_stream()

void orbit::server::Connection::read_body_stream ( std::function< void(std::string_view)>  on_data,
std::function< void()>  on_end 
)
overridevirtual

Asynchronously streams the incoming HTTP request body.

Parameters
on_dataCallback invoked when a body chunk is received.
on_endCallback invoked when the entire body has been read.

Implements orbit::http::ResponseWriter.

◆ send()

void orbit::server::Connection::send ( http::HttpResponse &&  response)
overridevirtual

Sends a complete HTTP response.

Takes ownership of any file descriptors managed by the response.

Parameters
responseThe HttpResponse to send.

Implements orbit::http::ResponseWriter.

◆ send_continue_if_expected()

void orbit::server::Connection::send_continue_if_expected ( )
private

Sends "100 Continue" once per request to a client that asked for it.

◆ send_data()

void orbit::server::Connection::send_data ( std::string_view  data,
bool  close_after = false 
)
private

◆ send_error()

void orbit::server::Connection::send_error ( http::HttpStatus  status,
const std::string &  message 
)
private

◆ send_headers()

void orbit::server::Connection::send_headers ( http::HttpResponse &  response)
overridevirtual

Sends only the HTTP headers.

Useful for streaming bodies or Server-Sent Events.

Parameters
responseThe HttpResponse containing the headers to send.

Implements orbit::http::ResponseWriter.

◆ send_sse_event()

void orbit::server::Connection::send_sse_event ( std::string_view  data,
std::string_view  event = "",
std::string_view  id = "" 
)
overridevirtual

Sends a Server-Sent Events (SSE) message.

Parameters
dataThe event data payload.
eventThe event type/name (optional).
idThe event ID (optional).

Implements orbit::http::ResponseWriter.

◆ set_header()

void orbit::server::Connection::set_header ( const std::string &  key,
const std::string &  value 
)
overridevirtual

Adds a default header that will be included in the final response.

Parameters
keyThe header name.
valueThe header value.

Implements orbit::http::ResponseWriter.

◆ set_limits()

void orbit::server::Connection::set_limits ( const ConnectionLimits &  limits)
inline

◆ set_timeouts()

void orbit::server::Connection::set_timeouts ( const ConnectionTimeouts &  timeouts)
inline

Sets the timeouts used for this connection. Call before start().

◆ start()

void orbit::server::Connection::start ( )

Starts processing the connection.

◆ submit_io()

template<typename Submit >
bool orbit::server::Connection::submit_io ( Submit &&  submit)
inlineprivate

◆ thread_pool()

concurrency::ThreadPool & orbit::server::Connection::thread_pool ( )
inlineoverridevirtual

Gets the thread pool to offload blocking tasks.

Returns
A reference to the concurrency::ThreadPool.

Implements orbit::http::ResponseWriter.

◆ trigger_read()

void orbit::server::Connection::trigger_read ( )
private

◆ trigger_write()

void orbit::server::Connection::trigger_write ( )
private

◆ upgrade_to_raw_stream()

void orbit::server::Connection::upgrade_to_raw_stream ( std::function< void(std::string_view)>  on_data,
std::function< void()>  on_close 
)
overridevirtual

Upgrades the connection to a raw bi-directional byte stream.

Parameters
on_dataCallback invoked when data is received.
on_closeCallback invoked when the stream is closed.

Implements orbit::http::ResponseWriter.

◆ upgrade_to_websocket()

void orbit::server::Connection::upgrade_to_websocket ( std::unique_ptr< http::websocket::WebSocketConnection >  ws_conn)

Upgrades the connection to a WebSocket.

Parameters
ws_connThe WebSocket connection instance.

◆ write_chunk()

void orbit::server::Connection::write_chunk ( std::string_view  chunk)
overridevirtual

Streams a chunk of data (for Chunked Transfer Encoding).

Parameters
chunkThe data chunk to write.

Implements orbit::http::ResponseWriter.

◆ write_raw()

void orbit::server::Connection::write_raw ( const std::vector< char > &  data)

Writes raw data to the connection.

Parameters
dataThe raw data to write.

Member Data Documentation

◆ active_write_buffer_

std::vector<char> orbit::server::Connection::active_write_buffer_
private

◆ async_read_buf_

char orbit::server::Connection::async_read_buf_[16384]
private

◆ body_stream_on_data_

std::function<void(std::string_view)> orbit::server::Connection::body_stream_on_data_
private

◆ body_stream_on_end_

std::function<void()> orbit::server::Connection::body_stream_on_end_
private

◆ client_ip_

std::string orbit::server::Connection::client_ip_
private

◆ close_after_write_

bool orbit::server::Connection::close_after_write_ {false}
private

◆ continue_sent_

std::atomic<bool> orbit::server::Connection::continue_sent_ {false}
private

◆ current_timer_id_

uint64_t orbit::server::Connection::current_timer_id_ {0}
private

◆ default_headers_

std::unordered_map<std::string, std::string> orbit::server::Connection::default_headers_
private

◆ expect_continue_

std::atomic<bool> orbit::server::Connection::expect_continue_ {false}
private

◆ file_fd_

int orbit::server::Connection::file_fd_ {-1}
private

◆ file_offset_

off_t orbit::server::Connection::file_offset_ {0}
private

◆ file_size_

off_t orbit::server::Connection::file_size_ {0}
private

◆ h2_session_

std::shared_ptr<http::h2::Http2Session> orbit::server::Connection::h2_session_
private

◆ h2c_decided_

bool orbit::server::Connection::h2c_decided_ = false
private

◆ headers_parsed_

bool orbit::server::Connection::headers_parsed_ = false
private

◆ interceptors_

std::vector<std::function<void(http::HttpResponse&)> > orbit::server::Connection::interceptors_
private

◆ io_mutex_

std::mutex orbit::server::Connection::io_mutex_
private

◆ is_head_request_

bool orbit::server::Connection::is_head_request_ {false}
private

◆ is_processing_request_

std::atomic<bool> orbit::server::Connection::is_processing_request_ {false}
private

◆ is_reading_

std::atomic<bool> orbit::server::Connection::is_reading_ {false}
private

◆ is_tls_handshake_complete_

bool orbit::server::Connection::is_tls_handshake_complete_ {false}
private

◆ is_writing_

std::atomic<bool> orbit::server::Connection::is_writing_ {false}
private

◆ limits_

ConnectionLimits orbit::server::Connection::limits_
private

◆ manager_

ConnectionManager& orbit::server::Connection::manager_
private

◆ max_body_size_

size_t orbit::server::Connection::max_body_size_
private

◆ message_started_

bool orbit::server::Connection::message_started_ = false
private

◆ parser_

std::unique_ptr<http::Http1Parser> orbit::server::Connection::parser_
private

◆ proactor_

network::Proactor& orbit::server::Connection::proactor_
private

◆ raw_stream_on_close_

std::function<void()> orbit::server::Connection::raw_stream_on_close_
private

◆ raw_stream_on_data_

std::function<void(std::string_view)> orbit::server::Connection::raw_stream_on_data_
private

◆ rbio_

BIO* orbit::server::Connection::rbio_ {nullptr}
private

◆ read_buffer_

std::vector<char> orbit::server::Connection::read_buffer_
private

◆ read_mutex_

std::mutex orbit::server::Connection::read_mutex_
mutableprivate

◆ removed_

bool orbit::server::Connection::removed_ = false
private

◆ request_complete_

bool orbit::server::Connection::request_complete_ = false
private

◆ response_chunked_

bool orbit::server::Connection::response_chunked_ {false}
private

◆ resume_after_write_

bool orbit::server::Connection::resume_after_write_ {false}
private

◆ router_

const routing::Router& orbit::server::Connection::router_
private

◆ should_close_

std::atomic<bool> orbit::server::Connection::should_close_ {false}
private

◆ shutdown_notified_

bool orbit::server::Connection::shutdown_notified_ = false
private

◆ socket_

network::Socket orbit::server::Connection::socket_
private

◆ ssl_

SSL* orbit::server::Connection::ssl_ {nullptr}
private

◆ state_

std::atomic<ConnectionState> orbit::server::Connection::state_ {ConnectionState::HTTP}
private

◆ streaming_request_

bool orbit::server::Connection::streaming_request_ = false
private

◆ thread_pool_

concurrency::ThreadPool& orbit::server::Connection::thread_pool_
private

◆ timeouts_

ConnectionTimeouts orbit::server::Connection::timeouts_
private

◆ timer_manager_

TimerManager& orbit::server::Connection::timer_manager_
private

◆ timer_mutex_

std::mutex orbit::server::Connection::timer_mutex_
private

◆ tls_inflight_

std::vector<char> orbit::server::Connection::tls_inflight_
private

◆ tls_mutex_

std::mutex orbit::server::Connection::tls_mutex_
private

◆ tls_write_buffer_

std::vector<char> orbit::server::Connection::tls_write_buffer_
private

◆ wbio_

BIO* orbit::server::Connection::wbio_ {nullptr}
private

◆ write_buffer_

std::vector<char> orbit::server::Connection::write_buffer_
private

◆ write_mutex_

std::mutex orbit::server::Connection::write_mutex_
mutableprivate

◆ ws_connection_

std::unique_ptr<http::websocket::WebSocketConnection> orbit::server::Connection::ws_connection_
private

The documentation for this class was generated from the following file: