Skip to content

Commit

Permalink
fix(server): start header read timeout immediately
Browse files Browse the repository at this point in the history
The `http1_header_read_timeout` used to start once there was a single
read of headers. This change makes it start the timer immediately, right
when the connection is estabilished.
  • Loading branch information
seanmonstar committed Mar 24, 2023
1 parent 47f614f commit f46fc9f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 148 deletions.
61 changes: 48 additions & 13 deletions src/proto/h1/conn.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::fmt;
#[cfg(feature = "server")]
use std::future::Future;
use std::io;
use std::marker::PhantomData;
#[cfg(feature = "server")]
use std::time::Duration;
use std::time::{Duration, Instant};

use bytes::{Buf, Bytes};
use http::header::{HeaderValue, CONNECTION};
Expand Down Expand Up @@ -192,32 +194,65 @@ where
debug_assert!(self.can_read_head());
trace!("Conn::read_head");

let msg = match ready!(self.io.parse::<T>(
#[cfg(feature = "server")]
if !self.state.h1_header_read_timeout_running {
if let Some(h1_header_read_timeout) = self.state.h1_header_read_timeout {
let deadline = Instant::now() + h1_header_read_timeout;
self.state.h1_header_read_timeout_running = true;
match self.state.h1_header_read_timeout_fut {
Some(ref mut h1_header_read_timeout_fut) => {
trace!("resetting h1 header read timeout timer");
self.state.timer.reset(h1_header_read_timeout_fut, deadline);
}
None => {
trace!("setting h1 header read timeout timer");
self.state.h1_header_read_timeout_fut = Some(self.state.timer.sleep_until(deadline));
}
}
}
}

let msg = match self.io.parse::<T>(
cx,
ParseContext {
cached_headers: &mut self.state.cached_headers,
req_method: &mut self.state.method,
h1_parser_config: self.state.h1_parser_config.clone(),
#[cfg(feature = "server")]
h1_header_read_timeout: self.state.h1_header_read_timeout,
#[cfg(feature = "server")]
h1_header_read_timeout_fut: &mut self.state.h1_header_read_timeout_fut,
#[cfg(feature = "server")]
h1_header_read_timeout_running: &mut self.state.h1_header_read_timeout_running,
#[cfg(feature = "server")]
timer: self.state.timer.clone(),
preserve_header_case: self.state.preserve_header_case,
#[cfg(feature = "ffi")]
preserve_header_order: self.state.preserve_header_order,
h09_responses: self.state.h09_responses,
#[cfg(feature = "ffi")]
on_informational: &mut self.state.on_informational,
}
)) {
Ok(msg) => msg,
Err(e) => return self.on_read_head_error(e),
) {
Poll::Ready(Ok(msg)) => msg,
Poll::Ready(Err(e)) => return self.on_read_head_error(e),
Poll::Pending => {
#[cfg(feature = "server")]
if self.state.h1_header_read_timeout_running {
if let Some(ref mut h1_header_read_timeout_fut) =
self.state.h1_header_read_timeout_fut
{
if Pin::new(h1_header_read_timeout_fut).poll(cx).is_ready() {
self.state.h1_header_read_timeout_running = false;

tracing::warn!("read header from client timeout");
return Poll::Ready(Some(Err(crate::Error::new_header_timeout())));
}
}
}

return Poll::Pending;
}
};

#[cfg(feature = "server")]
{
self.state.h1_header_read_timeout_running = false;
self.state.h1_header_read_timeout_fut = None;
}

// Note: don't deconstruct `msg` into local variables, it appears
// the optimizer doesn't remove the extra copies.

Expand Down
34 changes: 0 additions & 34 deletions src/proto/h1/io.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::cmp;
use std::fmt;
#[cfg(feature = "server")]
use std::future::Future;
use std::io::{self, IoSlice};
use std::marker::Unpin;
use std::mem::MaybeUninit;
Expand Down Expand Up @@ -183,14 +181,6 @@ where
cached_headers: parse_ctx.cached_headers,
req_method: parse_ctx.req_method,
h1_parser_config: parse_ctx.h1_parser_config.clone(),
#[cfg(feature = "server")]
h1_header_read_timeout: parse_ctx.h1_header_read_timeout,
#[cfg(feature = "server")]
h1_header_read_timeout_fut: parse_ctx.h1_header_read_timeout_fut,
#[cfg(feature = "server")]
h1_header_read_timeout_running: parse_ctx.h1_header_read_timeout_running,
#[cfg(feature = "server")]
timer: parse_ctx.timer.clone(),
preserve_header_case: parse_ctx.preserve_header_case,
#[cfg(feature = "ffi")]
preserve_header_order: parse_ctx.preserve_header_order,
Expand All @@ -201,12 +191,6 @@ where
)? {
Some(msg) => {
debug!("parsed {} headers", msg.head.headers.len());

#[cfg(feature = "server")]
{
*parse_ctx.h1_header_read_timeout_running = false;
parse_ctx.h1_header_read_timeout_fut.take();
}
return Poll::Ready(Ok(msg));
}
None => {
Expand All @@ -215,20 +199,6 @@ where
debug!("max_buf_size ({}) reached, closing", max);
return Poll::Ready(Err(crate::Error::new_too_large()));
}

#[cfg(feature = "server")]
if *parse_ctx.h1_header_read_timeout_running {
if let Some(h1_header_read_timeout_fut) =
parse_ctx.h1_header_read_timeout_fut
{
if Pin::new(h1_header_read_timeout_fut).poll(cx).is_ready() {
*parse_ctx.h1_header_read_timeout_running = false;

tracing::warn!("read header from client timeout");
return Poll::Ready(Err(crate::Error::new_header_timeout()));
}
}
}
}
}
if ready!(self.poll_read_from_io(cx)).map_err(crate::Error::new_io)? == 0 {
Expand Down Expand Up @@ -726,10 +696,6 @@ mod tests {
cached_headers: &mut None,
req_method: &mut None,
h1_parser_config: Default::default(),
h1_header_read_timeout: None,
h1_header_read_timeout_fut: &mut None,
h1_header_read_timeout_running: &mut false,
timer: Time::Empty,
preserve_header_case: false,
#[cfg(feature = "ffi")]
preserve_header_order: false,
Expand Down
15 changes: 0 additions & 15 deletions src/proto/h1/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
#[cfg(feature = "server")]
use std::{pin::Pin, time::Duration};

use bytes::BytesMut;
use http::{HeaderMap, Method};
use httparse::ParserConfig;

use crate::body::DecodedLength;
#[cfg(feature = "server")]
use crate::common::time::Time;
use crate::proto::{BodyLength, MessageHead};
#[cfg(feature = "server")]
use crate::rt::Sleep;

pub(crate) use self::conn::Conn;
pub(crate) use self::decode::Decoder;
Expand Down Expand Up @@ -78,14 +71,6 @@ pub(crate) struct ParseContext<'a> {
cached_headers: &'a mut Option<HeaderMap>,
req_method: &'a mut Option<Method>,
h1_parser_config: ParserConfig,
#[cfg(feature = "server")]
h1_header_read_timeout: Option<Duration>,
#[cfg(feature = "server")]
h1_header_read_timeout_fut: &'a mut Option<Pin<Box<dyn Sleep>>>,
#[cfg(feature = "server")]
h1_header_read_timeout_running: &'a mut bool,
#[cfg(feature = "server")]
timer: Time,
preserve_header_case: bool,
#[cfg(feature = "ffi")]
preserve_header_order: bool,
Expand Down
Loading

0 comments on commit f46fc9f

Please sign in to comment.