Debian Codegraph @

third_party/rust/hyper/src/proto/h2/server.rs

raw
1 use std::error::Error as StdError;
2 use std::marker::Unpin;
3 #[cfg(feature = "runtime")]
4 use std::time::Duration;
5
6 use bytes::Bytes;
7 use h2::server::{Connection, Handshake, SendResponse};
8 use h2::{Reason, RecvStream};
9 use http::{Method, Request};
10 use pin_project_lite::pin_project;
11 use tokio::io::{AsyncRead, AsyncWrite};
12 use tracing::{debug, trace, warn};
13
14 use super::{ping, PipeToSendStream, SendBuf};
15 use crate::body::HttpBody;
16 use crate::common::exec::ConnStreamExec;
17 use crate::common::{date, task, Future, Pin, Poll};
18 use crate::ext::Protocol;
19 use crate::headers;
20 use crate::proto::h2::ping::Recorder;
21 use crate::proto::h2::{H2Upgraded, UpgradedSendStream};
22 use crate::proto::Dispatched;
23 use crate::service::HttpService;
24
25 use crate::upgrade::{OnUpgrade, Pending, Upgraded};
26 use crate::{Body, Response};
27
28 // Our defaults are chosen for the "majority" case, which usually are not
29 // resource constrained, and so the spec default of 64kb can be too limiting
30 // for performance.
31 //
32 // At the same time, a server more often has multiple clients connected, and
33 // so is more likely to use more resources than a client would.
34 const DEFAULT_CONN_WINDOW: u32 = 1024 * 1024; // 1mb
35 const DEFAULT_STREAM_WINDOW: u32 = 1024 * 1024; // 1mb
36 const DEFAULT_MAX_FRAME_SIZE: u32 = 1024 * 16; // 16kb
37 const DEFAULT_MAX_SEND_BUF_SIZE: usize = 1024 * 400; // 400kb
38 // 16 MB "sane default" taken from golang http2
39 const DEFAULT_SETTINGS_MAX_HEADER_LIST_SIZE: u32 = 16 << 20;
40
41 #[derive(Clone, Debug)]
42 pub(crate) struct Config {
43 pub(crate) adaptive_window: bool,
44 pub(crate) initial_conn_window_size: u32,
45 pub(crate) initial_stream_window_size: u32,
46 pub(crate) max_frame_size: u32,
47 pub(crate) enable_connect_protocol: bool,
48 pub(crate) max_concurrent_streams: Option<u32>,
49 #[cfg(feature = "runtime")]
50 pub(crate) keep_alive_interval: Option<Duration>,
51 #[cfg(feature = "runtime")]
52 pub(crate) keep_alive_timeout: Duration,
53 pub(crate) max_send_buffer_size: usize,
54 pub(crate) max_header_list_size: u32,
55 }
56
57 impl Default for Config {
58 fn default() -> Config {
59 Config {
60 adaptive_window: false,
61 initial_conn_window_size: DEFAULT_CONN_WINDOW,
62 initial_stream_window_size: DEFAULT_STREAM_WINDOW,
63 max_frame_size: DEFAULT_MAX_FRAME_SIZE,
64 enable_connect_protocol: false,
65 max_concurrent_streams: None,
66 #[cfg(feature = "runtime")]
67 keep_alive_interval: None,
68 #[cfg(feature = "runtime")]
69 keep_alive_timeout: Duration::from_secs(20),
70 max_send_buffer_size: DEFAULT_MAX_SEND_BUF_SIZE,
71 max_header_list_size: DEFAULT_SETTINGS_MAX_HEADER_LIST_SIZE,
72 }
73 }
74 }
75
76 pin_project! {
77 pub(crate) struct Server<T, S, B, E>
78 where
79 S: HttpService<Body>,
80 B: HttpBody,
81 {
82 exec: E,
83 service: S,
84 state: State<T, B>,
85 }
86 }
87
88 enum State<T, B>
89 where
90 B: HttpBody,
91 {
92 Handshaking {
93 ping_config: ping::Config,
94 hs: Handshake<T, SendBuf<B::Data>>,
95 },
96 Serving(Serving<T, B>),
97 Closed,
98 }
99
100 struct Serving<T, B>
101 where
102 B: HttpBody,
103 {
104 ping: Option<(ping::Recorder, ping::Ponger)>,
105 conn: Connection<T, SendBuf<B::Data>>,
106 closing: Option<crate::Error>,
107 }
108
109 impl<T, S, B, E> Server<T, S, B, E>
110 where
111 T: AsyncRead + AsyncWrite + Unpin,
112 S: HttpService<Body, ResBody = B>,
113 S::Error: Into<Box<dyn StdError + Send + Sync>>,
114 B: HttpBody + 'static,
115 E: ConnStreamExec<S::Future, B>,
116 {
117 pub(crate) fn new(io: T, service: S, config: &Config, exec: E) -> Server<T, S, B, E> {
118 let mut builder = h2::server::Builder::default();
119 builder
120 .initial_window_size(config.initial_stream_window_size)
121 .initial_connection_window_size(config.initial_conn_window_size)
122 .max_frame_size(config.max_frame_size)
123 .max_header_list_size(config.max_header_list_size)
124 .max_send_buffer_size(config.max_send_buffer_size);
125 if let Some(max) = config.max_concurrent_streams {
126 builder.max_concurrent_streams(max);
127 }
128 if config.enable_connect_protocol {
129 builder.enable_connect_protocol();
130 }
131 let handshake = builder.handshake(io);
132
133 let bdp = if config.adaptive_window {
134 Some(config.initial_stream_window_size)
135 } else {
136 None
137 };
138
139 let ping_config = ping::Config {
140 bdp_initial_window: bdp,
141 #[cfg(feature = "runtime")]
142 keep_alive_interval: config.keep_alive_interval,
143 #[cfg(feature = "runtime")]
144 keep_alive_timeout: config.keep_alive_timeout,
145 // If keep-alive is enabled for servers, always enabled while
146 // idle, so it can more aggressively close dead connections.
147 #[cfg(feature = "runtime")]
148 keep_alive_while_idle: true,
149 };
150
151 Server {
152 exec,
153 state: State::Handshaking {
154 ping_config,
155 hs: handshake,
156 },
157 service,
158 }
159 }
160
161 pub(crate) fn graceful_shutdown(&mut self) {
162 trace!("graceful_shutdown");
163 match self.state {
164 State::Handshaking { .. } => {
165 // fall-through, to replace state with Closed
166 }
167 State::Serving(ref mut srv) => {
168 if srv.closing.is_none() {
169 srv.conn.graceful_shutdown();
170 }
171 return;
172 }
173 State::Closed => {
174 return;
175 }
176 }
177 self.state = State::Closed;
178 }
179 }
180
181 impl<T, S, B, E> Future for Server<T, S, B, E>
182 where
183 T: AsyncRead + AsyncWrite + Unpin,
184 S: HttpService<Body, ResBody = B>,
185 S::Error: Into<Box<dyn StdError + Send + Sync>>,
186 B: HttpBody + 'static,
187 E: ConnStreamExec<S::Future, B>,
188 {
189 type Output = crate::Result<Dispatched>;
190
191 fn poll(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> {
192 let me = &mut *self;
193 loop {
194 let next = match me.state {
195 State::Handshaking {
196 ref mut hs,
197 ref ping_config,
198 } => {
199 let mut conn = ready!(Pin::new(hs).poll(cx).map_err(crate::Error::new_h2))?;
200 let ping = if ping_config.is_enabled() {
201 let pp = conn.ping_pong().expect("conn.ping_pong");
202 Some(ping::channel(pp, ping_config.clone()))
203 } else {
204 None
205 };
206 State::Serving(Serving {
207 ping,
208 conn,
209 closing: None,
210 })
211 }
212 State::Serving(ref mut srv) => {
213 ready!(srv.poll_server(cx, &mut me.service, &mut me.exec))?;
214 return Poll::Ready(Ok(Dispatched::Shutdown));
215 }
216 State::Closed => {
217 // graceful_shutdown was called before handshaking finished,
218 // nothing to do here...
219 return Poll::Ready(Ok(Dispatched::Shutdown));
220 }
221 };
222 me.state = next;
223 }
224 }
225 }
226
227 impl<T, B> Serving<T, B>
228 where
229 T: AsyncRead + AsyncWrite + Unpin,
230 B: HttpBody + 'static,
231 {
232 fn poll_server<S, E>(
233 &mut self,
234 cx: &mut task::Context<'_>,
235 service: &mut S,
236 exec: &mut E,
237 ) -> Poll<crate::Result<()>>
238 where
239 S: HttpService<Body, ResBody = B>,
240 S::Error: Into<Box<dyn StdError + Send + Sync>>,
241 E: ConnStreamExec<S::Future, B>,
242 {
243 if self.closing.is_none() {
244 loop {
245 self.poll_ping(cx);
246
247 // Check that the service is ready to accept a new request.
248 //
249 // - If not, just drive the connection some.
250 // - If ready, try to accept a new request from the connection.
251 match service.poll_ready(cx) {
252 Poll::Ready(Ok(())) => (),
253 Poll::Pending => {
254 // use `poll_closed` instead of `poll_accept`,
255 // in order to avoid accepting a request.
256 ready!(self.conn.poll_closed(cx).map_err(crate::Error::new_h2))?;
257 trace!("incoming connection complete");
258 return Poll::Ready(Ok(()));
259 }
260 Poll::Ready(Err(err)) => {
261 let err = crate::Error::new_user_service(err);
262 debug!("service closed: {}", err);
263
264 let reason = err.h2_reason();
265 if reason == Reason::NO_ERROR {
266 // NO_ERROR is only used for graceful shutdowns...
267 trace!("interpreting NO_ERROR user error as graceful_shutdown");
268 self.conn.graceful_shutdown();
269 } else {
270 trace!("abruptly shutting down with {:?}", reason);
271 self.conn.abrupt_shutdown(reason);
272 }
273 self.closing = Some(err);
274 break;
275 }
276 }
277
278 // When the service is ready, accepts an incoming request.
279 match ready!(self.conn.poll_accept(cx)) {
280 Some(Ok((req, mut respond))) => {
281 trace!("incoming request");
282 let content_length = headers::content_length_parse_all(req.headers());
283 let ping = self
284 .ping
285 .as_ref()
286 .map(|ping| ping.0.clone())
287 .unwrap_or_else(ping::disabled);
288
289 // Record the headers received
290 ping.record_non_data();
291
292 let is_connect = req.method() == Method::CONNECT;
293 let (mut parts, stream) = req.into_parts();
294 let (mut req, connect_parts) = if !is_connect {
295 (
296 Request::from_parts(
297 parts,
298 crate::Body::h2(stream, content_length.into(), ping),
299 ),
300 None,
301 )
302 } else {
303 if content_length.map_or(false, |len| len != 0) {
304 warn!("h2 connect request with non-zero body not supported");
305 respond.send_reset(h2::Reason::INTERNAL_ERROR);
306 return Poll::Ready(Ok(()));
307 }
308 let (pending, upgrade) = crate::upgrade::pending();
309 debug_assert!(parts.extensions.get::<OnUpgrade>().is_none());
310 parts.extensions.insert(upgrade);
311 (
312 Request::from_parts(parts, crate::Body::empty()),
313 Some(ConnectParts {
314 pending,
315 ping,
316 recv_stream: stream,
317 }),
318 )
319 };
320
321 if let Some(protocol) = req.extensions_mut().remove::<h2::ext::Protocol>() {
322 req.extensions_mut().insert(Protocol::from_inner(protocol));
323 }
324
325 let fut = H2Stream::new(service.call(req), connect_parts, respond);
326 exec.execute_h2stream(fut);
327 }
328 Some(Err(e)) => {
329 return Poll::Ready(Err(crate::Error::new_h2(e)));
330 }
331 None => {
332 // no more incoming streams...
333 if let Some((ref ping, _)) = self.ping {
334 ping.ensure_not_timed_out()?;
335 }
336
337 trace!("incoming connection complete");
338 return Poll::Ready(Ok(()));
339 }
340 }
341 }
342 }
343
344 debug_assert!(
345 self.closing.is_some(),
346 "poll_server broke loop without closing"
347 );
348
349 ready!(self.conn.poll_closed(cx).map_err(crate::Error::new_h2))?;
350
351 Poll::Ready(Err(self.closing.take().expect("polled after error")))
352 }
353
354 fn poll_ping(&mut self, cx: &mut task::Context<'_>) {
355 if let Some((_, ref mut estimator)) = self.ping {
356 match estimator.poll(cx) {
357 Poll::Ready(ping::Ponged::SizeUpdate(wnd)) => {
358 self.conn.set_target_window_size(wnd);
359 let _ = self.conn.set_initial_window_size(wnd);
360 }
361 #[cfg(feature = "runtime")]
362 Poll::Ready(ping::Ponged::KeepAliveTimedOut) => {
363 debug!("keep-alive timed out, closing connection");
364 self.conn.abrupt_shutdown(h2::Reason::NO_ERROR);
365 }
366 Poll::Pending => {}
367 }
368 }
369 }
370 }
371
372 pin_project! {
373 #[allow(missing_debug_implementations)]
374 pub struct H2Stream<F, B>
375 where
376 B: HttpBody,
377 {
378 reply: SendResponse<SendBuf<B::Data>>,
379 #[pin]
380 state: H2StreamState<F, B>,
381 }
382 }
383
384 pin_project! {
385 #[project = H2StreamStateProj]
386 enum H2StreamState<F, B>
387 where
388 B: HttpBody,
389 {
390 Service {
391 #[pin]
392 fut: F,
393 connect_parts: Option<ConnectParts>,
394 },
395 Body {
396 #[pin]
397 pipe: PipeToSendStream<B>,
398 },
399 }
400 }
401
402 struct ConnectParts {
403 pending: Pending,
404 ping: Recorder,
405 recv_stream: RecvStream,
406 }
407
408 impl<F, B> H2Stream<F, B>
409 where
410 B: HttpBody,
411 {
412 fn new(
413 fut: F,
414 connect_parts: Option<ConnectParts>,
415 respond: SendResponse<SendBuf<B::Data>>,
416 ) -> H2Stream<F, B> {
417 H2Stream {
418 reply: respond,
419 state: H2StreamState::Service { fut, connect_parts },
420 }
421 }
422 }
423
424 macro_rules! reply {
425 ($me:expr, $res:expr, $eos:expr) => {{
426 match $me.reply.send_response($res, $eos) {
427 Ok(tx) => tx,
428 Err(e) => {
429 debug!("send response error: {}", e);
430 $me.reply.send_reset(Reason::INTERNAL_ERROR);
431 return Poll::Ready(Err(crate::Error::new_h2(e)));
432 }
433 }
434 }};
435 }
436
437 impl<F, B, E> H2Stream<F, B>
438 where
439 F: Future<Output = Result<Response<B>, E>>,
440 B: HttpBody,
441 B::Data: 'static,
442 B::Error: Into<Box<dyn StdError + Send + Sync>>,
443 E: Into<Box<dyn StdError + Send + Sync>>,
444 {
445 fn poll2(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<crate::Result<()>> {
446 let mut me = self.project();
447 loop {
448 let next = match me.state.as_mut().project() {
449 H2StreamStateProj::Service {
450 fut: h,
451 connect_parts,
452 } => {
453 let res = match h.poll(cx) {
454 Poll::Ready(Ok(r)) => r,
455 Poll::Pending => {
456 // Response is not yet ready, so we want to check if the client has sent a
457 // RST_STREAM frame which would cancel the current request.
458 if let Poll::Ready(reason) =
459 me.reply.poll_reset(cx).map_err(crate::Error::new_h2)?
460 {
461 debug!("stream received RST_STREAM: {:?}", reason);
462 return Poll::Ready(Err(crate::Error::new_h2(reason.into())));
463 }
464 return Poll::Pending;
465 }
466 Poll::Ready(Err(e)) => {
467 let err = crate::Error::new_user_service(e);
468 warn!("http2 service errored: {}", err);
469 me.reply.send_reset(err.h2_reason());
470 return Poll::Ready(Err(err));
471 }
472 };
473
474 let (head, body) = res.into_parts();
475 let mut res = ::http::Response::from_parts(head, ());
476 super::strip_connection_headers(res.headers_mut(), false);
477
478 // set Date header if it isn't already set...
479 res.headers_mut()
480 .entry(::http::header::DATE)
481 .or_insert_with(date::update_and_header_value);
482
483 if let Some(connect_parts) = connect_parts.take() {
484 if res.status().is_success() {
485 if headers::content_length_parse_all(res.headers())
486 .map_or(false, |len| len != 0)
487 {
488 warn!("h2 successful response to CONNECT request with body not supported");
489 me.reply.send_reset(h2::Reason::INTERNAL_ERROR);
490 return Poll::Ready(Err(crate::Error::new_user_header()));
491 }
492 let send_stream = reply!(me, res, false);
493 connect_parts.pending.fulfill(Upgraded::new(
494 H2Upgraded {
495 ping: connect_parts.ping,
496 recv_stream: connect_parts.recv_stream,
497 send_stream: unsafe { UpgradedSendStream::new(send_stream) },
498 buf: Bytes::new(),
499 },
500 Bytes::new(),
501 ));
502 return Poll::Ready(Ok(()));
503 }
504 }
505
506
507 if !body.is_end_stream() {
508 // automatically set Content-Length from body...
509 if let Some(len) = body.size_hint().exact() {
510 headers::set_content_length_if_missing(res.headers_mut(), len);
511 }
512
513 let body_tx = reply!(me, res, false);
514 H2StreamState::Body {
515 pipe: PipeToSendStream::new(body, body_tx),
516 }
517 } else {
518 reply!(me, res, true);
519 return Poll::Ready(Ok(()));
520 }
521 }
522 H2StreamStateProj::Body { pipe } => {
523 return pipe.poll(cx);
524 }
525 };
526 me.state.set(next);
527 }
528 }
529 }
530
531 impl<F, B, E> Future for H2Stream<F, B>
532 where
533 F: Future<Output = Result<Response<B>, E>>,
534 B: HttpBody,
535 B::Data: 'static,
536 B::Error: Into<Box<dyn StdError + Send + Sync>>,
537 E: Into<Box<dyn StdError + Send + Sync>>,
538 {
539 type Output = ();
540
541 fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> {
542 self.poll2(cx).map(|res| {
543 if let Err(e) = res {
544 debug!("stream error: {}", e);
545 }
546 })
547 }
548 }