Skip to content

stdlib/httpView source ↗

http

71 items · generated by scribe fromstdlib/http.fd· synced 2026-11-15

stdlib/http71 items

Contents

HttpRequest

A message as it arrived. For a response, method is the protocol word and target the status code.

pub struct HttpRequest {

Fields:

NameType
methodstr
targetstr
pathstr
querystr
versionstr
namesVec<str>
valsVec<str>
bodystr

Defined at line 36


http_scoped_header

A header of a scoped request, as an owned copy ("" when absent).

pub fn http_scoped_header(req: ScopedRequest, name: str) -> str {

Parameters:

  • req: ScopedRequest
  • name: str

Returns: str

Defined at line 64


http_header

The value of header name, any case, or "".

pub fn http_header(req: HttpRequest, name: str) -> str {

Parameters:

  • req: HttpRequest
  • name: str

Returns: str

Defined at line 85


http_keep_alive

Should the connection stay open after this request?

pub fn http_keep_alive(req: HttpRequest) -> bool {

Parameters:

  • req: HttpRequest

Returns: bool

Defined at line 97


http_parse_head

Parse a head: the first line and the headers.

pub fn http_parse_head(head: str) -> HttpRequest {

Parameters:

  • head: str

Returns: HttpRequest

Defined at line 133


parse_request

Parse raw request text into a JSON string with method, path, version, headers, and body fields.

pub fn parse_request(raw: str) -> str {

Parameters:

  • raw: str

Returns: str

Defined at line 212


build_response

Build an HTTP response from status, content-type, and body.

pub fn build_response(status: i64, content_type: str, body: str) -> str {

Parameters:

  • status: i64
  • content_type: str
  • body: str

Returns: str

Defined at line 232


build_response_headers

A response with extra headers, each “Name: value\r\n”.

pub fn build_response_headers(status: i64, content_type: str, headers: str, body: str) -> str {

Parameters:

  • status: i64
  • content_type: str
  • headers: str
  • body: str

Returns: str

Defined at line 237


status_text

HTTP status codes as strings.

pub fn status_text(code: i64) -> str {

Parameters:

  • code: i64

Returns: str

Defined at line 242


http_set_tls

For module https: how to read, write, close and accept a TLS session.

pub fn http_set_tls(recv: fn(i64, i64, i64, i64) -> i64, send: fn(i64, i64, i64) -> i64, close: fn(i64) -> i64, accept: fn(i64) -> i64) {

Parameters:

  • recv: fn(i64
  • send: fn(i64
  • close: fn(i64) -> i64
  • accept: fn(i64) -> i64

Defined at line 371


http_conn_new

A connection on a connected socket, with a TLS session or 0.

pub fn http_conn_new(fd: i64, tls: i64) -> i64 {

Parameters:

  • fd: i64
  • tls: i64

Returns: i64

Defined at line 379


http_conn_close

pub fn http_conn_close(c: i64) {

Parameters:

  • c: i64

Defined at line 390


http_read_message

Read a head and its body off the connection. method is "" at the end of the input, on a timeout, or when the message is too large. A response with no length reads to the end when to_end is set.

pub fn http_read_message(c: i64, timeout_ms: i64, to_end: bool) -> HttpRequest {

Parameters:

  • c: i64
  • timeout_ms: i64
  • to_end: bool

Returns: HttpRequest

Defined at line 565


http_conn_write

Write the whole string. Returns false if the connection failed.

pub fn http_conn_write(c: i64, data: str) -> bool {

Parameters:

  • c: i64
  • data: str

Returns: bool

Defined at line 596


http_not_found

pub fn http_not_found(req: HttpRequest) -> str {

Parameters:

  • req: HttpRequest

Returns: str

Defined at line 607


http_state_init

Allocate and reset the shared server state; called by the serve paths, safe to call again.

pub fn http_state_init() {

Defined at line 635


http_stop

Ask a running server to stop: the accept loop ends within ~200 ms and live connections drain. Safe to call from any task.

pub fn http_stop() {

Defined at line 648


http_set_health

Turn the built-in /health endpoint on (the default) or off.

pub fn http_set_health(on: bool) {

Parameters:

  • on: bool

Defined at line 655


http_health_handler

/health answers itself; everything else goes to the user’s handler.

pub fn http_health_handler(req: HttpRequest) -> str {

Parameters:

  • req: HttpRequest

Returns: str

Defined at line 667


http_conn_task

Serve one connection until it closes. The TLS handshake is here, so a client that stalls holds up only its own task.

pub fn http_conn_task(c: i64) -> i64 {

Parameters:

  • c: i64

Returns: i64

Defined at line 677


http_accept_task

Take connections off the listening socket, each its own task. On SIGTERM, SIGINT or http_stop() it stops accepting, drains live connections and closes the socket (CO8).

pub fn http_accept_task(fd: i64) -> i64 {

Parameters:

  • fd: i64

Returns: i64

Defined at line 729


http_serve

Serve HTTP on port until the process ends. Returns false if the port cannot be opened.

Nothing can dangle here: what a request and its response hold stays allocated, since handler may have kept it. A long-running server wants http_serve_scoped.

pub fn http_serve(port: i64, handler: fn(HttpRequest) -> str) -> bool {

Parameters:

  • port: i64
  • handler: fn(HttpRequest) -> str

Returns: bool

Defined at line 756


http_serve_scoped

As http_serve, with memory that stays flat: after each response, the request’s strings and the response are freed. The handler borrows the request (ScopedRequest): the compiler rejects a handler that keeps one of its strings, so this needs no promise (release M11; it was @unsafe).

pub fn http_serve_scoped(port: i64, handler: fn(ScopedRequest) -> str) -> bool {

Parameters:

  • port: i64
  • handler: fn(ScopedRequest) -> str

Returns: bool

Defined at line 769


http_set_scoped

For module https: serve scoped requests with handler.

pub fn http_set_scoped(handler: fn(ScopedRequest) -> str) {

Parameters:

  • handler: fn(ScopedRequest) -> str

Defined at line 776


http_listen_tasks

Serve a listening socket from tasks and return: the caller goes on, and they run whenever it waits. /health answers itself (CO8).

pub fn http_listen_tasks(fd: i64, handler: fn(HttpRequest) -> str) {

Parameters:

  • fd: i64
  • handler: fn(HttpRequest) -> str

Defined at line 783


http_serve_on

Serve a listening socket until SIGTERM, SIGINT or http_stop() asks for shutdown; live connections drain and the socket closes (CO8). Over TLS if secure (module https has set it up).

pub fn http_serve_on(fd: i64, secure: bool, handler: fn(HttpRequest) -> str) {

Parameters:

  • fd: i64
  • secure: bool
  • handler: fn(HttpRequest) -> str

Defined at line 794


handle_request

Default handler: echoes the request path.

pub fn handle_request(req: HttpRequest) -> str {

Parameters:

  • req: HttpRequest

Returns: str

Defined at line 805


serve

Serve handle_request on port.

pub fn serve(port: i64) -> void {

Parameters:

  • port: i64

Returns: void

Defined at line 810


http_request_arena_new

Create a new HTTP request arena. Returns a mark for later restore. All allocations made via http_request_alloc will be freed when http_request_arena_free is called.

pub fn http_request_arena_new() -> http_request_arena {

Returns: http_request_arena

Defined at line 843


http_request_alloc

Allocate size bytes within an HTTP request arena. The memory is freed when http_request_arena_free is called.

pub fn http_request_alloc(arena: http_request_arena, size: i64) -> i64 {

Parameters:

  • arena: http_request_arena
  • size: i64

Returns: i64

Defined at line 849


http_request_arena_free

Free an HTTP request arena. All allocations made via http_request_alloc are freed at once. Call this after the response has been sent.

pub fn http_request_arena_free(arena: http_request_arena) -> void {

Parameters:

  • arena: http_request_arena

Returns: void

Defined at line 855


http_parse_request_arena

Parse an HTTP request using a request arena. The parsed request and all intermediate allocations go into the arena. Returns a JSON string with method, path, headers, and body.

pub fn http_parse_request_arena(arena: http_request_arena, raw: str) -> str {

Parameters:

  • arena: http_request_arena
  • raw: str

Returns: str

Defined at line 862


http_build_response_arena

Build an HTTP response using a request arena. The response string and any intermediate allocations go into the arena.

pub fn http_build_response_arena(arena: http_request_arena, status: i64, content_type: str, body: str) -> str {

Parameters:

  • arena: http_request_arena
  • status: i64
  • content_type: str
  • body: str

Returns: str

Defined at line 872


router_new

Create a new router.

pub fn router_new() -> Router {

Returns: Router

Defined at line 898


router_add

Add a route to the router. method: “GET”, “POST”, “PUT”, “DELETE”, etc. path: path pattern (e.g. ”/”, “/users”, “/users/:id”) handler: function to call when the route matches

pub fn router_add(router: & mut Router, method: str, path: str, handler: route_handler) -> void {

Parameters:

  • router: & mut Router
  • method: str
  • path: str
  • handler: route_handler

Returns: void

Defined at line 908


router_dispatch

Match a request against the router. Returns the response string. If no route matches, returns a 404 response.

pub fn router_dispatch(router: Router, method: str, path: str) -> str {

Parameters:

  • router: Router
  • method: str
  • path: str

Returns: str

Defined at line 917


router_dispatch_request

Match a request against the router with the full request JSON. The handler receives the full request JSON string.

pub fn router_dispatch_request(router: Router, request: str) -> str {

Parameters:

  • router: Router
  • request: str

Returns: str

Defined at line 932


apply_middleware

Apply middleware to a handler. Returns a new handler that calls the middleware first, then the original handler. This is a simple composition — for now, middleware functions must be manually composed.

pub fn apply_middleware(handler: route_handler, mw: middleware) -> route_handler {

Parameters:

  • handler: route_handler
  • mw: middleware

Returns: route_handler

Defined at line 959


logging_middleware

Logging middleware — logs the request path before calling the handler.

pub fn logging_middleware(handler: route_handler) -> route_handler {

Parameters:

  • handler: route_handler

Returns: route_handler

Defined at line 964


cors_middleware

CORS middleware — adds CORS headers to the response.

pub fn cors_middleware(handler: route_handler) -> route_handler {

Parameters:

  • handler: route_handler

Returns: route_handler

Defined at line 973


Generated by scribe, the Fides documentation generator