HTTP module

Connection

Base HTTP connection managing socket I/O and message lifecycle

Connection

#include <icy/http/connection.h>

Inherits: SocketAdapter Subclassed by: ClientConnection, ServerConnection

Base HTTP connection managing socket I/O and message lifecycle

Public Methods

ReturnNameDescription
ConnectionCreates a Connection using the given TCP socket.
voidonHeadersCalled when the incoming HTTP headers have been fully parsed.
voidonPayloadCalled for each chunk of incoming body data after headers are complete.
voidonCompleteCalled when the incoming HTTP message is fully received.
voidonCloseCalled when the connection is closed.
ssize_tsend virtualSend raw data to the peer.
ssize_tsendOwned virtualSend an owned payload buffer to the peer.
ssize_tsendHeader virtualSend the outdoing HTTP header.
voidclose virtualClose the connection and schedule the object for deferred deletion.
voidmarkActive virtual inlineMarks the connection as active. Server connections override this to refresh the idle timer.
voidbeginStreaming virtual inlineExplicitly enter long-lived streaming mode. Base connections ignore this; server connections use it to disable keep-alive idle reaping while a response stream is active.
voidendStreaming virtual inlineExit long-lived streaming mode.
boolclosed constReturn true if the connection is closed.
icy::Errorerror constReturn the error object if any.
boolheaderAutoSendEnabled constReturn true if headers should be automatically sent.
voidsetHeaderAutoSendEnabledEnable or disable automatic header emission for the next outgoing send path.
voidreplaceAdapter virtualAssign the new ConnectionAdapter and setup the chain. The flow is: Connection <-> ConnectionAdapter <-> Socket. Takes ownership of the adapter (deferred deletion via uv loop).
voidreplaceAdapter virtualOverload for nullptr (used in destructor to clear adapter).
boolsecure constReturn true if the connection uses TLS/SSL.
net::TCPSocket::Ptr &socketReturn the underlying socket pointer.
net::SocketAdapter *adapter constReturn the underlying adapter pointer.
Request &requestThe HTTP request headers.
Response &responseThe HTTP response headers.
http::Message *incomingHeaderReturns the incoming HTTP message header (request or response depending on role).
http::Message *outgoingHeaderReturns the outgoing HTTP message header (request or response depending on role).

Connection

Connection(const net::TCPSocket::Ptr & socket)

Creates a Connection using the given TCP socket.

Parameters

  • socket The TCP socket to use for I/O. Defaults to a new TCPSocket.

onHeaders

void onHeaders()

Called when the incoming HTTP headers have been fully parsed.


onPayload

void onPayload(const MutableBuffer & buffer)

Called for each chunk of incoming body data after headers are complete.

Parameters

  • buffer Buffer containing the received data chunk.

onComplete

void onComplete()

Called when the incoming HTTP message is fully received.


onClose

void onClose()

Called when the connection is closed.


send

virtual

virtual ssize_t send(const char * data, size_t len, int flags)

Send raw data to the peer.

This is the zero-copy fast path. The caller retains ownership of the payload until the underlying async write completes.


sendOwned

virtual

virtual ssize_t sendOwned(Buffer && buffer, int flags)

Send an owned payload buffer to the peer.

Use this when the payload does not naturally outlive the current scope.


sendHeader

virtual

virtual ssize_t sendHeader()

Send the outdoing HTTP header.


close

virtual

virtual void close()

Close the connection and schedule the object for deferred deletion.


markActive

virtual inline

virtual inline void markActive()

Marks the connection as active. Server connections override this to refresh the idle timer.


beginStreaming

virtual inline

virtual inline void beginStreaming()

Explicitly enter long-lived streaming mode. Base connections ignore this; server connections use it to disable keep-alive idle reaping while a response stream is active.


endStreaming

virtual inline

virtual inline void endStreaming()

Exit long-lived streaming mode.


closed

const

bool closed() const

Return true if the connection is closed.


error

const

icy::Error error() const

Return the error object if any.


headerAutoSendEnabled

const

bool headerAutoSendEnabled() const

Return true if headers should be automatically sent.


setHeaderAutoSendEnabled

void setHeaderAutoSendEnabled(bool enabled)

Enable or disable automatic header emission for the next outgoing send path.


replaceAdapter

virtual

virtual void replaceAdapter(std::unique_ptr< net::SocketAdapter > adapter)

Assign the new ConnectionAdapter and setup the chain. The flow is: Connection <-> ConnectionAdapter <-> Socket. Takes ownership of the adapter (deferred deletion via uv loop).


replaceAdapter

virtual

virtual void replaceAdapter(std::nullptr_t)

Overload for nullptr (used in destructor to clear adapter).


secure

const

bool secure() const

Return true if the connection uses TLS/SSL.


socket

net::TCPSocket::Ptr & socket()

Return the underlying socket pointer.


adapter

const

net::SocketAdapter * adapter() const

Return the underlying adapter pointer.


request

Request & request()

The HTTP request headers.


response

Response & response()

The HTTP response headers.


incomingHeader

http::Message * incomingHeader()

Returns the incoming HTTP message header (request or response depending on role).


outgoingHeader

http::Message * outgoingHeader()

Returns the outgoing HTTP message header (request or response depending on role).

Protected Attributes

ReturnNameDescription
net::TCPSocket::Ptr_socket
net::SocketAdapter *_adapter
Request_request
Response_response
icy::Error_error
bool_closed
bool_shouldSendHeader

_socket

net::TCPSocket::Ptr _socket

_adapter

net::SocketAdapter * _adapter

_request

Request _request

_response

Response _response

_error

icy::Error _error

_closed

bool _closed

_shouldSendHeader

bool _shouldSendHeader

Protected Methods

ReturnNameDescription
voidsetError virtualSet the internal error. Note: Setting the error does not [close()](#close-27) the connection.
boolonSocketConnect virtualnet::SocketAdapter interface
boolonSocketRecv virtualCalled when data is received from the socket. Forwards the event to all registered receivers in priority order.
boolonSocketError virtualCalled when the socket encounters an error. Forwards the event to all registered receivers in priority order.
boolonSocketClose virtualCalled when the socket is closed. Forwards the event to all registered receivers in priority order.

setError

virtual

virtual void setError(const icy::Error & err)

Set the internal error. Note: Setting the error does not [close()](#close-27) the connection.


onSocketConnect

virtual

virtual bool onSocketConnect(net::Socket & socket)

net::SocketAdapter interface


onSocketRecv

virtual

virtual bool onSocketRecv(net::Socket & socket, const MutableBuffer & buffer, const net::Address & peerAddress)

Called when data is received from the socket. Forwards the event to all registered receivers in priority order.

Parameters

  • socket The socket that received data.

  • buffer The received data buffer.

  • peerAddress Address of the sender.

Returns

true to stop propagation to subsequent receivers.


onSocketError

virtual

virtual bool onSocketError(net::Socket & socket, const icy::Error & error)

Called when the socket encounters an error. Forwards the event to all registered receivers in priority order.

Parameters

  • socket The socket that encountered the error.

  • error Error details.

Returns

true to stop propagation to subsequent receivers.


onSocketClose

virtual

virtual bool onSocketClose(net::Socket & socket)

Called when the socket is closed. Forwards the event to all registered receivers in priority order.

Parameters

  • socket The socket that was closed.

Returns

true to stop propagation to subsequent receivers.

Public Types

NameDescription
Ptr

Ptr

std::shared_ptr< Connection > Ptr()