File syn_httpd.h¶
FileList > net > syn_httpd.h
Go to the source code of this file
Minimal HTTP/1.1 server — route-based, zero-alloc. More...
#include "../common/syn_defs.h"#include "../port/syn_port_socket.h"#include "../pt/syn_pt.h"#include "../sched/syn_task.h"#include <stdbool.h>#include <stddef.h>#include <stdint.h>
Classes¶
| Type | Name |
|---|---|
| struct | SYN_Httpd HTTP server context structure. |
| struct | SYN_HttpdRequest Parsed HTTP request container presented to route handlers. |
| struct | SYN_HttpdResponse HTTP response formatting state. |
| struct | SYN_HttpdRoute An HTTP routing entry configuration. |
Public Types¶
| Type | Name |
|---|---|
| enum | SYN_HttpMethod Supported HTTP request methods. |
| typedef void(* | SYN_HttpdHandler Route handler function. |
| enum | SYN_HttpdState Processing phase for the non-blocking request state machine. |
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. |
Detailed Description¶
Designed to run as a cooperative protothread task within the SyntropicOS scheduler. Yields between accepts so other tasks can run. Handles one request per scheduler tick.
** **
static void handle_status(const SYN_HttpdRequest *req,
SYN_HttpdResponse *resp, void *ctx) {
syn_httpd_status(resp, 200, "OK");
syn_httpd_header(resp, "Content-Type", "application/json");
syn_httpd_body_str(resp, "{\"status\":\"ok\"}");
}
static const SYN_HttpdRoute routes[] = {
{ SYN_HTTP_GET, "/api/status", handle_status, NULL },
};
// In your task array:
static uint8_t httpd_buf[1024];
static SYN_Httpd httpd;
syn_httpd_init(&httpd, 80, routes, 1, httpd_buf, sizeof(httpd_buf));
syn_task_create(&tasks[N], "httpd", syn_httpd_task, 2, &httpd);
Public Types Documentation¶
enum SYN_HttpMethod¶
Supported HTTP request methods.
typedef SYN_HttpdHandler¶
Route handler function.
Called when a request matches the route. The handler sends the response using syn_httpd_status(), syn_httpd_header(), syn_httpd_body().
enum SYN_HttpdState¶
Processing phase for the non-blocking request state machine.
syn_httpd_step() advances through these phases without blocking: IDLE → READING_HEADERS → DISPATCHING → IDLE
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.
The documentation for this class was generated from the following file src/syntropic/net/syn_httpd.h