Module std::net::http

HTTP server for handling web requests.

Provides a blocking HTTP server that listens on a TCP address, accepts incoming requests, and sends responses. Request-body reads time out after 30 seconds by default so slow trickle-feed clients cannot hold the server indefinitely.

Examples

import std::net::http;

fn main() {
    let server = http.listen(":8080");
    let req = server.accept();
    req.respond_text(200, "Hello from Hew!");
    req.free();
    server.close();
}

Contents

Functions

Function listen

pub fn listen(addr: string) -> Server

Create a new HTTP server listening on the given address.

The address format is "host:port" or ":port" to listen on all interfaces.

Examples

let server = http.listen(":8080");
server.close();
let server = http.listen("127.0.0.1:3000");
server.close();

Function try_respond

pub fn try_respond(req: Request, status: i64, content_type: string, body: string) -> Result<i64, fs.IoError>

Send a full HTTP response, returning Err(fs.IoError) on failure.

Function try_respond_text

pub fn try_respond_text(req: Request, status: i64, body: string) -> Result<i64, fs.IoError>

Send a plain-text HTTP response, returning Err(fs.IoError) on failure.

Function try_respond_json

pub fn try_respond_json(req: Request, status: i64, json: string) -> Result<i64, fs.IoError>

Send a JSON HTTP response, returning Err(fs.IoError) on failure.

Function try_respond_stream

pub fn try_respond_stream(req: Request, status: i64, content_type: string) -> Result<Sink<string>, fs.IoError>

Begin a streaming HTTP response, returning Err(fs.IoError) on failure.

Types

Struct Server

An HTTP server bound to a TCP address.

Created by calling http.listen(addr). Use accept() to wait for incoming requests in a loop, then call close() before the value goes out of scope.

Struct Request

An incoming HTTP request.

Obtained from server.accept(). Provides accessors for the request method, path, headers, and body. Must be responded to with one of the respond methods. Call free() before the value goes out of scope; dropping without free() is a resource leak.

Traits

Trait ServerMethods

Methods available on an HTTP Server.

Methods

fn accept(self: Self) -> Request

Block until a client connects and return the next Request.

fn set_request_timeout_ms(self: Self, timeout_ms: i64) -> i64

Set the per-request body read deadline in milliseconds.

fn close(self: Self)

Canonical release path for Server.

Callers MUST invoke close() before the value goes out of scope; dropping without close() is a resource leak.

Trait RequestMethods

Methods available on an HTTP Request.

Methods

fn method(self: Self) -> string

Return the HTTP method (e.g. "GET", "POST").

fn path(self: Self) -> string

Return the request path (e.g. "/api/users").

fn header(self: Self, name: string) -> string

Return the value of the named header, or an empty string.

fn headers(self: Self) -> Vec<(string, string)>

Return all request headers as a list of (name, value) pairs.

The returned vector is owned by the caller; Hew frees it automatically when it goes out of scope. Returns an empty vector if no headers were present or the request is invalid.

fn body(self: Self, encoding: string) -> string

Return the request body, decoded according to encoding.

Pass "utf-8" for text bodies, or "raw" for binary.

fn respond(self: Self, status: i64, content_type: string, body: string) -> i64

Send a full HTTP response with status, content-type, and body.

The Content-Length header is derived from body automatically.

fn respond_text(self: Self, status: i64, body: string) -> i64

Send a plain-text HTTP response.

Examples
req.respond_text(200, "OK");
req.respond_text(404, "Not Found");
fn respond_json(self: Self, status: i64, json: string) -> i64

Send a JSON HTTP response with Content-Type: application/json.

fn respond_stream(self: Self, status: i64, content_type: string) -> Sink<string>

Begin a streaming response and return a Sink for chunked output.

Each sink.write(chunk) sends data to the client immediately. Call sink.close() to finish the response.

Examples
let body = req.respond_stream(200, "text/plain");
body.write("chunk 1");
body.write("chunk 2");
body.close();
fn try_respond(self: Self, status: i64, content_type: string, body: string) -> Result<i64, fs.IoError>

Send a full HTTP response, returning Err(fs.IoError) on failure.

fn try_respond_text(self: Self, status: i64, body: string) -> Result<i64, fs.IoError>

Send a plain-text HTTP response, returning Err(fs.IoError) on failure.

fn try_respond_json(self: Self, status: i64, json: string) -> Result<i64, fs.IoError>

Send a JSON HTTP response, returning Err(fs.IoError) on failure.

fn try_respond_stream(self: Self, status: i64, content_type: string) -> Result<Sink<string>, fs.IoError>

Begin a streaming response, returning Err(fs.IoError) on failure.