abla/http/async

Generated from source.

Import with import "abla/http/async".

Source: abla/http/async/entry.ab

HttpAsyncOptions (class)

class HttpAsyncOptions(
    val maximumConnectionsPerHost: int = 8,
    val maximumResponseBytes: int = 8388608,
    val idleTimeoutMilliseconds: int = 30000,
    val dnsTimeoutMilliseconds: int = 3000,
    val dnsCacheSeconds: int = 60,
    // Empty uses the system trust store.
    val caPath: string = "",
    // TLS 1.3 session resumption: a ticket cache the caller owns (abla/tls/pure/resume: tlsSessionCacheNew), shared by every secure connection of the client; a new
    // connection to a host that has a ticket in it resumes (no certificate), and the tickets servers send are kept in it. null: no resumption.
    val sessions: TlsSessionCache? = null,
    val nameserver: string = "",
    val nameserverPort: int = 53,
    // The resolver's search domains and ndots, and its hosts file.
    val dnsConfigurationPath: string = "/etc/resolv.conf",
    val dnsHostsPath: string = "/etc/hosts",
    // HTTP/2 (RFC 9113): on by default for https: h2 is offered by ALPN and used when the server picks it (one connection is shared by many requests, so
    // maximumConnectionsPerHost does not bound it; header names of an h2 response are lower case; trailers are in `trailers()`). http2 = false keeps HTTP/1.1 on every
    // connection. Plain http:// is HTTP/1.1 unless http2PriorKnowledge speaks h2c on every http:// origin of this client (h2c stays off by default).
    val http2: bool = true,
    val http2PriorKnowledge: bool = false,
    // Concurrent requests on one h2 connection (also bounded by the server's SETTINGS_MAX_CONCURRENT_STREAMS).
    val maximumStreamsPerConnection: int = 100,
    // Header names HPACK never indexes, besides authorization, proxy-authorization, cookie and set-cookie.
    val http2NeverIndex: array<string> = [],
    // An h2 connection idle this long is probed with a PING before a request uses it.
    val http2PingAfterIdleMilliseconds: int = 30000,
    val maximumHeaderListBytes: int = 65536
)

Sets connection, DNS cache, timeout, and response limits for the asynchronous client.

HttpAsyncCompletion (class)

class HttpAsyncCompletion(
    val id: int,
    val status: int,
    val headers: array<HttpHeader>,
    val body: string,
    val error: string,
    val elapsedMilliseconds: int,
    // "h2" or "http/1.1" for a request that got a response, "" otherwise.
    val protocol: string = "",
    val trailerList: array<HttpHeader> = []
)

A finished request identified by id, with status, headers, body, error, elapsed time, protocol and trailers. Check succeeded before consuming the response.

HttpAsyncCompletion.succeeded (method)

fun succeeded(): bool

Returns true if the request completed without a transport or protocol error. The HTTP status can still report a server error.

HttpAsyncCompletion.trailers (method)

fun trailers(): array<HttpHeader>

Trailer fields an HTTP/2 response carried (lower case names), in order; empty when there were none and for HTTP/1.1.

HttpAsyncCompletion.header (extension fun)

fun HttpAsyncCompletion.header(name: string): string

Returns the last header matching name with ASCII-insensitive comparison, or an empty string when absent.

HTTP_STREAM_HEADERS (val)

val HTTP_STREAM_HEADERS

Streaming requests (requestStream, getStream): what happened to one, in order. HEADERS: status and headers; READABLE: bytes wait for readStream (sent when there were none waiting; read until it returns ""); WRITABLE: writeBody has room again; END: the response ended well (trailers); ERROR: the request failed (error; the id is done).

HTTP_STREAM_READABLE (val)

val HTTP_STREAM_READABLE

Event indicating that response body bytes are waiting to be read.

HTTP_STREAM_WRITABLE (val)

val HTTP_STREAM_WRITABLE

Event indicating that the request stream can accept more body bytes.

HTTP_STREAM_END (val)

val HTTP_STREAM_END

Terminal stream event with the successful response status and any trailers.

HTTP_STREAM_ERROR (val)

val HTTP_STREAM_ERROR

Terminal stream event containing the reason the request failed.

HttpAsyncStreamEvent (class)

class HttpAsyncStreamEvent(val kind: int, val id: int, val status: int = 0, val headers: array<HttpHeader> = [], val trailers: array<HttpHeader> = [], val error: string = "")

Holds the request identifier and the status, headers, trailers, or error for a stream event.

HttpAsyncStreamState (class)

class HttpAsyncStreamState(
    var inbox: string = "",
    var framing: int = 0,
    var left: int = 0,
    var phase: int = 0,
    var ended: bool = false,
    var paused: bool = false,
    var blocked: bool = false,
    var bodyDone: bool = false,
    var chunkedRequest: bool = false,
    var keepAlive: bool = true,
    var status: int = 0,
    var headers: array<HttpHeader> = [],
    var trailers: array<HttpHeader> = []
)

The HTTP/1.1 half of a streaming request (per request slot): the response body framing and what waits for readStream, and the request body's framing. framing: 0 response headers not read yet, 1 Content-Length (left bytes to come), 2 chunked (phase: 0 size line, 1 data, 2 CRLF after data, 3 after the last chunk), 3 until the close, 4 no body.

HttpAsyncTarget (class)

class HttpAsyncTarget(
    val secure: bool,
    val host: string,
    val port: int,
    val path: string,
    val valid: bool
)

Holds the parsed scheme, host, port, and request path with a URL validity flag.

HttpAsyncRead (class)

class HttpAsyncRead(var text: string, var status: int)

Holds temporary read bytes and the socket or TLS read status.

HttpAsyncClient (class)

resource class HttpAsyncClient(
    val poller: SocketPoller,
    val options: HttpAsyncOptions,
    val resolver: DnsAsyncResolver,
    val reader: HttpAsyncRead,
    // requests
    val requestIds: array<int>,
    val requestStates: array<int>,
    val requestKeys: array<string>,
    val requestHosts: array<string>,
    val requestPorts: array<int>,
    val requestSecure: array<bool>,
    val requestPayloads: array<string>,
    val requestDeadlines: array<int>,
    val requestStarted: array<int>,
    val requestRetried: array<bool>,
    // The parts of a request, kept for HTTP/2 (HTTP/1.1 uses the rendered payload), the connection slot and stream carrying it, and the response as it arrives.
    val requestMethods: array<string>,
    val requestTargets: array<string>,
    val requestAuthorities: array<string>,
    val requestHeaderLists: array<array<HttpHeader>>,
    val requestBodies: array<string>,
    val requestSlots: array<int>,
    val requestStreams: array<int>,
    val requestStatuses: array<int>,
    val requestResponseHeaders: array<array<HttpHeader>>,
    val requestChunks: array<array<string>>,
    val requestTrailers: array<array<HttpHeader>>,
    val freeRequests: array<int>,
    var freeRequestCount: int,
    // connections
    val connectionDescriptors: array<int>,
    val connectionSessions: array<int>,
    val connectionTls: array<TlsSession>,
    val connectionKeys: array<string>,
    val connectionHosts: array<string>,
    val connectionPorts: array<int>,
    val connectionSecure: array<bool>,
    val connectionStates: array<int>,
    val connectionRequests: array<int>,
    val connectionOutputs: array<string>,
    val connectionOutputOffsets: array<int>,
    val connectionInputs: array<string>,
    val connectionHeaderEnds: array<int>,
    val connectionUsed: array<int>,
    val connectionIdleSince: array<int>,
    // The resolver's query id while resolving, otherwise 0.
    val connectionDnsQueries: array<int>,
    val connectionWriteInterest: array<bool>,
    // 1 HTTP/1.1, 2 HTTP/2 (then connectionH2 is the connection's H2Client; other slots hold an unused one); the last time bytes arrived.
    val connectionProtocols: array<int>,
    val connectionH2: array<H2Client>,
    val connectionActivity: array<int>,
    val freeConnections: array<int>,
    var freeConnectionCount: int,
    // What each origin key speaks (httpAsyncOrigin*), until when that is believed.
    val originKeys: array<string>,
    val originProtocols: array<int>,
    val originExpires: array<int>,
    // descriptor -> connection slot * 2 + role (0 socket, 1 dns)
    val routes: array<int>,
    // DNS cache
    val dnsHosts: array<string>,
    val dnsAddresses: array<string>,
    val dnsExpires: array<int>,
    var completions: array<HttpAsyncCompletion>,
    var nextId: int,
    var activeRequests: int,
    var connectionsOpened: int,
    // Streaming requests, by request slot: whether it is one, its request body length (-1 unknown) and idle timeout; the events not taken yet.
    val requestStreaming: array<bool> = [],
    val requestLengths: array<int> = [],
    val requestIdle: array<int> = [],
    var streamEvents: array<HttpAsyncStreamEvent> = [],
    val streamStates: array<HttpAsyncStreamState> = []
)

Owns network descriptors, queued requests, connection pools, and pending HTTP events.

HttpAsyncClient.drop (method)

trusted fun drop(): void

Releases the client's open sockets and TLS connections.

httpAsyncClient (fun)

fun httpAsyncClient(options: HttpAsyncOptions = HttpAsyncOptions()): HttpAsyncClient

Returns a client with a new kernel poller and asynchronous resolver using the supplied options.

httpAsyncNumericIpv4 (fun)

fun httpAsyncNumericIpv4(host: string): string

Returns numeric IPv4 host as supplied, maps localhost to 127.0.0.1, and returns an empty string for names needing resolution.

httpAsyncConnect (fun)

fun httpAsyncConnect(address: string, port: int): int

Starts a nonblocking IPv4 connect. Returns the descriptor (connected or in progress) or a negative errno.

httpAsyncSocketError (fun)

fun httpAsyncSocketError(descriptor: int): int

Reads the pending socket error on descriptor. Returns zero when clear or a positive system error code for a pending socket error or failed query.

httpAsyncPlainRead (fun)

trusted fun httpAsyncPlainRead(descriptor: int, var result: HttpAsyncRead): void

Reads up to 65536 bytes from descriptor, retrying interruptions, and fills result. Status is a byte count, zero for EOF, or negative errno, and text is empty when no bytes were read.

httpAsyncNoSession (fun)

fun httpAsyncNoSession(): TlsSession

The TLS of a connection is a TlsSession of abla/tls/pure over its socket. A slot without TLS holds a failed placeholder.

HttpAsyncClient.tlsRead (extension fun)

fun HttpAsyncClient.tlsRead(slot: int): void

Status > 0: bytes read. Otherwise -2 = want read, -3 = want write, -6 = clean close, -1 = failure.

HttpAsyncClient.tlsWrite (extension fun)

fun HttpAsyncClient.tlsWrite(slot: int, contents: string, offset: int): int

Returns bytes written, -2 or -3 for a socket that must become readable or writable first, -1 for a failure.

HttpAsyncClient.tlsHandshake (extension fun)

fun HttpAsyncClient.tlsHandshake(slot: int): int

Returns 1 when complete, -2 or -3 while the socket must become readable or writable, -100 when the certificate was refused, -1 for any other failure.

HttpAsyncClient.tlsFree (extension fun)

fun HttpAsyncClient.tlsFree(slot: int): void

close_notify is offered once, the socket is shut down; the descriptor is closed by the caller.

HttpAsyncClient.route (extension fun)

fun HttpAsyncClient.route(descriptor: int, value: int): void

Associates a nonnegative descriptor with an event dispatch slot, growing the table as needed.

HttpAsyncClient.unroute (extension fun)

fun HttpAsyncClient.unroute(descriptor: int): void

Removes the dispatch mapping for a descriptor when it is present.

HttpAsyncClient.complete (extension fun)

fun HttpAsyncClient.complete(
    request: int,
    status: int,
    headers: array<HttpHeader>,
    body: string,
    error: string,
    protocol: string = ""
): void

Queues the terminal event or buffered completion for an active request and releases its slot.

HttpAsyncClient.closeConnection (extension fun)

fun HttpAsyncClient.closeConnection(slot: int): void

Closes the socket, unregisters it, and releases TLS state for the connection slot.

HttpAsyncClient.fail (extension fun)

fun HttpAsyncClient.fail(slot: int, error: string): void

Fails the connection's request (if any) and closes the connection. A request whose reused keep-alive connection died before any response byte is requeued once instead.

HttpAsyncClient.interest (extension fun)

fun HttpAsyncClient.interest(slot: int, write: bool): void

Updates the connection's writable poller interest while retaining readable interest.

HttpAsyncClient.allocateConnection (extension fun)

fun HttpAsyncClient.allocateConnection(request: int): int

Returns a fresh or reused connection slot initialized from the request.

HttpAsyncClient.cachedAddress (extension fun)

fun HttpAsyncClient.cachedAddress(host: string): string

Returns a numeric IPv4 address or unexpired cached DNS answer, or empty text when unresolved.

HttpAsyncClient.remember (extension fun)

fun HttpAsyncClient.remember(host: string, address: string, ttlSeconds: int): void

Stores a DNS answer with its time to live bounded by the cache options.

HttpAsyncClient.unwatchQuery (extension fun)

fun HttpAsyncClient.unwatchQuery(query: int): void

Stops watching a query's socket (before the resolver closes it).

HttpAsyncClient.startResolve (extension fun)

fun HttpAsyncClient.startResolve(slot: int): void

Starts asynchronous IPv4 DNS resolution for the connection slot.

HttpAsyncClient.dnsReady (extension fun)

fun HttpAsyncClient.dnsReady(slot: int): void

Consumes a ready DNS reply and advances the slot if resolution completed.

HttpAsyncClient.resolved (extension fun)

fun HttpAsyncClient.resolved(slot: int): void

The slot's query has finished: connect to its first IPv4 address, or fail.

HttpAsyncClient.startConnect (extension fun)

fun HttpAsyncClient.startConnect(slot: int, address: string): void

Starts a nonblocking connection or fails the slot if the socket cannot be opened.

HttpAsyncClient.connected (extension fun)

fun HttpAsyncClient.connected(slot: int): void

Starts the TLS handshake for HTTPS or begins the HTTP request write for a plain connection.

HttpAsyncClient.handshake (extension fun)

fun HttpAsyncClient.handshake(slot: int): void

Advances the connection's TLS handshake, updating readiness interests or failing the request if negotiation fails.

HttpAsyncClient.beginWrite (extension fun)

fun HttpAsyncClient.beginWrite(slot: int): void

(readSlot, writeSlot and pumpEvents are not called readReady, writeReady and dispatch: the region check of abla/http/event pairs member calls by name, and a TlsSession's readReady/writeReady retain what they are given.) Moves a connected (or pooled idle) connection onto its assigned request.

HttpAsyncClient.writeSlot (extension fun)

fun HttpAsyncClient.writeSlot(slot: int): void

Writes pending request bytes until they are sent, the socket blocks, or the connection fails.

HttpAsyncClient.parseResponse (extension fun)

fun HttpAsyncClient.parseResponse(slot: int, eof: bool): bool

Inspects buffered response bytes; completes the request when the message is whole. Returns true when the response finished.

HttpAsyncClient.readSlot (extension fun)

fun HttpAsyncClient.readSlot(slot: int): void

Consumes ready response bytes and handles complete responses, EOF, or failed reads.

HttpAsyncClient.socketReady (extension fun)

fun HttpAsyncClient.socketReady(slot: int, readable: bool, writable: bool, failed: bool): void

Dispatches readiness and failure signals according to the current connection state.

HttpAsyncClient.originProtocol (extension fun)

fun HttpAsyncClient.originProtocol(key: string, secure: bool): int

What the origin of key is believed to speak: HTTP/2 when a live connection of it does, else the memory of its last handshake (until it expires with the DNS cache), else unknown. Without http2 every origin speaks HTTP/1.1; http2PriorKnowledge makes every plain origin HTTP/2.

HttpAsyncClient.rememberOrigin (extension fun)

fun HttpAsyncClient.rememberOrigin(key: string, protocol: int): void

Caches the selected protocol for an origin until the configured expiry.

HttpAsyncClient.handshakeDone (extension fun)

fun HttpAsyncClient.handshakeDone(slot: int): void

The TLS handshake is done: what the server chose (ALPN) decides the protocol of the connection and is remembered for the origin.

HttpAsyncClient.startH2 (extension fun)

fun HttpAsyncClient.startH2(slot: int): void

The connection speaks HTTP/2 from here: its request (if any) becomes the first stream.

HttpAsyncClient.h2Attach (extension fun)

fun HttpAsyncClient.h2Attach(slot: int, request: int): bool

Starts request as a stream of the slot's connection; false when the connection has no room.

HttpAsyncClient.requestOnStream (extension fun)

fun HttpAsyncClient.requestOnStream(slot: int, stream: int): int

Returns the active request slot for an HTTP/2 stream or -1 when no request owns it.

HttpAsyncClient.h2Events (extension fun)

fun HttpAsyncClient.h2Events(slot: int, retryUnanswered: bool): void

Takes what the connection's H2Client reports (response headers, data, ends, resets) into the requests; closes the slot when the connection ended. retryUnanswered: a stream that got no response bytes when the connection broke goes back to the queue once.

HttpAsyncClient.h2Write (extension fun)

fun HttpAsyncClient.h2Write(slot: int): void

Writes what the connection's H2Client has to send (and what is left of the last write), until the socket would block.

HttpAsyncClient.h2Pump (extension fun)

fun HttpAsyncClient.h2Pump(slot: int): void

Processes pending HTTP/2 events and writes queued frames while the connection remains active.

HttpAsyncClient.h2Read (extension fun)

fun HttpAsyncClient.h2Read(slot: int): void

Reads ready HTTP/2 bytes and dispatches frames until blocked or the connection closes.

HttpAsyncClient.h2Fail (extension fun)

fun HttpAsyncClient.h2Fail(slot: int, error: string): void

An error on the connection (not the protocol's own): every stream fails with it.

HttpAsyncClient.h2Open (extension fun)

fun HttpAsyncClient.h2Open(key: string, now: int): int

A connection of the origin that can take a request now, or -1. One idle for a while is probed with a PING first and used when it answers.

HttpAsyncClient.h2Roomy (extension fun)

fun HttpAsyncClient.h2Roomy(key: string): bool

True when a live HTTP/2 connection of the origin has room for a stream (even if it is being probed with a PING first): no new connection is opened then.

HttpAsyncClient.connectionsComing (extension fun)

fun HttpAsyncClient.connectionsComing(key: string): int

Connections of the origin that are still on their way (resolving, connecting or handshaking).

HttpAsyncClient.openConnectionFor (extension fun)

fun HttpAsyncClient.openConnectionFor(request: int): void

Allocates a slot for a queued request and begins DNS resolution or connects to the cached address.

HttpAsyncClient.connectionsFor (extension fun)

fun HttpAsyncClient.connectionsFor(key: string): int

Returns the number of active connections for the given origin key.

HttpAsyncClient.idleConnection (extension fun)

fun HttpAsyncClient.idleConnection(key: string): int

Returns a reusable idle connection slot for the origin, or -1 when none is available.

HttpAsyncClient.pumpEvents (extension fun)

fun HttpAsyncClient.pumpEvents(): void

Assigns queued requests to existing connections or opens new ones within the connection limits.

HttpAsyncClient.sweep (extension fun)

fun HttpAsyncClient.sweep(now: int): void

Enforces request, DNS, and idle-connection deadlines at the supplied monotonic timestamp.

HttpAsyncClient.request (extension fun)

fun HttpAsyncClient.request(
    method: string,
    url: string,
    headers: array<HttpHeader> = [],
    body: string = "",
    timeoutMilliseconds: int = 10000
): int

Queues a request and returns its id; the completion carries the same id.

HttpAsyncClient.get (extension fun)

fun HttpAsyncClient.get(url: string, headers: array<HttpHeader> = [],
    timeoutMilliseconds: int = 10000): int

Starts a buffered GET and returns its request identifier, with errors delivered through the completion queue.

HttpAsyncClient.postJson (extension fun)

fun HttpAsyncClient.postJson(url: string, json: string, headers: array<HttpHeader> = [],
    timeoutMilliseconds: int = 10000): int

Starts a buffered POST with a JSON content type and returns the request identifier for its completion or error.

HttpAsyncClient.streamInterest (extension fun)

fun HttpAsyncClient.streamInterest(slot: int, write: bool): void

The connection's read interest follows the request: off while its inbox is full.

HttpAsyncClient.startStream (extension fun)

fun HttpAsyncClient.startStream(slot: int, request: int): void

Associates a connection with a streaming request and prepares its initial output.

HttpAsyncClient.streamWrite (extension fun)

fun HttpAsyncClient.streamWrite(slot: int): void

Writes what is queued until the socket would block; tells a blocked writeBody when half of the cap is free again.

HttpAsyncClient.streamRead (extension fun)

fun HttpAsyncClient.streamRead(slot: int): void

Reads what the socket has (until it would block or the inbox is full) and parses it.

HttpAsyncClient.streamParse (extension fun)

fun HttpAsyncClient.streamParse(slot: int, eof: bool): void

Takes the headers (once) and then the body bytes out of the connection's input into the request's inbox, by the response's framing.

HttpAsyncClient.streamFinish (extension fun)

fun HttpAsyncClient.streamFinish(slot: int): void

The response is whole and was read: END, and the connection is kept for another request when both bodies are done and the server allows it.

HttpAsyncClient.requestStream (extension fun)

fun HttpAsyncClient.requestStream(
    method: string,
    url: string,
    headers: array<HttpHeader> = [],
    length: int = -1,
    idleTimeoutMilliseconds: int = 30000
): int

Starts a streaming request and returns its identifier, with failures delivered as stream events.

HttpAsyncClient.getStream (extension fun)

fun HttpAsyncClient.getStream(url: string, headers: array<HttpHeader> = [], idleTimeoutMilliseconds: int = 30000): int

Starts a GET with a streaming response and returns the identifier used to read bytes and receive errors.

HttpAsyncClient.writeBody (extension fun)

fun HttpAsyncClient.writeBody(id: int, chunk: string): int

Writes request body bytes: returns how many were taken (a prefix of chunk), 0 when the stream cannot take more now (wait for WRITABLE), -1 when the id is not a live streaming request.

HttpAsyncClient.finishBody (extension fun)

fun HttpAsyncClient.finishBody(id: int): void

The request body is complete (the stream's END_STREAM follows what is still queued).

HttpAsyncClient.readStream (extension fun)

fun HttpAsyncClient.readStream(id: int, maximum: int = 65536): string

Up to maximum bytes of the response body. Reading is what credits the peer: not reading stalls it after one stream window. "" when nothing waits.

HttpAsyncClient.streamPending (extension fun)

fun HttpAsyncClient.streamPending(id: int): int

The response bytes that wait to be read.

HttpAsyncClient.takeStreamEvents (extension fun)

fun HttpAsyncClient.takeStreamEvents(): array<HttpAsyncStreamEvent>

The stream events since the last call.

HttpAsyncClient.cancelStream (extension fun)

fun HttpAsyncClient.cancelStream(id: int): void

Gives up on a streaming request: RST_STREAM(CANCEL); its last event is an ERROR "cancelled".

HttpAsyncClient.step (extension fun)

fun HttpAsyncClient.step(timeoutMilliseconds: int = 0): int

Progresses every request for up to timeoutMilliseconds (0 never blocks).

HttpAsyncClient.takeCompletions (extension fun)

fun HttpAsyncClient.takeCompletions(): array<HttpAsyncCompletion>

Returns the completed requests accumulated so far and clears the completion queue.

HttpAsyncClient.pending (extension fun)

fun HttpAsyncClient.pending(): int

Returns the number of active requests.

HttpAsyncClient.pollDescriptor (extension fun)

fun HttpAsyncClient.pollDescriptor(): int

The poller descriptor; register it with another event loop (for example WebSocketServer.watchExternal) to wake when any request makes progress.

Built from Abla v0.2.79 source ba5d6b7d. Examples and generated signatures follow that source revision.