41 releases

0.3.16 Apr 7, 2024
0.3.15 Mar 22, 2024
0.3.14 Feb 8, 2024
0.3.9 Dec 24, 2023
0.1.8 Jul 26, 2021

#759 in HTTP server

Download history 445/week @ 2023-12-22 1487/week @ 2023-12-29 1136/week @ 2024-01-05 1120/week @ 2024-01-12 1303/week @ 2024-01-19 1419/week @ 2024-01-26 1438/week @ 2024-02-02 1291/week @ 2024-02-09 1144/week @ 2024-02-16 882/week @ 2024-02-23 1097/week @ 2024-03-01 1214/week @ 2024-03-08 1727/week @ 2024-03-15 1944/week @ 2024-03-22 2033/week @ 2024-03-29 2124/week @ 2024-04-05

7,930 downloads per month
Used in 48 crates (11 directly)

MIT/Apache

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

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

~7MB
~185K SLoC