Skip to content

uvhttp_utils.h ​

Utility functions for UVHTTP.

This module provides utility functions including response handling, string operations, and validation helpers.

Functions ​

uvhttp_safe_strncpy(char *dest, const char *src, size_t dest_size) ​

Return Type: int

Safe string copy with size checking.

Parameters:

  • dest: Destination buffer
  • src: Source string to copy
  • dest_size: Size of destination buffer

Returns: int Number of characters copied (excluding null terminator)

Note: Ensures destination is null-terminated Note: Returns 0 if dest_size is 0

uvhttp_send_unified_response(uvhttp_response_t *response, const char *content, size_t length, int status_code) ​

Send unified response with custom content.

Parameters:

  • response: Response object
  • content: Response body content
  • length: Length of content in bytes
  • status_code: HTTP status code (e.g., 200, 404)

Returns: uvhttp_error_t UVHTTP_OK on success, error code otherwise

Note: Caller is responsible for setting Content-Type header Note: Automatically sets status code and sends response

uvhttp_send_error_response(uvhttp_response_t *response, int error_code, const char *error_message, const char *details) ​

Send error response in JSON format.

Parameters:

  • response: Response object
  • error_code: Error code to include in response
  • error_message: Error message
  • details: Additional error details (can be NULL)

Returns: uvhttp_error_t UVHTTP_OK on success, error code otherwise

Note: Response format: {"error": {"code": X, "message": "...", "details": "..."}} Note: Automatically sets Content-Type to application/json Note: Automatically sets status code based on error_code

uvhttp_is_valid_status_code(int status_code) ​

Return Type: int

Validate HTTP status code.

Parameters:

Returns: int TRUE if valid, FALSE otherwise

Note: Valid range: 100-599 Note: Checks for standard HTTP status code ranges

uvhttp_is_valid_ip_address(const char *ip) ​

Return Type: int

Validate IP address format (IPv4 or IPv6)

Parameters:

  • ip: IP address string to validate

Returns: int TRUE if valid, FALSE otherwise

Note: Supports both IPv4 (e.g., "192.168.1.1") and IPv6 (e.g., "::1") Note: Simplified validation, does not check all edge cases

Released under MIT License