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() {
let server = http.listen(":8080");
let req = server.accept();
req.respond_text(200, "Hello from Hew!");
req.free();
server.close();
}
listenCreate a new HTTP server listening on the given address.
The address format is "host:port" or ":port" to listen on all
interfaces.
let server = http.listen(":8080");
server.close();
let server = http.listen("127.0.0.1:3000");
server.close();
try_respondSend a full HTTP response, returning Err(fs.IoError) on failure.
try_respond_textSend a plain-text HTTP response, returning Err(fs.IoError) on failure.
try_respond_jsonSend a JSON HTTP response, returning Err(fs.IoError) on failure.
try_respond_streamBegin a streaming HTTP response, returning Err(fs.IoError) on failure.
ServerAn 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.
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. Call free() before the value goes out of
scope; dropping without free() is a resource leak.
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.
Canonical release path for Server.
Callers MUST invoke close() before the value goes out of scope;
dropping without close() is a resource leak.
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.
Pass "utf-8" for text bodies, or "raw" for binary.
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.
req.respond_text(200, "OK");
req.respond_text(404, "Not Found");
Send a JSON HTTP response with Content-Type: application/json.
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.
let body = req.respond_stream(200, "text/plain");
body.write("chunk 1");
body.write("chunk 2");
body.close();
Send a full HTTP response, returning Err(fs.IoError) on failure.
Send a plain-text HTTP response, returning Err(fs.IoError) on failure.
Send a JSON HTTP response, returning Err(fs.IoError) on failure.
Begin a streaming response, returning Err(fs.IoError) on failure.