HTTP module

Server

HTTP server implementation.

Server

#include <icy/http/server.h>

Inherits: SocketAdapter

HTTP server implementation.

This HTTP server is not strictly standards compliant. It was created to be a fast (nocopy where possible) solution for streaming media to web browsers.

Public Attributes

ReturnNameDescription
LocalSignal< void(ServerConnection::Ptr)>ConnectionSignals when a new connection has been created. A reference to the new connection object is provided.
LocalSignal< void()>ShutdownSignals when the server is shutting down.

Connection

LocalSignal< void(ServerConnection::Ptr)> Connection

Signals when a new connection has been created. A reference to the new connection object is provided.


Shutdown

LocalSignal< void()> Shutdown

Signals when the server is shutting down.

Public Methods

ReturnNameDescription
ServerConstructs an HTTP server on the given host and port using an internally created TCP socket.
ServerConstructs an HTTP server on the given address using an internally created TCP socket.
ServerConstructs an HTTP server on the given host and port using a caller-supplied socket. Useful for HTTPS by passing an SSLSocket. The event loop is derived from the socket.
ServerConstructs an HTTP server on the given address using a caller-supplied socket. The event loop is derived from the socket.
voidstartStart the HTTP server.
voidstopStop the HTTP server.
voidsetReusePort inlineEnable SO_REUSEPORT for multicore server instances. Must be called before start(). Allows multiple server instances to bind the same address:port with kernel-level load balancing (Linux 3.9+).
voidsetMaxPooledConnections inlineSet the maximum number of pooled connections (default 128). Set to 0 to disable connection pooling entirely.
voidsetKeepAliveTimeout inlineSet the keep-alive idle timeout in seconds (default 30). Connections idle longer than this are closed by the timer. Set to 0 to disable idle timeout.
size_tconnectionCount const inlineReturn the number of active connections (all states).
net::Address &addressReturn the server bind address.
const DateCache &dateCache const inlineReturn the cached Date header for use in responses.

Server

Server(const std::string & host, short port, uv::Loop * loop, std::unique_ptr< ServerConnectionFactory > factory)

Constructs an HTTP server on the given host and port using an internally created TCP socket.

Parameters

  • host Bind address (e.g. "0.0.0.0" or "127.0.0.1").

  • port TCP port to listen on.

  • loop Event loop to use. Defaults to the default libuv loop.

  • factory Connection and responder factory. Defaults to the base factory.


Server

Server(const net::Address & address, uv::Loop * loop, std::unique_ptr< ServerConnectionFactory > factory)

Constructs an HTTP server on the given address using an internally created TCP socket.

Parameters

  • address Bind address and port.

  • loop Event loop to use. Defaults to the default libuv loop.

  • factory Connection and responder factory.


Server

Server(const std::string & host, short port, net::TCPSocket::Ptr socket, std::unique_ptr< ServerConnectionFactory > factory)

Constructs an HTTP server on the given host and port using a caller-supplied socket. Useful for HTTPS by passing an SSLSocket. The event loop is derived from the socket.

Parameters

  • host Bind address.

  • port TCP port to listen on.

  • socket Pre-created socket (e.g. SSLSocket for HTTPS).

  • factory Connection and responder factory.


Server

Server(const net::Address & address, net::TCPSocket::Ptr socket, std::unique_ptr< ServerConnectionFactory > factory)

Constructs an HTTP server on the given address using a caller-supplied socket. The event loop is derived from the socket.

Parameters

  • address Bind address and port.

  • socket Pre-created socket (e.g. SSLSocket for HTTPS).

  • factory Connection and responder factory.


start

void start()

Start the HTTP server.


stop

void stop()

Stop the HTTP server.


setReusePort

inline

inline void setReusePort(bool enable)

Enable SO_REUSEPORT for multicore server instances. Must be called before start(). Allows multiple server instances to bind the same address:port with kernel-level load balancing (Linux 3.9+).


setMaxPooledConnections

inline

inline void setMaxPooledConnections(size_t n)

Set the maximum number of pooled connections (default 128). Set to 0 to disable connection pooling entirely.


setKeepAliveTimeout

inline

inline void setKeepAliveTimeout(int seconds)

Set the keep-alive idle timeout in seconds (default 30). Connections idle longer than this are closed by the timer. Set to 0 to disable idle timeout.


connectionCount

const inline

inline size_t connectionCount() const

Return the number of active connections (all states).


address

net::Address & address()

Return the server bind address.


dateCache

const inline

inline const DateCache & dateCache() const

Return the cached Date header for use in responses.

Protected Attributes

ReturnNameDescription
uv::Loop *_loop
net::Address_address
net::TCPSocket::Ptr_socket
Timer_timer
std::unique_ptr< ServerConnectionFactory >_factory
std::unordered_map< ServerConnection *, ServerConnection::Ptr >_connections
ConnectionPool_pool
DateCache_dateCache
int_keepAliveTimeout
bool_reusePort

_loop

uv::Loop * _loop

_address

net::Address _address

_socket

net::TCPSocket::Ptr _socket

_timer

Timer _timer

_factory

std::unique_ptr< ServerConnectionFactory > _factory

_connections

std::unordered_map< ServerConnection *, ServerConnection::Ptr > _connections

_pool

ConnectionPool _pool

_dateCache

DateCache _dateCache

_keepAliveTimeout

int _keepAliveTimeout {30}

_reusePort

bool _reusePort {false}

Protected Methods

ReturnNameDescription
std::unique_ptr< ServerResponder >createResponder
voidonClientSocketAccept
voidonConnectionReady
voidonConnectionClose
boolonSocketClose virtualCalled when the socket is closed. Forwards the event to all registered receivers in priority order.
voidonTimer
uv::Loop *loop const inlineReturn the event loop this server runs on.

createResponder

std::unique_ptr< ServerResponder > createResponder(ServerConnection & conn)

onClientSocketAccept

void onClientSocketAccept(const net::TCPSocket::Ptr & socket)

onConnectionReady

void onConnectionReady(ServerConnection & conn)

onConnectionClose

void onConnectionClose(ServerConnection & conn)

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.


onTimer

void onTimer()

loop

const inline

inline uv::Loop * loop() const

Return the event loop this server runs on.