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:
clientPointer to the client context to initialize.methodHTTP method to perform (e.g. "GET" or "POST").hostDestination hostname or IP address string.portDestination TCP port (e.g. 80).pathResource URL path (e.g. "/api/v1/update").content_typeType of content if body is present (e.g. "application/json").bodyPointer to binary data to transmit as request body (or NULL).body_lenLength of the request body in bytes.headersOptional array of custom HTTP headers to append.header_countNumber of custom headers in the array.body_cbCallback function for streaming response chunks.cb_ctxUser context passed through to the body callback.work_bufWorking buffer for socket buffering and parsing.work_buf_sizeSize 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.
Yields while resolving, connecting, transmitting requests, and streaming the response body chunks. Must be run inside the cooperative scheduler.
Parameters:
ptPointer to the cooperative protothread structure.taskPointer 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:
urlLocation URL string.orig_hostOriginal request host.orig_portOriginal request port.host_out[out] Redirected host.host_szHost buffer size.path_out[out] Redirected path.path_szPath buffer size.port_out[out] Redirected port.
function parse_uint¶
Parse a decimal unsigned integer.
Parameters:
sInput string.
Returns:
Parsed value.
function prefix_icase¶
Case-insensitive prefix match.
Parameters:
strString to test.prefixPrefix 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:
sockSocket.methodHTTP method string.hostHost header value.pathRequest path.headersCustom headers array.header_countNumber of custom headers.content_typeContent-Type (or NULL).content_lengthBody length (0 for no body).
Returns:
true on success.
function sock_write_str¶
Write a null-terminated string to the socket.
Parameters:
sockSocket.strString to send.
Returns:
true if all bytes sent.
Macro Definition Documentation¶
define HTTP_RECV_TIMEOUT_MS¶
HTTP receive timeout (ms).
The documentation for this class was generated from the following file src/syntropic/net/syn_http.c