Skip to content
CryoCryo home
Stdlibnet::http

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

PropertyType
statusStatusCode
versionString
headersHeaders
bodyArray<u8>

Constructors

static new(status: StatusCode) -> 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;

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>;

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;

Install body and set Content-Length to match. Ownership of the array transfers to the 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.

Trait implementations

implement trait Drop for struct Response

Constants

const MAX_BODY:         u64 = 16777216;
const MAX_HEADER_COUNT: u64 = 100;