Skip to content

Commit 8aee0ea

Browse files
committed
refactor(proxy/http): create linkerd-http-override-authority crate
NB: based on #3379 and #3380. this pull the `override_authority` submodule out of `linkerd-http-proxy` and into a standalone crate. Signed-off-by: katelyn martin <[email protected]>
1 parent 520d264 commit 8aee0ea

File tree

7 files changed

+57
-25
lines changed

7 files changed

+57
-25
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,6 +1753,16 @@ dependencies = [
17531753
"tracing",
17541754
]
17551755

1756+
[[package]]
1757+
name = "linkerd-http-override-authority"
1758+
version = "0.1.0"
1759+
dependencies = [
1760+
"http",
1761+
"linkerd-stack",
1762+
"tower",
1763+
"tracing",
1764+
]
1765+
17561766
[[package]]
17571767
name = "linkerd-http-prom"
17581768
version = "0.1.0"
@@ -2178,6 +2188,7 @@ dependencies = [
21782188
"linkerd-http-executor",
21792189
"linkerd-http-h2",
21802190
"linkerd-http-insert",
2191+
"linkerd-http-override-authority",
21812192
"linkerd-http-version",
21822193
"linkerd-io",
21832194
"linkerd-proxy-balance",

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ members = [
8585
"opencensus-proto",
8686
"opentelemetry-proto",
8787
"spiffe-proto",
88-
"tools",
88+
"tools", "linkerd/http/override-authority",
8989
]
9090

9191
[profile.release]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "linkerd-http-override-authority"
3+
version = "0.1.0"
4+
authors = ["Linkerd Developers <[email protected]>"]
5+
license = "Apache-2.0"
6+
edition = "2021"
7+
publish = false
8+
description = """
9+
Tower middleware to override request authorities.
10+
"""
11+
12+
[dependencies]
13+
http = "0.2"
14+
tower = { version = "0.4", default-features = false }
15+
tracing = "0.1"
16+
17+
linkerd-stack = { path = "../../stack" }

linkerd/proxy/http/src/override_authority.rs renamed to linkerd/http/override-authority/src/lib.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use http::{header::AsHeaderName, uri::Authority};
1+
use http::{
2+
header::AsHeaderName,
3+
uri::{self, Authority},
4+
};
25
use linkerd_stack::{layer, NewService, Param};
36
use std::{
47
fmt,
@@ -23,6 +26,27 @@ pub struct OverrideAuthority<S, H> {
2326
inner: S,
2427
}
2528

29+
/// Sets the [`Authority`][uri::Authority] of the given URI.
30+
pub fn set_authority(uri: &mut uri::Uri, auth: Authority) {
31+
let mut parts = uri::Parts::from(std::mem::take(uri));
32+
33+
parts.authority = Some(auth);
34+
35+
// If this was an origin-form target (path only),
36+
// then we can't *only* set the authority, as that's
37+
// an illegal target (such as `example.com/docs`).
38+
//
39+
// But don't set a scheme if this was authority-form (CONNECT),
40+
// since that would change its meaning (like `https://example.com`).
41+
if parts.path_and_query.is_some() {
42+
parts.scheme = Some(http::uri::Scheme::HTTP);
43+
}
44+
45+
let new = http::uri::Uri::from_parts(parts).expect("absolute uri");
46+
47+
*uri = new;
48+
}
49+
2650
// === impl NewOverrideAuthority ===
2751

2852
impl<H: Clone, N> NewOverrideAuthority<H, N> {

linkerd/proxy/http/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ linkerd-http-classify = { path = "../../http/classify" }
4646
linkerd-http-executor = { path = "../../http/executor" }
4747
linkerd-http-h2 = { path = "../../http/h2" }
4848
linkerd-http-insert = { path = "../../http/insert" }
49+
linkerd-http-override-authority = { path = "../../http/override-authority" }
4950
linkerd-http-version = { path = "../../http/version" }
5051
linkerd-io = { path = "../../io" }
5152
linkerd-proxy-balance = { path = "../balance" }

linkerd/proxy/http/src/lib.rs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ pub mod h2;
1515
mod header_from_target;
1616
pub mod normalize_uri;
1717
pub mod orig_proto;
18-
mod override_authority;
1918
mod retain;
2019
mod server;
2120
pub mod stream_timeouts;
@@ -33,7 +32,6 @@ pub use self::{
3332
detect::DetectHttp,
3433
header_from_target::NewHeaderFromTarget,
3534
normalize_uri::{MarkAbsoluteForm, NewNormalizeUri},
36-
override_authority::{AuthorityOverride, NewOverrideAuthority},
3735
retain::Retain,
3836
server::{NewServeHttp, Params as ServerParams, ServeHttp},
3937
stream_timeouts::{EnforceTimeouts, StreamTimeouts},
@@ -48,6 +46,7 @@ pub use hyper::body::HttpBody;
4846
pub use linkerd_http_box::{BoxBody, BoxRequest, BoxResponse, EraseResponse};
4947
pub use linkerd_http_executor::TracingExecutor;
5048
pub use linkerd_http_insert as insert;
49+
pub use linkerd_http_override_authority::{AuthorityOverride, NewOverrideAuthority};
5150
pub use linkerd_http_version::{self as version, Version};
5251

5352
#[derive(Clone, Debug)]
@@ -94,26 +93,6 @@ where
9493
v.to_str().ok()?.parse().ok()
9594
}
9695

97-
fn set_authority(uri: &mut uri::Uri, auth: uri::Authority) {
98-
let mut parts = uri::Parts::from(std::mem::take(uri));
99-
100-
parts.authority = Some(auth);
101-
102-
// If this was an origin-form target (path only),
103-
// then we can't *only* set the authority, as that's
104-
// an illegal target (such as `example.com/docs`).
105-
//
106-
// But don't set a scheme if this was authority-form (CONNECT),
107-
// since that would change its meaning (like `https://example.com`).
108-
if parts.path_and_query.is_some() {
109-
parts.scheme = Some(http::uri::Scheme::HTTP);
110-
}
111-
112-
let new = http::uri::Uri::from_parts(parts).expect("absolute uri");
113-
114-
*uri = new;
115-
}
116-
11796
fn strip_connection_headers(headers: &mut http::HeaderMap) {
11897
if let Some(val) = headers.remove(header::CONNECTION) {
11998
if let Ok(conn_header) = val.to_str() {

linkerd/proxy/http/src/normalize_uri.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ where
123123
};
124124

125125
trace!(%authority, "Normalizing URI");
126-
crate::set_authority(req.uri_mut(), authority);
126+
linkerd_http_override_authority::set_authority(req.uri_mut(), authority);
127127
}
128128
}
129129

0 commit comments

Comments
 (0)