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

Download history 799/week @ 2024-07-21 821/week @ 2024-07-28 821/week @ 2024-08-04 720/week @ 2024-08-11 460/week @ 2024-08-18 503/week @ 2024-08-25 571/week @ 2024-09-01 737/week @ 2024-09-08 391/week @ 2024-09-15 539/week @ 2024-09-22 654/week @ 2024-09-29 174/week @ 2024-10-06 368/week @ 2024-10-13 471/week @ 2024-10-20 699/week @ 2024-10-27 565/week @ 2024-11-03

2,114 downloads per month
Used in 50 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

~7.5MB
~187K SLoC