Skip to content

uvhttp_server.c ​

Functions ​

write_503_response_cb(uv_write_t *req, int status) ​

Return Type: void

Parameters:

  • req: write request object
  • status: write operation state

on_connection(uv_stream_t *server_handle, int status) ​

Return Type: void

Parameters:

  • server_handle: server handle
  • status: connection state

uvhttp_server_new(uv_loop_t *loop, uvhttp_server_t **server) ​

create new HTTP Server

Parameters:

  • loop: Event loop (must be provided by application layer, cannot be NULL)
  • server: output parameter, used for receive server pointer

Returns: UVHTTP_OK success, other value represents failure

Note: Success when, *server is set to valid server object, must use uvhttp_server_free release Note: Failure when, *server is set to NULL

uvhttp_server_free(uvhttp_server_t *server) ​

Release a server and everything it owns.

Parameters:

  • server: Server to release (may be NULL, then this is a no-op)

Returns: UVHTTP_OK on success, error code otherwise

Note: Ownership contract: once set with uvhttp_server_set_router / uvhttp_server_set_context, the router and context are owned by the server and are released by this function. The caller MUST NOT free them separately, otherwise the double free occurs. The same applies to any config reached through the context. Note: If the server was created with uvhttp_server_new_with_loop, the internal event loop is also closed here.

uvhttp_server_new_with_loop(uvhttp_server_t **server) ​

create Server withinfrastructureevent loop

Parameters:

  • server: output parameter, used for receive server pointer

Returns: UVHTTP_OK success, other value represents failure

Note: Creating the libuv event loop internally, the library will handle the lifecycle of the event loop (including cleanup) Note: Related to uvhttp_server_new, this function is suitable for application scenarios that do not need to manage the event loop themselves

uvhttp_server_listen(uvhttp_server_t *server, const char *host, int port) ​

Start listening for incoming connections.

Parameters:

  • server: Server created with uvhttp_server_new / uvhttp_server_new_with_loop
  • host: Bind address (e.g. "0.0.0.0" or "127.0.0.1")
  • port: TCP port to bind

Returns: UVHTTP_OK on success, error code otherwise

Note: The server must be driven by the libuv event loop (uv_run) after this call; the server keeps no background threads.

uvhttp_server_set_handler(uvhttp_server_t *server, uvhttp_request_handler_t handler) ​

Set the request handler used when no router is attached.

Parameters:

  • server: Server to configure
  • handler: Request handler callback

uvhttp_server_set_router(uvhttp_server_t *server, uvhttp_router_t *router) ​

Attach a router to the server.

Parameters:

  • server: Server to configure
  • router: Router created with uvhttp_router_new

Returns: UVHTTP_OK on success, error code otherwise

Note: Ownership contract: after this call the server owns the router and releases it in uvhttp_server_free. Do NOT call uvhttp_router_free on it yourself.

health_check_handler(uvhttp_request_t *req, uvhttp_response_t *resp) ​

Return Type: int

uvhttp_server_enable_health_check(uvhttp_server_t *server, const char *path) ​

uvhttp_server_set_context(uvhttp_server_t *server, struct uvhttp_context *context) ​

Attach a context (shared configuration/state) to the server.

Parameters:

  • server: Server to configure
  • context: Context created with uvhttp_context_create

Returns: UVHTTP_OK on success, error code otherwise

Note: Ownership contract: after this call the server owns the context and releases it in uvhttp_server_free. Do NOT free it yourself.

uvhttp_server_stop(uvhttp_server_t *server) ​

Stop listening and close accepted connections.

Parameters:

  • server: Server to stop

Returns: UVHTTP_OK on success, error code otherwise

Note: The server object itself is NOT released; call uvhttp_server_free afterwards to release it.

uvhttp_server_enable_tls(uvhttp_server_t *server, void *tls_ctx) ​

uvhttp_server_disable_tls(uvhttp_server_t *server) ​

uvhttp_server_is_tls_enabled(uvhttp_server_t *server) ​

Return Type: int

create_simple_server_internal(uv_loop_t *loop, const char *host, int port, uvhttp_server_builder_t **server) ​

uvhttp_server_create(uv_loop_t *loop, const char *host, int port, uvhttp_server_builder_t **server) ​

Parameters:

  • loop: libuv event loop (must be provided by application layer)
  • host: listen address
  • port: listen port
  • server: output parameter, return created server

Returns: UVHTTP_OK success, other value represents error

add_route_internal(uvhttp_server_builder_t *server, const char *path, uvhttp_method_t method, uvhttp_request_handler_t handler) ​

Return Type: [uvhttp_server_builder_t](#structuvhttp__server__builder__t) *

uvhttp_get(uvhttp_server_builder_t *server, const char *path, uvhttp_request_handler_t handler) ​

Return Type: [uvhttp_server_builder_t](#structuvhttp__server__builder__t) *

uvhttp_post(uvhttp_server_builder_t *server, const char *path, uvhttp_request_handler_t handler) ​

Return Type: [uvhttp_server_builder_t](#structuvhttp__server__builder__t) *

uvhttp_put(uvhttp_server_builder_t *server, const char *path, uvhttp_request_handler_t handler) ​

Return Type: [uvhttp_server_builder_t](#structuvhttp__server__builder__t) *

uvhttp_delete(uvhttp_server_builder_t *server, const char *path, uvhttp_request_handler_t handler) ​

Return Type: [uvhttp_server_builder_t](#structuvhttp__server__builder__t) *

uvhttp_any(uvhttp_server_builder_t *server, const char *path, uvhttp_request_handler_t handler) ​

Return Type: [uvhttp_server_builder_t](#structuvhttp__server__builder__t) *

uvhttp_set_max_connections(uvhttp_server_builder_t *server, int max_conn) ​

Return Type: [uvhttp_server_builder_t](#structuvhttp__server__builder__t) *

uvhttp_set_timeout(uvhttp_server_builder_t *server, int timeout) ​

Return Type: [uvhttp_server_builder_t](#structuvhttp__server__builder__t) *

uvhttp_set_max_body_size(uvhttp_server_builder_t *server, size_t size) ​

Return Type: [uvhttp_server_builder_t](#structuvhttp__server__builder__t) *

uvhttp_get_param(uvhttp_request_t *request, const char *name) ​

Return Type: const char *

uvhttp_get_header(uvhttp_request_t *request, const char *name) ​

Return Type: const char *

uvhttp_get_body(uvhttp_request_t *request) ​

Return Type: const char *

uvhttp_server_run(uvhttp_server_builder_t *server) ​

Return Type: int

uvhttp_server_stop_simple(uvhttp_server_builder_t *server) ​

Return Type: void

uvhttp_server_simple_free(uvhttp_server_builder_t *server) ​

Return Type: void

default_handler(uvhttp_request_t *request, uvhttp_response_t *response) ​

Return Type: int

uvhttp_serve(uv_loop_t *loop, const char *host, int port) ​

Return Type: int

uvhttp_server_set_timeout_callback(uvhttp_server_t *server, uvhttp_timeout_callback_t callback, void *user_data) ​

Released under MIT License