Server API Spec
Overview
The Server module manages the HTTP server lifecycle: creation, binding, listening, connection acceptance, and graceful shutdown. It is the top-level object that ties together the event loop, router, TLS context, and WebSocket connection management.
Interfaces
uvhttp_server_new
- Signature:
uvhttp_error_t uvhttp_server_new(uv_loop_t* loop, uvhttp_server_t** server) - Purpose: Create a new HTTP server instance
- Preconditions:
loopmust be a valid, initializeduv_loop_t.servermust be a non-NULL pointer to auvhttp_server_t*that will receive the result. - Postconditions: On success,
*serverpoints to a valid server withis_listening=0,freed=0,active_connections=0,handler=NULL,router=NULL,config=NULL,context=NULL,tls_ctx=NULL. - Error conditions:
UVHTTP_ERROR_INVALID_PARAM:looporserveris NULLUVHTTP_ERROR_OUT_OF_MEMORY: allocation failure
- Thread safety: Not thread-safe. Must be called from the event loop thread.
uvhttp_server_listen
- Signature:
uvhttp_error_t uvhttp_server_listen(uvhttp_server_t* server, const char* host, int port) - Purpose: Bind to a host:port and start accepting connections
- Preconditions:
servermust be valid (created byuvhttp_server_new), not already listening. A handler or router must have been set. - Postconditions: On success,
server->is_listening=1, the TCP handle is bound and listening. On failure, server state is unchanged. - Error conditions:
UVHTTP_ERROR_INVALID_PARAM:serverorhostis NULLUVHTTP_ERROR_SERVER_LISTEN: bind or listen syscall failed
- Thread safety: Not thread-safe.
uvhttp_server_stop
- Signature:
uvhttp_error_t uvhttp_server_stop(uvhttp_server_t* server) - Purpose: Stop accepting new connections. Existing connections continue.
- Preconditions:
servermust be listening. - Postconditions:
server->is_listening=0. The TCP handle is closed. - Error conditions:
UVHTTP_ERROR_INVALID_PARAM:serveris NULLUVHTTP_ERROR_NOT_FOUND: server is not listening
- Thread safety: Not thread-safe.
uvhttp_server_free
- Signature:
uvhttp_error_t uvhttp_server_free(uvhttp_server_t* server) - Purpose: Free all server resources. Must be called after stop.
- Preconditions:
servermust be valid. Should not be listening (callstopfirst). - Postconditions: All server memory is freed. The
freedflag prevents double-free. Any remaining connections are cleaned up. - Error conditions:
UVHTTP_ERROR_INVALID_PARAM:serveris NULL- Double-free is handled gracefully (returns UVHTTP_OK on second call)
- Thread safety: Not thread-safe.
uvhttp_server_set_handler
- Signature:
uvhttp_error_t uvhttp_server_set_handler(uvhttp_server_t* server, uvhttp_request_handler_t handler) - Purpose: Set the default request handler for all requests
- Preconditions:
servermust be valid.handlermust be non-NULL. - Postconditions:
server->handleris set to the provided handler. - Error conditions:
UVHTTP_ERROR_INVALID_PARAM:serverorhandleris NULL
- Thread safety: Not thread-safe.
uvhttp_server_set_router
- Signature:
uvhttp_error_t uvhttp_server_set_router(uvhttp_server_t* server, uvhttp_router_t* router) - Purpose: Attach a router for path-based request dispatching
- Preconditions:
servermust be valid.routermust be a valid router. - Postconditions:
server->routeris set. The server does not own the router; the caller must free it after the server. - Error conditions:
UVHTTP_ERROR_INVALID_PARAM:serverorrouteris NULL
- Thread safety: Not thread-safe.
uvhttp_server_set_context
- Signature:
uvhttp_error_t uvhttp_server_set_context(uvhttp_server_t* server, struct uvhttp_context* context) - Purpose: Attach a context object for shared state
- Preconditions:
servermust be valid.contextmust be a valid context. - Postconditions:
server->contextis set. - Error conditions:
UVHTTP_ERROR_INVALID_PARAM:serverorcontextis NULL
- Thread safety: Not thread-safe.
uvhttp_server_enable_tls / uvhttp_server_disable_tls
- Signature:
uvhttp_error_t uvhttp_server_enable_tls(uvhttp_server_t* server, uvhttp_tls_context_t* tls_ctx)/uvhttp_error_t uvhttp_server_disable_tls(uvhttp_server_t* server) - Purpose: Enable or disable TLS on the server
- Preconditions:
servermust be valid. TLS must be compiled in (UVHTTP_FEATURE_TLS). - Postconditions:
server->tls_enabledis set accordingly. - Error conditions:
UVHTTP_ERROR_INVALID_PARAM:serveris NULLUVHTTP_ERROR_TLS_INIT: TLS context is invalid
- Thread safety: Not thread-safe.
- Feature gate:
#if UVHTTP_FEATURE_TLS
Behavior Rules
Server-request binding: Each incoming connection creates a
uvhttp_request_tanduvhttp_response_tpair. The handler is called once per request.Handler dispatch priority: If a router is set, the router is consulted first. If the router finds a matching handler, it is used. Otherwise, the default handler is used.
Connection limit: The server enforces
max_connections. When the limit is reached, new connections receive a 503 response.Graceful shutdown:
uvhttp_server_stopstops accepting new connections. Existing connections are allowed to complete.uvhttp_server_freecleans up all resources.Double-free protection: The
freedflag prevents double-free. Callinguvhttp_server_freetwice is safe.Rate limiting: When enabled, the server tracks request count per time window. When the limit is exceeded, new requests receive a 429 response.
Performance Requirements
- Connection acceptance: O(1) per new connection
- Handler dispatch: O(1) when router cache is used
- Memory: ~256 bytes per server instance (plus per-connection allocations)
Test Requirements
- Server creation and destruction (with and without router, config, context)
- Listen on valid and invalid hosts/ports
- Stop and restart
- Multiple server instances on the same loop
- Connection limit enforcement
- Double-free protection
- Rate limiting enable/disable/check
- TLS enable/disable
- WebSocket connection management enable/disable
- Handler dispatch with router and without
- Server configuration via builder API