1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-08-19 12:25:37 +02:00

fix request head timeout (#2611)

This commit is contained in:
Rob Ede
2022-01-31 17:30:34 +00:00
committed by GitHub
parent b3e84b5c4b
commit 3200de3f34
38 changed files with 1303 additions and 767 deletions

View File

@@ -17,7 +17,7 @@ use crate::{
h1::{Codec, ExpectHandler, UpgradeHandler},
service::HttpFlow,
test::{TestBuffer, TestSeqBuffer},
Error, HttpMessage, KeepAlive, Method, OnConnectData, Request, Response,
Error, HttpMessage, KeepAlive, Method, OnConnectData, Request, Response, StatusCode,
};
fn find_slice(haystack: &[u8], needle: &[u8], from: usize) -> Option<usize> {
@@ -34,7 +34,13 @@ fn stabilize_date_header(payload: &mut [u8]) {
}
fn ok_service() -> impl Service<Request, Response = Response<impl MessageBody>, Error = Error> {
fn_service(|_req: Request| ready(Ok::<_, Error>(Response::ok())))
status_service(StatusCode::OK)
}
fn status_service(
status: StatusCode,
) -> impl Service<Request, Response = Response<impl MessageBody>, Error = Error> {
fn_service(move |_req: Request| ready(Ok::<_, Error>(Response::new(status))))
}
fn echo_path_service(
@@ -65,11 +71,15 @@ fn echo_payload_service() -> impl Service<Request, Response = Response<Bytes>, E
#[actix_rt::test]
async fn late_request() {
let _ = env_logger::try_init();
let mut buf = TestBuffer::empty();
let cfg = ServiceConfig::new(KeepAlive::Disabled, 100, 0, false, None);
let cfg = ServiceConfig::new(
KeepAlive::Disabled,
Duration::from_millis(100),
Duration::ZERO,
false,
None,
);
let services = HttpFlow::new(ok_service(), ExpectHandler, None);
let h1 = Dispatcher::<_, _, _, _, UpgradeHandler>::new(
@@ -127,10 +137,16 @@ async fn late_request() {
}
#[actix_rt::test]
async fn test_basic() {
async fn oneshot_connection() {
let buf = TestBuffer::new("GET /abcd HTTP/1.1\r\n\r\n");
let cfg = ServiceConfig::new(KeepAlive::Disabled, 100, 0, false, None);
let cfg = ServiceConfig::new(
KeepAlive::Disabled,
Duration::from_millis(100),
Duration::ZERO,
false,
None,
);
let services = HttpFlow::new(echo_path_service(), ExpectHandler, None);
let h1 = Dispatcher::<_, _, _, _, UpgradeHandler>::new(
@@ -179,10 +195,16 @@ async fn test_basic() {
}
#[actix_rt::test]
async fn test_keep_alive_timeout() {
async fn keep_alive_timeout() {
let buf = TestBuffer::new("GET /abcd HTTP/1.1\r\n\r\n");
let cfg = ServiceConfig::new(KeepAlive::Timeout(1), 100, 0, false, None);
let cfg = ServiceConfig::new(
KeepAlive::Timeout(Duration::from_millis(200)),
Duration::from_millis(100),
Duration::ZERO,
false,
None,
);
let services = HttpFlow::new(echo_path_service(), ExpectHandler, None);
let h1 = Dispatcher::<_, _, _, _, UpgradeHandler>::new(
@@ -229,7 +251,7 @@ async fn test_keep_alive_timeout() {
.await;
// sleep slightly longer than keep-alive timeout
sleep(Duration::from_millis(1100)).await;
sleep(Duration::from_millis(250)).await;
lazy(|cx| {
assert!(
@@ -252,10 +274,16 @@ async fn test_keep_alive_timeout() {
}
#[actix_rt::test]
async fn test_keep_alive_follow_up_req() {
async fn keep_alive_follow_up_req() {
let mut buf = TestBuffer::new("GET /abcd HTTP/1.1\r\n\r\n");
let cfg = ServiceConfig::new(KeepAlive::Timeout(2), 100, 0, false, None);
let cfg = ServiceConfig::new(
KeepAlive::Timeout(Duration::from_millis(500)),
Duration::from_millis(100),
Duration::ZERO,
false,
None,
);
let services = HttpFlow::new(echo_path_service(), ExpectHandler, None);
let h1 = Dispatcher::<_, _, _, _, UpgradeHandler>::new(
@@ -302,7 +330,7 @@ async fn test_keep_alive_follow_up_req() {
.await;
// sleep for less than KA timeout
sleep(Duration::from_millis(200)).await;
sleep(Duration::from_millis(100)).await;
lazy(|cx| {
assert!(
@@ -371,7 +399,7 @@ async fn test_keep_alive_follow_up_req() {
}
#[actix_rt::test]
async fn test_req_parse_err() {
async fn req_parse_err() {
lazy(|cx| {
let buf = TestBuffer::new("GET /test HTTP/1\r\n\r\n");
@@ -413,7 +441,13 @@ async fn pipelining_ok_then_ok() {
",
);
let cfg = ServiceConfig::new(KeepAlive::Disabled, 1, 1, false, None);
let cfg = ServiceConfig::new(
KeepAlive::Disabled,
Duration::from_millis(1),
Duration::from_millis(1),
false,
None,
);
let services = HttpFlow::new(echo_path_service(), ExpectHandler, None);
@@ -477,7 +511,13 @@ async fn pipelining_ok_then_bad() {
",
);
let cfg = ServiceConfig::new(KeepAlive::Disabled, 1, 1, false, None);
let cfg = ServiceConfig::new(
KeepAlive::Disabled,
Duration::from_millis(1),
Duration::from_millis(1),
false,
None,
);
let services = HttpFlow::new(echo_path_service(), ExpectHandler, None);
@@ -531,10 +571,16 @@ async fn pipelining_ok_then_bad() {
}
#[actix_rt::test]
async fn test_expect() {
async fn expect_handling() {
lazy(|cx| {
let mut buf = TestSeqBuffer::empty();
let cfg = ServiceConfig::new(KeepAlive::Disabled, 0, 0, false, None);
let cfg = ServiceConfig::new(
KeepAlive::Disabled,
Duration::ZERO,
Duration::ZERO,
false,
None,
);
let services = HttpFlow::new(echo_payload_service(), ExpectHandler, None);
@@ -562,7 +608,6 @@ async fn test_expect() {
// polls: manual
assert_eq!(h1.poll_count, 1);
eprintln!("poll count: {}", h1.poll_count);
if let DispatcherState::Normal { ref inner } = h1.inner {
let io = inner.io.as_ref().unwrap();
@@ -603,10 +648,16 @@ async fn test_expect() {
}
#[actix_rt::test]
async fn test_eager_expect() {
async fn expect_eager() {
lazy(|cx| {
let mut buf = TestSeqBuffer::empty();
let cfg = ServiceConfig::new(KeepAlive::Disabled, 0, 0, false, None);
let cfg = ServiceConfig::new(
KeepAlive::Disabled,
Duration::ZERO,
Duration::ZERO,
false,
None,
);
let services = HttpFlow::new(echo_path_service(), ExpectHandler, None);
@@ -663,7 +714,7 @@ async fn test_eager_expect() {
}
#[actix_rt::test]
async fn test_upgrade() {
async fn upgrade_handling() {
struct TestUpgrade;
impl<T> Service<(Request, Framed<T, Codec>)> for TestUpgrade {
@@ -683,7 +734,13 @@ async fn test_upgrade() {
lazy(|cx| {
let mut buf = TestSeqBuffer::empty();
let cfg = ServiceConfig::new(KeepAlive::Disabled, 0, 0, false, None);
let cfg = ServiceConfig::new(
KeepAlive::Disabled,
Duration::ZERO,
Duration::ZERO,
false,
None,
);
let services = HttpFlow::new(ok_service(), ExpectHandler, Some(TestUpgrade));