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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
use crate::{
Buffer, Conn, Headers, HttpContext, Method, ProtocolSession, Status, TypeSet, Version,
h2::H2Connection, h3::H3Connection, received_body::read_buffered,
};
use fieldwork::Fieldwork;
use futures_lite::{AsyncRead, AsyncWrite};
use std::{
borrow::Cow,
fmt::{self, Debug, Formatter},
io,
net::IpAddr,
pin::Pin,
str,
sync::Arc,
task::{self, Poll},
time::Instant,
};
use trillium_macros::AsyncWrite;
/// This struct represents a http upgrade. It contains all of the data available on a Conn, as well
/// as owning the underlying transport.
///
/// **Important implementation note**: When reading directly from the transport, ensure that you
/// read from `buffer` first if there are bytes in it. Alternatively, read directly from the
/// Upgrade, as that [`AsyncRead`] implementation will drain the buffer first before reading from
/// the transport.
#[derive(AsyncWrite, Fieldwork)]
#[fieldwork(get, get_mut, set, with, take, into_field, rename_predicates)]
pub struct Upgrade<Transport> {
/// The http request headers
request_headers: Headers,
/// The http response headers as set on the underlying [`Conn`] before the upgrade was
/// negotiated. These have already been sent to the peer; preserved here for inspection.
response_headers: Headers,
/// The request path
#[field(get = false)]
path: Cow<'static, str>,
/// The http request method
#[field(copy)]
method: Method,
/// Any state that has been accumulated on the Conn before negotiating the upgrade
state: TypeSet,
/// The underlying io (often a `TcpStream` or similar)
#[async_write]
transport: Transport,
/// Any bytes that have been read from the underlying transport already.
///
/// It is your responsibility to process these bytes before reading directly from the
/// transport.
#[field(deref = "[u8]", into_field = false, set = false, with = false)]
buffer: Buffer,
/// The [`HttpContext`] shared for this server
#[field(deref = false)]
context: Arc<HttpContext>,
/// the ip address of the connection, if available
#[field(copy)]
peer_ip: Option<IpAddr>,
/// the wall-clock time at which the underlying [`Conn`] was constructed. Useful for
/// instrumentation that wants elapsed time across the upgrade transition.
#[field(copy)]
start_time: Instant,
/// the :authority http/3 pseudo-header
authority: Option<Cow<'static, str>>,
/// the :scheme http/3 pseudo-header
scheme: Option<Cow<'static, str>>,
/// the [`ProtocolSession`] for this upgrade — bundles the per-protocol session state
/// (h2/h3 connection driver and stream id) that was attached to the originating Conn.
/// `Http1` for upgrades from h1 / synthetic conns.
#[field = false]
protocol_session: ProtocolSession,
/// the :protocol http/3 pseudo-header
protocol: Option<Cow<'static, str>>,
/// the http version
#[field = "http_version"]
version: Version,
/// the http response status set on the underlying [`Conn`] at the time the upgrade was
/// negotiated (typically `101 Switching Protocols` or `200 OK` for CONNECT). `None` if no
/// status was set explicitly.
#[field(copy)]
status: Option<Status>,
/// whether this connection was deemed secure by the handler stack
secure: bool,
}
impl<Transport> Upgrade<Transport> {
#[doc(hidden)]
pub fn new(
request_headers: Headers,
path: impl Into<Cow<'static, str>>,
method: Method,
transport: Transport,
buffer: Buffer,
version: Version,
) -> Self {
Self {
request_headers,
response_headers: Headers::new(),
path: path.into(),
method,
transport,
buffer,
state: TypeSet::new(),
context: Arc::default(),
peer_ip: None,
start_time: Instant::now(),
authority: None,
scheme: None,
protocol_session: ProtocolSession::Http1,
protocol: None,
secure: false,
version,
status: None,
}
}
/// the [`H2Connection`] driver for this upgrade, if it originated from an HTTP/2 stream
pub fn h2_connection(&self) -> Option<&Arc<H2Connection>> {
self.protocol_session.h2_connection()
}
/// the h2 stream id for this upgrade, if it originated from an HTTP/2 stream
pub fn h2_stream_id(&self) -> Option<u32> {
self.protocol_session.h2_stream_id()
}
/// the [`H3Connection`] driver for this upgrade, if it originated from an HTTP/3 stream
pub fn h3_connection(&self) -> Option<&Arc<H3Connection>> {
self.protocol_session.h3_connection()
}
/// the h3 stream id for this upgrade, if it originated from an HTTP/3 stream
pub fn h3_stream_id(&self) -> Option<u64> {
self.protocol_session.h3_stream_id()
}
/// Take any buffered bytes
pub fn take_buffer(&mut self) -> Vec<u8> {
std::mem::take(&mut self.buffer).into()
}
#[doc(hidden)]
pub fn buffer_and_transport_mut(&mut self) -> (&mut Buffer, &mut Transport) {
(&mut self.buffer, &mut self.transport)
}
/// borrow the shared state [`TypeSet`] for this application
pub fn shared_state(&self) -> &TypeSet {
self.context.shared_state()
}
/// the http request path up to but excluding any query component
pub fn path(&self) -> &str {
match self.path.split_once('?') {
Some((path, _)) => path,
None => &self.path,
}
}
/// retrieves the query component of the path
pub fn querystring(&self) -> &str {
self.path
.split_once('?')
.map(|(_, query)| query)
.unwrap_or_default()
}
/// Modify the transport type of this upgrade.
///
/// This is useful for boxing the transport in order to erase the type argument.
pub fn map_transport<T: AsyncRead + AsyncWrite + Send + Sync + Unpin + 'static>(
self,
f: impl Fn(Transport) -> T,
) -> Upgrade<T> {
// Manual respread: rustc treats `Upgrade<Transport>` and `Upgrade<T>` as disjoint
// and rejects `..self` without the unstable `type_changing_struct_update` feature.
// If a new field is added to `Upgrade`, update this respread, `Conn::map_transport`
// (`conn.rs`), and `From<Conn> for Upgrade` below — they share this drift hazard.
Upgrade {
transport: f(self.transport),
path: self.path,
method: self.method,
state: self.state,
buffer: self.buffer,
request_headers: self.request_headers,
response_headers: self.response_headers,
context: self.context,
peer_ip: self.peer_ip,
start_time: self.start_time,
authority: self.authority,
scheme: self.scheme,
protocol_session: self.protocol_session,
protocol: self.protocol,
version: self.version,
status: self.status,
secure: self.secure,
}
}
}
impl<Transport> Debug for Upgrade<Transport> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct(&format!("Upgrade<{}>", std::any::type_name::<Transport>()))
.field("request_headers", &self.request_headers)
.field("response_headers", &self.response_headers)
.field("path", &self.path)
.field("method", &self.method)
.field("buffer", &self.buffer)
.field("context", &self.context)
.field("state", &self.state)
.field("transport", &format_args!(".."))
.field("peer_ip", &self.peer_ip)
.field("start_time", &self.start_time)
.field("authority", &self.authority)
.field("scheme", &self.scheme)
.field("protocol_session", &self.protocol_session)
.field("protocol", &self.protocol)
.field("version", &self.version)
.field("status", &self.status)
.field("secure", &self.secure)
.finish()
}
}
impl<Transport> From<Conn<Transport>> for Upgrade<Transport> {
fn from(conn: Conn<Transport>) -> Self {
// Exhaustive destructure (no `..` rest pattern) so that adding a new field to
// `Conn` is a compile error here, forcing a deliberate carry-vs-drop decision
// for the upgrade transition. The discarded fields below are response-body /
// request-body / instrumentation state that is meaningless once the conn has
// crossed into the upgrade phase. This shares a drift hazard with
// `Conn::map_transport` (`conn.rs`) and `Upgrade::map_transport` above.
let Conn {
request_headers,
response_headers,
path,
method,
state,
transport,
buffer,
context,
peer_ip,
start_time,
authority,
scheme,
protocol_session,
protocol,
version,
status,
secure,
// Deliberately dropped — response-body / request-body lifecycle state with
// no role on the upgraded transport.
response_body: _,
request_body_state: _,
after_send: _,
request_trailers: _,
} = conn;
Self {
request_headers,
response_headers,
path,
method,
state,
transport,
buffer,
context,
peer_ip,
start_time,
authority,
scheme,
protocol_session,
protocol,
version,
status,
secure,
}
}
}
impl<Transport: AsyncRead + Unpin> AsyncRead for Upgrade<Transport> {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut task::Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
let Self {
transport, buffer, ..
} = &mut *self;
read_buffered(buffer, transport, cx, buf)
}
}