Skip to content

Http

Module: std.http
Source: std/http.zn

Usage Example

use std.http;
fn main() {
let addr = SocketAddr.from_str("127.0.0.1:8080").unwrap();
let server = HttpServer.bind(&addr).unwrap();
@println("serving on http://{}", addr);
while (true) {
switch (server.accept()) {
.Ok(stream) => {
let _ = HttpServer.read_request(&stream).unwrap();
let _ = Response.ok("Hello World\n").send(&stream).unwrap();
},
.Err(e) => @println("accept: {}", e.kind()),
};
};
}

Method

HTTP request method


Get,
Post,
Put,
Delete,
Patch,
Head,
Options,
Unknown,

HttpServer

Tiny HTTP server over TcpListener


Fields:

listener: TcpListener,

Constructors:

pub fn bind(addr: *const SocketAddr) Result[Self, IoError];

Getters:

pub fn accept(*const self) Result[TcpStream, IoError];
pub fn read_request(stream: *TcpStream) Result[Request, IoError];

Header

One Name: value header line


Fields:

pub name: String,
pub value: String,

Headers

Header map. Lookup is case insensitive and clones the value


Fields:

inner: List[Header],

Methods:

pub fn push(*self, name: String, value: String);
pub fn get(*const self, name: []const char) Option[String];

Request

Parsed HTTP request


Fields:

pub method: Method,
pub path: String,
pub headers: Headers,
pub body: String,

Methods:

pub fn header(*const self, name: []const char) Option[String];

Response

HTTP response builder


Fields:

status: u16,
body: String,

Constructors:

pub fn new(status: u16, body: []const char) Self;
pub fn empty(status: u16) Self;

Getters:

pub fn status_code(*const self) u16;

Setters:

pub fn set_status_code(*self, code: u16);

Named Constructors:

pub fn ok(body: []const char) Self;
pub fn not_found() Self;
pub fn continue_() Self;
pub fn switching_protocols() Self;
pub fn processing() Self;
pub fn early_hints() Self;
pub fn created(body: []const char) Self;
pub fn accepted(body: []const char) Self;
pub fn non_authoritative_information(body: []const char) Self;
pub fn no_content() Self;
pub fn reset_content() Self;
pub fn partial_content(body: []const char) Self;
pub fn multi_status(body: []const char) Self;
pub fn already_reported(body: []const char) Self;
pub fn im_used(body: []const char) Self;
pub fn multiple_choices(body: []const char) Self;
pub fn moved_permanently(body: []const char) Self;
pub fn found(body: []const char) Self;
pub fn see_other(body: []const char) Self;
pub fn not_modified() Self;
pub fn use_proxy(body: []const char) Self;
pub fn temporary_redirect(body: []const char) Self;
pub fn permanent_redirect(body: []const char) Self;
pub fn bad_request(body: []const char) Self;
pub fn unauthorized(body: []const char) Self;
pub fn payment_required(body: []const char) Self;
pub fn forbidden(body: []const char) Self;
pub fn method_not_allowed(body: []const char) Self;
pub fn not_acceptable(body: []const char) Self;
pub fn proxy_authentication_required(body: []const char) Self;
pub fn request_timeout(body: []const char) Self;
pub fn conflict(body: []const char) Self;
pub fn gone(body: []const char) Self;
pub fn payload_too_large(body: []const char) Self;
pub fn im_a_teapot(body: []const char) Self;
pub fn unprocessable_content(body: []const char) Self;
pub fn locked(body: []const char) Self;
pub fn failed_dependency(body: []const char) Self;
pub fn too_early(body: []const char) Self;
pub fn upgrade_required(body: []const char) Self;
pub fn precondition_required(body: []const char) Self;
pub fn too_many_requests(body: []const char) Self;
pub fn request_header_fields_too_large(body: []const char) Self;
pub fn unavailable_for_legal_reasons(body: []const char) Self;
pub fn internal_server_error(body: []const char) Self;
pub fn not_implemented(body: []const char) Self;
pub fn bad_gateway(body: []const char) Self;
pub fn service_unavailable(body: []const char) Self;
pub fn gateway_timeout(body: []const char) Self;
pub fn http_version_not_supported(body: []const char) Self;
pub fn variant_also_negotiates(body: []const char) Self;
pub fn insufficient_storage(body: []const char) Self;
pub fn loop_detected(body: []const char) Self;
pub fn not_extended(body: []const char) Self;
pub fn network_authentication_required(body: []const char) Self;

Send:

pub fn send(*self, stream: *TcpStream) Result[void, IoError];