Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_http_response.h File Reference

HTTP/1.1 response: validated headers, memory or file body, framing. More...

#include "nilorea/n_str.h"
#include <stddef.h>
#include <sys/types.h>
Include dependency graph for n_http_response.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  N_HTTP_RESP
 a response under construction (fields are private, use the functions) More...

Macros

#define N_HTTP_RESPONSE_HEADERS_MAX   64
 most headers a response may carry
#define N_HTTP_RESPONSE_VALUE_MAX   8192
 longest header value accepted, in bytes

Typedefs

typedef ssize_t(* n_http_write_fn) (void *ctx, const void *buf, size_t len)
 a write function: write up to len bytes, return how many were written (at least 1) or -1 on a fatal error; partial writes are retried

Enumerations

enum  N_HTTP_RANGE { N_HTTP_RANGE_NONE = 0 , N_HTTP_RANGE_OK = 1 , N_HTTP_RANGE_UNSATISFIABLE = 2 }
 result of n_http_parse_range() More...

Functions

N_HTTP_RANGE n_http_parse_range (const char *value, size_t len, long long total, long long *first, long long *last)
 interpret a Range header for a body of total bytes
int n_http_response_add_header (N_HTTP_RESP *r, const char *name, const char *value)
 add a header line (repeats allowed, e.g.
int n_http_response_append (N_HTTP_RESP *r, const void *data, size_t len)
 append bytes to the memory body (drops a file body)
long long n_http_response_body_length (const N_HTTP_RESP *r)
 body length the response declares (Content-Length), 0 for a bodiless status
int n_http_response_file (N_HTTP_RESP *r, int fd, long long offset, long long length)
 use a file range as the body; the response owns fd from now on (it is closed on free, also when this call fails) and drops any memory body
void n_http_response_free (N_HTTP_RESP **r)
 free a response (closing its file body) and set *r to NULL
const char * n_http_response_get_header (const N_HTTP_RESP *r, const char *name)
 value of the first header of that name (ASCII case-insensitive)
int n_http_response_head (const N_HTTP_RESP *r, N_STR **out)
 append the status line and headers, through the blank line
N_HTTP_RESP * n_http_response_new (int status)
 new response with a status, keep-alive off, HTTP/1.1
size_t n_http_response_remove_header (N_HTTP_RESP *r, const char *name)
 remove every header of that name (ASCII case-insensitive)
int n_http_response_set_connection (N_HTTP_RESP *r, int keep_alive, int version_minor, int head_only)
 connection handling for this response
int n_http_response_set_header (N_HTTP_RESP *r, const char *name, const char *value)
 set a header: remove every header of that name (ASCII case-insensitive), then add it; same refusals as add_header
int n_http_response_set_status (N_HTTP_RESP *r, int status)
 change the status code
int n_http_response_write (const N_HTTP_RESP *r, n_http_write_fn fn, void *ctx)
 write the whole response (head, then body unless HEAD or bodiless)
const char * n_http_status_reason (int status)
 reason phrase of a status code ("Not Found"); a generic class phrase for an unlisted code, "" outside 100..599

Detailed Description

HTTP/1.1 response: validated headers, memory or file body, framing.

Builds a response a server can trust on the wire: every header name must be a token and every value is refused if it holds a CR, LF, NUL or other control byte, so no input reaching a header can split the response or inject one. The framing headers are the builder's, never the caller's: Content-Length is computed, Connection follows the keep-alive decision, Transfer-Encoding is never sent, and Date is added when the caller did not set one. A HEAD response carries the headers of the full response and no body; 1xx, 204 and 304 responses never carry a body or Content-Length.

The response does no I/O of its own: n_http_response_write() hands the bytes to a caller-supplied write function (plain socket, TLS, a buffer in a test), and streams a file body in chunks rather than loading it.

Author
Castagnier Mickael
Version
1.0
Date
2026

Definition in file n_http_response.h.