Skip to main content

actix_http/h2/
dispatcher.rs

1use std::{
2    cmp,
3    error::Error as StdError,
4    future::Future,
5    marker::PhantomData,
6    net,
7    pin::{pin, Pin},
8    rc::Rc,
9    task::{Context, Poll},
10};
11
12use actix_codec::{AsyncRead, AsyncWrite};
13use actix_service::Service;
14use actix_utils::future::poll_fn;
15use bytes::{Bytes, BytesMut};
16use futures_core::ready;
17use h2::{
18    server::{Connection, SendResponse},
19    Ping, PingPong,
20};
21use pin_project_lite::pin_project;
22use tokio::time::{sleep, Sleep};
23
24use crate::{
25    body::{BodySize, BoxBody, MessageBody},
26    config::{GracefulShutdownFuture, ServiceConfig},
27    header::{
28        HeaderName, HeaderValue, CONNECTION, CONTENT_LENGTH, DATE, TRANSFER_ENCODING, UPGRADE,
29    },
30    service::HttpFlow,
31    Extensions, Method, OnConnectData, Payload, Request, Response, ResponseHead,
32};
33
34const CHUNK_SIZE: usize = 16_384;
35
36pin_project! {
37    /// Dispatcher for HTTP/2 protocol.
38    pub struct Dispatcher<T, S, B, X, U> {
39        flow: Rc<HttpFlow<S, X, U>>,
40        connection: Connection<T, Bytes>,
41        conn_data: Option<Rc<Extensions>>,
42        config: ServiceConfig,
43        peer_addr: Option<net::SocketAddr>,
44        ping_pong: Option<H2PingPong>,
45        graceful_shutdown: Option<GracefulShutdownFuture>,
46        _phantom: PhantomData<B>
47    }
48}
49
50impl<T, S, B, X, U> Dispatcher<T, S, B, X, U>
51where
52    T: AsyncRead + AsyncWrite + Unpin,
53{
54    pub(crate) fn new(
55        mut conn: Connection<T, Bytes>,
56        flow: Rc<HttpFlow<S, X, U>>,
57        config: ServiceConfig,
58        peer_addr: Option<net::SocketAddr>,
59        conn_data: OnConnectData,
60        timer: Option<Pin<Box<Sleep>>>,
61    ) -> Self {
62        let ping_pong = config.keep_alive().duration().map(|dur| H2PingPong {
63            timer: timer
64                .map(|mut timer| {
65                    // reuse timer slot if it was initialized for handshake
66                    timer.as_mut().reset((config.now() + dur).into());
67                    timer
68                })
69                .unwrap_or_else(|| Box::pin(sleep(dur))),
70            in_flight: false,
71            ping_pong: conn.ping_pong().unwrap(),
72        });
73
74        let graceful_shutdown = config.graceful_shutdown();
75
76        Self {
77            flow,
78            config,
79            peer_addr,
80            connection: conn,
81            conn_data: conn_data.0.map(Rc::new),
82            ping_pong,
83            graceful_shutdown,
84            _phantom: PhantomData,
85        }
86    }
87
88    /// Starts a graceful shutdown when the server shutdown signal is ready.
89    fn poll_shutdown(&mut self, cx: &mut Context<'_>) {
90        let shutdown = self
91            .graceful_shutdown
92            .as_mut()
93            .is_some_and(|signal| signal.as_mut().poll(cx).is_ready());
94
95        if shutdown {
96            self.graceful_shutdown = None;
97
98            // Sends GOAWAY, then a second GOAWAY with the last processed stream ID after a PING
99            // round trip. `poll_accept` keeps driving in-flight streams and returns `None` once
100            // they have all finished.
101            self.connection.graceful_shutdown();
102        }
103    }
104}
105
106struct H2PingPong {
107    /// Handle to send ping frames from the peer.
108    ping_pong: PingPong,
109
110    /// True when a ping has been sent and is waiting for a reply.
111    in_flight: bool,
112
113    /// Timeout for pong response.
114    timer: Pin<Box<Sleep>>,
115}
116
117impl<T, S, B, X, U> Future for Dispatcher<T, S, B, X, U>
118where
119    T: AsyncRead + AsyncWrite + Unpin,
120
121    S: Service<Request>,
122    S::Error: Into<Response<BoxBody>>,
123    S::Future: 'static,
124    S::Response: Into<Response<B>>,
125
126    B: MessageBody,
127{
128    type Output = Result<(), crate::error::DispatchError>;
129
130    #[inline]
131    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
132        let this = self.get_mut();
133
134        this.poll_shutdown(cx);
135
136        loop {
137            match Pin::new(&mut this.connection).poll_accept(cx)? {
138                Poll::Ready(Some((req, tx))) => {
139                    let (parts, body) = req.into_parts();
140                    let payload = crate::h2::Payload::new(body);
141                    let pl = Payload::H2 { payload };
142                    let mut req = Request::with_payload(pl);
143                    let head_req = parts.method == Method::HEAD;
144
145                    let head = req.head_mut();
146                    head.uri = parts.uri;
147                    head.method = parts.method;
148                    head.version = parts.version;
149                    head.headers = parts.headers.into();
150                    head.peer_addr = this.peer_addr;
151
152                    req.conn_data.clone_from(&this.conn_data);
153
154                    let fut = this.flow.service.call(req);
155                    let config = this.config.clone();
156
157                    // multiplex request handling with spawn task
158                    tokio::task::spawn_local(async move {
159                        // resolve service call and send response.
160                        let res = match fut.await {
161                            Ok(res) => handle_response(res.into(), tx, config, head_req).await,
162                            Err(err) => {
163                                let res: Response<BoxBody> = err.into();
164                                handle_response(res, tx, config, head_req).await
165                            }
166                        };
167
168                        // log error.
169                        if let Err(err) = res {
170                            match err {
171                                DispatchError::SendResponse(err) => {
172                                    tracing::trace!("Error sending response: {err:?}");
173                                }
174                                DispatchError::SendData(err) => {
175                                    tracing::warn!("Send data error: {err:?}");
176                                }
177                                DispatchError::ResponseBody(err) => {
178                                    tracing::error!("Response payload stream error: {err:?}");
179                                }
180                            }
181                        }
182                    });
183                }
184                Poll::Ready(None) => return Poll::Ready(Ok(())),
185
186                Poll::Pending => match this.ping_pong.as_mut() {
187                    Some(ping_pong) => loop {
188                        if ping_pong.in_flight {
189                            // When there is an in-flight ping-pong, poll pong and keep-alive
190                            // timer. On successful pong received, update keep-alive timer to
191                            // determine the next timing of ping pong.
192                            match ping_pong.ping_pong.poll_pong(cx)? {
193                                Poll::Ready(_) => {
194                                    ping_pong.in_flight = false;
195
196                                    let dead_line = this.config.keep_alive_deadline().unwrap();
197                                    ping_pong.timer.as_mut().reset(dead_line.into());
198                                }
199                                Poll::Pending => {
200                                    return ping_pong.timer.as_mut().poll(cx).map(|_| Ok(()));
201                                }
202                            }
203                        } else {
204                            // When there is no in-flight ping-pong, keep-alive timer is used to
205                            // wait for next timing of ping-pong. Therefore, at this point it serves
206                            // as an interval instead.
207                            ready!(ping_pong.timer.as_mut().poll(cx));
208
209                            ping_pong.ping_pong.send_ping(Ping::opaque())?;
210
211                            let dead_line = this.config.keep_alive_deadline().unwrap();
212                            ping_pong.timer.as_mut().reset(dead_line.into());
213
214                            ping_pong.in_flight = true;
215                        }
216                    },
217                    None => return Poll::Pending,
218                },
219            }
220        }
221    }
222}
223
224enum DispatchError {
225    SendResponse(h2::Error),
226    SendData(h2::Error),
227    ResponseBody(Box<dyn StdError>),
228}
229
230async fn handle_response<B>(
231    res: Response<B>,
232    mut tx: SendResponse<Bytes>,
233    config: ServiceConfig,
234    head_req: bool,
235) -> Result<(), DispatchError>
236where
237    B: MessageBody,
238{
239    let (res, body) = res.replace_body(());
240
241    // prepare response.
242    let mut size = body.size();
243    let res = prepare_response(config, res.head(), &mut size);
244    let eof_or_head = size.is_eof() || head_req;
245
246    // send response head and return on eof.
247    let mut stream = tx
248        .send_response(res, eof_or_head)
249        .map_err(DispatchError::SendResponse)?;
250
251    if eof_or_head {
252        return Ok(());
253    }
254
255    let mut body = pin!(body);
256
257    // poll response body and send chunks to client
258    while let Some(res) = poll_fn(|cx| body.as_mut().poll_next(cx)).await {
259        let mut chunk = res.map_err(|err| DispatchError::ResponseBody(err.into()))?;
260
261        'send: loop {
262            let chunk_size = cmp::min(chunk.len(), CHUNK_SIZE);
263
264            // reserve enough space and wait for stream ready.
265            stream.reserve_capacity(chunk_size);
266
267            match poll_fn(|cx| stream.poll_capacity(cx)).await {
268                // No capacity left. drop body and return.
269                None => return Ok(()),
270
271                Some(Err(err)) => return Err(DispatchError::SendData(err)),
272
273                Some(Ok(cap)) => {
274                    // split chunk to writeable size and send to client
275                    let len = chunk.len();
276                    let bytes = chunk.split_to(cmp::min(len, cap));
277
278                    stream
279                        .send_data(bytes, false)
280                        .map_err(DispatchError::SendData)?;
281
282                    // Current chuck completely sent. break send loop and poll next one.
283                    if chunk.is_empty() {
284                        break 'send;
285                    }
286                }
287            }
288        }
289    }
290
291    // response body streaming finished. send end of stream and return.
292    stream
293        .send_data(Bytes::new(), true)
294        .map_err(DispatchError::SendData)?;
295
296    Ok(())
297}
298
299fn prepare_response(
300    config: ServiceConfig,
301    head: &ResponseHead,
302    size: &mut BodySize,
303) -> http::Response<()> {
304    let mut has_date = false;
305    let mut skip_len = size != &BodySize::Stream;
306
307    let mut res = http::Response::new(());
308    *res.status_mut() = head.status;
309    *res.version_mut() = http::Version::HTTP_2;
310
311    // Content length
312    match head.status {
313        http::StatusCode::NO_CONTENT
314        | http::StatusCode::CONTINUE
315        | http::StatusCode::PROCESSING => *size = BodySize::None,
316        http::StatusCode::SWITCHING_PROTOCOLS => {
317            skip_len = true;
318            *size = BodySize::Stream;
319        }
320        _ => {}
321    }
322
323    match size {
324        BodySize::None | BodySize::Stream => {}
325
326        BodySize::Sized(0) => {
327            #[allow(clippy::declare_interior_mutable_const)]
328            const HV_ZERO: HeaderValue = HeaderValue::from_static("0");
329            res.headers_mut().insert(CONTENT_LENGTH, HV_ZERO);
330        }
331
332        BodySize::Sized(len) => {
333            let mut buf = itoa::Buffer::new();
334
335            res.headers_mut().insert(
336                CONTENT_LENGTH,
337                HeaderValue::from_str(buf.format(*len)).unwrap(),
338            );
339        }
340    };
341
342    // copy headers
343    for (key, value) in head.headers.iter() {
344        match key {
345            // omit HTTP/1.x only headers according to:
346            // https://datatracker.ietf.org/doc/html/rfc7540#section-8.1.2.2
347            &CONNECTION | &TRANSFER_ENCODING | &UPGRADE => continue,
348
349            &CONTENT_LENGTH if skip_len => continue,
350            &DATE => has_date = true,
351
352            // omit HTTP/1.x only headers according to:
353            // https://datatracker.ietf.org/doc/html/rfc7540#section-8.1.2.2
354            hdr if hdr == HeaderName::from_static("keep-alive")
355                || hdr == HeaderName::from_static("proxy-connection") =>
356            {
357                continue
358            }
359
360            _ => {}
361        }
362
363        res.headers_mut().append(key, value.clone());
364    }
365
366    // set date header
367    if !has_date {
368        let mut bytes = BytesMut::with_capacity(29);
369        config.write_date_header_value(&mut bytes);
370        res.headers_mut().insert(
371            DATE,
372            // SAFETY: serialized date-times are known ASCII strings
373            unsafe { HeaderValue::from_maybe_shared_unchecked(bytes.freeze()) },
374        );
375    }
376
377    res
378}