http

    Streaming HTTP protocol suite for MoonBit with HTTP/1.0, HTTP/1.1, HTTP/2, HTTP/3, QUIC, TLS integration, and policy-driven transports.

    http
    http1
    http2
    http3
    quic
    tls
    networking
    Download zip
    Author
    Version
    0.9.2
    License
    Apache-2.0
    Last updated
    3 days ago
    Downloads
    70

    #ZSeanYves/http

    A streaming HTTP library for MoonBit with support for HTTP/1.0, HTTP/1.1, HTTP/2, HTTP/3 and QUIC v1. Use the protocol codecs on their own, or integrate a client, connection pool or server listener into your application.

    #Features

    • HTTP/1.0 and HTTP/1.1 — Incremental request and response parsing and encoding. HTTP/1.0 closes connections by default and supports explicit keep-alive. HTTP/1.1 adds chunked transfer and trailers.
    • HTTP/2 — Frame encoding and decoding, HPACK, multiplexing, flow control, GOAWAY and h2c.
    • HTTP/3 and QUIC v1 — QPACK, request streams, packet protection, loss recovery and connection ID rotation. QUIC packet protection on Wasm, Wasm-GC and JavaScript uses an experimental cryptography implementation.
    • Streaming requests and responses — Read and write bodies incrementally with backpressure, cancellation and trailers. Async service tests run on Native, Wasm and JavaScript. Wasm-GC runs synchronous protocol tests and has compile-only coverage for async services.
    • HTTP policies — Opt in to authentication, cookies, caching, redirects, retries and content decoding.
    • Native clients and servers — TCP, UDP, DNS, HTTP clients, connection pools and listeners. Built-in network adapters are available on Native only.
    • Native TLS — TLS over TCP uses OpenSSL 3.x. HTTP/3 handshakes require OpenSSL 3.5 or a later 3.x release. The built-in OpenSSL adapter is available on Native only.
    • Host I/O — Wasm and JavaScript can use adapter/callback with host-provided async byte streams. HTTP/3 also requires host datagram transport and a QUIC TLS handshake provider. The library has no built-in browser fetch or WebSocket adapter.

    #Installation and usage

    Version 0.9.2 uses the socket APIs in MoonBit 1.0.0-rc1. To install the published module from Mooncakes, run

    moon add ZSeanYves/http

    The following example runs on all four backends. Import the protocol/http1 package for tests in moon.pkg.

    import {
    "ZSeanYves/http/protocol/http1",
    } for "test"

    Add this test to request_test.mbt and run moon test to parse a request.

    test "decode a request" {
    let decoder = @http1.RequestDecoder::new()
    decoder.feed(b"GET /hello HTTP/1.1\r\nHost: localhost\r\n\r\n").unwrap()
    guard decoder.next() is Ok(Some(RequestHead(head))) else {
    fail("expected request headers")
    }
    assert_eq(head.uri.value, "/hello")
    assert_true(decoder.next() is Ok(Some(RequestEnd)))
    }

    #Send an HTTPS GET on Native

    Add these imports to the Native package’s moon.pkg. The example uses HTTP/1.1, accepts an optional PEM trust bundle, and collects at most 1 MiB of response data.

    import {
    "ZSeanYves/http/adapter/native/client" @native_client,
    "ZSeanYves/http/application/client",
    "ZSeanYves/http/core/body",
    "ZSeanYves/http/core/types",
    "ZSeanYves/http/runtime/transport",
    }
    supported_targets = "+native"

    pub async fn get(uri : @types.Uri, trust_anchors? : Bytes) -> Bytes {
    @native_client.with_client(
    async fn(client) {
    let response = client.send(client.request(Get, uri, @client.Upload::bytes(b"")))
    @body.collect(response.body, max_bytes=1024 * 1024).0
    },
    version=Http11,
    tls={ ..@transport.TlsOptions::defaults(), alpn: ["http/1.1"], trust_anchors },
    policy=@transport.AllowAllPolicy::new(),
    )
    }