Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: use ExtractParam in NewNormalizeUri #2245

Merged
merged 1 commit into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 8 additions & 19 deletions linkerd/app/outbound/src/http/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,16 @@ impl<N> Outbound<N> {
.push(http_tracing::server(rt.span_sink.clone(), trace_labels()))
.push(http::BoxResponse::layer()),
)
.push_map_target(|Target(t)| t)
// Convert origin form HTTP/1 URIs to absolute form for Hyper's
// `Client`.
.push(http::NewNormalizeUri::layer())
.push_map_target(Target)
.push(http::NewNormalizeUri::layer_via(|target: &T| {
let addr = match target.param() {
Logical::Route(addr, _) => Addr::from(addr),
Logical::Forward(Remote(ServerAddr(addr)), _) => Addr::from(addr),
};

http::normalize_uri::DefaultAuthority(Some(addr.to_http_authority()))
}))
// Record when a HTTP/1 URI originated in absolute form
.push_on_service(http::normalize_uri::MarkAbsoluteForm::layer())
.push(svc::ArcNewService::layer())
Expand Down Expand Up @@ -119,22 +124,6 @@ impl<N> Outbound<N> {
}
}

// === impl Target ===

impl<T> svc::Param<http::normalize_uri::DefaultAuthority> for Target<T>
where
T: svc::Param<Logical>,
{
fn param(&self) -> http::normalize_uri::DefaultAuthority {
let addr = match self.0.param() {
Logical::Route(addr, _) => Addr::from(addr),
Logical::Forward(Remote(ServerAddr(addr)), _) => Addr::from(addr),
};

http::normalize_uri::DefaultAuthority(Some(addr.to_http_authority()))
}
}

// === impl ServerRescue ===

impl ServerRescue {
Expand Down
28 changes: 19 additions & 9 deletions linkerd/proxy/http/src/normalize_uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ use super::h1;
use futures::{future, TryFutureExt};
use http::uri::Authority;
use linkerd_error::Error;
use linkerd_stack::{layer, NewService, Param};
use linkerd_stack::{layer, ExtractParam, NewService};
use std::task::{Context, Poll};
use thiserror::Error;
use tracing::trace;

#[derive(Clone, Debug)]
pub struct NewNormalizeUri<N> {
pub struct NewNormalizeUri<X, N> {
extract: X,
inner: N,
}

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

// === impl NewNormalizeUri ===

impl<N> NewNormalizeUri<N> {
impl<N> NewNormalizeUri<(), N> {
pub fn layer() -> impl layer::Layer<N, Service = Self> + Copy + Clone {
layer::mk(Self::new)
layer::mk(|inner| Self::new((), inner))
}
}

fn new(inner: N) -> Self {
Self { inner }
impl<X, N> NewNormalizeUri<X, N> {
pub fn layer_via(extract: X) -> impl layer::Layer<N, Service = Self> + Clone
where
X: Clone,
{
layer::mk(move |inner| Self::new(extract.clone(), inner))
}

fn new(extract: X, inner: N) -> Self {
Self { inner, extract }
}
}

impl<T, N> NewService<T> for NewNormalizeUri<N>
impl<T, X, N> NewService<T> for NewNormalizeUri<X, N>
where
T: Param<DefaultAuthority>,
X: ExtractParam<DefaultAuthority, T>,
N: NewService<T>,
{
type Service = NormalizeUri<N::Service>;

fn new_service(&self, target: T) -> Self::Service {
let DefaultAuthority(default) = target.param();
let DefaultAuthority(default) = self.extract.extract_param(&target);
let inner = self.inner.new_service(target);
NormalizeUri::new(inner, default)
}
Expand Down