std/net
TCP, TLS, DNS resolution, and HTTP client.
from "std/net" import { fetch, resolve, TcpStream, TlsStream, Response, NetError }Types
TcpStream
struct TcpStream {
fd: i32,
}An owned TCP socket. Closed when dropped.
TlsStream
struct TlsStream {
fd: i32,
ssl: i64,
ctx: i64,
}An owned TLS connection over TCP. Freed when dropped.
Response
struct Response {
status: i32,
headers: string,
body: string,
}HTTP response returned by fetch and friends.
Response.text
fn text(self): stringReturn the response body as a string.
Response.json
fn json(self): JsonParse the response body as JSON.
Response.ok
fn ok(self): boolTrue if status is 200-299.
Response.header
fn header(self, name: &string): stringGet a response header value by name.
FetchOptions
struct FetchOptions {
method: string,
headers: string,
body: string,
}Options for fetchWith. Set headers as "Key: Value\r\n" pairs.
NetError
enum NetError {
DnsFailure(string),
ConnectionFailed(string),
TlsError(string),
SendFailed(string),
Other(string),
}Functions
ip4
fn ip4(a: u8, b: u8, c: u8, d: u8): u32Construct an IPv4 address from octets.
resolve
fn resolve(hostname: &string): Result<u32, NetError>DNS lookup — resolve a hostname to an IPv4 address.
TcpStream.connect
fn TcpStream.connect(addr: u32, port: u16): Result<TcpStream, NetError>Open a TCP connection.
stream.send
fn send(self: &TcpStream, data: &string): Result<i64, NetError>Send data over a TCP connection. Returns bytes sent.
stream.recv
fn recv(self: &TcpStream): Result<string, NetError>Receive data from a TCP connection.
TlsStream.connect
fn TlsStream.connect(addr: u32, port: u16, hostname: &string): Result<TlsStream, NetError>Open a TLS connection. The hostname is used for SNI.
stream.send (TLS)
fn send(self: &TlsStream, data: &string): Result<i64, NetError>Send data over a TLS connection.
stream.recv (TLS)
fn recv(self: &TlsStream): Result<string, NetError>Receive data from a TLS connection.
fetch
fn fetch(url: &string): Result<Response, NetError>HTTP GET with automatic TLS and DNS resolution.
let resp = fetch("https://httpbin.org/get")!
writeStdout(&resp.body)fetchWith
fn fetchWith(url: &string, opts: FetchOptions): Result<Response, NetError>HTTP request with full control over method, headers, and body.
fetchPost
fn fetchPost(url: &string, body: &string): Result<Response, NetError>HTTP POST with a body.
fetchPut
fn fetchPut(url: &string, body: &string): Result<Response, NetError>HTTP PUT with a body.
fetchDelete
fn fetchDelete(url: &string): Result<Response, NetError>HTTP DELETE.
fetchPatch
fn fetchPatch(url: &string, body: &string): Result<Response, NetError>HTTP PATCH with a body.