ProtoCore v0.0.1
Deterministic, zero-heap network stack for embedded targets
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.

Macros

#define PC_FNV_OFFSET   2166136261u
 
#define PC_FNV_PRIME   16777619u
 
#define PC_HASH_HTTP10   (fnv1a("HTTP/1.0"))
 
#define PC_HASH_HTTP11   (fnv1a("HTTP/1.1"))
 
#define PC_CC_TCHAR   0x01
 
#define PC_CC_VCHAR   0x02
 
#define PC_CC_FIELD_VALUE   0x04
 

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.

Macro Definition Documentation

◆ PC_FNV_OFFSET

#define PC_FNV_OFFSET   2166136261u

Definition at line 46 of file http_parser.cpp.

◆ PC_FNV_PRIME

#define PC_FNV_PRIME   16777619u

Definition at line 47 of file http_parser.cpp.

◆ PC_HASH_HTTP10

#define PC_HASH_HTTP10   (fnv1a("HTTP/1.0"))

Definition at line 54 of file http_parser.cpp.

◆ PC_HASH_HTTP11

#define PC_HASH_HTTP11   (fnv1a("HTTP/1.1"))

Definition at line 55 of file http_parser.cpp.

◆ PC_CC_TCHAR

#define PC_CC_TCHAR   0x01

Definition at line 69 of file http_parser.cpp.

◆ PC_CC_VCHAR

#define PC_CC_VCHAR   0x02

Definition at line 70 of file http_parser.cpp.

◆ PC_CC_FIELD_VALUE

#define PC_CC_FIELD_VALUE   0x04

Definition at line 71 of 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 156 of file http_parser.cpp.

References HttpReq::_version_hash, PARSE_COMPLETE, PARSE_METHOD, HttpReq::parse_state, PC_FNV_OFFSET, 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 548 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 pc_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 560 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 PC_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 pc_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 727 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 801 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 813 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 883 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(), PC::http_poll_slot(), http_reset(), req_is_head(), and PC::send_chunked().