HTTP module

ClientConnection

HTTP client connection for managing request/response lifecycle.

ClientConnection

#include <icy/http/client.h>

Inherits: Connection

HTTP client connection for managing request/response lifecycle.

Public Attributes

ReturnNameDescription
void *opaqueOptional unmanaged client data pointer. Not used by the connection internally.
NullSignalConnectStatus signals.
Signal< void(Response &)>HeadersSignals when the response HTTP header has been received.
Signal< void(const MutableBuffer &)>PayloadSignals when raw data is received.
Signal< void(const Response &)>CompleteSignals when the HTTP transaction is complete.
Signal< void(const icy::Error &)>ErrorSignals when the underlying transport reports an error.
Signal< void(Connection &)>CloseSignals when the connection is closed.
ProgressSignalIncomingProgressSignals download progress (0-100%)

opaque

void * opaque

Optional unmanaged client data pointer. Not used by the connection internally.


Connect

NullSignal Connect

Status signals.

Signals when the client socket is connected and data can flow


Headers

Signal< void(Response &)> Headers

Signals when the response HTTP header has been received.


Payload

Signal< void(const MutableBuffer &)> Payload

Signals when raw data is received.


Complete

Signal< void(const Response &)> Complete

Signals when the HTTP transaction is complete.


Error

Signal< void(const icy::Error &)> Error

Signals when the underlying transport reports an error.


Close

Signal< void(Connection &)> Close

Signals when the connection is closed.


IncomingProgress

ProgressSignal IncomingProgress

Signals download progress (0-100%)

Public Methods

ReturnNameDescription
ClientConnectionCreates a ClientConnection to the given URL, pre-populating the request URI and Host header. The response status is initialised to 502 Bad Gateway until a real response is received.
voidstart virtualStarts the internal HTTP request.
voidstart virtualStarts the given HTTP request, replacing the internal request object.
ssize_tsend virtualSends raw data to the peer, initiating a connection first if needed. Data is buffered internally until the connection is established.
voidsetReadStream virtualSets the output stream to which incoming response body data is written. The stream pointer is owned by the connection and freed with it. Must be called before start().
StreamT &readStream inlineReturns a reference to the read stream cast to the specified type.

ClientConnection

ClientConnection(const URL & url, const net::TCPSocket::Ptr & socket)

Creates a ClientConnection to the given URL, pre-populating the request URI and Host header. The response status is initialised to 502 Bad Gateway until a real response is received.

Parameters

  • url Target URL. Scheme, host, port and path are extracted automatically.

  • socket TCP socket to use. Defaults to a plain TCPSocket; pass an SSLSocket for HTTPS.


start

virtual

virtual void start()

Starts the internal HTTP request.

Calls connect() internally if the socket is not already connecting or connected. The actual request will be sent when the socket is connected.

Exceptions

  • std::runtime_error if already connecting.

start

virtual

virtual void start(http::Request & req)

Starts the given HTTP request, replacing the internal request object.

Calls connect() internally if the socket is not already connecting or connected. The actual request will be sent when the socket is connected.

Parameters

  • req The HTTP request to send. Replaces the internal request.

Exceptions

  • std::runtime_error if already connecting.

send

virtual

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

Sends raw data to the peer, initiating a connection first if needed. Data is buffered internally until the connection is established.

Parameters

  • data Pointer to the data buffer.

  • len Number of bytes to send.

  • flags Socket send flags (unused for HTTP).

Returns

Number of bytes sent or buffered.


setReadStream

virtual

virtual void setReadStream(std::ostream * os)

Sets the output stream to which incoming response body data is written. The stream pointer is owned by the connection and freed with it. Must be called before start().

Parameters

  • os Pointer to the output stream. Takes ownership.

Exceptions

  • std::runtime_error if already connecting.

readStream

inline

template<class StreamT> inline StreamT & readStream()

Returns a reference to the read stream cast to the specified type.

Parameters

  • StreamT The concrete stream type to cast to.

Returns

Reference to the stream.

Exceptions

  • std::runtime_error if no read stream has been set.

  • std::bad_cast if the stream is not of type StreamT.

Protected Attributes

ReturnNameDescription
URL_url
bool_connect
bool_active
bool_complete
std::vector< PendingWrite >_outgoingBuffer
std::unique_ptr< std::ostream >_readStream

_url

URL _url

_connect

bool _connect

_active

bool _active

_complete

bool _complete

_outgoingBuffer

std::vector< PendingWrite > _outgoingBuffer

_readStream

std::unique_ptr< std::ostream > _readStream

Protected Methods

ReturnNameDescription
voidconnect virtualConnects to the server endpoint. All sent data is buffered until the connection is made.
http::Message *incomingHeader virtualReturns the incoming HTTP message header (request or response depending on role).
http::Message *outgoingHeader virtualReturns the outgoing HTTP message header (request or response depending on role).
boolonSocketConnect virtualnet::SocketAdapter interface

connect

virtual

virtual void connect()

Connects to the server endpoint. All sent data is buffered until the connection is made.


incomingHeader

virtual

virtual http::Message * incomingHeader()

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


outgoingHeader

virtual

virtual http::Message * outgoingHeader()

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


onSocketConnect

virtual

virtual bool onSocketConnect(net::Socket & socket)

net::SocketAdapter interface

Public Types

NameDescription
Ptr

Ptr

std::shared_ptr< ClientConnection > Ptr()

Private Methods

ReturnNameDescription
voidonHeaders virtualConnection interface.
voidonPayload virtualCalled for each chunk of incoming response body data.
voidonComplete virtualCalled when the full HTTP response has been received.
voidonClose virtualCalled when the connection is closed.
boolonSocketError virtualCalled when the underlying transport encounters an error.

onHeaders

virtual

virtual void onHeaders()

Connection interface.

Called when the response headers have been parsed.


onPayload

virtual

virtual void onPayload(const MutableBuffer & buffer)

Called for each chunk of incoming response body data.


onComplete

virtual

virtual void onComplete()

Called when the full HTTP response has been received.


onClose

virtual

virtual void onClose()

Called when the connection is closed.


onSocketError

virtual

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

Called when the underlying transport encounters an error.