Connection API Spec
Overview
The Connection module manages TCP connections, HTTP parsing via llhttp, request/response lifecycle, and connection cleanup. Each connection is tied to a single TCP socket and handles one or more HTTP requests (keep-alive).
Interfaces
uvhttp_connection_new
- Signature:
uvhttp_error_t uvhttp_connection_new(uvhttp_server_t* server, uvhttp_connection_t** conn) - Purpose: Create a new connection object for an accepted TCP socket
- Preconditions:
servermust be valid.connmust be non-NULL. - Postconditions: On success,
*connpoints to a valid connection withstate=UVHTTP_CONN_STATE_NEW,serverset,tcp_handleinitialized. - Error conditions:
UVHTTP_ERROR_INVALID_PARAM: server or conn is NULLUVHTTP_ERROR_OUT_OF_MEMORY: allocation failureUVHTTP_ERROR_CONNECTION_LIMIT: server is at max connections
- Thread safety: Not thread-safe.
uvhttp_connection_start
- Signature:
uvhttp_error_t uvhttp_connection_start(uvhttp_connection_t* conn) - Purpose: Start reading from the connection
- Preconditions:
connmust be valid, in NEW state. - Postconditions: State transitions to
UVHTTP_CONN_STATE_HTTP_READING. Read callback is registered. - Error conditions:
UVHTTP_ERROR_INVALID_PARAM: conn is NULLUVHTTP_ERROR_CONNECTION_INIT: failed to start reading
- Thread safety: Not thread-safe.
uvhttp_connection_close
- Signature:
void uvhttp_connection_close(uvhttp_connection_t* conn) - Purpose: Initiate connection close. Resources are freed in the close callback.
- Preconditions:
connmust be valid. Can be NULL (no-op). - Postconditions: TCP handle close is initiated. Resources are freed in
on_handle_closecallback. - Thread safety: Not thread-safe.
uvhttp_connection_free
- Signature:
void uvhttp_connection_free(uvhttp_connection_t* conn) - Purpose: Free connection resources immediately
- Preconditions:
connmust be valid. Should only be called after close is complete. - Postconditions: All connection memory is freed. The
freedflag prevents double-free. - Error conditions: NULL conn is a no-op.
- Thread safety: Not thread-safe.
uvhttp_connection_set_state
- Signature:
void uvhttp_connection_set_state(uvhttp_connection_t* conn, uvhttp_connection_state_t state) - Purpose: Update connection state
- Preconditions:
connmust be valid. - Postconditions:
conn->stateis updated. - Thread safety: Not thread-safe.
Connection States
NEW → HTTP_READING → (request complete) → HTTP_WRITING → (response sent) → HTTP_READING
NEW → HTTP_READING → (error) → CLOSING → CLOSED
NEW → HTTP_READING → (upgrade) → WEBSOCKET_OPEN → WEBSOCKET_CLOSING → CLOSEDBehavior Rules
Keep-Alive: After a response is sent, the connection returns to HTTP_READING state for the next request. The keep-alive timeout is configurable.
Request/response lifecycle: Each request creates a
uvhttp_request_tanduvhttp_response_tpair. These are recycled for keep-alive connections.Error handling: Parse errors, timeout, or connection reset transition to CLOSING state. The error is logged and the connection is cleaned up.
Double-free protection: The
freedflag prevents double-free. All cleanup functions check this flag before proceeding.TLS support: When TLS is enabled, read/write operations go through mbedtls SSL functions instead of raw TCP.
WebSocket upgrade: When a WebSocket upgrade request is detected, the connection transitions to WebSocket mode.
Test Requirements
- Connection creation and destruction
- State transitions
- Keep-Alive request/response cycle
- Error handling (parse errors, timeout)
- Double-free protection
- TLS connection lifecycle
- WebSocket upgrade path
- NULL parameter handling for all public functions
- Connection limit enforcement