Skip to content

File syn_http.h

FileList > net > syn_http.h

Go to the source code of this file

Cooperative HTTP/1.1 client — streaming, zero-alloc, non-blocking. 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_HttpClient
HTTP client context structure.
struct SYN_HttpHeader
Key-value pair representing an HTTP header.
struct SYN_HttpResponse
HTTP response metadata.

Public Types

Type Name
typedef bool(* SYN_HttpBodyCallback
Callback invoked to stream chunks of the HTTP response body.
enum SYN_HttpState
States for the HTTP client cooperative state machine.

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.

Detailed Description

Performs HTTP GET and POST requests over a TCP socket as a cooperative protothread task. Response bodies are delivered via streaming callback.

Public Types Documentation

typedef SYN_HttpBodyCallback

Callback invoked to stream chunks of the HTTP response body.

typedef bool(* SYN_HttpBodyCallback) (const uint8_t *data, size_t len, void *ctx);

Parameters:

  • data Pointer to the received chunk of body data.
  • len Length of the data chunk in bytes.
  • ctx User-defined context pointer passed to syn_http_client_init().

Returns:

true to continue reading body, false to abort.


enum SYN_HttpState

States for the HTTP client cooperative state machine.

enum SYN_HttpState {
    SYN_HTTP_STATE_IDLE,
    SYN_HTTP_STATE_CONNECTING,
    SYN_HTTP_STATE_SENDING_REQUEST,
    SYN_HTTP_STATE_READING_HEADERS,
    SYN_HTTP_STATE_READING_BODY,
    SYN_HTTP_STATE_DONE,
    SYN_HTTP_STATE_ERROR
};


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.



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