kex docs Standard Library

Net.HTTP

module Net.HTTP

Buffered HTTP clients, responses, and a small declaration-ordered server router. Requests never follow redirects or perform generic retries implicitly.

using Net.HTTP

let response = HTTP.get("https://example.test/").try
response.status.success?   # => true

For connection reuse and statistics, own a client explicitly:

let client = Client.open.try
let response = client.get("https://example.test/").try
client.close.try

record Headers

An insertion-ordered HTTP field collection. Names compare case-insensitively and duplicate fields are preserved.

Fields

entries
[(String, String)]

Implements Showable, Inspectable.

Methods

add

add(name: String, value: String) -> Headers

Appends a field without replacing existing fields of the same name.

Examples

+Headers.empty.add("Accept", "text/plain")+

set

set(name: String, value: String) -> Headers

Replaces all fields of name with one value.

Examples

+headers.set("Content-Type", "application/json")+

remove

remove(name: String) -> Headers

Removes every field matching name case-insensitively.

get

get(name: String) -> String?

Returns the first matching field value.

getAll

getAll(name: String) -> [String]

Returns every matching value in insertion order.

showValue Showable

showValue : String

Renders fields while replacing authorization and cookie values with ***.

inspectValue Inspectable

inspectValue(colors: Bool) -> String

Structural inspection uses the same credential-safe rendering.

From Showable

record Status

A validated HTTP status code in 100..599.

Fields

Methods

informational?

informational? : Bool

Returns whether the status is in 100..199

success?

success? : Bool

Returns whether the status is in 200..299

redirect?

redirect? : Bool

Returns whether the status is in 300..399

clientError?

clientError? : Bool

Returns whether the status is in 400..499

serverError?

serverError? : Bool

Returns whether the status is in 500..599

record Response<B>

A typed HTTP response envelope whose body representation is explicit.

Fields

status
Status
headers
Headers
body
B

record Request<B>

A typed HTTP request envelope whose body representation is explicit.

Fields

method
String
target
URI
headers
Headers
body
B

record RouteContext

Route captures decoded after path segmentation.

Fields

parameters
Map<String, String> optional

Methods

parameter

parameter(name: String) -> Result<String, NetError>

Returns one decoded named or wildcard route capture.

Returns the capture, or Parse when absent

record Context

Per-request server context.

Fields

type Handler

A buffered HTTP route handler.

record Route

One declared route; routers preserve declaration order.

Fields

method
String
path
String
handler
Handler

record Router

An immutable, declaration-ordered HTTP router.

Fields

routes
[Route] optional

Methods

route

route(method: String, path: String, handler: Handler) -> Router

Appends a route; earlier matching declarations win.

get

get(path: String, handler: Handler) -> Router

Appends a GET route. GET also supplies automatic HEAD fallback.

head

head(path: String, handler: Handler) -> Router

Appends an explicit HEAD route, overriding automatic GET fallback.

options

options(path: String, handler: Handler) -> Router

Appends an explicit OPTIONS route, overriding generated OPTIONS.

post

post(path: String, handler: Handler) -> Router

Appends a POST route.

put

put(path: String, handler: Handler) -> Router

Appends a PUT route.

patch

patch(path: String, handler: Handler) -> Router

Appends a PATCH route.

delete

delete(path: String, handler: Handler) -> Router

Appends a DELETE route.

record ShutdownReport

Counts and elapsed time from graceful server shutdown.

Fields

completed
Integer
failed
Integer
forced
Integer
elapsedMilliseconds
Integer

record ServerOptions

Bounded HTTP server resources and default graceful-shutdown duration.

Fields

maximumHandlers
Integer optional
backlog
Integer optional
gracefulShutdown
Duration optional

record PoolOptions

HTTP connection-pool bounds and idle lifetime.

Fields

perOrigin
Integer optional
total
Integer optional
queuedRequests
Integer optional
idleExpiryMilliseconds
Integer optional

record ClientOptions

Options owned by an explicit HTTP client.

Fields

pool
PoolOptions optional

record ClientStatistics

Lifetime request/reuse counters plus current pooled connections.

Fields

openConnections
Integer
requests
Integer
reusedConnections
Integer

record ClientCloseReport

Resources released by Client.close.

Fields

closedConnections
Integer

type Client

The pooled HTTP client. An opaque handle over the connection pool that owns it; Client.open makes one and client.close releases it.

Methods

request

request(method: String, url: String, headers: Headers, body: Binary) -> Result<Response<Binary>, NetError>

Sends a buffered request. Redirects and generic retries are not implicit.

get

get(url: String) -> Result<Response<Binary>, NetError>

Sends a buffered GET request.

post

post(url: String, body: Binary) -> Result<Response<Binary>, NetError>

Sends a buffered binary POST request.

put

put(url: String, body: Binary) -> Result<Response<Binary>, NetError>

Sends a buffered binary PUT request.

patch

patch(url: String, body: Binary) -> Result<Response<Binary>, NetError>

Sends a buffered binary PATCH request.

delete

delete(url: String) -> Result<Response<Binary>, NetError>

Sends a DELETE request with an empty body.

head

head(url: String) -> Result<Response<Binary>, NetError>

Sends a HEAD request; the returned body is empty.

options

options(url: String) -> Result<Response<Binary>, NetError>

Sends an OPTIONS request.

statistics

statistics : ClientStatistics

Returns current pool and lifetime request counters

close

close : Result<ClientCloseReport, NetError>

Idempotently closes the client and every idle pooled connection.

module Net.HTTP.Headers

empty constant

empty : Headers

from

from(entries: [(String, String)]) -> Result<Headers, NetError>

Validates header names and values without folding duplicates.

Returns validated fields, or Parse

Examples

+Headers.from([("Accept", "application/json")]).try+

parse

parse(text: String) -> Result<Headers, NetError>

Parses CRLF- or LF-separated header fields.

Returns fields in source order, or Parse

module Net.HTTP.Status

from

from(code: Integer) -> Result<Status, NetError>

module Net.HTTP.Response

binary

binary(status: Integer, body: Binary, headers: Headers) -> Response<Binary>

Builds a buffered binary response with validated headers.

Examples

+Response.binary(200, Binary.empty, Headers.empty)+

text

text(status: Integer, body: String) -> Response<Binary>

Builds a UTF-8 text response with an explicit text/plain content type.

Examples

+Response.text(200, "hello")+

empty

empty(status: Integer) -> Response<Binary>

Builds a response with an empty body.

module Net.HTTP.Router

build constant

Examples

+Router.build.get("/health", { |request, context| Response.text(200, "ok") })+

module Net.HTTP.Server

start

start(endpoint: Net.Socket.TCP.Endpoint, router: Router, options: ServerOptions) -> Result<Running, NetError>

Starts a server with conservative defaults and returns immediately.

Examples
let router = Router.build.get("/health", do |request, context|
  Response.text(200, "ok")
end)
let endpoint = Net.Socket.TCP.Endpoint.loopback(Net.Port.from(0).try)
let server = Server.start(endpoint, router).try
Server.stop(server).try

serve

serve(endpoint: Net.Socket.TCP.Endpoint, router: Router, options: ServerOptions) -> Result<Void, NetError>

Starts with defaults and blocks until the server stops.

stop

stop(server: Running, grace: Duration) -> Result<ShutdownReport, NetError>

Gracefully stops using the duration captured at start.

join

join(server: Running) -> Result<Void, NetError>

Waits until the server owner exits.

running?

running?(server: Running) -> Bool

Returns whether the server owner is alive

localAddress

localAddress(server: Running) -> Net.Socket.TCP.Endpoint

Returns the bound address, including ephemeral port

type Running

An opaque asynchronous HTTP server handle.

module Net.HTTP.Client

open

open : Result<Client, NetError>
open(options: ClientOptions) -> Result<Client, NetError>

Opens an explicit pooled client with conservative defaults.

Examples

+Client.open.try+

module Net.HTTP.HTTP

request

request : String -> String -> Headers -> Binary -> Result<Response<Binary>, NetError>

Sends one stateless buffered request with no redirect or hidden retry.

Examples

+HTTP.request("GET", "https://example.test/").try+

get

get(url: String) -> Result<Response<Binary>, NetError>

Sends one stateless buffered GET.

Examples

+HTTP.get("https://example.test/").try+

delete

delete(url: String) -> Result<Response<Binary>, NetError>

Sends one stateless DELETE with an empty body.

head

head(url: String) -> Result<Response<Binary>, NetError>

Sends one stateless HEAD and returns an empty response body.

options

options(url: String) -> Result<Response<Binary>, NetError>

Sends one stateless OPTIONS request.

post

post(url: String, body: Binary) -> Result<Response<Binary>, NetError>

Sends one stateless buffered binary POST.

put

put(url: String, body: Binary) -> Result<Response<Binary>, NetError>

Sends one stateless buffered binary PUT.

patch

patch(url: String, body: Binary) -> Result<Response<Binary>, NetError>

Sends one stateless buffered binary PATCH.

Submodules

  • Net.HTTP.WebSocket High-level RFC 6455 client messages. The runtime handles fragmentation and ping/pong frames; reconnect and heartbeat policies remain application-owned.