Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 43891d0

Browse files
committedJan 26, 2024
feat: SetHost and Http1RequestTarget middlewares
1 parent bfde94b commit 43891d0

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed
 

‎src/client/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33
/// Legacy implementations of `connect` module and `Client`
44
#[cfg(feature = "client-legacy")]
55
pub mod legacy;
6+
7+
/// Client services
8+
#[cfg(any(feature = "http1", feature = "http2"))]
9+
pub mod services;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
use http::{uri::Scheme, Method, Request, Uri};
2+
use hyper::service::Service;
3+
use tracing::warn;
4+
5+
/// A `Service` that normalizes the request target.
6+
pub struct Http1RequestTarget<S> {
7+
inner: S,
8+
is_proxied: bool,
9+
}
10+
11+
impl<S> Http1RequestTarget<S> {
12+
/// Create a new `Http1RequestTarget` service.
13+
pub fn new(inner: S, is_proxied: bool) -> Self {
14+
Self { inner, is_proxied }
15+
}
16+
}
17+
18+
impl<S, B> Service<Request<B>> for Http1RequestTarget<S>
19+
where
20+
S: Service<Request<B>>,
21+
{
22+
type Response = S::Response;
23+
type Error = S::Error;
24+
type Future = S::Future;
25+
26+
fn call(&self, mut req: Request<B>) -> Self::Future {
27+
// CONNECT always sends authority-form, so check it first...
28+
if req.method() == Method::CONNECT {
29+
authority_form(req.uri_mut());
30+
} else if self.is_proxied {
31+
absolute_form(req.uri_mut());
32+
} else {
33+
origin_form(req.uri_mut());
34+
}
35+
self.inner.call(req)
36+
}
37+
}
38+
39+
fn origin_form(uri: &mut Uri) {
40+
let path = match uri.path_and_query() {
41+
Some(path) if path.as_str() != "/" => {
42+
let mut parts = ::http::uri::Parts::default();
43+
parts.path_and_query = Some(path.clone());
44+
Uri::from_parts(parts).expect("path is valid uri")
45+
}
46+
_none_or_just_slash => {
47+
debug_assert!(Uri::default() == "/");
48+
Uri::default()
49+
}
50+
};
51+
*uri = path
52+
}
53+
54+
fn absolute_form(uri: &mut Uri) {
55+
debug_assert!(uri.scheme().is_some(), "absolute_form needs a scheme");
56+
debug_assert!(
57+
uri.authority().is_some(),
58+
"absolute_form needs an authority"
59+
);
60+
// If the URI is to HTTPS, and the connector claimed to be a proxy,
61+
// then it *should* have tunneled, and so we don't want to send
62+
// absolute-form in that case.
63+
if uri.scheme() == Some(&Scheme::HTTPS) {
64+
origin_form(uri);
65+
}
66+
}
67+
68+
fn authority_form(uri: &mut Uri) {
69+
if let Some(path) = uri.path_and_query() {
70+
// `https://hyper.rs` would parse with `/` path, don't
71+
// annoy people about that...
72+
if path != "/" {
73+
warn!("HTTP/1.1 CONNECT request stripping path: {:?}", path);
74+
}
75+
}
76+
*uri = match uri.authority() {
77+
Some(auth) => {
78+
let mut parts = ::http::uri::Parts::default();
79+
parts.authority = Some(auth.clone());
80+
Uri::from_parts(parts).expect("authority is valid")
81+
}
82+
None => {
83+
unreachable!("authority_form with relative uri");
84+
}
85+
};
86+
}

‎src/client/services/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mod http1_request_target;
2+
mod set_host;
3+
4+
pub use http1_request_target::Http1RequestTarget;
5+
pub use set_host::SetHost;

‎src/client/services/set_host.rs

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use http::{header::HOST, uri::Port, HeaderValue, Request, Uri};
2+
use hyper::service::Service;
3+
4+
/// A `Service` that sets the `Host` header, if it's missing, based on the request URI.
5+
pub struct SetHost<S> {
6+
inner: S,
7+
}
8+
9+
impl<S> SetHost<S> {
10+
/// Create a new `SetHost` service.
11+
pub fn new(inner: S) -> Self {
12+
Self { inner }
13+
}
14+
}
15+
16+
impl<S, B> Service<Request<B>> for SetHost<S>
17+
where
18+
S: Service<Request<B>>,
19+
{
20+
type Response = S::Response;
21+
type Error = S::Error;
22+
type Future = S::Future;
23+
24+
fn call(&self, mut req: Request<B>) -> Self::Future {
25+
let uri = req.uri().clone();
26+
req.headers_mut().entry(HOST).or_insert_with(|| {
27+
let hostname = uri.host().expect("authority implies host");
28+
if let Some(port) = get_non_default_port(&uri) {
29+
let s = format!("{}:{}", hostname, port);
30+
HeaderValue::from_str(&s)
31+
} else {
32+
HeaderValue::from_str(hostname)
33+
}
34+
.expect("uri host is valid header value")
35+
});
36+
self.inner.call(req)
37+
}
38+
}
39+
40+
fn get_non_default_port(uri: &Uri) -> Option<Port<&str>> {
41+
match (uri.port().map(|p| p.as_u16()), is_schema_secure(uri)) {
42+
(Some(443), true) => None,
43+
(Some(80), false) => None,
44+
_ => uri.port(),
45+
}
46+
}
47+
48+
fn is_schema_secure(uri: &Uri) -> bool {
49+
uri.scheme_str()
50+
.map(|scheme_str| matches!(scheme_str, "wss" | "https"))
51+
.unwrap_or_default()
52+
}

0 commit comments

Comments
 (0)
Please sign in to comment.