ProtoCore v0.0.1
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
protocore.cpp File Reference

Layer 7 (Application) - HTTP routing and request handler implementation. More...

Go to the source code of this file.

Classes

struct  InstanceCtx
 

Functions

const char * status_text (int code)
 Convert an HTTP status code to its standard reason phrase.
 
void fill_route_base (Route *r, const char *path)
 Register a route in the route table.
 
bool req_is_head (uint8_t slot_id)
 True if the request in slot slot_id used the HEAD method (send headers, no body).
 

Variables

SendCtx s_send
 
const char PC_RESP_HDR_OVERFLOW []
 The fixed reply sent when a response's own headers will not fit RESP_HDR_BUF_SIZE.
 
const size_t PC_RESP_HDR_OVERFLOW_LEN = sizeof(PC_RESP_HDR_OVERFLOW) - 1
 Length of PC_RESP_HDR_OVERFLOW, taken with sizeof where the array bound is still visible.
 

Detailed Description

Layer 7 (Application) - HTTP routing and request handler implementation.

Dispatch pipeline (called from PC::handle())

handle()
└─ server_tick() ← drain FreeRTOS event queue
└─ for each slot:
ParseState::PARSE_COMPLETE → match_and_execute()
ParseState::PARSE_ERROR → send(400)
ParseState
States of the HTTP/1.1 request parser.
Definition http_parser.h:53
@ PARSE_ERROR
Unrecoverable parse failure → 400.
@ PARSE_ENTITY_TOO_LARGE
Content-Length > BODY_BUF_SIZE → 413.
@ PARSE_COMPLETE
Full request parsed; ready for dispatch.
@ PARSE_URI_TOO_LONG
Path exceeds MAX_PATH_LEN → 414.
void server_tick(int worker_id)
Drive the session layer for one Arduino loop iteration.
Definition session.cpp:103

Route table Routes are stored in a fixed-size array of Route structs. Both exact and wildcard (suffix *) routes are supported; exact routes always take priority because the loop checks them in insertion order and returns on the first match.

PCB lifecycle / teardown ownership All TCP I/O and teardown go through the transport-layer connection API (pc_conn_send / pc_conn_flush / pc_conn_begin_close / pc_conn_close / pc_conn_abort_slot), so this layer never calls lwIP or touches the raw tcp_pcb directly. The transport owns the teardown order for every close: it detaches the pcb from its lwIP callbacks and sets the slot ConnState::CONN_FREE (pcb nulled) BEFORE the FIN/RST, on the captured pcb pointer. This means any lwIP error callback that fires mid-teardown sees the slot as already free and takes no action - preventing a double-free. L7 passes only the slot index: pc_conn_close(slot) for a graceful local close, pc_conn_abort_slot(slot) for a hard RST, pc_conn_begin_close(slot) for the drain-then-close dwell.

Definition in file protocore.cpp.

Function Documentation

◆ status_text()

const char * status_text ( int  code)

Convert an HTTP status code to its standard reason phrase.

Reason phrase for an HTTP status code (e.g. 404 -> "Not Found").

Covers the 18 codes that arise in typical REST micro-server usage. Unknown codes produce "Unknown" so callers never receive a null pointer.

Parameters
codeHTTP status integer.
Returns
Pointer to a string-literal reason phrase; never null.

Definition at line 119 of file protocore.cpp.

Referenced by PC::redirect(), PC::send(), PC::send_chunked(), PC::send_empty(), and PC::send_template().

◆ fill_route_base()

void fill_route_base ( Route r,
const char *  path 
)

Register a route in the route table.

Initialize the common fields (path, flags) of a route-table entry from its pattern.

Paths are stored null-terminated and truncated to MAX_PATH_LEN. The trailing character of the stored path is inspected to detect wildcard routes: any path ending in * is treated as a prefix match.

Registrations beyond MAX_ROUTES are silently ignored - callers should verify return values if overflow is a concern.

Parameters
pathURL path to match, e.g. "/api/*".
methodHTTP method that triggers this route.
callbackHandler invoked with (slot_id, request).

Definition at line 886 of file protocore.cpp.

References Route::iface_filter, Route::is_active, Route::is_param, Route::is_regex, Route::is_wildcard, MAX_PATH_LEN, Route::path, and PC_IFACE_ANY.

Referenced by PC::on(), PC::on(), and PC::on_regex().

◆ req_is_head()

bool req_is_head ( uint8_t  slot_id)

True if the request in slot slot_id used the HEAD method (send headers, no body).

Definition at line 1463 of file protocore.cpp.

References http_pool.

Referenced by PC::send(), PC::send_chunked(), and PC::send_template().

Variable Documentation

◆ s_send

SendCtx s_send

Definition at line 104 of file protocore.cpp.

Referenced by PC::http_poll_slot(), and PC::send_chunked().

◆ PC_RESP_HDR_OVERFLOW

const char PC_RESP_HDR_OVERFLOW[]
Initial value:
= "HTTP/1.1 500 Internal Server Error\r\n"
"Content-Length: 0\r\n"
"Connection: close\r\n\r\n"

The fixed reply sent when a response's own headers will not fit RESP_HDR_BUF_SIZE.

A header block cut short has no terminating CRLF, so the peer keeps reading the body as headers and the connection desynchronizes - which is why truncating is not an option and this always- fitting reply goes out instead. Connection: close, because the request is not recoverable.

Definition at line 440 of file protocore.cpp.

Referenced by PC::send().

◆ PC_RESP_HDR_OVERFLOW_LEN

const size_t PC_RESP_HDR_OVERFLOW_LEN = sizeof(PC_RESP_HDR_OVERFLOW) - 1

Length of PC_RESP_HDR_OVERFLOW, taken with sizeof where the array bound is still visible.

Definition at line 446 of file protocore.cpp.

Referenced by PC::send().