DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
http_parser.cpp File Reference

Standalone HTTP/1.1 request parser - implementation. More...

Go to the source code of this file.

Functions

void http_parser_reset (HttpReq *req)
 Reset a parser context to the initial (ParseState::PARSE_METHOD) state.
 
void http_parser_feed (HttpReq *p, uint8_t byte)
 Feed one byte to the parser state machine.
 
const char * http_get_header (const HttpReq *req, const char *key)
 Look up a header value by name (case-insensitive).
 
bool http_get_cookie (const HttpReq *req, const char *name, char *out, size_t out_size)
 Read a named cookie from the request Cookie header (RFC 6265 4.2.1).
 
bool http_forwarded_client (const HttpReq *req, char *ip_out, size_t ip_cap, bool *is_https)
 Recover the original client from a reverse-proxy Forwarded (RFC 7239) or de-facto X-Forwarded-For / X-Forwarded-Proto header.
 
const char * http_get_query (const HttpReq *req, const char *key)
 Look up a query parameter value by name (case-sensitive).
 
bool http_get_form (const HttpReq *req, const char *key, char *out, size_t out_size)
 Look up an application/x-www-form-urlencoded body field by name.
 
const char * http_get_param (const HttpReq *req, const char *key)
 Look up a captured path parameter by name (case-sensitive).
 

Variables

HttpReq http_pool [CONN_POOL_SLOTS]
 Pool of parser contexts, one per connection-pool slot (incl. reserved dispatch slots).
 

Detailed Description

Standalone HTTP/1.1 request parser - implementation.

No dependency on transport, session, or lwIP. Consumes one byte at a time via http_parser_feed(); the presentation layer is responsible for pulling bytes out of whatever transport buffer it uses.

Definition in file http_parser.cpp.

Function Documentation

◆ http_parser_reset()

void http_parser_reset ( HttpReq req)

Reset a parser context to the initial (ParseState::PARSE_METHOD) state.

Zeroes all fields and sets parse_state = ParseState::PARSE_METHOD. Call before the first use, after each completed or failed request, and on connection events.

Parameters
reqParser context to reset. Must not be null.

Definition at line 148 of file http_parser.cpp.

References HttpReq::_version_hash, PARSE_COMPLETE, PARSE_METHOD, HttpReq::parse_state, and HttpReq::slot_id.

Referenced by http_reset().

◆ http_parser_feed()

◆ http_get_header()

const char * http_get_header ( const HttpReq req,
const char *  key 
)

Look up a header value by name (case-insensitive).

Parameters
reqParsed request.
keyHeader field name (e.g. "Content-Type").
Returns
Pointer to the null-terminated value, or nullptr if not found.

Definition at line 506 of file http_parser.cpp.

References HttpReq::header_count, HttpReq::headers, Header::key, and Header::val.

Referenced by http_forwarded_client(), http_get_cookie(), http_get_form(), and multipart_parse().

◆ http_get_cookie()

bool http_get_cookie ( const HttpReq req,
const char *  name,
char *  out,
size_t  out_size 
)

Read a named cookie from the request Cookie header (RFC 6265 4.2.1).

Parses the name1=value1; name2=value2 list and copies the value of cookie name (case-sensitive) into out (null-terminated, bounded by out_size; a surrounding DQUOTE pair is stripped). Pairs with the session / CSRF / auth features (e.g. reading a session-id cookie).

Returns
true if the cookie was found (value in out), false otherwise.

Definition at line 516 of file http_parser.cpp.

References http_get_header(), and MAX_VAL_LEN.

◆ http_forwarded_client()

bool http_forwarded_client ( const HttpReq req,
char *  ip_out,
size_t  ip_cap,
bool *  is_https 
)

Recover the original client from a reverse-proxy Forwarded (RFC 7239) or de-facto X-Forwarded-For / X-Forwarded-Proto header.

Writes the leftmost (original-client) address into ip_out as its RFC 5952 canonical text (bounded by ip_cap; use DET_IP_STR_MAX for the widest IPv6), and sets is_https from proto=https / X-Forwarded-Proto: https. Both IPv4 (with an optional :port) and IPv6 (bracketed for="[2001:db8::1]:port" or a bare X-Forwarded-For literal) are recovered; the candidate is validated with det_ip_parse, so unknown / obfuscated _id identifiers and malformed tokens are rejected. The CALLER must only trust this when the TCP peer is a configured trusted upstream - the header is client-spoofable.

Returns
true if a valid client address (IPv4 or IPv6) was written to ip_out.

Definition at line 636 of file http_parser.cpp.

References http_get_header(), and MAX_VAL_LEN.

◆ http_get_query()

const char * http_get_query ( const HttpReq req,
const char *  key 
)

Look up a query parameter value by name (case-sensitive).

Parameters
reqParsed request.
keyParameter name.
Returns
Pointer to the null-terminated value (empty string if key= with no value), or nullptr if the key is absent.

Definition at line 693 of file http_parser.cpp.

References QueryParam::key, HttpReq::query_count, HttpReq::query_params, and QueryParam::val.

◆ http_get_form()

bool http_get_form ( const HttpReq req,
const char *  key,
char *  out,
size_t  out_size 
)

Look up an application/x-www-form-urlencoded body field by name.

Parses the request body on demand (no extra per-request storage) when the Content-Type is application/x-www-form-urlencoded, and copies the raw (not percent-decoded, matching http_get_query()) value of key into out. Useful for classic HTML form POSTs.

Parameters
reqParsed request (body must be buffered, i.e. not streamed).
keyField name (case-sensitive).
outCaller buffer; always null-terminated on a true return.
out_sizeSize of out in bytes (must be >= 1).
Returns
true and fills out if the field is present; false otherwise (out is set to an empty string).

Definition at line 703 of file http_parser.cpp.

References HttpReq::body, HttpReq::body_len, and http_get_header().

◆ http_get_param()

const char * http_get_param ( const HttpReq req,
const char *  key 
)

Look up a captured path parameter by name (case-sensitive).

Path parameters are the :name segments of a matched route pattern (e.g. route "/users/:id" matching "/users/42" captures id"42"). Populated by the dispatcher when the route matches; valid for the duration of the handler.

Parameters
reqParsed request.
keyParameter name without the leading :.
Returns
Pointer to the null-terminated value, or nullptr if absent.

Definition at line 753 of file http_parser.cpp.

References QueryParam::key, HttpReq::path_param_count, HttpReq::path_params, and QueryParam::val.

Variable Documentation

◆ http_pool

Pool of parser contexts, one per connection-pool slot (incl. reserved dispatch slots).

Definition at line 16 of file http_parser.cpp.

Referenced by http_parse(), DetWebServer::http_poll_slot(), http_reset(), req_is_head(), and DetWebServer::send_chunked().