Skip to content

Commit c2e6c03

Browse files
authored
http: Use ExtractParam in NewNormalizeUri (#2245)
This change refactors `http::NormalizeUri` to take an `ExtractParam` for extracting the default authority, rather than requiring `T: Param<DefaultAuthority>`. This way, we can remove the wrapper target type that's used only to temporarily add this `Param` impl.
1 parent 50b106b commit c2e6c03

File tree

2 files changed

+27
-28
lines changed

2 files changed

+27
-28
lines changed

linkerd/app/outbound/src/http/server.rs

+8-19
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,16 @@ impl<N> Outbound<N> {
7878
.push(http_tracing::server(rt.span_sink.clone(), trace_labels()))
7979
.push(http::BoxResponse::layer()),
8080
)
81-
.push_map_target(|Target(t)| t)
8281
// Convert origin form HTTP/1 URIs to absolute form for Hyper's
8382
// `Client`.
84-
.push(http::NewNormalizeUri::layer())
85-
.push_map_target(Target)
83+
.push(http::NewNormalizeUri::layer_via(|target: &T| {
84+
let addr = match target.param() {
85+
Logical::Route(addr, _) => Addr::from(addr),
86+
Logical::Forward(Remote(ServerAddr(addr)), _) => Addr::from(addr),
87+
};
88+
89+
http::normalize_uri::DefaultAuthority(Some(addr.to_http_authority()))
90+
}))
8691
// Record when a HTTP/1 URI originated in absolute form
8792
.push_on_service(http::normalize_uri::MarkAbsoluteForm::layer())
8893
.push(svc::ArcNewService::layer())
@@ -119,22 +124,6 @@ impl<N> Outbound<N> {
119124
}
120125
}
121126

122-
// === impl Target ===
123-
124-
impl<T> svc::Param<http::normalize_uri::DefaultAuthority> for Target<T>
125-
where
126-
T: svc::Param<Logical>,
127-
{
128-
fn param(&self) -> http::normalize_uri::DefaultAuthority {
129-
let addr = match self.0.param() {
130-
Logical::Route(addr, _) => Addr::from(addr),
131-
Logical::Forward(Remote(ServerAddr(addr)), _) => Addr::from(addr),
132-
};
133-
134-
http::normalize_uri::DefaultAuthority(Some(addr.to_http_authority()))
135-
}
136-
}
137-
138127
// === impl ServerRescue ===
139128

140129
impl ServerRescue {

linkerd/proxy/http/src/normalize_uri.rs

+19-9
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ use super::h1;
1818
use futures::{future, TryFutureExt};
1919
use http::uri::Authority;
2020
use linkerd_error::Error;
21-
use linkerd_stack::{layer, NewService, Param};
21+
use linkerd_stack::{layer, ExtractParam, NewService};
2222
use std::task::{Context, Poll};
2323
use thiserror::Error;
2424
use tracing::trace;
2525

2626
#[derive(Clone, Debug)]
27-
pub struct NewNormalizeUri<N> {
27+
pub struct NewNormalizeUri<X, N> {
28+
extract: X,
2829
inner: N,
2930
}
3031

@@ -51,25 +52,34 @@ pub struct MarkAbsoluteForm<S> {
5152

5253
// === impl NewNormalizeUri ===
5354

54-
impl<N> NewNormalizeUri<N> {
55+
impl<N> NewNormalizeUri<(), N> {
5556
pub fn layer() -> impl layer::Layer<N, Service = Self> + Copy + Clone {
56-
layer::mk(Self::new)
57+
layer::mk(|inner| Self::new((), inner))
5758
}
59+
}
5860

59-
fn new(inner: N) -> Self {
60-
Self { inner }
61+
impl<X, N> NewNormalizeUri<X, N> {
62+
pub fn layer_via(extract: X) -> impl layer::Layer<N, Service = Self> + Clone
63+
where
64+
X: Clone,
65+
{
66+
layer::mk(move |inner| Self::new(extract.clone(), inner))
67+
}
68+
69+
fn new(extract: X, inner: N) -> Self {
70+
Self { inner, extract }
6171
}
6272
}
6373

64-
impl<T, N> NewService<T> for NewNormalizeUri<N>
74+
impl<T, X, N> NewService<T> for NewNormalizeUri<X, N>
6575
where
66-
T: Param<DefaultAuthority>,
76+
X: ExtractParam<DefaultAuthority, T>,
6777
N: NewService<T>,
6878
{
6979
type Service = NormalizeUri<N::Service>;
7080

7181
fn new_service(&self, target: T) -> Self::Service {
72-
let DefaultAuthority(default) = target.param();
82+
let DefaultAuthority(default) = self.extract.extract_param(&target);
7383
let inner = self.inner.new_service(target);
7484
NormalizeUri::new(inner, default)
7585
}

0 commit comments

Comments
 (0)