std::net::httpHTTP 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.
import std.net.http;
fn main() {
match http.listen(":8080") {
.Ok(server) => {
let req = server.accept();
req.respond_text(200, "Hello from Hew!").expect("respond_text succeeds");
req.close();
server.close();
}
.Err(_err) => {
println(http.listen_error());
}
}
}
server_portReturn the TCP port selected for this listener.
This is primarily useful after binding to port 0, which asks the kernel
to choose an available ephemeral port.
listen_errorReturn the detail of this actor's most recent HTTP failure.
Reads the same runtime authority as http_client.last_error(); the name
differs only because both files share one package pub-name namespace.
Returns the empty string when the last HTTP call succeeded. Call it
immediately after an Err from listen for the precise, non-secret
reason.
listenCreate a new HTTP server listening on the given address.
The address format is "host:port" or ":port" to listen on all
interfaces.
Returns Err(net.NetError) when the address cannot be bound โ the port is
already in use, the address is not local, or the string is not a valid
endpoint. Use http.listen_error() for the precise detail.
import std.net;
import std.net.http;
fn main() {
match http.listen("127.0.0.1:0") {
.Ok(server) => server.close(),
.Err(_) => println(f"listen failed"),
}
}
respondSend a full HTTP response.
respond_textSend a plain-text HTTP response.
respond_jsonSend a JSON HTTP response.
respond_streamBegin a streaming HTTP response.
ServerAn HTTP server bound to a TCP address.
Created by calling http.listen(addr), which returns Err when the
address cannot be bound. Use accept() to wait for
incoming requests in a loop, then call close() before the value goes
out of scope.
RequestAn 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. Requests close automatically at scope exit; call
close() to release one early.
ServerMethodsMethods available on an HTTP Server.
Block until a client connects and return the next Request.
Set the per-request body read deadline in milliseconds.
RequestMethodsMethods available on an HTTP Request.
Return the HTTP method (e.g. "GET", "POST").
Return the request path (e.g. "/api/users").
Return the value of the named header, or an empty 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.
Return the request body, decoded according to encoding.
Text is decoded as UTF-8. The encoding argument is reserved; binary data that is not UTF-8 cannot be returned as a string.
Send a full HTTP response with status, content-type, and body.
The Content-Length header is derived from body automatically.
Send a plain-text HTTP response.
import std.net.http;
fn main() {
match http.listen(":8080") {
.Ok(server) => {
let req = server.accept();
req.respond_text(200, "OK").expect("respond_text succeeds");
req.close();
server.close();
}
.Err(_err) => println(http.listen_error()),
}
}
Send a JSON HTTP response with Content-Type: application/json.
Begin a streaming response and return a Sink for chunked output.
Each sink.send(chunk) sends data to the client immediately.
Call sink.close() to finish the response.
import std.net.http;
fn main() {
match http.listen(":8080") {
.Ok(server) => {
let req = server.accept();
let body = req.respond_stream(200, "text/plain").expect("begin response");
body.send("chunk 1").expect("send");
body.send("chunk 2").expect("send");
body.close();
server.close();
}
.Err(_err) => println(http.listen_error()),
}
}