2021-02-11 23:39:54 +01:00
|
|
|
//! HTTP/2 protocol.
|
2021-01-04 01:49:02 +01:00
|
|
|
|
|
|
|
use std::{
|
2021-12-02 19:16:34 +01:00
|
|
|
future::Future,
|
2021-01-04 01:49:02 +01:00
|
|
|
pin::Pin,
|
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
2019-02-02 05:18:44 +01:00
|
|
|
|
2021-12-02 19:16:34 +01:00
|
|
|
use actix_codec::{AsyncRead, AsyncWrite};
|
2022-01-31 18:30:34 +01:00
|
|
|
use actix_rt::time::{sleep_until, Sleep};
|
2019-02-06 20:44:15 +01:00
|
|
|
use bytes::Bytes;
|
2021-01-04 01:49:02 +01:00
|
|
|
use futures_core::{ready, Stream};
|
2021-12-02 19:16:34 +01:00
|
|
|
use h2::{
|
|
|
|
server::{handshake, Connection, Handshake},
|
|
|
|
RecvStream,
|
|
|
|
};
|
2019-02-06 20:44:15 +01:00
|
|
|
|
2022-01-31 18:30:34 +01:00
|
|
|
use crate::{
|
|
|
|
config::ServiceConfig,
|
|
|
|
error::{DispatchError, PayloadError},
|
|
|
|
};
|
|
|
|
|
2019-02-06 20:44:15 +01:00
|
|
|
mod dispatcher;
|
2019-02-02 05:18:44 +01:00
|
|
|
mod service;
|
|
|
|
|
2019-03-07 07:56:34 +01:00
|
|
|
pub use self::dispatcher::Dispatcher;
|
2019-02-06 20:44:15 +01:00
|
|
|
pub use self::service::H2Service;
|
2021-12-02 19:16:34 +01:00
|
|
|
|
2021-01-04 01:49:02 +01:00
|
|
|
/// HTTP/2 peer stream.
|
2019-02-06 20:44:15 +01:00
|
|
|
pub struct Payload {
|
2021-01-04 01:49:02 +01:00
|
|
|
stream: RecvStream,
|
2019-02-06 20:44:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Payload {
|
2021-01-04 01:49:02 +01:00
|
|
|
pub(crate) fn new(stream: RecvStream) -> Self {
|
|
|
|
Self { stream }
|
2019-02-06 20:44:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Stream for Payload {
|
2019-11-15 10:54:11 +01:00
|
|
|
type Item = Result<Bytes, PayloadError>;
|
|
|
|
|
2021-12-08 07:01:11 +01:00
|
|
|
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
2019-11-15 10:54:11 +01:00
|
|
|
let this = self.get_mut();
|
2019-02-06 20:44:15 +01:00
|
|
|
|
2021-01-04 01:49:02 +01:00
|
|
|
match ready!(Pin::new(&mut this.stream).poll_data(cx)) {
|
|
|
|
Some(Ok(chunk)) => {
|
2019-02-06 20:44:15 +01:00
|
|
|
let len = chunk.len();
|
2021-01-04 01:49:02 +01:00
|
|
|
|
|
|
|
match this.stream.flow_control().release_capacity(len) {
|
|
|
|
Ok(()) => Poll::Ready(Some(Ok(chunk))),
|
|
|
|
Err(err) => Poll::Ready(Some(Err(err.into()))),
|
2019-02-06 20:44:15 +01:00
|
|
|
}
|
|
|
|
}
|
2021-01-04 01:49:02 +01:00
|
|
|
Some(Err(err)) => Poll::Ready(Some(Err(err.into()))),
|
|
|
|
None => Poll::Ready(None),
|
2019-02-06 20:44:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-02 19:16:34 +01:00
|
|
|
|
|
|
|
pub(crate) fn handshake_with_timeout<T>(
|
|
|
|
io: T,
|
|
|
|
config: &ServiceConfig,
|
|
|
|
) -> HandshakeWithTimeout<T>
|
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite + Unpin,
|
|
|
|
{
|
|
|
|
HandshakeWithTimeout {
|
|
|
|
handshake: handshake(io),
|
2022-01-31 18:30:34 +01:00
|
|
|
timer: config
|
|
|
|
.client_request_deadline()
|
|
|
|
.map(|deadline| Box::pin(sleep_until(deadline.into()))),
|
2021-12-02 19:16:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct HandshakeWithTimeout<T: AsyncRead + AsyncWrite + Unpin> {
|
|
|
|
handshake: Handshake<T>,
|
|
|
|
timer: Option<Pin<Box<Sleep>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Future for HandshakeWithTimeout<T>
|
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite + Unpin,
|
|
|
|
{
|
|
|
|
type Output = Result<(Connection<T, Bytes>, Option<Pin<Box<Sleep>>>), DispatchError>;
|
|
|
|
|
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
let this = self.get_mut();
|
|
|
|
|
|
|
|
match Pin::new(&mut this.handshake).poll(cx)? {
|
2022-01-31 18:30:34 +01:00
|
|
|
// return the timer on success handshake; its slot can be re-used for h2 ping-pong
|
2021-12-02 19:16:34 +01:00
|
|
|
Poll::Ready(conn) => Poll::Ready(Ok((conn, this.timer.take()))),
|
|
|
|
Poll::Pending => match this.timer.as_mut() {
|
|
|
|
Some(timer) => {
|
|
|
|
ready!(timer.as_mut().poll(cx));
|
|
|
|
Poll::Ready(Err(DispatchError::SlowRequestTimeout))
|
|
|
|
}
|
|
|
|
None => Poll::Pending,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-24 18:47:47 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::panic::{RefUnwindSafe, UnwindSafe};
|
|
|
|
|
|
|
|
use static_assertions::assert_impl_all;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
assert_impl_all!(Payload: Unpin, Send, Sync, UnwindSafe, RefUnwindSafe);
|
|
|
|
}
|