Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
HTTP3: minimal blocking HTTP/3 client over QUIC

Data Structures

struct  N_HTTP3_RESPONSE
 Result of an HTTP/3 exchange. More...
 

Macros

#define N_HTTP3_BODY_CAP   (16u * 1024u * 1024u)
 Hard cap on the response body kept in memory (bytes).
 
#define N_HTTP3_DEFAULT_TIMEOUT_MS   15000
 Default per-request budget (milliseconds) when the caller passes <= 0.
 
#define N_HTTP3_REQ_ALLOW_ILLEGAL_HEADERS   1u
 n_http3_request_ex flag: send the caller's extra headers WITHOUT the built-in connection-specific / framing filter (so an otherwise-forbidden header such as transfer-encoding, connection or upgrade reaches the wire).
 

Functions

int n_http3_available (void)
 Report whether HTTP/3 support was compiled into the library.
 
int n_http3_get (const char *url, int timeout_ms, N_HTTP3_RESPONSE *out)
 Convenience wrapper: HTTP/3 GET of url.
 
int n_http3_parse_url (const char *url, char *host, size_t hostsz, char *port, size_t portsz, char *path, size_t pathsz)
 Split an https URL into host, port and path (pure; available in every build).
 
int n_http3_request (const char *method, const char *url, const char *headers, const unsigned char *body, size_t body_len, int timeout_ms, N_HTTP3_RESPONSE *out)
 Perform one blocking HTTP/3 request over a fresh QUIC connection.
 
int n_http3_request_ex (const char *method, const char *url, const char *headers, const unsigned char *body, size_t body_len, int timeout_ms, unsigned flags, N_HTTP3_RESPONSE *out)
 Perform one blocking HTTP/3 request, with request flags.
 
void n_http3_response_free (N_HTTP3_RESPONSE *r)
 Free the heap buffers held by r and zero the struct (safe on NULL / zeroed).
 

Detailed Description


Data Structure Documentation

◆ N_HTTP3_RESPONSE

struct N_HTTP3_RESPONSE

Result of an HTTP/3 exchange.

Free the inner buffers with n_http3_response_free.

Definition at line 69 of file n_http3.h.

+ Collaboration diagram for N_HTTP3_RESPONSE:
Data Fields
unsigned char * body Response body bytes (NOT NUL-terminated), or NULL.
size_t body_len Number of valid bytes in body.
char * error Failure reason (heap), or NULL on a completed exchange.
char * headers Response header block as joined "name: value\r\n" lines (lowercased names), or NULL.
int status HTTP status code, or 0 if none was received.

Macro Definition Documentation

◆ N_HTTP3_BODY_CAP

#define N_HTTP3_BODY_CAP   (16u * 1024u * 1024u)

Hard cap on the response body kept in memory (bytes).

Definition at line 58 of file n_http3.h.

◆ N_HTTP3_DEFAULT_TIMEOUT_MS

#define N_HTTP3_DEFAULT_TIMEOUT_MS   15000

Default per-request budget (milliseconds) when the caller passes <= 0.

Definition at line 56 of file n_http3.h.

◆ N_HTTP3_REQ_ALLOW_ILLEGAL_HEADERS

#define N_HTTP3_REQ_ALLOW_ILLEGAL_HEADERS   1u

n_http3_request_ex flag: send the caller's extra headers WITHOUT the built-in connection-specific / framing filter (so an otherwise-forbidden header such as transfer-encoding, connection or upgrade reaches the wire).

For protocol-conformance and request-smuggling security testing of how an HTTP/3 endpoint or a downgrading front-end handles an illegal framing header. Pseudo-headers (a name starting with ':') are still rejected. Use only against authorized targets.

Definition at line 66 of file n_http3.h.

Function Documentation

◆ n_http3_available()

int n_http3_available ( void  )

Report whether HTTP/3 support was compiled into the library.

Returns
1 when built with HAVE_HTTP3 (n_http3_request can run), 0 otherwise.

Definition at line 1104 of file n_http3.c.

Referenced by main().

+ Here is the caller graph for this function:

◆ n_http3_get()

int n_http3_get ( const char *  url,
int  timeout_ms,
N_HTTP3_RESPONSE out 
)

Convenience wrapper: HTTP/3 GET of url.

Parameters
urlThe absolute https URL. NULL/empty returns -1.
timeout_msOverall budget in ms; <= 0 uses the default.
outResult (see n_http3_request). NULL returns -1.
Returns
0 on a completed exchange, -1 otherwise.

Definition at line 128 of file n_http3.c.

References n_http3_request().

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ n_http3_parse_url()

int n_http3_parse_url ( const char *  url,
char *  host,
size_t  hostsz,
char *  port,
size_t  portsz,
char *  path,
size_t  pathsz 
)

Split an https URL into host, port and path (pure; available in every build).

Only the https scheme is accepted. When the URL omits the port, "443" is written. When it omits the path, "/" is written. IPv6 literals in brackets are accepted and the brackets are stripped from the host.

Parameters
urlThe absolute URL (must start with "https://"). NULL returns -1.
hostOut buffer for the host. May be NULL to skip.
hostszSize of host.
portOut buffer for the port (decimal string). May be NULL to skip.
portszSize of port.
pathOut buffer for the path (with a leading '/'). May be NULL to skip.
pathszSize of path.
Returns
0 on success, -1 on a malformed URL or a too-small buffer.

Definition at line 57 of file n_http3.c.

References port.

Referenced by test_parse_url().

+ Here is the caller graph for this function:

◆ n_http3_request()

int n_http3_request ( const char *  method,
const char *  url,
const char *  headers,
const unsigned char *  body,
size_t  body_len,
int  timeout_ms,
N_HTTP3_RESPONSE out 
)

Perform one blocking HTTP/3 request over a fresh QUIC connection.

Resolves the host, opens a QUIC connection with ALPN "h3", sends the request, and collects the response. On success out->status is set and out->error is NULL; on failure the function returns -1 and out->error holds the reason. The caller always frees out with n_http3_response_free.

Parameters
methodThe HTTP method (e.g. "GET", "POST"). NULL uses "GET".
urlThe absolute https URL. NULL/empty returns -1.
headersOptional extra request headers as "Name: Value\r\n" lines, or NULL.
bodyOptional request body bytes, or NULL.
body_lenLength of body in bytes (0 when none).
timeout_msOverall budget in ms; <= 0 uses N_HTTP3_DEFAULT_TIMEOUT_MS.
outResult, zeroed then filled. NULL returns -1.
Returns
0 on a completed exchange (status received), -1 otherwise (out->error set).

Definition at line 1108 of file n_http3.c.

References N_HTTP3_RESPONSE::error.

Referenced by n_http3_get(), n_http3_request_ex(), and test_request_guards().

+ Here is the caller graph for this function:

◆ n_http3_request_ex()

int n_http3_request_ex ( const char *  method,
const char *  url,
const char *  headers,
const unsigned char *  body,
size_t  body_len,
int  timeout_ms,
unsigned  flags,
N_HTTP3_RESPONSE out 
)

Perform one blocking HTTP/3 request, with request flags.

As n_http3_request, plus flags. With N_HTTP3_REQ_ALLOW_ILLEGAL_HEADERS the caller's extra headers bypass the connection-specific / framing filter, so a normally-forbidden header (transfer-encoding, connection, upgrade, keep-alive, proxy-connection) is placed on the HTTP/3 request as-is. This is for protocol-conformance and request-smuggling security testing (how an endpoint or a downgrading front-end treats an illegal framing header); pseudo-headers (names starting with ':') are still rejected. n_http3_request is exactly n_http3_request_ex with flags 0.

Parameters
methodThe HTTP method (e.g. "GET", "POST"). NULL uses "GET".
urlThe absolute https URL. NULL/empty returns -1.
headersOptional extra request headers as "Name: Value\r\n" lines, or NULL.
bodyOptional request body bytes, or NULL.
body_lenLength of body in bytes (0 when none).
timeout_msOverall budget in ms; <= 0 uses N_HTTP3_DEFAULT_TIMEOUT_MS.
flagsBitwise OR of N_HTTP3_REQ_* flags (0 for the default, filtered behaviour).
outResult, zeroed then filled. NULL returns -1.
Returns
0 on a completed exchange (status received), -1 otherwise (out->error set).

Definition at line 1123 of file n_http3.c.

References n_http3_request().

Referenced by main(), and test_request_guards().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ n_http3_response_free()

void n_http3_response_free ( N_HTTP3_RESPONSE r)

Free the heap buffers held by r and zero the struct (safe on NULL / zeroed).

Parameters
rThe response to release.

Definition at line 43 of file n_http3.c.

References N_HTTP3_RESPONSE::body, N_HTTP3_RESPONSE::body_len, N_HTTP3_RESPONSE::error, N_HTTP3_RESPONSE::headers, and N_HTTP3_RESPONSE::status.

Referenced by main(), and test_request_guards().

+ Here is the caller graph for this function: