response
import std::net::http::response; · source
Types
Response
type struct Response {
status: StatusCode;
version: String;
headers: Headers;
body: Array<u8>;
static new(status: StatusCode) -> Response;
static text(status: StatusCode, body: Str) -> Response;
static json(status: StatusCode, body: Array<u8>) -> Response;
set_body(mut &this, body: Array<u8>) -> void;
static from_status_line(line: Str) -> Result<Response, IoError>;
encode_into(&this, out: Array<u8>*) -> Result<(), IoError>;
}
Response::new(status) is the bare form; Response::text(status, body) and Response::json(status, body) set the body and the matching Content-Type. set_body installs a body and sets Content-Length to match; encode_into(out) writes the complete HTTP/1.1 message.
function health(req: Request) -> Response {
return Response::text(StatusCode::ok(), Str::new("ok"));
}
from_status_line parses the first line of an incoming response; the header and body reads that follow it live in conn. MAX_BODY is the hard cap against a runaway or attacker-controlled Content-Length, and MAX_HEADER_COUNT bounds the header block.
Properties
| Property | Type |
|---|---|
status | StatusCode |
version | String |
headers | Headers |
body | Array<u8> |
Constructors
static new(status: StatusCode) -> Response;
static text(status: StatusCode, body: Str) -> Response;
static text(status: StatusCode, body: Str) -> Response;Convenience: Response::text(StatusCode::ok(), "hello") with Content-Type text/plain; charset=utf-8 and Content-Length set.
static json(status: StatusCode, body: Array<u8>) -> Response;
static json(status: StatusCode, body: Array<u8>) -> Response;Convenience for JSON: sets Content-Type and Content-Length. The caller is responsible for producing valid JSON; no validation is performed.
static from_status_line(line: Str) -> Result<Response, IoError>;
static from_status_line(line: Str) -> Result<Response, IoError>;Parse a Status-Line (HTTP/x.y SP code SP reason, no CRLF) into a response with empty headers and no body. A missing code or a non-numeric one is InvalidData. The reason phrase is advisory and discarded; the code is what callers dispatch on.
Methods
set_body(mut &this, body: Array<u8>) -> void;
set_body(mut &this, body: Array<u8>) -> void;Install body and set Content-Length to match. Ownership of the array transfers to the response.
encode_into(&this, out: Array<u8>*) -> Result<(), IoError>;
encode_into(&this, out: Array<u8>*) -> Result<(), IoError>;Append the whole message — status line, headers, blank line, body — to out, normally the connection's pending() buffer.
Trait implementations
implement trait Drop for struct Response
Constants
const MAX_BODY: u64 = 16777216;
const MAX_HEADER_COUNT: u64 = 100;type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct Response {
status: StatusCode;
version: String;
headers: Headers;
body: Array<u8>;
}HTTP response.
Response.status: StatusCodetype struct StatusCode {
code: u16;
}Response.version: Stringtype struct String<A = GlobalAlloc> {
buffer: RawBuffer<u8, A>;
length: u64;
}String: owned, heap-allocated, length-typed UTF-8.
std::collections::stringResponse.headers: Headerstype struct Headers {
inner: HashMap<String, String>;
}HTTP headers: case-insensitive name/value map.
std::net::http::headersResponse.body: Array<u8>type struct Array<T, A = GlobalAlloc> where A: Allocator {
ptr: T*;
length: i64;
capacity: i64;
alloc: A;
}Array<T, A>: growable, heap-backed, contiguous sequence.
std::collections::arraystatic name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static Response::new(status: StatusCode) -> Response(parameter) status: StatusCodetype struct Response {
status: StatusCode;
version: String;
headers: Headers;
body: Array<u8>;
}HTTP response.
std::net::http::responsestatic Response::text(status: StatusCode, body: Str) -> ResponseConvenience: Response::text(StatusCode::ok(), "hello") with Content-Type text/plain; charset=utf-8 and Content-Length set.
(parameter) body: Strtype struct Str {
bytes: u8*;
length: u64;
}Str: a borrowed UTF-8 string slice.
std::collections::strstatic Response::json(status: StatusCode, body: Array<u8>) -> ResponseConvenience for JSON: sets Content-Type and Content-Length. The caller is responsible for producing valid JSON; no validation is performed.
(parameter) body: Array<u8>Response::set_body(mut &this, body: Array<u8>) -> voidInstall body and set Content-Length to match. Ownership of the array transfers to the response.
mut &this
mut &TypeAn exclusive, mutating borrow. As a receiver, mut &this lets the method modify the fields - and callers can see that from the signature alone. As a parameter type, mut &T lets the callee mutate through the reference while the caller keeps ownership.
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
static Response::from_status_line(line: Str) -> Result<Response, IoError>Parse a Status-Line (HTTP/x.y SP code SP reason, no CRLF) into a response with empty headers and no body. A missing code or a non-numeric one is InvalidData. The reason phrase is advisory and discarded; the code is what callers dispatch on.
(parameter) line: Strtype struct IoError {
kind: IoErrorKind;
os_code: i32;
}Response::encode_into(&this, out: Array<u8>*) -> Result<(), IoError>Append the whole message — status line, headers, blank line, body — to out, normally the connection's pending() buffer.
(parameter) out: Array<u8>*primitive ()The unit type: a value that carries no information, and the empty tuple. Distinct from void, which is the absence of a value - Result<(), E> succeeds with nothing to return. 0 bytes.
function name<T>(param: Type) -> Ret { ... }Declares a function. Parameters are written name: Type and are never inferred; the return type follows -> and defaults to void. Declaration order does not matter: every signature is collected before any body is checked, so functions may call each other freely and recurse.
function health(req: Request) -> Response(parameter) req: Requesttype struct Request {
method: Method;
path: String;
version: String;
headers: Headers;
body: Array<u8>;
params: HashMap<String, String>;
}HTTP request.
std::net::http::requestreturn value;Leaves the current function with the given value. A function whose return type is not void must return one; a bare return; is for void. In an async function it completes the future with the value.
static Response::text(status: StatusCode, body: Str) -> ResponseConvenience: Response::text(StatusCode::ok(), "hello") with Content-Type text/plain; charset=utf-8 and Content-Length set.
static StatusCode::ok() -> StatusCodestatic Str::new(text: string) -> StrConstruct from a NUL-terminated C-style string literal. The caller certifies that text is valid UTF-8 and outlives this view. The NUL terminator is not included in the view.
const NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const MAX_BODY: u64 = 16777216;const MAX_HEADER_COUNT: u64 = 100;import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
static Response::new(status: StatusCode) -> ResponseResponse.status: StatusCodeResponse.body: Array<u8>static Response::json(status: StatusCode, body: Array<u8>) -> ResponseConvenience for JSON: sets Content-Type and Content-Length. The caller is responsible for producing valid JSON; no validation is performed.
std::net::http::responseResponse::set_body(mut &this, body: Array<u8>) -> voidInstall body and set Content-Length to match. Ownership of the array transfers to the response.
Response::encode_into(&this, out: Array<u8>*) -> Result<(), IoError>Append the whole message — status line, headers, blank line, body — to out, normally the connection's pending() buffer.
static Response::from_status_line(line: Str) -> Result<Response, IoError>Parse a Status-Line (HTTP/x.y SP code SP reason, no CRLF) into a response with empty headers and no body. A missing code or a non-numeric one is InvalidData. The reason phrase is advisory and discarded; the code is what callers dispatch on.
const MAX_BODY: u64 = 16777216;const MAX_HEADER_COUNT: u64 = 100;Response.version: StringResponse.headers: HeadersIoErrorKind::InvalidDataimplement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement trait Trait for Type { ... }Introduces a trait implementation: the block supplies, for one concrete type, the method bodies the trait requires.
language referenceimplement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language reference