Struct trillium_http::Conn[][src]

pub struct Conn<Transport> { /* fields omitted */ }
Expand description

A http connection

Unlike in other rust http implementations, this struct represents both the request and the response, and holds the transport over which the response will be sent.

Implementations

read any number of new Conns from the transport and call the provided handler function until either the connection is closed or an upgrade is requested. A return value of Ok(None) indicates a closed connection, while a return value of Ok(Some(upgrade)) represents an upgrade.

See the documentation for Conn for a full example.

returns a read-only reference to the state typemap for this conn

stability note: this is not unlikely to be removed at some point, as this may end up being more of a trillium concern than a trillium_http concern

returns a mutable reference to the state typemap for this conn

stability note: this is not unlikely to be removed at some point, as this may end up being more of a trillium concern than a trillium_http concern

returns an immutable reference to the request headers. it is not currently possible to mutate request headers for a conn that has been read from a transport. For synthetic conns, use Conn::request_headers_mut

returns a mutable reference to the response headers

sets the http status code from any TryInto<StatusCode>.

Note: This currently will set the s

assert!(conn.status().is_none());

conn.set_status(200); // a status can be set as a u16
assert_eq!(conn.status().unwrap(), StatusCode::Ok);

conn.set_status(StatusCode::ImATeapot); // or as a StatusCode
assert_eq!(conn.status().unwrap(), StatusCode::ImATeapot);

retrieves the current response status code for this conn, if it has been set. See Conn::set_status for example usage.

retrieves the path part of the request url, up to and excluding any query component

let mut conn = Conn::new_synthetic(Method::Get, "/some/path?and&a=query", ());
assert_eq!(conn.path(), "/some/path");

Sets the response body to anything that is impl Into<Body>.

conn.set_response_body("hello");
conn.set_response_body(String::from("hello"));
conn.set_response_body(vec![99, 97, 116]);

returns a reference to the current response body, if it has been set

remove the response body from this conn and return it

assert!(conn.response_body().is_none());
conn.set_response_body("hello");
assert!(conn.response_body().is_some());
let body = conn.take_response_body();
assert!(body.is_some());
assert!(conn.response_body().is_none());

returns the http method for this conn’s request.

let mut conn = Conn::new_synthetic(Method::Get, "/some/path?and&a=query", ());
assert_eq!(conn.method(), Method::Get);

returns the encoding_rs::Encoding for this request, as determined from the mime-type charset, if available

let mut conn = Conn::new_synthetic(Method::Get, "/", ());
assert_eq!(conn.request_encoding(), encoding_rs::WINDOWS_1252); // the default
conn.request_headers_mut().insert("content-type", "text/plain;charset=utf-16");
assert_eq!(conn.request_encoding(), encoding_rs::UTF_16LE);

returns the encoding_rs::Encoding for this response, as determined from the mime-type charset, if available

let mut conn = Conn::new_synthetic(Method::Get, "/", ());
assert_eq!(conn.response_encoding(), encoding_rs::WINDOWS_1252); // the default
conn.response_headers().insert("content-type", "text/plain;charset=utf-16");
assert_eq!(conn.response_encoding(), encoding_rs::UTF_16LE);

returns a ReceivedBody that references this conn. the conn retains all data and holds the singular transport, but the ReceivedBody provides an interface to read body content

let mut conn = Conn::new_synthetic(Method::Get, "/", "hello");
let request_body = conn.request_body().await;
assert_eq!(request_body.content_length(), Some(5));
assert_eq!(request_body.read_string().await.unwrap(), "hello");

returns a clone of the stopper::Stopper for this Conn. use this to gracefully stop long-running futures and streams inside of handler functions

Create a new Conn from the provided transport, as well as any bytes that have already been read from the transport, and a Stopper instance that will be used to signal graceful shutdown.

predicate function to indicate whether the connection is secure. note that this does not necessarily indicate that the transport itself is secure, as it may indicate that trillium_http is behind a trusted reverse proxy that has terminated tls and provided appropriate headers to indicate this.

calculates any auto-generated headers for this conn prior to sending it

applies a mapping function from one transport to another. This is particularly useful for boxing the transport. unless you’re sure this is what you’re looking for, you probably don’t want to be using this

Construct a new synthetic conn with provided method, path, and body.

let conn = Conn::new_synthetic(Method::Get, "/", "hello");
assert_eq!(conn.method(), Method::Get);
assert_eq!(conn.path(), "/");

A Conn provides the ability to mutate request headers with request_headers_mut. This is only provided on synthetic requests for now, since it doesn’t generally make sense to mutate headers for a request that is read from an io transport.

let mut conn = Conn::new_synthetic(Method::Get, "/", "hello");
conn.request_headers_mut().insert("content-type", "application/json");
assert_eq!(conn.request_headers()["content-type"], "application/json");

Replaces the synthetic body. This is intended for testing use.

Trait Implementations

Formats the value using the given formatter. Read more

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.