Last updated: 2026-06-12

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:12

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:27

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:126

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:149

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:158

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:166

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:186

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:198

method

Connection.isPeerClosed

#
pub fn isPeerClosed(self: *Connection) bool

Check whether the remote peer has already closed the streaming connection.

HTTP/1.1 clients commonly half-close the upload side after sending the request; a recv(MSG_PEEK) of zero then looks like a closed socket even though response writes are still valid. Let write failures be the source of truth so long OpenAI-compatible streams do not self-abort.

Parameters
self
Active connection to inspect.
Returns

Always `false` (stub — rely on write-path errors for detection).

src/server/http.zig:211

method

Connection.close

#
pub fn close(self: *Connection) void

Close the underlying TCP stream.

Parameters
self
Connection to close.

src/server/http.zig:218

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:225

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:238

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:248

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:257

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:264