Skip to content

Commit ce0ca4c

Browse files
committed
Use Host header instead of URL to find host
1 parent 8edf8e8 commit ce0ca4c

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/reverse_proxy.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ use std::{
33
fmt::{self, Display},
44
mem,
55
net::SocketAddr,
6-
str::FromStr,
6+
str::{self, FromStr},
77
sync::Arc,
88
};
99

1010
use axum::{
1111
body::Body,
1212
extract::{Request, State},
1313
http::{
14+
header::HOST,
1415
uri::{Authority, Parts, PathAndQuery, Scheme},
1516
StatusCode, Uri,
1617
},
@@ -96,7 +97,22 @@ impl RoutingTable {
9697
let req_uri = request.uri();
9798

9899
// First, attempt to match a domain.
99-
let opt_domain = req_uri.host().and_then(Domain::new);
100+
let opt_domain = if let Some(host_header) = request
101+
.headers()
102+
.get(HOST)
103+
.and_then(|h| str::from_utf8(h.as_bytes()).ok())
104+
{
105+
let candidate = if let Some(colon) = host_header.rfind(':') {
106+
&host_header[..colon]
107+
} else {
108+
host_header
109+
};
110+
111+
Domain::new(candidate)
112+
} else {
113+
None
114+
};
115+
100116
if let Some(pc) = opt_domain.and_then(|domain| self.get_domain_route(&domain)) {
101117
// We only need to swap the protocol and domain and we're good to go.
102118
let mut parts = req_uri.clone().into_parts();

0 commit comments

Comments
 (0)