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

Standalone HTTP/1.1 request parser — no transport dependency. More...

#include "DetWebServerConfig.h"
#include <Arduino.h>

Go to the source code of this file.

Classes

struct  Header
 A single HTTP header field (key: value). More...
 
struct  QueryParam
 A single parsed query-string parameter. More...
 
struct  HttpReq
 Fully-parsed HTTP/1.1 request. More...
 

Enumerations

enum  ParseState {
  PARSE_METHOD , PARSE_PATH , PARSE_QUERY , PARSE_VERSION ,
  PARSE_HEADER_KEY , PARSE_HEADER_VAL , PARSE_EXPECT_LF , PARSE_EXPECT_BODY_LF ,
  PARSE_BODY , PARSE_COMPLETE , PARSE_ERROR , PARSE_ENTITY_TOO_LARGE ,
  PARSE_URI_TOO_LONG
}
 States of the HTTP/1.1 request parser. More...
 
enum  HttpVersion { HTTP_UNKNOWN = 0 , HTTP_10 , HTTP_11 }
 Parsed HTTP protocol version. More...
 

Functions

void http_parser_reset (HttpReq *req)
 Reset a parser context to the initial (PARSE_METHOD) state.
 
void http_parser_feed (HttpReq *req, 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).
 
const char * http_get_query (const HttpReq *req, const char *key)
 Look up a query parameter value by name (case-sensitive).
 

Variables

HttpReq http_pool [MAX_CONNS]
 Pool of parser contexts, one per transport slot.
 

Detailed Description

Standalone HTTP/1.1 request parser — no transport dependency.

The parser is a pure byte-stream state machine. It has no knowledge of ring buffers, TCP PCBs, or FreeRTOS. Feed it bytes one at a time via http_parser_feed() and inspect HttpReq::parse_state to know when the request is ready.

State machine

PARSE_METHOD ──space──────► PARSE_PATH
PARSE_PATH ──space──────► PARSE_VERSION
PARSE_PATH ──'?'────────► PARSE_QUERY
PARSE_QUERY ──space──────► PARSE_VERSION (calls parse_query_params)
PARSE_VERSION ──CR─────────► PARSE_EXPECT_LF
PARSE_EXPECT_LF ──LF─────────► PARSE_HEADER_KEY
PARSE_HEADER_KEY ──':'────────► PARSE_HEADER_VAL
PARSE_HEADER_KEY ──CR─────────► PARSE_EXPECT_BODY_LF (blank line)
PARSE_HEADER_VAL ──CR─────────► PARSE_EXPECT_LF (stores header)
PARSE_EXPECT_BODY_LF ──LF (CL=0)──► PARSE_COMPLETE
PARSE_EXPECT_BODY_LF ──LF (CL>BUF)► PARSE_ENTITY_TOO_LARGE (→ 413)
PARSE_EXPECT_BODY_LF ──LF (else)──► PARSE_BODY
PARSE_BODY ──(all read)──► PARSE_COMPLETE
PARSE_PATH (overflow) ───────────► PARSE_URI_TOO_LONG (→ 414)
Any state + protocol error ──────► PARSE_ERROR (→ 400)
@ PARSE_BODY
Reading the request body.
Definition http_parser.h:62
@ PARSE_HEADER_VAL
Reading a header field value.
Definition http_parser.h:59
@ PARSE_QUERY
Reading the raw query string (after ?).
Definition http_parser.h:56
@ PARSE_COMPLETE
Full request parsed; ready for dispatch.
Definition http_parser.h:63
@ PARSE_HEADER_KEY
Reading a header field name.
Definition http_parser.h:58
@ PARSE_EXPECT_BODY_LF
Consuming the LF of the blank-line CRLF.
Definition http_parser.h:61
@ PARSE_VERSION
Accumulating HTTP/1.x — hashed for validation.
Definition http_parser.h:57
@ PARSE_URI_TOO_LONG
Path exceeds MAX_PATH_LEN → 414.
Definition http_parser.h:66
@ PARSE_EXPECT_LF
Consuming the LF of a header-line CRLF pair.
Definition http_parser.h:60
@ PARSE_ENTITY_TOO_LARGE
Content-Length > BODY_BUF_SIZE → 413.
Definition http_parser.h:65
@ PARSE_PATH
Reading the URL path component.
Definition http_parser.h:55
@ PARSE_ERROR
Unrecoverable parse failure → 400.
Definition http_parser.h:64
@ PARSE_METHOD
Reading the HTTP method (GET, POST, …).
Definition http_parser.h:54
Author
Douglas Quigg (dstroy0)
Date
2026

Definition in file http_parser.h.

Enumeration Type Documentation

◆ ParseState

enum ParseState

States of the HTTP/1.1 request parser.

Advance via http_parser_feed(). The application layer inspects this after each feed call or after draining a complete chunk.

Enumerator
PARSE_METHOD 

Reading the HTTP method (GET, POST, …).

PARSE_PATH 

Reading the URL path component.

PARSE_QUERY 

Reading the raw query string (after ?).

PARSE_VERSION 

Accumulating HTTP/1.x — hashed for validation.

PARSE_HEADER_KEY 

Reading a header field name.

PARSE_HEADER_VAL 

Reading a header field value.

PARSE_EXPECT_LF 

Consuming the LF of a header-line CRLF pair.

PARSE_EXPECT_BODY_LF 

Consuming the LF of the blank-line CRLF.

PARSE_BODY 

Reading the request body.

PARSE_COMPLETE 

Full request parsed; ready for dispatch.

PARSE_ERROR 

Unrecoverable parse failure → 400.

PARSE_ENTITY_TOO_LARGE 

Content-Length > BODY_BUF_SIZE → 413.

PARSE_URI_TOO_LONG 

Path exceeds MAX_PATH_LEN → 414.

Definition at line 52 of file http_parser.h.

◆ HttpVersion

Parsed HTTP protocol version.

Populated from the request line (HTTP/1.0 or HTTP/1.1) using an FNV-1a hash accumulated during PARSE_VERSION. The application layer may use this to drive keep-alive semantics: HTTP/1.1 defaults to persistent connections; HTTP/1.0 defaults to close.

Enumerator
HTTP_UNKNOWN 

Version string did not match any known token.

HTTP_10 

HTTP/1.0 — close semantics by default.

HTTP_11 

HTTP/1.1 — persistent connection by default.

Definition at line 77 of file http_parser.h.

Function Documentation

◆ http_parser_reset()

void http_parser_reset ( HttpReq req)

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

Zeroes all fields and sets parse_state = 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 150 of file http_parser.cpp.

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

Referenced by http_reset().

◆ http_parser_feed()

void http_parser_feed ( HttpReq req,
uint8_t  byte 
)

Feed one byte to the parser state machine.

Returns immediately without modifying state when parse_state is already PARSE_COMPLETE, PARSE_ERROR, PARSE_ENTITY_TOO_LARGE, or PARSE_URI_TOO_LONG.

Parameters
reqParser context for this request.
byteNext byte from the HTTP stream.

Definition at line 159 of file http_parser.cpp.

References HttpReq::_version_hash, HttpReq::body, BODY_BUF_SIZE, HttpReq::body_bytes_read, HttpReq::body_len, HttpReq::content_length, HttpReq::current_token_idx, HttpReq::header_count, HttpReq::headers, HTTP_10, HTTP_11, HTTP_UNKNOWN, Header::key, MAX_HEADERS, MAX_KEY_LEN, MAX_PATH_LEN, MAX_QUERY_LEN, MAX_VAL_LEN, HttpReq::method, PARSE_BODY, PARSE_COMPLETE, PARSE_ENTITY_TOO_LARGE, PARSE_ERROR, PARSE_EXPECT_BODY_LF, PARSE_EXPECT_LF, PARSE_HEADER_KEY, PARSE_HEADER_VAL, PARSE_METHOD, PARSE_PATH, PARSE_QUERY, HttpReq::parse_state, PARSE_URI_TOO_LONG, PARSE_VERSION, HttpReq::path, HttpReq::path_idx, HttpReq::query, HttpReq::query_idx, Header::val, and HttpReq::version.

Referenced by http_parse().

◆ 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 389 of file http_parser.cpp.

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

Referenced by multipart_parse().

◆ 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 399 of file http_parser.cpp.

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

Variable Documentation

◆ http_pool

HttpReq http_pool[MAX_CONNS]
extern

Pool of parser contexts, one per transport slot.

Definition at line 15 of file http_parser.cpp.

Referenced by DetWebServer::handle(), http_parse(), and http_reset().