File syn_httpd.c¶
FileList > net > syn_httpd.c
Go to the source code of this file
Minimal HTTP/1.1 server — fully non-blocking implementation. More...
#include "../port/syn_port_system.h"#include "../util/syn_assert.h"#include "syn_httpd.h"#include <string.h>#include "../util/syn_fmt.h"
Public Functions¶
| Type | Name |
|---|---|
| void | syn_httpd_body (SYN_HttpdResponse * resp, const void * data, size_t len) Send response body data. |
| void | syn_httpd_body_str (SYN_HttpdResponse * resp, const char * str) Send a string as the response body. |
| void | syn_httpd_header (const SYN_HttpdResponse * resp, const char * name, const char * value) Add a response header. |
| SYN_Status | syn_httpd_init (SYN_Httpd * srv, uint16_t port, const SYN_HttpdRoute * routes, size_t route_count, uint8_t * work_buf, size_t work_buf_size) Initialize and start the HTTP server. |
| int | syn_httpd_read_body (const SYN_HttpdRequest * req, const SYN_HttpdResponse * resp, void * buf, size_t max_len) Read request body data (for POST/PUT). |
| void | syn_httpd_status (const SYN_HttpdResponse * resp, int code, const char * reason) Begin the response with a status line. |
| SYN_Status | syn_httpd_step (SYN_Httpd * srv) Handle one incoming request (non-protothread version). |
| void | syn_httpd_stop (SYN_Httpd * srv) Stop the server and close the listener socket. |
| SYN_PT_Status | syn_httpd_task (SYN_PT * pt, SYN_Task * task) Protothread task function for the HTTP server. |
Public Static Functions¶
| Type | Name |
|---|---|
| void | dispatch_request (SYN_Httpd * srv) Handle the DISPATCHING phase — parse, route, respond, close. |
| void | drop_client (SYN_Httpd * srv) Drop the active client and reset to IDLE. |
| void | finalize_headers (SYN_HttpdResponse * resp) Flush the header block — sends the blank line after headers. |
| const SYN_HttpdRoute * | match_route (const SYN_Httpd * srv, const SYN_HttpdRequest * req) Match a route against the request. |
| int | parse_headers_from_buf (SYN_Socket sock, SYN_HttpdRequest * req, uint8_t * buf, size_t total) Parse request line + headers from already-buffered data. |
| SYN_HttpMethod | parse_method (const char * str, size_t len) Parse the method string into enum. |
| uint32_t | parse_uint (const char * s) |
| bool | prefix_icase (const char * str, const char * prefix) |
| void | send_error (SYN_Socket sock, int code, const char * reason) Send a simple error response. |
| bool | sock_write (SYN_Socket sock, const char * str) Write a null-terminated string to the socket. |
Macros¶
| Type | Name |
|---|---|
| define | HTTPD_CLIENT_TIMEOUT_MS 5000 |
Detailed Description¶
Designed around the cooperative scheduler: every call to syn_httpd_step() does a bounded amount of work and returns immediately. Socket I/O uses timeout=0 exclusively. Stale connections are timed out via the tick clock.
Public Functions Documentation¶
function syn_httpd_body¶
Send response body data.
Automatically finalizes headers on first call. Can be called multiple times for streaming responses.
Parameters:
respHTTP response state to write to.dataPointer to binary data to transmit.lenLength of data in bytes.
function syn_httpd_body_str¶
Send a string as the response body.
Convenience wrapper for syn_httpd_body().
Parameters:
respHTTP response state to write to.strNull-terminated string to send.
function syn_httpd_header¶
Add a response header.
Must be called after syn_httpd_status() and before syn_httpd_body().
Parameters:
respHTTP response state to write to.nameHeader key string (e.g. "Content-Type").valueHeader value string (e.g. "text/plain").
function syn_httpd_init¶
Initialize and start the HTTP server.
SYN_Status syn_httpd_init (
SYN_Httpd * srv,
uint16_t port,
const SYN_HttpdRoute * routes,
size_t route_count,
uint8_t * work_buf,
size_t work_buf_size
)
Creates a listening socket on the given port.
Parameters:
srvServer instance to initialize.portPort to listen on (typically 80).routesRoute table (must outlive the server).route_countNumber of routes.work_bufShared work buffer for request parsing + responses.work_buf_sizeBuffer size (512+ recommended).
Returns:
SYN_OK on success, SYN_ERROR if bind/listen fails.
function syn_httpd_read_body¶
Read request body data (for POST/PUT).
int syn_httpd_read_body (
const SYN_HttpdRequest * req,
const SYN_HttpdResponse * resp,
void * buf,
size_t max_len
)
Parameters:
reqRequest info.respResponse writer context.bufBuffer to read into.max_lenBuffer capacity in bytes.
Returns:
Bytes read, 0 at end of body, -1 on error.
function syn_httpd_status¶
Begin the response with a status line.
Must be called first. E.g., syn_httpd_status(resp, 200, "OK").
Parameters:
respHTTP response state to write to.codeHTTP numeric status code (e.g. 200).reasonHuman readable status message (e.g. "OK").
function syn_httpd_step¶
Handle one incoming request (non-protothread version).
Accepts one connection, parses the request, dispatches to the matching route handler, and closes the connection. Use this if you prefer manual control over the server loop.
Parameters:
srvServer instance.
Returns:
SYN_OK if a request was handled, SYN_TIMEOUT if no client connected, SYN_ERROR on parse failure.
function syn_httpd_stop¶
Stop the server and close the listener socket.
Parameters:
srvServer instance to stop.
function syn_httpd_task¶
Protothread task function for the HTTP server.
Register this as a SYN_TaskFunc with user_data pointing to a SYN_Httpd instance. The task accepts a connection, handles one request, then yields back to the scheduler.
Parameters:
ptPointer to the cooperative protothread context.taskPointer to the scheduler task structure.
Returns:
PT_WAITING or PT_EXITED status.
Public Static Functions Documentation¶
function dispatch_request¶
Handle the DISPATCHING phase — parse, route, respond, close.
Called once when headers are fully buffered. Runs synchronously but all data is already in work_buf — no blocking socket reads needed for headers.
Parameters:
srvServer instance with complete headers in work_buf.
function drop_client¶
Drop the active client and reset to IDLE.
Parameters:
srvServer instance.
function finalize_headers¶
Flush the header block — sends the blank line after headers.
Parameters:
respResponse context.
function match_route¶
Match a route against the request.
Parameters:
srvServer instance.reqParsed request.
Returns:
Matching route, or NULL.
function parse_headers_from_buf¶
Parse request line + headers from already-buffered data.
static int parse_headers_from_buf (
SYN_Socket sock,
SYN_HttpdRequest * req,
uint8_t * buf,
size_t total
)
This is a pure parsing function — no socket I/O. The caller guarantees that buf contains a complete header block ending with "\r\n\r\n".
Parameters:
sockClient socket (stored in req for body reads).req[out] Parsed request.bufWork buffer containing the raw request.totalNumber of valid bytes in buf.
Returns:
0 on success, -1 on parse error.
function parse_method¶
Parse the method string into enum.
Parameters:
strMethod string (e.g. "GET").lenLength of the method string.
Returns:
Corresponding SYN_HttpMethod.
function parse_uint¶
function prefix_icase¶
function send_error¶
Send a simple error response.
Parameters:
sockClient socket.codeHTTP status code.reasonReason phrase (e.g. "Not Found").
function sock_write¶
Write a null-terminated string to the socket.
Parameters:
sockSocket to write to.strString to send.
Returns:
true if all bytes were sent.
Macro Definition Documentation¶
define HTTPD_CLIENT_TIMEOUT_MS¶
Maximum time (ms) to wait for a client to send complete headers after connecting. Enforced non-blockingly via the tick clock — not passed to any socket recv call.
The documentation for this class was generated from the following file src/syntropic/net/syn_httpd.c