Skip to content

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.

void syn_httpd_body (
    SYN_HttpdResponse * resp,
    const void * data,
    size_t len
) 

Automatically finalizes headers on first call. Can be called multiple times for streaming responses.

Parameters:

  • resp HTTP response state to write to.
  • data Pointer to binary data to transmit.
  • len Length of data in bytes.

function syn_httpd_body_str

Send a string as the response body.

void syn_httpd_body_str (
    SYN_HttpdResponse * resp,
    const char * str
) 

Convenience wrapper for syn_httpd_body().

Parameters:

  • resp HTTP response state to write to.
  • str Null-terminated string to send.

function syn_httpd_header

Add a response header.

void syn_httpd_header (
    const SYN_HttpdResponse * resp,
    const char * name,
    const char * value
) 

Must be called after syn_httpd_status() and before syn_httpd_body().

Parameters:

  • resp HTTP response state to write to.
  • name Header key string (e.g. "Content-Type").
  • value Header 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:

  • srv Server instance to initialize.
  • port Port to listen on (typically 80).
  • routes Route table (must outlive the server).
  • route_count Number of routes.
  • work_buf Shared work buffer for request parsing + responses.
  • work_buf_size Buffer 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:

  • req Request info.
  • resp Response writer context.
  • buf Buffer to read into.
  • max_len Buffer 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.

void syn_httpd_status (
    const SYN_HttpdResponse * resp,
    int code,
    const char * reason
) 

Must be called first. E.g., syn_httpd_status(resp, 200, "OK").

Parameters:

  • resp HTTP response state to write to.
  • code HTTP numeric status code (e.g. 200).
  • reason Human readable status message (e.g. "OK").

function syn_httpd_step

Handle one incoming request (non-protothread version).

SYN_Status syn_httpd_step (
    SYN_Httpd * srv
) 

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:

  • srv Server 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.

void syn_httpd_stop (
    SYN_Httpd * srv
) 

Parameters:

  • srv Server instance to stop.

function syn_httpd_task

Protothread task function for the HTTP server.

SYN_PT_Status syn_httpd_task (
    SYN_PT * pt,
    SYN_Task * task
) 

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:

  • pt Pointer to the cooperative protothread context.
  • task Pointer 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.

static void dispatch_request (
    SYN_Httpd * srv
) 

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:

  • srv Server instance with complete headers in work_buf.

function drop_client

Drop the active client and reset to IDLE.

static void drop_client (
    SYN_Httpd * srv
) 

Parameters:

  • srv Server instance.

function finalize_headers

Flush the header block — sends the blank line after headers.

static void finalize_headers (
    SYN_HttpdResponse * resp
) 

Parameters:

  • resp Response context.

function match_route

Match a route against the request.

static const SYN_HttpdRoute * match_route (
    const SYN_Httpd * srv,
    const SYN_HttpdRequest * req
) 

Parameters:

  • srv Server instance.
  • req Parsed 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:

  • sock Client socket (stored in req for body reads).
  • req [out] Parsed request.
  • buf Work buffer containing the raw request.
  • total Number of valid bytes in buf.

Returns:

0 on success, -1 on parse error.


function parse_method

Parse the method string into enum.

static SYN_HttpMethod parse_method (
    const char * str,
    size_t len
) 

Parameters:

  • str Method string (e.g. "GET").
  • len Length of the method string.

Returns:

Corresponding SYN_HttpMethod.


function parse_uint

static inline uint32_t parse_uint (
    const char * s
) 

function prefix_icase

static inline bool prefix_icase (
    const char * str,
    const char * prefix
) 

function send_error

Send a simple error response.

static void send_error (
    SYN_Socket sock,
    int code,
    const char * reason
) 

Parameters:

  • sock Client socket.
  • code HTTP status code.
  • reason Reason phrase (e.g. "Not Found").

function sock_write

Write a null-terminated string to the socket.

static bool sock_write (
    SYN_Socket sock,
    const char * str
) 

Parameters:

  • sock Socket to write to.
  • str String to send.

Returns:

true if all bytes were sent.


Macro Definition Documentation

define HTTPD_CLIENT_TIMEOUT_MS

#define HTTPD_CLIENT_TIMEOUT_MS `5000`

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