1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//! This crate provides the http implementations for Trillium.
//!
//! ## Stability
//!
//! As this is primarily intended for internal use by the [Trillium
//! crate](https://docs.trillium.rs/trillium), the api is likely to be
//! less stable than that of the higher level abstractions in Trillium.
//!
//! ## Protocol dispatch
//!
//! trillium-http supports HTTP/1.0, HTTP/1.1, HTTP/2, and (via `trillium-quinn`)
//! HTTP/3 on the same listener. The version that a given connection speaks is
//! decided at accept time:
//!
//! | Listener | ALPN result | First bytes | Protocol |
//! |---|---|---|---|
//! | TCP + TLS | `h2` | — | HTTP/2 over TLS |
//! | TCP + TLS | `http/1.1` | — | HTTP/1.1 over TLS |
//! | TCP + TLS | absent or other | match HTTP/2 preface (`PRI * HTTP/2.0…`) | HTTP/2 "prior knowledge" over TLS |
//! | TCP + TLS | absent or other | anything else | HTTP/1.1 over TLS |
//! | TCP, cleartext | — | match HTTP/2 preface | HTTP/2 "prior knowledge" (h2c) |
//! | TCP, cleartext | — | anything else | HTTP/1.x |
//! | QUIC | — | — | HTTP/3 |
//!
//! h2c via the HTTP/1.1 `Upgrade` mechanism (RFC 7540 §3.2, removed in RFC 9113)
//! is **not** supported — if an `Upgrade: h2c` header arrives on an h1 request it
//! is logged and ignored.
//!
//! The TLS acceptors shipped with trillium that advertise ALPN (`trillium-rustls` and
//! `trillium-openssl`) advertise `h2, http/1.1` automatically. `trillium-native-tls`
//! does not surface ALPN. Users with custom TLS configs are responsible for advertising
//! `h2` themselves if h2 is desired. Clients on TLS stacks that don't expose an ALPN
//! knob can still reach h2 via the prior-knowledge preface path — ALPN comes back
//! absent and the server peeks the first 24 bytes.
//!
//! All h2/h3-specific tuning flows through [`HttpConfig`] — see its field
//! documentation for the full list of knobs (stream / connection flow-control
//! windows, max concurrent streams, max frame size, etc.).
//!
//! ## 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.
//!
//! ```
//! fn main() -> trillium_http::Result<()> {
//! smol::block_on(async {
//! use async_net::TcpListener;
//! use futures_lite::StreamExt;
//! use std::sync::Arc;
//! use trillium_http::HttpContext;
//!
//! let context = Arc::new(HttpContext::default());
//! let listener = TcpListener::bind(("localhost", 0)).await?;
//! let local_addr = listener.local_addr().unwrap();
//! let server_handle = smol::spawn({
//! let context = context.clone();
//! async move {
//! let mut incoming = context.swansong().interrupt(listener.incoming());
//!
//! while let Some(Ok(stream)) = incoming.next().await {
//! smol::spawn(context.clone().run(stream, |mut conn| async move {
//! conn.set_response_body("hello world");
//! conn.set_status(200);
//! conn
//! }))
//! .detach()
//! }
//! }
//! });
//!
//! // this example uses the trillium client
//! // any other http client would work here too
//! let client = trillium_client::Client::new(trillium_smol::ClientConfig::default())
//! .with_base(local_addr);
//! let mut client_conn = client.get("/").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"
//! );
//!
//! context.shut_down().await; // stop the server after one request
//! server_handle.await; // wait for the server to shut down
//! Ok(())
//! })
//! }
//! ```
pub
pub use http_compat0;
pub use http_compat1;
pub use ;
pub use Buffer;
pub use Buffer;
pub use BufWriter;
pub use BufWriter;
pub use Conn;
pub use ConnectionStatus;
pub use copy;
pub use copy;
pub use ;
pub use ;
pub use HttpConfig;
pub use ;
pub use Method;
pub use MutCow;
pub use ProtocolSession;
pub use ProtocolSession;
pub use ReceivedBody;
pub use ;
pub use Status;
pub use Swansong;
pub use Synthetic;
pub use ;
pub use Upgrade;
pub use Version;
/// A pre-rendered http response to send when the server is at capacity.
pub const SERVICE_UNAVAILABLE: & = b"HTTP/1.1 503 Service Unavailable\r
Connection: close\r
Content-Length: 0\r
Retry-After: 60\r
\r\n";
/// The version of this crate
pub const CRATE_VERSION: &str = env!;