4#ifndef __CWIST_HTTP_H__
5#define __CWIST_HTTP_H__
11#include <netinet/in.h>
12#include <sys/socket.h>
40#define CWIST_HTTP_MAX_HEADER_SIZE (8 * 1024)
41#define CWIST_HTTP_MAX_BODY_SIZE (10 * 1024 * 1024)
42#define CWIST_HTTP_READ_BUFFER_SIZE (16 * 1024)
43#define CWIST_HTTP_TIMEOUT_MS 30000
217 void (*handler_func)(
int client_fd,
void *ctx),
void *ctx);
226 void (*handler)(
int,
void *),
void *ctx);
cwist_http_request * cwist_http_receive_request(int client_fd, char *read_buf, size_t buf_size, size_t *buf_len, cwist_http_parse_error_t *err_out)
Receive and parse an HTTP request from a socket.
int headers_have_content_length(cwist_http_header_node *headers)
Detect whether a header list already defines Content-Length.
void cwist_http_send_error_response(int fd, int status, const char *msg)
Send a minimal error response (Connection: close) for a failed request.
struct cwist_http_request cwist_http_request
const int CWIST_HTTP_LISTEN_FAILED
size_t cwist_http_serialize_headers(cwist_http_response *res, char *buf, size_t buf_size)
Serialize only the status line and headers into a caller buffer.
cwist_error_t cwist_http_server_loop(int server_fd, cwist_server_config *config, void(*handler)(int, void *), void *ctx)
Run the main HTTP accept loop using the configured concurrency strategy.
const int CWIST_CREATE_SOCKET_FAILED
cwist_error_t cwist_http_send_response(int client_fd, cwist_http_response *res)
Send a response over a socket.
struct cwist_http_response cwist_http_response
cwist_http_parse_error_t
Failure reason reported by the request receive APIs.
@ CWIST_HTTP_PARSE_BODY_TOO_LARGE
@ CWIST_HTTP_PARSE_HEADER_OVERFLOW
@ CWIST_HTTP_PARSE_MALFORMED
@ CWIST_HTTP_PARSE_TE_UNSUPPORTED
@ CWIST_HTTP_PARSE_EXPECT_FAILED
int cwist_make_socket_ipv4(struct sockaddr_in *sockv4, const char *address, uint16_t port, uint16_t backlog)
Create an IPv4 socket and bind/listen.
char * cwist_http_header_get(cwist_http_header_node *head, const char *key)
Find a header value by key.
cwist_error_t cwist_http_send_response_head(int client_fd, cwist_http_response *res)
Send only the status line and headers (HEAD replies).
@ CWIST_HTTP_UNAUTHORIZED
@ CWIST_HTTP_INTERNAL_ERROR
@ CWIST_HTTP_NOT_IMPLEMENTED
void cwist_http_request_destroy(cwist_http_request *req)
Destroy an HTTP request object.
cwist_http_request * cwist_http_parse_request(const char *raw_request)
Parse a raw HTTP request string.
void cwist_http_response_add_security_headers(cwist_http_response *res)
Add default security headers (CSP, X-Frame-Options, etc.) if missing.
cwist_sstring * cwist_http_stringify_response(cwist_http_response *res)
Serialize a response to a string.
cwist_error_t cwist_http_header_add(cwist_http_header_node **head, const char *key, const char *value)
Add a header to the list.
void cwist_http_response_destroy(cwist_http_response *res)
Destroy an HTTP response object.
const int CWIST_HTTP_BIND_FAILED
void cwist_http_header_free_all(cwist_http_header_node *head)
Free all headers.
cwist_http_response * cwist_http_response_create(void)
Create a new HTTP response object.
struct cwist_server_config cwist_server_config
cwist_error_t cwist_accept_socket(int server_fd, struct sockaddr *sockv4, void(*handler_func)(int client_fd, void *ctx), void *ctx)
Accept sockets and invoke a handler callback.
const int CWIST_HTTP_UNAVAILABLE_ADDRESS
const char * cwist_http_method_to_string(cwist_http_method_t method)
Convert method enum to string.
cwist_http_method_t cwist_http_string_to_method(const char *method_str)
Convert method string to enum.
const int CWIST_HTTP_SETSOCKOPT_FAILED
struct cwist_http_header_node cwist_http_header_node
Linked list for headers to handle multiple headers easily.
cwist_http_request * cwist_http_request_create(void)
Create a new HTTP request object.
Compatibility header: forwards to <cwist/net/http/query.h>.
Integrated error structure used throughout the system.
cwist_query_map * query_params
Parsed query parameters.
cwist_sstring * path
e.g., "/users/1"
cwist_sstring * query
e.g., "active=true" (raw)
uint32_t stream_id
HTTP/2 or HTTP/3 stream ID (0 for HTTP/1.1).
cwist_sstring * version
e.g., "HTTP/1.1"
void * private_data
Internal framework use (protocol-specific context).
cwist_query_map * path_params
Parsed path parameters (e.g. :id)
cwist_http_header_node * headers
cwist_http_method_t method
HTTP Response Object. Supports standard string body or Zero-Copy pointer body.
cwist_sstring * version
e.g., "HTTP/1.1"
cwist_sstring * status_text
e.g., "OK"
cwist_http_status_t status_code
cwist_http_header_node * headers
bool use_forking
Process per request.
bool use_threading
Thread per request.
bool use_epoll
Use epoll for accepting.