Last updated: 2026-07-19

API Server

Http

All API Sections

Minimal HTTP/1.1 server for the OpenAI-compatible inference API.

Provides connection-level request parsing, JSON and SSE response helpers, and a TCP listener that hands off accepted connections to the route dispatcher.

3 exports 12 methods src/server/http.zig

3 exports shown

struct

Connection

#
pub const Connection = struct

Active client connection with request/response capabilities.

Wraps a TCP stream and provides helpers for reading HTTP requests and writing JSON, error, and SSE responses.

src/server/http.zig:18

Methods

9

method

Connection.readRequest

#
pub fn readRequest(self: *Connection) !Request

Read and parse an HTTP/1.1 request from the connection.

Reads headers until `\r\n\r\n`, extracts method/path/Content-Length, then reads the remaining body bytes.

Parameters
self
Active connection to read from.
Returns

Parsed request with method, path, and body.

src/server/http.zig:37

method

Connection.sendJson

#
pub fn sendJson(self: *Connection, status: u16, body: []const u8) !void

Send a JSON response with the given HTTP status code.

Parameters
self
Active connection to write to.
status
HTTP status code (200, 400, 404, etc.).
body
JSON-encoded response body.

src/server/http.zig:157

method

Connection.sendError

#
pub fn sendError(self: *Connection, status: u16, err_type: []const u8, message: []const u8) !void

Send an OpenAI-format JSON error response.

Parameters
self
Active connection to write to.
status
HTTP status code for the error.
err_type
OpenAI error type string (e.g. "invalid_request_error").
message
Human-readable error message.

src/server/http.zig:180

method

Connection.sendSseStart

#
pub fn sendSseStart(self: *Connection) !void

Send SSE streaming response headers with chunked transfer encoding.

After this call, use writeSseEvent to send individual events.

Parameters
self
Active connection to write to.

src/server/http.zig:189

method

Connection.writeSseEvent

#
pub fn writeSseEvent(self: *Connection, data: []const u8) !void

Write a single SSE event as a chunked transfer-encoding frame.

Parameters
self
Active connection to write to.
data
Event payload (typically JSON), sent as `data: {payload}\n\n`.

src/server/http.zig:197

method

Connection.writeSseComment

#
pub fn writeSseComment(self: *Connection, text: []const u8) !void

Write a chunked SSE comment line (`: {text}\n\n`) to keep the connection alive.

Useful when a client or intermediate proxy would time out on a quiet stream before the next model token is ready.

Parameters
self
Active connection to write to.
text
Comment payload sent verbatim after the `: ` prefix.

src/server/http.zig:217

method

Connection.writeSseDone

#
pub fn writeSseDone(self: *Connection) !void

Write the final SSE `[DONE]` event and send the chunked transfer terminator.

Parameters
self
Active connection to write to.

src/server/http.zig:229

method

Connection.isPeerClosed

#
pub fn isPeerClosed(self: *Connection) bool

Check whether the remote connection is definitively dead, to let a streaming decode loop bail out before the next token instead of discovering the same thing on the next write.

This only reports `true` on an unambiguous signal: a TCP reset, refusal, or an already-disconnected socket. A zero-byte peek (the peer's write side reached EOF) is deliberately treated as "not closed": HTTP/1.1 clients commonly half-close the upload side right after sending the request body while still reading the response, so that signal cannot distinguish "client hung up" from "compliant client, still receiving the SSE stream". Bailing out on it would truncate valid streams for such clients — worse than the wasted decode time this function is meant to save. Only a write failure (`catch return` at each write-path call site) proves that case.

Parameters
self
Active connection to inspect.
Returns

`true` only for a hard reset/refused/disconnected socket.

src/server/http.zig:251

method

Connection.close

#
pub fn close(self: *Connection) void

Close the underlying TCP stream and free any heap-allocated overflow body from `readRequest`.

Parameters
self
Connection to close.

src/server/http.zig:269

enum

Method

#
pub const Method = enum GET, POST, OPTIONS, UNKNOWN }

HTTP request method parsed from the request line.

`UNKNOWN` is the fallback for any method not explicitly handled by the server.

src/server/http.zig:277

struct

Server

#
pub const Server = struct

HTTP server that binds and listens on a TCP port.

Accepts connections and wraps them in Connection structs for request handling.

src/server/http.zig:292

Methods

3

method

Server.init

#
pub fn init(allocator: std.mem.Allocator, port: u16) !Server

Bind to all interfaces on the given port and start listening.

Parameters
allocator
Allocator for connection resources.
port
TCP port to listen on.
Returns

A Server ready to accept connections.

src/server/http.zig:302

method

Server.accept

#
pub fn accept(self: *Server) !Connection

Block until a client connects, then return a Connection for that client.

Parameters
self
Active server to accept on.
Returns

A new Connection wrapping the accepted TCP stream.

src/server/http.zig:311

method

Server.deinit

#
pub fn deinit(self: *Server) void

Stop listening and release the server socket.

Parameters
self
Server to tear down.

src/server/http.zig:318