trillium_http/
lib.rs

1#![forbid(unsafe_code)]
2#![deny(
3    clippy::dbg_macro,
4    missing_copy_implementations,
5    rustdoc::missing_crate_level_docs,
6    missing_debug_implementations,
7    nonstandard_style,
8    unused_qualifications
9)]
10#![warn(missing_docs, clippy::pedantic, clippy::perf, clippy::cargo)]
11#![allow(clippy::must_use_candidate, clippy::module_name_repetitions)]
12/*!
13This crate provides the http 1.x implementation for Trillium.
14
15## Stability
16
17As this is primarily intended for internal use by the [Trillium
18crate](https://docs.trillium.rs/trillium), the api is likely to be
19less stable than that of the higher level abstractions in Trillium.
20
21## Example
22
23This is an elaborate example that demonstrates some of `trillium_http`'s
24capabilities.  Please note that trillium itself provides a much more
25usable interface on top of `trillium_http`, at very little cost.
26
27```
28# fn main() -> trillium_http::Result<()> { smol::block_on(async {
29use async_net::{TcpListener, TcpStream};
30use futures_lite::StreamExt;
31use stopper::Stopper;
32use trillium_http::{Conn, Result};
33
34let stopper = Stopper::new();
35let listener = TcpListener::bind(("localhost", 0)).await?;
36let port = listener.local_addr()?.port();
37
38let server_stopper = stopper.clone();
39let server_handle = smol::spawn(async move {
40    let mut incoming = server_stopper.stop_stream(listener.incoming());
41
42    while let Some(Ok(stream)) = incoming.next().await {
43        let stopper = server_stopper.clone();
44        smol::spawn(Conn::map(stream, stopper, |mut conn: Conn<TcpStream>| async move {
45            conn.set_response_body("hello world");
46            conn.set_status(200);
47            conn
48         })).detach()
49    }
50
51    Result::Ok(())
52});
53
54// this example uses the trillium client
55// any other http client would work here too
56let url = format!("http://localhost:{}/", port);
57let client = trillium_client::Client::new(trillium_smol::ClientConfig::default());
58let mut client_conn = client.get(&*url).await?;
59
60assert_eq!(client_conn.status().unwrap(), 200);
61assert_eq!(client_conn.response_headers().get_str("content-length"), Some("11"));
62assert_eq!(
63    client_conn.response_body().read_string().await?,
64    "hello world"
65);
66
67stopper.stop(); // stop the server after one request
68server_handle.await?; // wait for the server to shut down
69# Result::Ok(()) }) }
70```
71*/
72
73mod received_body;
74pub use received_body::ReceivedBody;
75
76#[cfg(feature = "unstable")]
77pub use received_body::ReceivedBodyState;
78
79mod error;
80pub use error::{Error, Result};
81
82mod conn;
83pub use conn::{Conn, SERVER};
84
85mod connection_status;
86pub use connection_status::ConnectionStatus;
87
88mod synthetic;
89pub use synthetic::Synthetic;
90
91mod upgrade;
92pub use upgrade::Upgrade;
93
94pub use stopper::Stopper;
95
96mod mut_cow;
97pub(crate) use mut_cow::MutCow;
98
99mod util;
100
101mod body;
102pub use body::Body;
103
104mod state_set;
105pub use state_set::StateSet;
106
107mod headers;
108pub use headers::{HeaderName, HeaderValue, HeaderValues, Headers, KnownHeaderName};
109
110mod status;
111pub use status::Status;
112
113mod method;
114pub use method::Method;
115
116mod version;
117pub use version::Version;
118
119/// Types to represent the bidirectional data stream over which the
120/// HTTP protocol is communicated
121pub mod transport;
122
123/// A pre-rendered http response to send when the server is at capacity.
124pub const SERVICE_UNAVAILABLE: &[u8] = b"HTTP/1.1 503 Service Unavailable\r
125Connection: close\r
126Content-Length: 0\r
127Retry-After: 60\r
128\r\n";
129
130#[cfg(feature = "http-compat-1")]
131pub mod http_compat1;
132
133#[cfg(feature = "http-compat")]
134pub mod http_compat0;
135
136#[cfg(feature = "http-compat")]
137pub use http_compat0 as http_compat; // for semver
138
139mod bufwriter;
140pub(crate) use bufwriter::BufWriter;
141
142mod http_config;
143pub use http_config::HttpConfig;
144
145pub(crate) mod after_send;
146
147mod buffer;
148#[cfg(feature = "unstable")]
149pub use buffer::Buffer;
150#[cfg(not(feature = "unstable"))]
151pub(crate) use buffer::Buffer;
152
153mod copy;
154#[cfg(feature = "unstable")]
155pub use copy::copy;
156#[cfg(not(feature = "unstable"))]
157pub(crate) use copy::copy;
158
159mod liveness;