Skip to content

uvhttp_server.h ​

Functions ​

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_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_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, uvhttp_tls_context_t *tls_ctx) ​

uvhttp_server_disable_tls(uvhttp_server_t *server) ​

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_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.

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_enable_rate_limit(uvhttp_server_t *server, int max_requests, int window_seconds) ​

Parameters:

  • server: Server
  • max_requests: whenwindowallowRequest(scope: 1-1000000)
  • window_seconds: whenwindow(seconds, scope: 1-86400)

Returns: UVHTTP_OK Success, othervaluerepresentsFailure

uvhttp_server_disable_rate_limit(uvhttp_server_t *server) ​

Parameters:

  • server: Server

Returns: UVHTTP_OK Success, othervaluerepresentsFailure

uvhttp_server_check_rate_limit(uvhttp_server_t *server) ​

Parameters:

  • server: Server

Returns: UVHTTP_OK allowRequest, UVHTTP_ERROR_RATE_LIMIT_EXCEEDED exceedRate limit

uvhttp_server_add_rate_limit_whitelist(uvhttp_server_t *server, const char *client_ip) ​

Parameters:

  • server: Server
  • client_ip: ClientIPaddress( "127.0.0.1")

Returns: UVHTTP_OK Success, othervaluerepresentsFailure

uvhttp_server_get_rate_limit_status(uvhttp_server_t *server, const char *client_ip, int *remaining, uint64_t *reset_time) ​

Parameters:

  • server: Server
  • client_ip: ClientIPaddress(currentUse, Parameterextend)
  • remaining: remainingRequest(output)
  • reset_time: Timestamp(output, seconds)

Returns: UVHTTP_OK Success, othervaluerepresentsFailure

uvhttp_server_reset_rate_limit_client(uvhttp_server_t *server, const char *client_ip) ​

Parameters:

  • server: Server
  • client_ip: ClientIPaddress(currentUse, Parameterextend)

Returns: UVHTTP_OK Success, othervaluerepresentsFailure

uvhttp_server_clear_rate_limit_all(uvhttp_server_t *server) ​

Parameters:

  • server: Server

Returns: UVHTTP_OK Success, othervaluerepresentsFailure

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

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_enable_health_check(uvhttp_server_t *server, const char *path) ​

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

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

Return Type: int

uvhttp_server_register_ws_handler(uvhttp_server_t *server, const char *path, uvhttp_ws_handler_t *handler) ​

uvhttp_server_ws_send(uvhttp_ws_connection_t *ws_conn, const char *data, size_t len) ​

uvhttp_server_ws_close(uvhttp_ws_connection_t *ws_conn, int code, const char *reason) ​

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

Return Type: [uvhttp_ws_handler_t](#structuvhttp__ws__handler__t) *

uvhttp_server_ws_enable_connection_management(uvhttp_server_t *server, int timeout_seconds, int heartbeat_interval) ​

uvhttp_server_ws_disable_connection_management(uvhttp_server_t *server) ​

uvhttp_server_ws_get_connection_count(uvhttp_server_t *server) ​

Return Type: int

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

Return Type: int

uvhttp_server_ws_broadcast(uvhttp_server_t *server, const char *path, const char *data, size_t len) ​

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

uvhttp_server_ws_add_connection(uvhttp_server_t *server, uvhttp_ws_connection_t *ws_conn, const char *path) ​

Return Type: void

uvhttp_server_ws_remove_connection(uvhttp_server_t *server, uvhttp_ws_connection_t *ws_conn) ​

Return Type: void

uvhttp_server_ws_update_activity(uvhttp_server_t *server, uvhttp_ws_connection_t *ws_conn) ​

Return Type: void

uvhttp_request_init(uvhttp_request_t *request, uv_tcp_t *client) ​

uvhttp_request_cleanup(uvhttp_request_t *request) ​

Return Type: void

Released under MIT License