std/net
TCP and DNS resolution. The TLS socket (TlsStream) and the HTTP client (fetch, FetchResponse, FetchOptions) live in std/fetch — kept out of std/net so a plain-TCP program links without OpenSSL.
from "std/net" import { resolve, TcpStream, TcpListener, NetError }
from "std/fetch" import { fetch, TlsStream, FetchResponse }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.
FetchResponse
struct FetchResponse {
status: i32,
headers: string,
body: string,
}HTTP response returned by the fetch functions.
FetchResponse.text
fn FetchResponse.text(self: &FetchResponse): stringReturn the response body as a string.
FetchResponse.json
fn FetchResponse.json(self: &FetchResponse): JsonParse the response body as JSON.
FetchResponse.ok
fn FetchResponse.ok(self: &FetchResponse): boolTrue if status is 200-299.
FetchResponse.header
fn FetchResponse.header(self: &FetchResponse, name: &string): Option<string>Get a response header value by name; None when the header is absent.
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<FetchResponse, 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<FetchResponse, NetError>HTTP request with full control over method, headers, and body.
fetchPost
fn fetchPost(url: &string, body: &string): Result<FetchResponse, NetError>HTTP POST with a body.
fetchPut
fn fetchPut(url: &string, body: &string): Result<FetchResponse, NetError>HTTP PUT with a body.
fetchDelete
fn fetchDelete(url: &string): Result<FetchResponse, NetError>HTTP DELETE.
fetchPatch
fn fetchPatch(url: &string, body: &string): Result<FetchResponse, NetError>HTTP PATCH with a body.