Skip to content
Systems programming for what’s next.
— Vex

Core Server (http/server) ​

http/server is the compact direct-callback entry point. It delegates to the same http/fiber::App HTTP/1 state machine used by routed services, so there is no second parser, accept loop, or deadline policy to drift from production.

Use Server for a single direct request/response callback. Use App directly when a service needs routes or middleware. Both paths have bounded incremental bodies, pipelined suffixes, deadlines, connection admission, worker lifecycle and response coalescing.

Minimal Hello World ​

vex
import { Server, Request, Response } from "http";

fn main(): i32 {
    let! srv = Server.new("0.0.0.0", 8080);
    srv.serve(handler);
    return 0;
}

fn handler(req: &Request, res: &Response!) {
    res.sendString(req.fd, "Hello, Vex!");
}

Direct callback contract ​

The request is valid for the callback only. Response is encoded into connection-owned reusable storage and drained after the callback returns. A handler may still use the familiar res.sendString(req.fd, ...) form; in this mode it is buffered rather than performing an independent socket write.

For cancellation-aware serving, use server.serveWithContext(context, handler). For direct raw-socket parsing experiments, parseRequest(fd) remains an explicit low-level function, not a competing server runtime.

Request Struct ​

FieldTypeDescription
methodstr"GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"
pathstrURL path without query string (e.g. /api/users)
querystrRaw query string (e.g. page=1&sort=name)
versionstr"HTTP/1.1" or "HTTP/1.0"
headersHeadersParsed header collection (case-insensitive)
bodystrRequest body (for POST/PUT/PATCH)
fdi64Native-width socket handle
streamIdi32Reserved stream identifier; the direct HTTP/1 Server facade uses 0 (Fiber HTTP/2 routes use Ctx)
validbooltrue if parsing succeeded
contentLengthi64Parsed Content-Length value (-1 if absent)
keepAliveboolParser-selected reuse policy from HTTP version and all Connection tokens; close wins

Request Accessor Methods ​

vex
req.header("Authorization")    // Get header value (case-insensitive)
req.hasHeader("X-Custom")      // Check if header exists
req.isMethod("POST")           // Match method (case-sensitive)
req.hasBody()                  // true if body.len() > 0
req.contentType()              // Get Content-Type header value
req.isJSON()                   // true if Content-Type == "application/json"

Headers (Headers Collection) ​

Headers are stored as parallel Vec<string> arrays (names + values) with byte-level case-insensitive comparison. No Map overhead, no hash computation—just linear scan optimized for the typical 5–15 header count.

MethodDescription
Headers.new()Create empty collection
hdrs.get(name): stringGet value (case-insensitive), returns "" if not found
hdrs.has(name): boolCheck existence
hdrs.set(name, value)Set/replace (removes existing, then appends)
hdrs.add(name, value)Append (allows duplicates, e.g. Set-Cookie)
hdrs.del(name)Remove all matching (in-place, zero-alloc)
hdrs.clear()Remove all headers
hdrs.shrinkToFit()Release excess capacity
hdrs.len(): i32Count of headers

Cookies ​

The cookie.vx module parses request-cookie pairs and serializes configured Set-Cookie attributes. It is not a browser cookie jar or a session store. Cookie values are application data; the serializer does not create, encrypt or sign an authentication token.

Building Cookies ​

vex
import { Cookie } from "http";

// Simple session cookie
let session = Cookie.new("session_id", "abc123");
// → "session_id=abc123; Path=/; HttpOnly; SameSite=Lax"

// Persistent cookie (expires in 1 hour)
let remember = Cookie.persistent("remember", "yes", 3600);
// → "remember=yes; Max-Age=3600; Path=/; HttpOnly; SameSite=Lax"

// Deletion cookie
let logout = Cookie.delete("session_id");
// → "session_id=; Max-Age=0; Path=/"

// Set on response
res.header("Set-Cookie", session.toString());

Use an application-generated opaque session value, not the literal abc123. For an HTTPS deployment, set secure = true explicitly; its default is false. The cookie settings do not enable TLS in the server. Deletion must use the same path/domain scope as the original cookie. Keep cookie names and attributes under application control; the serializer does not escape arbitrary values.

Parsing Incoming Cookies ​

vex
import { parseCookies } from "http";

let pairs = parseCookies(req.header("Cookie"));
// "session_id=abc123; theme=dark"
// → [CookiePair{name:"session_id", value:"abc123"}, CookiePair{name:"theme", value:"dark"}]
FieldTypeDescription
namestringCookie name
valuestringCookie value
pathstringPath attribute (default "/")
domainstringDomain attribute (empty = current)
maxAgei32Max-Age in seconds (-1 = session, 0 = delete)
secureboolHTTPS only flag
httpOnlyboolNo JS access flag
sameSitestring"Strict", "Lax", "None", or ""

Response Builder ​

vex
import { Response, respondText, respondJSON, respondError, respondRedirect } from "http";

// Builder pattern
let! res = Response.new();
res.status(201)
   .header("X-Request-ID", "req-001")
   .contentType("application/json");
res.sendJSON(req.fd, "{\"created\": true}");

// One-liner convenience functions
respondText(fd, "Hello!");                    // 200 text/plain
respondJSON(fd, "{\"ok\":true}");             // 200 application/json
respondError(fd, 404, "Not Found");           // 404 with message
respondRedirect(fd, "/login");                // 302 redirect

Internal Parser Architecture ​

Underneath, request parsing is handled by a dedicated parser/ subsystem:

FilePurpose
parser/scanner.vxLow-level byte scanner with position tracking
parser/request.vxHTTP/1.1 request line + header parser
parser/fiber_request.vxOptimized Fiber-compatible incremental parser
parser/headers.vxHeader-specific parsing logic
parser/body.vxBody extraction with Content-Length validation
parser/chunked.vxTransfer-Encoding: chunked decoder
parser/response.vxClient-side response parser
parser/h2.vxHTTP/2 frame parser
parser/hpack.vxHPACK header compression (RFC 7541)
parser/huffman.vxHuffman coding tables for HPACK
parser/ws.vxWebSocket frame parser
parser/stream.vxStreaming parser for large payloads

Released under the MIT License.