42 releases
0.3.17 | May 30, 2024 |
---|---|
0.3.15 | Mar 22, 2024 |
0.3.9 | Dec 24, 2023 |
0.3.6 | Nov 23, 2023 |
0.1.8 | Jul 26, 2021 |
#1307 in HTTP server
2,114 downloads per month
Used in 50 crates
(11 directly)
255KB
5K
SLoC
Welcome to Trillium!
📖 Guide 📖
The guide provides an architectural overview and lay of the land connecting the trillium crates.
📑 Rustdocs 📑
The rustdocs represent the best way to learn about any of trillium's individual crates and the specific interfaces.
Legal:
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
lib.rs
:
This crate provides the http 1.x implementation for Trillium.
Stability
As this is primarily intended for internal use by the Trillium crate, the api is likely to be less stable than that of the higher level abstractions in Trillium.
Example
This is an elaborate example that demonstrates some of trillium_http
's
capabilities. Please note that trillium itself provides a much more
usable interface on top of trillium_http
, at very little cost.
use async_net::{TcpListener, TcpStream};
use futures_lite::StreamExt;
use stopper::Stopper;
use trillium_http::{Conn, Result};
let stopper = Stopper::new();
let listener = TcpListener::bind(("localhost", 0)).await?;
let port = listener.local_addr()?.port();
let server_stopper = stopper.clone();
let server_handle = smol::spawn(async move {
let mut incoming = server_stopper.stop_stream(listener.incoming());
while let Some(Ok(stream)) = incoming.next().await {
let stopper = server_stopper.clone();
smol::spawn(Conn::map(stream, stopper, |mut conn: Conn<TcpStream>| async move {
conn.set_response_body("hello world");
conn.set_status(200);
conn
})).detach()
}
Result::Ok(())
});
// this example uses the trillium client
// any other http client would work here too
let url = format!("http://localhost:{}/", port);
let client = trillium_client::Client::new(trillium_smol::ClientConfig::default());
let mut client_conn = client.get(&*url).await?;
assert_eq!(client_conn.status().unwrap(), 200);
assert_eq!(client_conn.response_headers().get_str("content-length"), Some("11"));
assert_eq!(
client_conn.response_body().read_string().await?,
"hello world"
);
stopper.stop(); // stop the server after one request
server_handle.await?; // wait for the server to shut down
Dependencies
~7.5MB
~187K SLoC