veko-lang.org
Download Free EPUB 3.0

Part II · Files, Codecs, Web & Zero-FFI SQLite

Chapter 5

HTTP/1.1, HTTP/2, QUIC HTTP/3 & Homoiconic Web Apps

Concurrent HTTP batching, XSS-safe homoiconic HTML5 views, constant-memory streaming, and multi-worker TLS 1.3 + HTTP/2 + WAN QUIC HTTP/3 servers

NETWORKING, WEB & DATABASES

Unified io/Stream Stack, Hybrid PQC TLS 1.3, Web Router & Zero-FFI Databases

End-to-end network services, HTML5 rendering, and wire-native database drivers running across all three runtimes.

TRANSPORT & SECURITY LAYER io/* + net/dns + net/tls13 HTTP, WEB & REAL-TIME LAYER web + html + net/websocket + http2 PERSISTENCE & PACKAGING db/kv + db/postgres + db/redis + vkp Encrypted stream HTTP/1.1 HTTPS / WSS State & sessions SQL & cache Polymorphic I/Oio/Stream Capability Recordio/fs + io/net + io/open Secure TransportDNS & Hybrid PQC TLS 1.3RFC 8446 + X25519MLKEM768 Web Tierweb Router & HTML5 DSLMiddleware + auto-escaped html ProtocolsHTTP Client, HTTP/2 & WSnet/client + http2 + websocket Embedded StoreEmbedded db/kv EngineCRC-32 WAL + put-sealed External DBsPostgres v3, Redis & SidecarSCRAM-SHA-256 + RESP2/3
Component & ModuleArchitectural Role
io/Stream Capability Record
lib/io.vk + lib/io/net.vk
Record-of-closures interface unifying TCP sockets, files, and in-memory test buffers.
DNS & Hybrid PQC TLS 1.3
lib/net/dns.vk + lib/net/tls13.vk
Resolves DNS records over UDP/TCP and negotiates TLS 1.3 with X.509 chain verification.
web Router & HTML5 DSL
lib/web.vk + lib/html.vk
Routes parameterized paths (:id, *splat) through composable middleware and typed HTML5 nodes.
HTTP Client, HTTP/2 & WS
lib/net/client.vk + lib/net/websocket.vk
Provides redirect-following HTTP/1.1 batching, HTTP/2 HPACK framing, and RFC 6455 WebSockets.
Embedded db/kv Engine
lib/db/kv.vk
Crash-safe append-only log-structured key-value database with positioned pread/pwrite.
Postgres v3, Redis & Sidecar
lib/db/postgres.vk + lib/db/redis.vk
Speaks PostgreSQL v3 extended query wire protocol and Redis RESP directly over io/Stream.

Recipe 5.1: Concurrent Non-Blocking HTTP Batch Fetching, HEAD Probes & Keep-Alive Pools (lib/net/client.vk)

Fetch multiple endpoints concurrently on a single readiness reactor thread via nc/get-many (with built-in public CA root verification across Ed25519, ECDSA P-256/P-384, and RSA), issue zero-body nc/head checks, or reuse persistent sockets via nc/new-pool:

VEKO
use [nc = net/client]

probe-cluster = fn [urls block[str]] block[i64] [
    opts = nc/RequestOpts { timeout = 5s, pqc = true }
    responses = nc/get-many urls opts
    responses \ map fn [r] [
        if err? r [ 0 ] else [ r.status ]
    ]
]

check-release-size = fn [url str] str [
    r = nc/head url (nc/RequestOpts { timeout = 5s })
    map/get/or r.headers 'content-length' [ '0' ]
]

Recipe 5.2: XSS-Safe Homoiconic HTML5 Views & Web Router (lib/html.vk + lib/web.vk)

Author HTML5 components as native Veko [.tag { attrs } [ children ]] blocks where text nodes are automatically HTML-escaped against XSS, and GET routes automatically answer RFC-compliant HEAD requests:

VEKO
use [web, html]

status-card = fn [service_name str, proto str] block [
    [.article { class = 'card' } [
        [.h2 {} [`Service: {service_name}`]]
        [.p {} [`Active wire protocol: {proto}`]]
    ]]
]

pub make-app = fn [] web/App [
    mut app = web/new-app
    web/use-security-headers mut app

    web/get mut app '/status/:service' fn [req web/Request] web/Response [
        svc   = web/param req 'service' 'veko-site'
        proto = web/header req 'X-Veko-Proto' 'h1'
        web/view 200 (status-card svc proto)
    ]

    web/get mut app '/api/v1/ping' fn [req web/Request] web/Response [
        web/json-resp 200 { ok = true, proto = (web/header req 'X-Veko-Proto' 'h1') }
    ]
    app
]

Recipe 5.3: Multi-Worker Native TLS 1.3 + HTTP/2 (h2) + WAN QUIC HTTP/3 (h3)

Run one web/App across multi-core shared-nothing TCP :443 workers (https-accept-loop + https-worker-loop with mtime certificate hot-reload, 5s handshake deadlines, and HTTP/2 Rapid Reset guards), UDP :443 WAN QUIC v1 HTTP/3 (ReassemblyBuf + PTO retransmission), and TCP :80 (Let's Encrypt ACME HTTP-01 + 301 redirect) with zero reverse proxy:

VEKO
use [web, inet = io/net, tls = net/tls13]

serve-production = fn [cert_pem str, key_pem str, acme_dir str] any [
    _ = tls/ensure-dev-p256-identity cert_pem key_pem 'veko-lang.org'
    mut rel = tls/new-cert-reloader cert_pem key_pem

    // 1. Spawn port :80 ACME HTTP-01 + 301 HTTPS redirect reactor
    _ = spawn [
        r_ln = inet/listen-nonblock '0.0.0.0:80'
        _ = web/serve-reactor r_ln (web/make-acme-redirect-app acme_dir 'veko-lang.org' 443) (web/AsyncServerOpts { max_requests = 0 })
        .ok
    ]

    // 2. Spawn UDP :443 WAN QUIC v1 HTTP/3 listener
    usock = net/udp-bind '0.0.0.0' 443
    _ = spawn [
        mut h3_rel = tls/new-cert-reloader cert_pem key_pem
        _ = web/serve-h3 usock (fn [] any [ tls/get-identity mut h3_rel ]) (make-app) 0
        .ok
    ]

    // 3. Dispatch TCP :443 TLS 1.3 (h2 + http/1.1) across 4 shared-nothing workers
    work_ch = chan/new 128
    opts = web/HttpsServerOpts {
        max_requests      = 0
        handshake_timeout = 5s
        alt_svc           = 'h3=":443"; ma=86400'
    }
    mut w = 0
    while w < 4 [
        _ = spawn [
            _ = web/https-worker-loop work_ch (make-app) opts
            .ok
        ]
        w = w + 1
    ]
    ln = net/listen-tcp .ip '0.0.0.0:443'
    web/https-accept-loop ln mut rel work_ch 0
]

Recipe 5.4: Constant-Memory File, 206 Range & Chunked Response Streaming

Serve multi-hundred-megabyte SDK archives, video slices, or generated io/Stream feeds without buffering payloads in RAM (serve-static-range automatically switches to 64 KiB positioned os/pread streaming across HTTP/1.1 and flow-controlled HTTP/2 DATA frames for files and byte ranges > 256 KiB):

VEKO
use [io, web]

register-download-routes = fn [mut app web/App] [
    web/get mut app '/downloads/:archive' fn [req web/Request] web/Response [
        name  = web/param req 'archive' ''
        range = web/header req 'Range' ''
        web/serve-static-range 'downloads' name range
    ]

    web/get mut app '/events/export.ndjson' fn [req web/Request] web/Response [
        payload = bytes/from-str '{"seq":1,"ok":true}\n{"seq":2,"ok":true}\n'
        st = io/from-bytes payload
        // Pass -1 content_len (or use web/chunked-response) for HTTP/1.1 chunked / H2 DATA streaming
        web/stream-response 200 'application/x-ndjson' st (len payload)
    ]
]