Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(client): remove host header on http2 / http3 #1163

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion client/src/h2/proto/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use xitca_http::{
http::{
self,
const_header_name::PROTOCOL,
header::{HeaderValue, CONNECTION, CONTENT_LENGTH, DATE, TRANSFER_ENCODING, UPGRADE},
header::{HeaderValue, CONNECTION, CONTENT_LENGTH, DATE, HOST, TRANSFER_ENCODING, UPGRADE},
method::Method,
},
};
Expand Down Expand Up @@ -61,6 +61,9 @@ where
req.headers_mut().remove(TRANSFER_ENCODING);
req.headers_mut().remove(UPGRADE);

// remove host header if present, some web server may send 400 bad request if host header is present (like nginx)
req.headers_mut().remove(HOST);

if !req.headers().contains_key(DATE) {
let date = date.with_date(HeaderValue::from_bytes).unwrap();
req.headers_mut().append(DATE, date);
Expand Down
12 changes: 7 additions & 5 deletions client/src/h3/proto/dispatcher.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
use core::{future::poll_fn, net::SocketAddr, pin::pin};

use ::h3_quinn::quinn::Endpoint;
use futures_core::stream::Stream;
use xitca_http::date::DateTime;

use crate::{
body::{BodyError, BodySize, ResponseBody},
bytes::{Buf, Bytes},
date::DateTimeHandle,
h3::{Connection, Error},
http::{
header::{HeaderValue, CONTENT_LENGTH, DATE},
header::{HeaderValue, CONTENT_LENGTH, DATE, HOST},
Method, Request, Response,
},
};
use ::h3_quinn::quinn::Endpoint;
use futures_core::stream::Stream;
use xitca_http::date::DateTime;

pub(crate) async fn send<B, E>(
stream: &mut Connection,
Expand Down Expand Up @@ -49,6 +48,9 @@ where
}
};

// remove host header if present, some web server may send 400 bad request if host header is present.
req.headers_mut().remove(HOST);

if !req.headers().contains_key(DATE) {
let date = date.with_date(HeaderValue::from_bytes).unwrap();
req.headers_mut().append(DATE, date);
Expand Down
35 changes: 35 additions & 0 deletions test/tests/h2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,32 @@ async fn h2_get() -> Result<(), Error> {
Ok(())
}

#[tokio::test]
async fn h2_no_host_header() -> Result<(), Error> {
let mut handle = test_h2_server(fn_service(handle))?;

let server_url = format!("https://{}/host", handle.ip_port_string());

let c = Client::new();

for _ in 0..3 {
let mut req = c.get(&server_url).version(Version::HTTP_2);
req.headers_mut().insert(header::HOST, "localhost".parse().unwrap());

let mut res = req.send().await?;
assert_eq!(res.status().as_u16(), 200);
assert!(!res.can_close_connection());
let body = res.string().await?;
assert_eq!("", body);
}

handle.try_handle()?.stop(false);

handle.await?;

Ok(())
}

#[tokio::test]
async fn h2_post() -> Result<(), Error> {
let mut handle = test_h2_server(fn_service(handle))?;
Expand Down Expand Up @@ -156,6 +182,15 @@ async fn handle(req: Request<RequestExt<h2::RequestBody>>) -> Result<Response<Re

match (req.method(), req.uri().path()) {
(&Method::GET, "/") => Ok(Response::new(Bytes::from("GET Response").into())),
(&Method::GET, "/host") => Ok(Response::new(
Bytes::from(
req.headers()
.get(header::HOST)
.map(|v| v.to_str().unwrap().to_string())
.unwrap_or_default(),
)
.into(),
)),
(&Method::CONNECT, "/") => {
let (_, mut body) = req.into_parts();
Ok(Response::new(ResponseBody::box_stream(async_stream::stream! {
Expand Down
35 changes: 35 additions & 0 deletions test/tests/h3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,32 @@ async fn h3_get() -> Result<(), Error> {
Ok(())
}

#[tokio::test]
async fn h3_no_host_header() -> Result<(), Error> {
let mut handle = test_h3_server(fn_service(handle))?;

let server_url = format!("https://{}/host", handle.ip_port_string());

let c = Client::new();

for _ in 0..3 {
let mut req = c.get(&server_url).version(Version::HTTP_3);
req.headers_mut().insert(header::HOST, "localhost".parse().unwrap());

let mut res = req.send().await?;
assert_eq!(res.status().as_u16(), 200);
assert!(!res.can_close_connection());
let body = res.string().await?;
assert_eq!("", body);
}

handle.try_handle()?.stop(false);

handle.await?;

Ok(())
}

#[tokio::test]
async fn h3_post() -> Result<(), Error> {
let mut handle = test_h3_server(fn_service(handle))?;
Expand Down Expand Up @@ -64,6 +90,15 @@ async fn handle(req: Request<RequestExt<h3::RequestBody>>) -> Result<Response<Re

match (req.method(), req.uri().path()) {
(&Method::GET, "/") => Ok(Response::new(Bytes::from("GET Response").into())),
(&Method::GET, "/host") => Ok(Response::new(
Bytes::from(
req.headers()
.get(header::HOST)
.map(|v| v.to_str().unwrap().to_string())
.unwrap_or_default(),
)
.into(),
)),
(&Method::POST, "/") => {
let (parts, mut body) = req.into_parts();

Expand Down
Loading