Module http

HTTP server for handling web requests.

Provides a blocking HTTP server that listens on a TCP address, accepts incoming requests, and sends responses.

Examples

import std::http;

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

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");
let server = http.listen("127.0.0.1:3000");

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.

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.

Traits

Trait ServerMethods

Methods available on an HTTP Server.

Methods

fn accept(self: Server) -> Request fn close(self: Server)

Trait RequestMethods

Methods available on an HTTP Request.

Methods

fn method(self: Request) -> string fn path(self: Request) -> string fn header(self: Request, name: string) -> string fn body(self: Request, encoding: string) -> string fn respond(self: Request, status: i32, content_type: string, content_length: i64, body: string) -> i32 fn respond_text(self: Request, status: i32, body: string) -> i32 fn respond_json(self: Request, status: i32, json: string) -> i32 fn free(self: Request)