Module: net.http

Source: ./net/http.reef


Overview

net.http - HTTP/1.1 client with HTTPS support

Provides HTTP client functionality for making GET and POST requests. Supports both HTTP and HTTPS (TLS/SSL) connections with configurable timeouts. Automatically handles chunked transfer encoding (RFC 7230).

RECOMMENDED API (handles both text and binary safely):

  • http_get_auto(url) - auto-detects content type, returns HttpAutoResponse
  • http_auto_body_string(resp) - get body as text (for text content)
  • http_auto_body_bytes(resp) - get body as bytes (for binary content)
  • http_auto_is_binary(resp) - check if content is binary

For streaming large files to disk:

  • http_download_file(url, path) - stream directly to disk (memory efficient)

LEGACY TEXT-ONLY API (do not use for binary data - will truncate at null bytes):

  • http_get(url) - returns HttpResponse with string body
  • http_response_body(resp) - returns string (truncates binary data!)

Types

HttpUrl

HTTP URL components

Fields:

Name Type
scheme string
host string
port int
path string
query string
is_valid bool

HttpRequest

HTTP Request

Fields:

Name Type
method string
url HttpUrl
headers [string]
header_count int
body string

HttpResponse

HTTP Response NOTE: no error field -- a completed transaction (including 4xx/5xx) is Ok(HttpResponse); only a transport failure (connect/send/recv) is Err.

Fields:

Name Type
status_code int
status_text string
headers [string]
header_count int
body string

HttpBinaryResponse

HTTP Binary Response (for binary data that may contain null bytes) NOTE: no error field -- see HttpResponse.

Fields:

Name Type
status_code int
status_text string
headers [string]
header_count int
body_bytes [uint8]
body_length int

HttpAutoResponse

HTTP Auto Response (safe for both text and binary - recommended) Stores body as bytes internally, provides accessors for both formats NOTE: no error field -- see HttpResponse.

Fields:

Name Type
status_code int
status_text string
headers [string]
header_count int
body_bytes [uint8]
body_length int
content_type string
is_binary bool

HttpDownloadProgress

HTTP Download Progress (for progress-reporting downloads)

Fields:

Name Type
total_bytes int
downloaded_bytes int
elapsed_ms int64
speed_bps int
status string
error string

DownloadResultVerbose

Verbose download result with more info

Fields:

Name Type
success bool
is_redirect bool
location string
error string
bytes_received int
content_length int

DownloadResult

Download result with redirect info Internal-only. error/cancelled are NOT exposed as struct fields on any public type -- they exist purely so the public http_download_file / http_download_file_callback boundary can build a proper Result: error carries the failure reason into Err (fixes the old no-.error asymmetry vs. DownloadResultVerbose), cancelled distinguishes a user-cancelled callback download (-> Ok(false), not an error) from a transport failure (-> Err).

Fields:

Name Type
success bool
is_redirect bool
location string
cancelled bool
error string

ChunkedResult

Chunked decoding result type

Fields:

Name Type
data [uint8]
length int

Functions

fn reef_string_alloc(len: int): string

FFI declaration for string allocation

fn http_parse_url(url: string): HttpUrl

fn parse_int(s: string): int

Simple int parser

fn http_request_new(method: string, url: string): HttpRequest

fn http_get(url: string): res.Result[HttpResponse, error.Error]

Perform HTTP GET request Follows HTTP redirects automatically (up to 5 redirects) Ok(HttpResponse) on any completed transaction (status is DATA -- a 404/500 is still Ok); Err(error.Error) only on transport failure.

fn http_post(url: string, body: string): res.Result[HttpResponse, error.Error]

fn http_post_json(url: string, json: string): res.Result[HttpResponse, error.Error]

fn http_get_bytes(url: string): res.Result[HttpBinaryResponse, error.Error]

Download binary data into memory as a byte array Use this for files that may contain null bytes (images, executables, etc.) Follows HTTP redirects automatically (up to 5 redirects)

fn http_download_file(url: string, path: string): res.Result[bool, error.Error]

Download a file directly to disk (streaming, memory-efficient) Follows HTTP redirects automatically (up to 5 redirects) Ok(true) on success; Err(error.Error) on failure, carrying the reason.

fn http_download_progress(url: string, path: string): res.Result[HttpDownloadProgress, error.Error]

Download a file with verbose progress reporting Ok(final HttpDownloadProgress) on complete; Err(error.Error) on transport failure (the progress struct's own status/error fields still carry the same detail for callers who want to inspect it directly, but the Result is now the primary success/failure signal).

fn http_download_verbose_tcp(parsed_url: HttpUrl, request_str: string, path: string, progress: HttpDownloadProgress): DownloadResultVerbose

Internal: Verbose download over TCP

fn http_download_verbose_tls(parsed_url: HttpUrl, request_str: string, path: string, progress: HttpDownloadProgress): DownloadResultVerbose

Internal: Verbose download over TLS

fn create_progress(total: int, downloaded: int, start_ms: int64, status: string): HttpDownloadProgress

Helper: Create a progress struct with calculated speed

fn create_error_progress(error_msg: string): HttpDownloadProgress

Helper: Create an error progress struct

fn http_download_file_tls_callback(parsed_url: HttpUrl, request_str: string, path: string, callback: (HttpDownloadProgress) -> bool): DownloadResult

Internal: Download file over TLS with progress callback

fn http_download_file_tcp_callback(parsed_url: HttpUrl, request_str: string, path: string, callback: (HttpDownloadProgress) -> bool): DownloadResult

Internal: Download file over TCP with progress callback

fn http_download_file_callback(url: string, path: string, callback: (HttpDownloadProgress) -> bool): res.Result[bool, error.Error]

Download a file with progress callback The callback is invoked periodically with download progress updates Return false from callback to cancel the download Follows HTTP redirects automatically (up to 5 redirects) Ok(true) on success; Ok(false) if the callback cancelled the download (user-cancel is NOT an error); Err(error.Error) on transport failure.

fn get_content_length_from_bytes(data: [uint8], header_len: int): int

Helper: Get Content-Length from header bytes

fn http_send(req: HttpRequest): res.Result[HttpResponse, error.Error]

fn http_send_timeout(req: HttpRequest, timeout_ms: int): res.Result[HttpResponse, error.Error]

fn http_send_tcp(host: string, port: int, request_str: string, timeout_ms: int): res.Result[HttpResponse, error.Error]

Internal: Send HTTP request over plain TCP

fn http_send_tls(host: string, port: int, request_str: string): res.Result[HttpResponse, error.Error]

Internal: Send HTTP request over TLS (HTTPS)

fn http_send_binary(req: HttpRequest): res.Result[HttpBinaryResponse, error.Error]

Internal: Send request and get binary response

fn http_send_binary_tcp(host: string, port: int, request_str: string): res.Result[HttpBinaryResponse, error.Error]

Internal: Send binary request over plain TCP

fn http_send_binary_tls(host: string, port: int, request_str: string): res.Result[HttpBinaryResponse, error.Error]

Internal: Send binary request over TLS

fn http_download_file_tcp_internal(parsed_url: HttpUrl, request_str: string, path: string): DownloadResult

Internal: Download file over TCP (streaming to disk) Returns DownloadResult with redirect info if applicable

fn http_download_file_tls_internal(parsed_url: HttpUrl, request_str: string, path: string): DownloadResult

Internal: Download file over TLS (streaming to disk) Returns DownloadResult with redirect info if applicable

fn append_bytes(dest: [uint8], dest_len: int, src: [uint8], src_len: int): [uint8]

Append bytes to a byte array (returns new array)

fn find_header_end(data: [uint8], len: int): int

Find \r\n\r\n in byte array (returns position of first \r, or -1)

fn parse_status_from_bytes(data: [uint8], header_len: int): int

Parse HTTP status code from header bytes

fn is_chunked_from_bytes(data: [uint8], header_len: int): bool

Check if Transfer-Encoding: chunked from header bytes

fn matches_transfer_encoding(data: [uint8], pos: int, len: int): bool

Helper: check if bytes match "transfer-encoding:" (case insensitive)

fn is_redirect_status(status: int): bool

Check if status code is a redirect

fn get_location_from_bytes(data: [uint8], header_len: int): string

Extract Location header from response bytes (case-insensitive)

fn resolve_redirect_url(base: HttpUrl, location: string): string

Resolve redirect URL (handles relative and absolute URLs)

fn decode_chunked_bytes(data: [uint8], len: int): ChunkedResult

Decode chunked transfer encoding from byte array

fn parse_hex_from_bytes(data: [uint8], start: int, length: int): int

Parse hex number from byte array

fn parse_http_response_binary(data: [uint8], len: int): res.Result[HttpBinaryResponse, error.Error]

Parse HTTP binary response from raw bytes

fn bytes_to_string(data: [uint8], start: int, length: int): string

Convert bytes to string (using runtime allocation)

fn parse_hex_int(s: string): int

Parse a hex string to integer

fn decode_chunked(data: string): string

Decode chunked transfer encoding Format: chunk-size CRLF chunk-data CRLF ... 0 CRLF CRLF

fn is_chunked_transfer(headers: [string], header_count: int): bool

Check if response uses chunked transfer encoding

fn parse_http_response(data: string): res.Result[HttpResponse, error.Error]

fn http_response_status(resp: HttpResponse): int

fn http_response_body(resp: HttpResponse): string

fn http_response_header(resp: HttpResponse, name: string): string

fn http_response_is_ok(resp: HttpResponse): bool

fn http_binary_response_status(resp: HttpBinaryResponse): int

fn http_binary_response_body(resp: HttpBinaryResponse): [uint8]

fn http_binary_response_length(resp: HttpBinaryResponse): int

fn http_binary_response_header(resp: HttpBinaryResponse, name: string): string

fn http_binary_response_is_ok(resp: HttpBinaryResponse): bool

fn http_get_auto(url: string): res.Result[HttpAutoResponse, error.Error]

Perform HTTP GET with automatic content-type detection Stores body as bytes internally, safe for both text and binary Follows HTTP redirects automatically (up to 5 redirects)

fn http_get_auto_tcp(host: string, port: int, request_str: string): res.Result[HttpAutoResponse, error.Error]

Internal: Auto HTTP over TCP

fn http_get_auto_tls(host: string, port: int, request_str: string): res.Result[HttpAutoResponse, error.Error]

Internal: Auto HTTP over TLS

fn parse_http_auto_response(data: [uint8], len: int): res.Result[HttpAutoResponse, error.Error]

Parse HTTP auto response from raw bytes

fn is_binary_content_type(content_type: string): bool

Check if content-type indicates binary data

fn http_auto_status(resp: HttpAutoResponse): int

fn http_auto_body_string(resp: HttpAutoResponse): string

Get body as string (safe for text content) Warning: For binary content, this may truncate at null bytes

fn http_auto_body_bytes(resp: HttpAutoResponse): [uint8]

Get body as bytes (safe for all content)

fn http_auto_body_length(resp: HttpAutoResponse): int

fn http_auto_header(resp: HttpAutoResponse, name: string): string

fn http_auto_content_type(resp: HttpAutoResponse): string

fn http_auto_is_binary(resp: HttpAutoResponse): bool

fn http_auto_is_ok(resp: HttpAutoResponse): bool


Procedures

proc http_request_set_header(req: HttpRequest, name: string, value: string)

proc http_request_set_body(req: HttpRequest, body: string)

proc copy_bytes(dest: [uint8], dest_offset: int, src: [uint8], src_offset: int, length: int)

Copy bytes from source to destination


Generated by reefc doc