pub struct Conn { /* private fields */ }Expand description
§A Trillium HTTP connection.
A Conn represents both the request and response of a http connection, as well as any application state that is associated with that connection.
§with_{attribute} naming convention
A convention that is used throughout trillium is that any interface
that is named with_{attribute} will take ownership of the conn, set
the attribute and return the conn, enabling chained calls like:
use trillium_testing::TestServer;
struct MyState(&'static str);
async fn handler(mut conn: trillium::Conn) -> trillium::Conn {
conn.with_response_header("content-type", "text/plain")
.with_state(MyState("hello"))
.with_body("hey there")
.with_status(418)
}
let app = TestServer::new(handler).await;
app.get("/")
.await
.assert_status(418)
.assert_body("hey there")
.assert_header("content-type", "text/plain");If you need to set a property on the conn without moving it,
set_{attribute} associated functions will be your huckleberry, as is
conventional in other rust projects.
§State
Every trillium Conn contains a state type which is a set that contains at most one element for
each type. State is the primary way that handlers attach data to a conn as it passes through a
tuple handler. State access should generally be implemented by libraries using a private type
and exposed with a ConnExt trait. See library
patterns for more elaboration and examples.
§In relation to trillium_http::Conn
trillium::Conn is currently implemented as an abstraction on top of a
trillium_http::Conn. In particular, trillium::Conn boxes the transport so that application
code can be written without transport generics. See
Transport for further reading on this.
Implementations§
Source§impl Conn
impl Conn
Sourcepub fn ok(self, body: impl Into<Body>) -> Conn
pub fn ok(self, body: impl Into<Body>) -> Conn
Conn::ok is a convenience function for the common pattern of
setting a body and a 200 status in one call. It is exactly
identical to conn.with_status(200).with_body(body).halt()
See Body::new_streaming and Body::new_with_trailers to construct a body from an
AsyncRead or BodySource
use trillium::Conn;
use trillium_testing::TestServer;
let handler = |conn: Conn| async move { conn.ok("hello") };
let app = TestServer::new(handler).await;
app.get("/").await.assert_ok().assert_body("hello");Sourcepub fn status(&self) -> Option<Status>
pub fn status(&self) -> Option<Status>
returns the response status for this Conn, if it has been set.
use trillium::Conn;
use trillium_testing::TestServer;
let handler = |mut conn: Conn| async move {
assert!(conn.status().is_none());
conn.set_status(200);
assert_eq!(conn.status().unwrap(), trillium_http::Status::Ok);
conn.ok("pass")
};
let app = TestServer::new(handler).await;
app.get("/").await.assert_ok();Sourcepub fn set_status(&mut self, status: impl TryInto<Status>) -> &mut Conn
pub fn set_status(&mut self, status: impl TryInto<Status>) -> &mut Conn
assigns a status to this response. see Conn::status for example usage
Sourcepub fn with_status(self, status: impl TryInto<Status>) -> Conn
pub fn with_status(self, status: impl TryInto<Status>) -> Conn
sets the response status for this Conn and returns it. note that
this does not set the halted status.
use trillium::Conn;
use trillium_testing::TestServer;
let handler = |conn: Conn| async move { conn.with_status(418) };
let app = TestServer::new(handler).await;
app.get("/").await.assert_status(418);Sourcepub fn with_body(self, body: impl Into<Body>) -> Conn
pub fn with_body(self, body: impl Into<Body>) -> Conn
Sets the response body from any impl Into<Body> and returns the
Conn for fluent chaining. Note that this does not set the response
status or halted. See Conn::ok for a function that does both
of those.
See Body::new_streaming and Body::new_with_trailers to construct a body from an
AsyncRead or BodySource
use trillium::Conn;
use trillium_testing::TestServer;
let handler = |conn: Conn| async move { conn.with_body("hello") };
let app = TestServer::new(handler).await;
app.get("/").await.assert_body_contains("hello");Sourcepub fn set_body(&mut self, body: impl Into<Body>) -> &mut Conn
pub fn set_body(&mut self, body: impl Into<Body>) -> &mut Conn
Sets the response body from any impl Into<Body>. Note that this does not set the response
status or halted.
See Body::new_streaming and Body::new_with_trailers to construct a body from an
AsyncRead or BodySource
use trillium::Conn;
use trillium_testing::TestServer;
let handler = |mut conn: Conn| async move {
conn.set_body("hello");
assert_eq!(conn.response_len(), Some(5));
conn.ok("pass")
};
let app = TestServer::new(handler).await;
app.get("/").await.assert_ok();Sourcepub fn take_response_body(&mut self) -> Option<Body>
pub fn take_response_body(&mut self) -> Option<Body>
Removes the response body from the Conn
use trillium::Conn;
use trillium_testing::TestServer;
let handler = |mut conn: Conn| async move {
conn.set_body("hello");
let body = conn.take_response_body().unwrap();
assert_eq!(body.len(), Some(5));
assert_eq!(conn.response_len(), None);
conn.ok("pass")
};
let app = TestServer::new(handler).await;
app.get("/").await.assert_ok();Sourcepub fn response_body(&self) -> Option<&Body>
pub fn response_body(&self) -> Option<&Body>
Borrows the response body from the Conn
use trillium::Conn;
use trillium_testing::TestServer;
let handler = |mut conn: Conn| async move {
conn.set_body("hello");
let body = conn.response_body().unwrap();
assert_eq!(body.len(), Some(5));
assert!(body.is_static());
assert_eq!(body.static_bytes(), Some(&b"hello"[..]));
conn.ok("pass")
};
let app = TestServer::new(handler).await;
app.get("/").await.assert_ok();Sourcepub fn state<T>(&self) -> Option<&T>
pub fn state<T>(&self) -> Option<&T>
Attempts to retrieve a &T from the state set
use trillium::Conn;
use trillium_testing::TestServer;
struct Hello;
let handler = |mut conn: Conn| async move {
assert!(conn.state::<Hello>().is_none());
conn.insert_state(Hello);
assert!(conn.state::<Hello>().is_some());
conn.ok("pass")
};
let app = TestServer::new(handler).await;
app.get("/").await.assert_ok();Sourcepub fn state_mut<T>(&mut self) -> Option<&mut T>
pub fn state_mut<T>(&mut self) -> Option<&mut T>
Attempts to retrieve a &mut T from the state set
Sourcepub fn insert_state<T>(&mut self, state: T) -> Option<T>
pub fn insert_state<T>(&mut self, state: T) -> Option<T>
Inserts a new type into the state set. See Conn::state
for an example.
Returns the previously-set instance of this type, if any
Sourcepub fn with_state<T>(self, state: T) -> Conn
pub fn with_state<T>(self, state: T) -> Conn
Puts a new type into the state set and returns the
Conn. this is useful for fluent chaining
Sourcepub fn take_state<T>(&mut self) -> Option<T>
pub fn take_state<T>(&mut self) -> Option<T>
Removes a type from the state set and returns it, if present
Sourcepub fn state_entry<T>(&mut self) -> Entry<'_, T>
pub fn state_entry<T>(&mut self) -> Entry<'_, T>
Returns an Entry for the state typeset that can be used with functions like
Entry::or_insert, Entry::or_insert_with, Entry::and_modify, and others.
Attempts to borrow a T from the immutable shared state set
Sourcepub async fn request_body(&mut self) -> ReceivedBody<'_, Box<dyn Transport>>
pub async fn request_body(&mut self) -> ReceivedBody<'_, Box<dyn Transport>>
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.
See also: Conn::request_body_string for a convenience function
when the content is expected to be utf8.
§Examples
use trillium::Conn;
use trillium_testing::TestServer;
let handler = |mut conn: Conn| async move {
let request_body = conn.request_body().await;
assert_eq!(request_body.content_length(), Some(12));
assert_eq!(request_body.read_string().await.unwrap(), "request body");
conn.ok("pass")
};
let app = TestServer::new(handler).await;
app.post("/").with_body("request body").await.assert_ok();Sourcepub async fn request_body_string(&mut self) -> Result<String, Error>
pub async fn request_body_string(&mut self) -> Result<String, Error>
Convenience function to read the content of a request body as a String.
§Errors
This will return an error variant if either there is an IO failure on the underlying transport or if the body content is not a utf8 string.
§Examples
use trillium::Conn;
use trillium_testing::TestServer;
let handler = |mut conn: Conn| async move {
assert_eq!(conn.request_body_string().await.unwrap(), "request body");
conn.ok("pass")
};
let app = TestServer::new(handler).await;
app.post("/").with_body("request body").await.assert_ok();Sourcepub fn response_len(&self) -> Option<u64>
pub fn response_len(&self) -> Option<u64>
if there is a response body for this conn and it has a known fixed length, it is returned from this function
use trillium::Conn;
use trillium_testing::TestServer;
let handler = |conn: Conn| async move { conn.with_body("hello") };
let app = TestServer::new(handler).await;
app.get("/").await.assert_body_contains("hello");Sourcepub fn method(&self) -> Method
pub fn method(&self) -> Method
returns the request method for this conn.
use trillium::Conn;
use trillium_http::Method;
use trillium_testing::TestServer;
let handler = |conn: Conn| async move {
assert_eq!(conn.method(), Method::Get);
conn.ok("pass")
};
let app = TestServer::new(handler).await;
app.get("/").await.assert_ok();Sourcepub fn response_headers(&self) -> &Headers
pub fn response_headers(&self) -> &Headers
Borrow the response headers
Sourcepub fn response_headers_mut(&mut self) -> &mut Headers
pub fn response_headers_mut(&mut self) -> &mut Headers
Mutably borrow the response headers
Sourcepub fn request_headers(&self) -> &Headers
pub fn request_headers(&self) -> &Headers
Borrow the request headers
Sourcepub fn request_headers_mut(&mut self) -> &mut Headers
pub fn request_headers_mut(&mut self) -> &mut Headers
Mutably borrow request headers
Sourcepub fn request_trailers(&self) -> Option<&Headers>
pub fn request_trailers(&self) -> Option<&Headers>
Borrow the request trailers, if any
Trailers are only populated after reading a request body that includes trailers to completion.
Sourcepub fn with_response_header(
self,
header_name: impl Into<HeaderName<'static>>,
header_value: impl Into<HeaderValues>,
) -> Conn
pub fn with_response_header( self, header_name: impl Into<HeaderName<'static>>, header_value: impl Into<HeaderValues>, ) -> Conn
Insert a header name and value/values into the response headers and return the conn.
See also Headers::insert and Headers::append
For a slight performance improvement, use a KnownHeaderName as
the first argument instead of a str.
Sourcepub fn insert_response_header(
&mut self,
header_name: impl Into<HeaderName<'static>>,
header_value: impl Into<HeaderValues>,
)
pub fn insert_response_header( &mut self, header_name: impl Into<HeaderName<'static>>, header_value: impl Into<HeaderValues>, )
Insert a header name and value/values into the response headers.
See also Headers::insert and Headers::append
For a slight performance improvement, use a KnownHeaderName.
Sourcepub fn path(&self) -> &str
pub fn path(&self) -> &str
returns the path for this request. note that this may not represent the entire http request path if running nested routers.
Sourcepub fn querystring(&self) -> &str
pub fn querystring(&self) -> &str
returns query part of the request path
use trillium::Conn;
use trillium_testing::TestServer;
let handler = |conn: Conn| async move {
let querystring = conn.querystring();
if querystring == "c&d=e" {
conn.ok("has query")
} else {
conn.ok("no query")
}
};
let app = TestServer::new(handler).await;
app.get("/a/b?c&d=e").await.assert_body("has query");
app.get("/a/b").await.assert_body("no query");§Parsing
Trillium does not include a querystring parsing library, as there is no universal standard for querystring encodings of arrays, but several library options exist, inluding:
QueryStrong (by the author of trillium)
serde_qs
querystring
serde_querystring
Sourcepub const fn halt(self) -> Conn
pub const fn halt(self) -> Conn
sets the halted attribute of this conn, preventing later
processing in a given tuple handler. returns
the conn for fluent chaining
use trillium::Conn;
use trillium_testing::TestServer;
let handler = |conn: Conn| async move { conn.halt() };
let app = TestServer::new(handler).await;
app.get("/").await.assert_status(404);Sourcepub const fn set_halted(&mut self, halted: bool) -> &mut Conn
pub const fn set_halted(&mut self, halted: bool) -> &mut Conn
sets the halted attribute of this conn. see Conn::halt.
use trillium::Conn;
use trillium_testing::TestServer;
let handler = |mut conn: Conn| async move {
assert!(!conn.is_halted());
conn.set_halted(true);
assert!(conn.is_halted());
conn.ok("pass")
};
let app = TestServer::new(handler).await;
app.get("/").await.assert_ok();Sourcepub const fn is_halted(&self) -> bool
pub const fn is_halted(&self) -> bool
retrieves the halted state of this conn. see Conn::halt.
Sourcepub fn is_secure(&self) -> bool
pub fn is_secure(&self) -> bool
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.
Sourcepub fn start_time(&self) -> Instant
pub fn start_time(&self) -> Instant
The Instant that the first header bytes for this conn were
received, before any processing or parsing has been performed.
Sourcepub fn into_inner<T>(self) -> Conn<T>where
T: Transport,
pub fn into_inner<T>(self) -> Conn<T>where
T: Transport,
transforms this trillium::Conn into a trillium_http::Conn
with the specified transport type. Please note that this will
panic if you attempt to downcast from trillium’s boxed
transport into the wrong transport type. Also note that this
is a lossy conversion, dropping the halted state and any
nested router path data.
§Panics
This will panic if you attempt to downcast to the wrong Transport type.
Sourcepub fn peer_ip(&self) -> Option<IpAddr>
pub fn peer_ip(&self) -> Option<IpAddr>
retrieves the remote ip address for this conn, if available.
Sourcepub fn set_peer_ip(&mut self, peer_ip: Option<IpAddr>) -> &mut Conn
pub fn set_peer_ip(&mut self, peer_ip: Option<IpAddr>) -> &mut Conn
sets the remote ip address for this conn.
Sourcepub fn push_path(&mut self, path: String)
pub fn push_path(&mut self, path: String)
for router implementations. pushes a route segment onto the path
Sourcepub async fn cancel_on_disconnect<'a, Fut>(
&'a mut self,
fut: Fut,
) -> Option<<Fut as Future>::Output>
pub async fn cancel_on_disconnect<'a, Fut>( &'a mut self, fut: Fut, ) -> Option<<Fut as Future>::Output>
Cancels and drops the future if reading from the transport results in an error or empty read
If the client disconnects from the conn’s transport, this function will return None. If the future completes without disconnection, this future will return Some containing the output of the future.
The use of this method is not advised if your connected http client employs pipelining (rarely seen in the wild), as it will buffer an unbounded number of requests
Note that the inner future cannot borrow conn, so you will need to clone or take any information needed to execute the future prior to executing this method.
§Example
async fn something_slow_and_cancel_safe() -> String {
String::from("this was not actually slow")
}
async fn handler(mut conn: Conn) -> Conn {
match conn
.cancel_on_disconnect(async { something_slow_and_cancel_safe().await })
.await
{
Some(returned_body) => conn.ok(returned_body),
None => conn,
}
}Sourcepub async fn is_disconnected(&mut self) -> bool
pub async fn is_disconnected(&mut self) -> bool
Check if the transport is connected by testing attempting to read from the transport
§Example
This is best to use at appropriate points in a long-running handler, like:
async fn handler(mut conn: Conn) -> Conn {
for _ in 0..100 {
if conn.is_disconnected().await {
return conn;
}
something_slow_but_not_cancel_safe().await;
}
conn.ok("ok!")
}Sourcepub fn http_version(&self) -> Version
pub fn http_version(&self) -> Version
Returns the http version over which this Conn is being communicated
Sourcepub fn path_and_query(&self) -> &str
pub fn path_and_query(&self) -> &str
retrieves the combined path and any query