Module <cweb/http.h> — HTTP request and response
Reparsing, construction, and freeing HTTP/1.1 Request and Response structures.
Important types (overview)
Request: method, path, version, header list, body, session ID/cookie reference,Session*.Response: status line, headers, body (body_len), optional async fields (state,async_data,async_cancel).ResponseState:NPROCESSED,PROCESSING,PROCESSED,ERROR— for async handling (e.g. fetch).- Constants:
MAX_HEADERS,MAX_PATH_LEN,READ_BUFFER_SIZE.
Request lifecycle
Request *cweb_parse_request(const char *raw_request, size_t len)
- Purpose: Parses a raw text buffer (typically from socket) into a
Request. - Parameters:
raw_request— start of HTTP raw block (null-terminated or length vialen).len— usable length in bytes.
- Return: pointer to newly allocated
Request, orNULLon parse error. - Memory: on success free with
cweb_free_http_request.
void cweb_free_http_request(Request *req)
- Purpose: Frees all resources allocated by the framework for this request.
- Parameters:
req— valid pointer or per convention framework handling (NULLideally no-op). - Return: none.
Response lifecycle
Response *cweb_create_response(void)
- Purpose: Creates empty response with sensible defaults.
- Return: pointer to new
Response. - Memory: free with
cweb_free_http_responsewhen no longer needed (and not still referenced by send path).
void cweb_free_http_response(Response *res)
- Purpose: Frees response including body as managed by framework.
- Parameters: valid
Response*. - Return: none.
char *cweb_serialize_response(Response *res, size_t *total_len)
- Purpose: Builds raw HTTP bytes (status line + headers + body) for socket output.
- Parameters:
res— filled response.total_len— output: total length of returned buffer.
- Return: newly allocated buffer with bytes to send; caller must
free(unless framework send path takes ownership — for manual use free consistently).
void cweb_add_response_header(Response *res, const char *key, const char *value)
- Purpose: Adds HTTP header (
key:value). - Parameters:
res— response;key,value— C strings (expected valid until serialize/send).
void cweb_add_performance_headers(Response *res, const char *content_type)
- Purpose: Adds headers relevant for performance hints/timing (framework-specific).
- Parameters:
content_type— e.g.text/html,application/json.
void cweb_add_preload_headers(Response *res)
- Purpose: Adds preload link headers for prioritized resources (if set by framework).
Helpers
const char *cweb_get_status_message(int code)
- Purpose: Returns canonical reason phrase text for numeric HTTP status (e.g.
200→"OK"). - Parameters:
code— HTTP status code. - Return: static string; do not free.
const char *cweb_get_request_header(const Request *req, const char *key)
- Purpose: Looks up request header case-insensitively by name.
- Parameters:
req— parsed request;key— header name (e.g."Cookie"). - Return: pointer to value or
NULLif missing. String lives inRequest; valid untilcweb_free_http_request.
const char *cweb_get_response_header(const Response *res, const char *key)
- Purpose: Same for response headers.
- Return: value or
NULL; validity tied toreslifetime.
See also
- server.md —
cweb_send_response, pending responses - compress.md —
cweb_auto_compress - session.md — session on request