pkg:http/server

HTTP/1.1 server over raw TCP sockets.

import "pkg:http/server"

Overview

Provides a simple HTTP server toolkit. Binds to an address, accepts connections, parses incoming HTTP requests, and sends HTTP responses. Built on vera:net for sockets, vera:memory for buffer operations, and vera:string for byte scanning and parsing.

Dependencies: vera:net, vera:memory, vera:string

Functions

http_server_start

function http_server_start(ip: ptr, ip_len: i32, port: i32) -> i32

Create a TCP server socket, bind to the given address, and start listening for connections. Enables address reuse (SO_REUSEADDR) so the server can restart quickly. Uses a listen backlog of 128.

Parameters:

  • ip (ptr) — pointer to null-terminated IP string (e.g., "0.0.0.0\0")
  • ip_len (i32) — length of the IP string, including the null terminator
  • port (i32) — TCP port to listen on

Returns: the server socket file descriptor, or -1 on error.


http_server_accept

function http_server_accept(server_fd: i32) -> i32

Accept an incoming client connection. Blocks until a client connects.

Parameters:

  • server_fd (i32) — the server socket returned by http_server_start

Returns: the client socket file descriptor, or -1 on error.


http_read_request

function http_read_request(client_fd: i32, buf: ptr, buf_size: i64) -> i64

Read an HTTP request from a client socket. Reads until the end-of-headers marker (\r\n\r\n) is found and any Content-Length body bytes have been received, or buf_size bytes have been read.

Parameters:

  • client_fd (i32) — the client socket to read from
  • buf (ptr) — caller-allocated buffer for the request
  • buf_size (i64) — size of buf in bytes

Returns: total bytes read (headers + body), or -1 on error (client disconnected before sending any data).


http_request_method

function http_request_method(buf: ptr, len: i64) -> i32

Extract the HTTP method from a request buffer. Parses the first token of the request line.

Parameters:

  • buf (ptr) — pointer to the raw HTTP request
  • len (i64) — length of the request in bytes

Returns: an integer identifying the HTTP method:

ValueMethod
1GET
2POST
3PUT
4DELETE
0unknown

http_request_path

function http_request_path(buf: ptr, len: i64, out_buf: ptr, out_buf_size: i64) -> i64

Extract the request path from an HTTP request. Parses "METHOD /path HTTP/1.1\r\n..." and copies the path into out_buf. If the path is longer than out_buf_size, it is clamped.

Parameters:

  • buf (ptr) — pointer to the raw HTTP request
  • len (i64) — length of the request in bytes
  • out_buf (ptr) — caller-allocated buffer for the extracted path
  • out_buf_size (i64) — size of out_buf in bytes

Returns: length of the path copied into out_buf, or -1 on parse error.


http_request_body

function http_request_body(buf: ptr, len: i64) -> i64

Find the byte offset where the request body begins. Searches for the \r\n\r\n separator and returns the offset immediately after it.

Parameters:

  • buf (ptr) — pointer to the raw HTTP request
  • len (i64) — length of the request in bytes

Returns: byte offset where the body starts, or -1 if headers are not complete.


http_send_response

function http_send_response(client_fd: i32, status: i32, content_type: ptr, ct_len: i64, body: ptr, body_len: i64) -> i64

Send an HTTP response with status line, headers, and body. Automatically includes Content-Type, Content-Length, and Connection: close headers. Supported status codes: 200, 400, 404, 500.

Parameters:

  • client_fd (i32) — the client socket to send on
  • status (i32) — HTTP status code (200, 400, 404, or 500)
  • content_type (ptr) — pointer to Content-Type value string (e.g., "text/html")
  • ct_len (i64) — length of the content type string
  • body (ptr) — pointer to the response body bytes
  • body_len (i64) — length of the body in bytes

Returns: total bytes sent (headers + body), or -1 on error.


http_send_error

function http_send_error(client_fd: i32, status: i32) -> i64

Send a canned error response with a plain text body. Supports status codes 400, 404, and 500. Any unrecognized code defaults to 500.

Parameters:

  • client_fd (i32) — the client socket to send on
  • status (i32) — HTTP status code (400, 404, or 500)

Returns: total bytes sent, or -1 on error.