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(
12 clippy::must_use_candidate,
13 clippy::module_name_repetitions,
14 clippy::multiple_crate_versions
15)]
16//! This crate provides the http implementations for Trillium.
17//!
18//! ## Stability
19//!
20//! As this is primarily intended for internal use by the [Trillium
21//! crate](https://docs.trillium.rs/trillium), the api is likely to be
22//! less stable than that of the higher level abstractions in Trillium.
23//!
24//! ## Example
25//!
26//! This is an elaborate example that demonstrates some of `trillium_http`'s
27//! capabilities. Please note that trillium itself provides a much more
28//! usable interface on top of `trillium_http`, at very little cost.
29//!
30//! ```
31//! fn main() -> trillium_http::Result<()> {
32//! smol::block_on(async {
33//! use async_net::TcpListener;
34//! use futures_lite::StreamExt;
35//! use std::sync::Arc;
36//! use trillium_http::HttpContext;
37//!
38//! let context = Arc::new(HttpContext::default());
39//! let listener = TcpListener::bind(("localhost", 0)).await?;
40//! let local_addr = listener.local_addr().unwrap();
41//! let server_handle = smol::spawn({
42//! let context = context.clone();
43//! async move {
44//! let mut incoming = context.swansong().interrupt(listener.incoming());
45//!
46//! while let Some(Ok(stream)) = incoming.next().await {
47//! smol::spawn(context.clone().run(stream, |mut conn| async move {
48//! conn.set_response_body("hello world");
49//! conn.set_status(200);
50//! conn
51//! }))
52//! .detach()
53//! }
54//! }
55//! });
56//!
57//! // this example uses the trillium client
58//! // any other http client would work here too
59//! let client = trillium_client::Client::new(trillium_smol::ClientConfig::default())
60//! .with_base(local_addr);
61//! let mut client_conn = client.get("/").await?;
62//!
63//! assert_eq!(client_conn.status().unwrap(), 200);
64//! assert_eq!(
65//! client_conn.response_headers().get_str("content-length"),
66//! Some("11")
67//! );
68//! assert_eq!(
69//! client_conn.response_body().read_string().await?,
70//! "hello world"
71//! );
72//!
73//! context.shut_down().await; // stop the server after one request
74//! server_handle.await; // wait for the server to shut down
75//! Ok(())
76//! })
77//! }
78//! ```
79
80#[cfg(test)]
81#[doc = include_str!("../README.md")]
82mod readme {}
83
84pub(crate) mod after_send;
85mod body;
86mod buffer;
87mod bufwriter;
88mod conn;
89mod connection_status;
90mod copy;
91mod error;
92pub mod h3;
93pub mod headers;
94#[cfg(feature = "http-compat-0")]
95pub mod http_compat0;
96#[cfg(feature = "http-compat-1")]
97pub mod http_compat1;
98mod http_config;
99mod http_context;
100mod liveness;
101mod method;
102mod mut_cow;
103mod received_body;
104mod status;
105mod synthetic;
106mod upgrade;
107mod util;
108mod version;
109
110pub use body::{Body, BodySource};
111#[cfg(feature = "unstable")]
112#[doc(hidden)]
113pub use buffer::Buffer;
114#[cfg(not(feature = "unstable"))]
115pub(crate) use buffer::Buffer;
116#[cfg(feature = "unstable")]
117pub use bufwriter::BufWriter;
118#[cfg(not(feature = "unstable"))]
119pub(crate) use bufwriter::BufWriter;
120pub use conn::Conn;
121pub use connection_status::ConnectionStatus;
122#[cfg(feature = "unstable")]
123#[doc(hidden)]
124pub use copy::copy;
125#[cfg(not(feature = "unstable"))]
126pub(crate) use copy::copy;
127pub use error::{Error, Result};
128pub use headers::{HeaderName, HeaderValue, HeaderValues, Headers, KnownHeaderName, SERVER_HEADER};
129pub use http_config::HttpConfig;
130pub use http_context::HttpContext;
131pub use method::Method;
132pub(crate) use mut_cow::MutCow;
133pub use received_body::ReceivedBody;
134#[cfg(feature = "unstable")]
135#[doc(hidden)]
136pub use received_body::{H3BodyFrameType, ReceivedBodyState};
137pub use status::Status;
138pub use swansong::Swansong;
139#[doc(hidden)]
140pub use synthetic::Synthetic;
141pub use type_set::{self, TypeSet};
142pub use upgrade::Upgrade;
143pub use version::Version;
144
145/// A pre-rendered http response to send when the server is at capacity.
146pub const SERVICE_UNAVAILABLE: &[u8] = b"HTTP/1.1 503 Service Unavailable\r
147Connection: close\r
148Content-Length: 0\r
149Retry-After: 60\r
150\r\n";
151
152/// The version of this crate
153pub const CRATE_VERSION: &str = env!("CARGO_PKG_VERSION");