pkg:http/client

HTTP/1.1 client over raw TCP sockets.

import "pkg:http/client"

Overview

Provides HTTP GET and POST over TCP. Supports hostnames (resolved via DNS), IP addresses, and "localhost". Built on vera:net for sockets, vera:net/dns for hostname resolution, vera:memory for buffer operations, and vera:string for byte scanning and parsing.

Dependencies: vera:net, vera:net/dns, vera:memory, vera:string

Functions

http_get

function http_get(host: ptr, host_len: i32, port: i32, path: ptr, path_len: i64, resp_buf: ptr, resp_buf_size: i64) -> i64

Perform an HTTP/1.1 GET request.

Parameters:

  • host (ptr) — pointer to null-terminated host string (IP or hostname, e.g., "example.com\0")
  • host_len (i32) — length of the host string, including the null terminator
  • port (i32) — TCP port (usually 80)
  • path (ptr) — pointer to path string (e.g., "/index.html")
  • path_len (i64) — length of the path string
  • resp_buf (ptr) — caller-allocated buffer for the raw HTTP response
  • resp_buf_size (i64) — size of resp_buf in bytes

Returns: total bytes written to resp_buf, or -1 on error.


http_post

function http_post(host: ptr, host_len: i32, port: i32, path: ptr, path_len: i64, body: ptr, body_len: i64, content_type: ptr, ct_len: i64, resp_buf: ptr, resp_buf_size: i64) -> i64

Perform an HTTP/1.1 POST request.

Parameters:

  • host (ptr) — pointer to null-terminated host string (IP, hostname, or "localhost")
  • host_len (i32) — length of the host string, including the null terminator
  • port (i32) — TCP port (usually 80)
  • path (ptr) — pointer to request path string
  • path_len (i64) — length of the path string
  • body (ptr) — pointer to request body bytes
  • body_len (i64) — length of the body in bytes
  • content_type (ptr) — pointer to Content-Type header value (e.g., "application/json")
  • ct_len (i64) — length of the content type string
  • resp_buf (ptr) — caller-allocated buffer for the raw HTTP response
  • resp_buf_size (i64) — size of resp_buf in bytes

Returns: total bytes written to resp_buf, or -1 on error.


http_parse_status

function http_parse_status(resp: ptr, resp_len: i64) -> i32

Extract the HTTP status code from a raw response.

Expects the response to start with "HTTP/1.x NNN ...". Parses the 3-digit status code starting at byte offset 9.

Parameters:

  • resp (ptr) — pointer to the raw HTTP response buffer
  • resp_len (i64) — length of the response in bytes

Returns: the 3-digit HTTP status code (e.g., 200), or -1 on parse error.


http_find_body

function http_find_body(resp: ptr, resp_len: i64) -> i64

Find the byte offset where the response body begins.

Searches for the \r\n\r\n header/body separator and returns the offset immediately after it.

Parameters:

  • resp (ptr) — pointer to the raw HTTP response buffer
  • resp_len (i64) — length of the response in bytes

Returns: byte offset where the body starts, or -1 if the separator was not found.


http_get_content_length

function http_get_content_length(resp: ptr, resp_len: i64) -> i64

Parse the Content-Length header value from a raw HTTP response.

Searches for the "Content-Length: " header and parses the decimal integer that follows it.

Parameters:

  • resp (ptr) — pointer to the raw HTTP response buffer
  • resp_len (i64) — length of the response in bytes

Returns: the content length as an integer, or -1 if the header was not found.

Example

import "pkg:http/client"
import "vera:memory"

data host: "example.com\0"
data path: "/"

; In a function:
call buf, allocate, buf_size
call resp_len, http_get, p_host, host_len, port, p_path, path_len, buf, buf_size
call status, http_parse_status, buf, resp_len
call body_off, http_find_body, buf, resp_len