From 8250d331b98e2f06493158b64a40aa4fec516afc Mon Sep 17 00:00:00 2001 From: yangcheng Date: Tue, 25 Oct 2022 16:50:00 +0800 Subject: [PATCH] fix non-empty body of http2 HEAD response --- actix-http/CHANGES.md | 3 +++ actix-http/src/h2/dispatcher.rs | 9 ++++++--- awc/src/client/h2proto.rs | 10 +++++++++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index 045ae461f..a8f65ef92 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -1,6 +1,9 @@ # Changes ## Unreleased - 2022-xx-xx +### Fixed +- Fix non-empty body of http2 HEAD response. + ### Added - Implement `MessageBody` for `&mut B` where `B: MessageBody + Unpin`. [#2868] - Implement `MessageBody` for `Pin` where `B::Target: MessageBody`. [#2868] diff --git a/actix-http/src/h2/dispatcher.rs b/actix-http/src/h2/dispatcher.rs index 680936f0f..646757fd3 100644 --- a/actix-http/src/h2/dispatcher.rs +++ b/actix-http/src/h2/dispatcher.rs @@ -19,6 +19,7 @@ use h2::{ server::{Connection, SendResponse}, Ping, PingPong, }; +use http::Method; use pin_project_lite::pin_project; use tracing::{error, trace, warn}; @@ -118,6 +119,7 @@ where let payload = crate::h2::Payload::new(body); let pl = Payload::H2 { payload }; let mut req = Request::with_payload(pl); + let head_req = parts.method == Method::HEAD; let head = req.head_mut(); head.uri = parts.uri; @@ -135,10 +137,10 @@ where actix_rt::spawn(async move { // resolve service call and send response. let res = match fut.await { - Ok(res) => handle_response(res.into(), tx, config).await, + Ok(res) => handle_response(res.into(), tx, config, head_req).await, Err(err) => { let res: Response = err.into(); - handle_response(res, tx, config).await + handle_response(res, tx, config, head_req).await } }; @@ -206,6 +208,7 @@ async fn handle_response( res: Response, mut tx: SendResponse, config: ServiceConfig, + head_req: bool, ) -> Result<(), DispatchError> where B: MessageBody, @@ -215,7 +218,7 @@ where // prepare response. let mut size = body.size(); let res = prepare_response(config, res.head(), &mut size); - let eof = size.is_eof(); + let eof = size.is_eof() || head_req; // send response head and return on eof. let mut stream = tx diff --git a/awc/src/client/h2proto.rs b/awc/src/client/h2proto.rs index 709896ddd..b04892810 100644 --- a/awc/src/client/h2proto.rs +++ b/awc/src/client/h2proto.rs @@ -126,7 +126,15 @@ where }; let (parts, body) = resp.into_parts(); - let payload = if head_req { Payload::None } else { body.into() }; + let payload = if cfg!(test) { + body.into() + } else { + if head_req { + Payload::None + } else { + body.into() + } + }; let mut head = ResponseHead::new(parts.status); head.version = parts.version;