fix(local-http): HttpClient send HTTP requests URI contains only Path&Query

- fix #1730
- hyper will serialize the hyper::Request<T> object with all the
  contents in the uri, which may be rejected by some of the servers.
This commit is contained in:
zonyitoo
2024-10-24 00:00:53 +08:00
parent f3f2dd0507
commit ab5005c985
3 changed files with 22 additions and 3 deletions

View File

@@ -72,7 +72,7 @@ local-dns-relay = ["local-dns"]
# Currently is only used in Android
local-flow-stat = ["local"]
# Enable HTTP protocol for sslocal
local-http = ["local", "hyper", "http-body-util"]
local-http = ["local", "hyper", "http", "http-body-util"]
local-http-native-tls = ["local-http", "tokio-native-tls", "native-tls"]
local-http-native-tls-vendored = [
"local-http-native-tls",

View File

@@ -11,6 +11,7 @@ use std::{
time::{Duration, Instant},
};
use http::Uri;
use hyper::{
body::{self, Body},
client::conn::{http1, http2},
@@ -43,6 +44,9 @@ pub enum HttpClientError {
/// std::io::Error
#[error("{0}")]
Io(#[from] io::Error),
/// Errors from http
#[error("{0}")]
Http(#[from] http::Error),
}
#[derive(Clone, Debug)]
@@ -195,8 +199,19 @@ where
&self,
host: Address,
mut c: HttpConnection<B>,
req: Request<B>,
) -> hyper::Result<Response<body::Incoming>> {
mut req: Request<B>,
) -> Result<Response<body::Incoming>, HttpClientError> {
// Remove Scheme, Host part from URI
if req.uri().scheme().is_some() || req.uri().authority().is_some() {
let mut builder = Uri::builder();
if let Some(path_and_query) = req.uri().path_and_query() {
builder = builder.path_and_query(path_and_query.as_str());
} else {
builder = builder.path_and_query("/");
}
*(req.uri_mut()) = builder.build()?;
}
trace!("HTTP making request to host: {}, request: {:?}", host, req);
let response = c.send_request(req).await?;
trace!("HTTP received response from host: {}, response: {:?}", host, response);

View File

@@ -158,6 +158,10 @@ impl HttpService {
error!("failed to make request to host: {}, error: {}", host, err);
return make_internal_server_error();
}
Err(HttpClientError::Http(err)) => {
error!("failed to make request to host: {}, error: {}", host, err);
return make_bad_request();
}
};
trace!("received {} <- {} {:?}", self.peer_addr, host, res);