Skip to content

File syn_http.c

FileList > net > syn_http.c

Go to the source code of this file

Cooperative HTTP/1.1 client implementation.

  • #include "../port/syn_port_system.h"
  • #include "../util/syn_assert.h"
  • #include "../util/syn_fmt.h"
  • #include "syn_http.h"
  • #include <stdlib.h>
  • #include <string.h>

Public Functions

Type Name
SYN_Status syn_http_client_init (SYN_HttpClient * client, const char * method, const char * host, uint16_t port, const char * path, const char * content_type, const uint8_t * body, size_t body_len, const SYN_HttpHeader * headers, uint8_t header_count, SYN_HttpBodyCallback body_cb, void * cb_ctx, uint8_t * work_buf, size_t work_buf_size)
Initialize the HTTP client struct.
SYN_PT_Status syn_http_client_task (SYN_PT * pt, SYN_Task * task)
Cooperative task to drive the HTTP client.

Public Static Functions

Type Name
void parse_redirect_url (const char * url, const char * orig_host, uint16_t orig_port, char * host_out, size_t host_sz, char * path_out, size_t path_sz, uint16_t * port_out)
Parse a redirect Location header URL.
uint32_t parse_uint (const char * s)
Parse a decimal unsigned integer.
bool prefix_icase (const char * str, const char * prefix)
Case-insensitive prefix match.
bool send_request (SYN_Socket sock, const char * method, const char * host, const char * path, const SYN_HttpHeader * headers, uint8_t header_count, const char * content_type, size_t content_length)
Build and send an HTTP request line + headers.
bool sock_write_str (SYN_Socket sock, const char * str)
Write a null-terminated string to the socket.

Macros

Type Name
define HTTP_RECV_TIMEOUT_MS 10000

Public Functions Documentation

function syn_http_client_init

Initialize the HTTP client struct.

SYN_Status syn_http_client_init (
    SYN_HttpClient * client,
    const char * method,
    const char * host,
    uint16_t port,
    const char * path,
    const char * content_type,
    const uint8_t * body,
    size_t body_len,
    const SYN_HttpHeader * headers,
    uint8_t header_count,
    SYN_HttpBodyCallback body_cb,
    void * cb_ctx,
    uint8_t * work_buf,
    size_t work_buf_size
) 

Configures the request metadata, target destination, custom headers, streaming callback, and temporary work buffer.

Parameters:

  • client Pointer to the client context to initialize.
  • method HTTP method to perform (e.g. "GET" or "POST").
  • host Destination hostname or IP address string.
  • port Destination TCP port (e.g. 80).
  • path Resource URL path (e.g. "/api/v1/update").
  • content_type Type of content if body is present (e.g. "application/json").
  • body Pointer to binary data to transmit as request body (or NULL).
  • body_len Length of the request body in bytes.
  • headers Optional array of custom HTTP headers to append.
  • header_count Number of custom headers in the array.
  • body_cb Callback function for streaming response chunks.
  • cb_ctx User context passed through to the body callback.
  • work_buf Working buffer for socket buffering and parsing.
  • work_buf_size Size of the working buffer in bytes.

Returns:

SYN_OK on success, or an error code on invalid parameters.


function syn_http_client_task

Cooperative task to drive the HTTP client.

SYN_PT_Status syn_http_client_task (
    SYN_PT * pt,
    SYN_Task * task
) 

Yields while resolving, connecting, transmitting requests, and streaming the response body chunks. Must be run inside the cooperative scheduler.

Parameters:

  • pt Pointer to the cooperative protothread structure.
  • task Pointer to the corresponding task control block.

Returns:

PT_WAITING, PT_EXITED, or another protothread status code.


Public Static Functions Documentation

function parse_redirect_url

Parse a redirect Location header URL.

static void parse_redirect_url (
    const char * url,
    const char * orig_host,
    uint16_t orig_port,
    char * host_out,
    size_t host_sz,
    char * path_out,
    size_t path_sz,
    uint16_t * port_out
) 

Parameters:

  • url Location URL string.
  • orig_host Original request host.
  • orig_port Original request port.
  • host_out [out] Redirected host.
  • host_sz Host buffer size.
  • path_out [out] Redirected path.
  • path_sz Path buffer size.
  • port_out [out] Redirected port.

function parse_uint

Parse a decimal unsigned integer.

static uint32_t parse_uint (
    const char * s
) 

Parameters:

  • s Input string.

Returns:

Parsed value.


function prefix_icase

Case-insensitive prefix match.

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

Parameters:

  • str String to test.
  • prefix Prefix to match.

Returns:

true if match.


function send_request

Build and send an HTTP request line + headers.

static bool send_request (
    SYN_Socket sock,
    const char * method,
    const char * host,
    const char * path,
    const SYN_HttpHeader * headers,
    uint8_t header_count,
    const char * content_type,
    size_t content_length
) 

Parameters:

  • sock Socket.
  • method HTTP method string.
  • host Host header value.
  • path Request path.
  • headers Custom headers array.
  • header_count Number of custom headers.
  • content_type Content-Type (or NULL).
  • content_length Body length (0 for no body).

Returns:

true on success.


function sock_write_str

Write a null-terminated string to the socket.

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

Parameters:

  • sock Socket.
  • str String to send.

Returns:

true if all bytes sent.


Macro Definition Documentation

define HTTP_RECV_TIMEOUT_MS

#define HTTP_RECV_TIMEOUT_MS `10000`

HTTP receive timeout (ms).



The documentation for this class was generated from the following file src/syntropic/net/syn_http.c