Skip to content

Commit cb9f511

Browse files
committed
refactor(proxy/http): create linkerd-http-upgrade crate
this moves the inter-related `upgrade` and `glue` submodules out of the `linkerd-proxy-http` library and into a new standalone crate. Signed-off-by: katelyn martin <[email protected]>
1 parent 9f5862f commit cb9f511

File tree

11 files changed

+107
-39
lines changed

11 files changed

+107
-39
lines changed

Cargo.lock

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,6 +1855,28 @@ dependencies = [
18551855
"tracing",
18561856
]
18571857

1858+
[[package]]
1859+
name = "linkerd-http-upgrade"
1860+
version = "0.1.0"
1861+
dependencies = [
1862+
"bytes",
1863+
"drain",
1864+
"futures",
1865+
"http",
1866+
"http-body",
1867+
"hyper",
1868+
"linkerd-duplex",
1869+
"linkerd-error",
1870+
"linkerd-http-version",
1871+
"linkerd-io",
1872+
"linkerd-stack",
1873+
"pin-project",
1874+
"tokio",
1875+
"tower",
1876+
"tracing",
1877+
"try-lock",
1878+
]
1879+
18581880
[[package]]
18591881
name = "linkerd-http-version"
18601882
version = "0.1.0"
@@ -2228,6 +2250,7 @@ dependencies = [
22282250
"linkerd-http-override-authority",
22292251
"linkerd-http-retain",
22302252
"linkerd-http-stream-timeouts",
2253+
"linkerd-http-upgrade",
22312254
"linkerd-http-version",
22322255
"linkerd-io",
22332256
"linkerd-proxy-balance",

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ members = [
3737
"linkerd/http/retry",
3838
"linkerd/http/route",
3939
"linkerd/http/stream-timeouts",
40+
"linkerd/http/upgrade",
4041
"linkerd/http/version",
4142
"linkerd/identity",
4243
"linkerd/idle-cache",

linkerd/http/upgrade/Cargo.toml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
[package]
2+
name = "linkerd-http-upgrade"
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+
Facilities for HTTP/1 upgrades.
10+
"""
11+
12+
[dependencies]
13+
bytes = "1"
14+
drain = "0.1"
15+
futures = { version = "0.3", default-features = false }
16+
http = "0.2"
17+
http-body = "0.4"
18+
hyper = { version = "0.14", default-features = false, features = ["client"] }
19+
pin-project = "1"
20+
tokio = { version = "1", default-features = false }
21+
tower = { version = "0.4", default-features = false }
22+
tracing = "0.1"
23+
try-lock = "0.2"
24+
25+
linkerd-duplex = { path = "../../duplex" }
26+
linkerd-error = { path = "../../error" }
27+
linkerd-http-version = { path = "../version" }
28+
linkerd-io = { path = "../../io" }
29+
linkerd-stack = { path = "../../stack" }

linkerd/proxy/http/src/glue.rs renamed to linkerd/http/upgrade/src/glue.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub struct HyperConnectFuture<F> {
4747
inner: F,
4848
absolute_form: bool,
4949
}
50+
5051
// === impl UpgradeBody ===
5152

5253
impl HttpBody for UpgradeBody {
@@ -107,7 +108,7 @@ impl From<hyper::Body> for UpgradeBody {
107108
}
108109

109110
impl UpgradeBody {
110-
pub(crate) fn new(
111+
pub fn new(
111112
body: hyper::Body,
112113
upgrade: Option<(Http11Upgrade, hyper::upgrade::OnUpgrade)>,
113114
) -> Self {
@@ -129,7 +130,7 @@ impl PinnedDrop for UpgradeBody {
129130
// === impl HyperConnect ===
130131

131132
impl<C, T> HyperConnect<C, T> {
132-
pub(super) fn new(connect: C, target: T, absolute_form: bool) -> Self {
133+
pub fn new(connect: C, target: T, absolute_form: bool) -> Self {
133134
HyperConnect {
134135
connect,
135136
target,
@@ -140,7 +141,7 @@ impl<C, T> HyperConnect<C, T> {
140141

141142
impl<C, T> Service<hyper::Uri> for HyperConnect<C, T>
142143
where
143-
C: MakeConnection<(crate::Version, T)> + Clone + Send + Sync,
144+
C: MakeConnection<(linkerd_http_version::Version, T)> + Clone + Send + Sync,
144145
C::Connection: Unpin + Send,
145146
C::Future: Unpin + Send + 'static,
146147
T: Clone + Send + Sync,
@@ -157,7 +158,7 @@ where
157158
HyperConnectFuture {
158159
inner: self
159160
.connect
160-
.connect((crate::Version::Http1, self.target.clone())),
161+
.connect((linkerd_http_version::Version::Http1, self.target.clone())),
161162
absolute_form: self.absolute_form,
162163
}
163164
}

linkerd/http/upgrade/src/lib.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//! Facilities for HTTP/1 upgrades.
2+
3+
pub use self::upgrade::Service;
4+
5+
pub mod glue;
6+
pub mod upgrade;
7+
8+
pub fn strip_connection_headers(headers: &mut http::HeaderMap) {
9+
use http::header;
10+
if let Some(val) = headers.remove(header::CONNECTION) {
11+
if let Ok(conn_header) = val.to_str() {
12+
// A `Connection` header may have a comma-separated list of
13+
// names of other headers that are meant for only this specific
14+
// connection.
15+
//
16+
// Iterate these names and remove them as headers.
17+
for name in conn_header.split(',') {
18+
let name = name.trim();
19+
headers.remove(name);
20+
}
21+
}
22+
}
23+
24+
// Additionally, strip these "connection-level" headers always, since
25+
// they are otherwise illegal if upgraded to HTTP2.
26+
headers.remove(header::UPGRADE);
27+
headers.remove("proxy-connection");
28+
headers.remove("keep-alive");
29+
}

linkerd/proxy/http/src/upgrade.rs renamed to linkerd/http/upgrade/src/upgrade.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ impl Drop for Inner {
162162
}
163163

164164
// === impl Service ===
165+
165166
impl<S> Service<S> {
166167
pub fn new(service: S, upgrade_drain_signal: drain::Watch) -> Self {
167168
Self {

linkerd/proxy/http/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ linkerd-http-insert = { path = "../../http/insert" }
4949
linkerd-http-override-authority = { path = "../../http/override-authority" }
5050
linkerd-http-retain = { path = "../../http/retain" }
5151
linkerd-http-stream-timeouts = { path = "../../http/stream-timeouts" }
52+
linkerd-http-upgrade = { path = "../../http/upgrade" }
5253
linkerd-http-version = { path = "../../http/version" }
5354
linkerd-io = { path = "../../io" }
5455
linkerd-proxy-balance = { path = "../balance" }

linkerd/proxy/http/src/h1.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
use crate::{
2-
glue::HyperConnect,
3-
upgrade::{Http11Upgrade, HttpConnect},
4-
TracingExecutor,
5-
};
1+
use crate::TracingExecutor;
62
use futures::prelude::*;
73
use http::{
84
header::{CONTENT_LENGTH, TRANSFER_ENCODING},
95
uri::Uri,
106
};
117
use linkerd_error::{Error, Result};
128
use linkerd_http_box::BoxBody;
9+
use linkerd_http_upgrade::{
10+
glue::HyperConnect,
11+
upgrade::{Http11Upgrade, HttpConnect},
12+
};
1313
use linkerd_stack::MakeConnection;
1414
use std::{pin::Pin, time::Duration};
1515
use tracing::{debug, trace};
@@ -161,7 +161,7 @@ where
161161
upgrade.insert_half(hyper::upgrade::on(&mut rsp));
162162
}
163163
} else {
164-
crate::strip_connection_headers(rsp.headers_mut());
164+
linkerd_http_upgrade::strip_connection_headers(rsp.headers_mut());
165165
}
166166

167167
rsp.map(BoxBody::new)

linkerd/proxy/http/src/lib.rs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ pub mod balance;
88
pub mod client;
99
pub mod client_handle;
1010
pub mod detect;
11-
mod glue;
1211
pub mod h1;
1312
pub mod h2;
1413
mod header_from_target;
@@ -17,7 +16,6 @@ pub mod orig_proto;
1716
mod server;
1817
pub mod strip_header;
1918
pub mod timeout;
20-
pub mod upgrade;
2119

2220
pub use self::{
2321
balance::NewBalance,
@@ -45,6 +43,7 @@ pub use linkerd_http_insert as insert;
4543
pub use linkerd_http_override_authority::{AuthorityOverride, NewOverrideAuthority};
4644
pub use linkerd_http_retain::{self as retain, Retain};
4745
pub use linkerd_http_stream_timeouts::{self as stream_timeouts, EnforceTimeouts, StreamTimeouts};
46+
pub use linkerd_http_upgrade as upgrade;
4847
pub use linkerd_http_version::{self as version, Version};
4948

5049
#[derive(Clone, Debug)]
@@ -90,25 +89,3 @@ where
9089
let v = req.headers().get(header)?;
9190
v.to_str().ok()?.parse().ok()
9291
}
93-
94-
fn strip_connection_headers(headers: &mut http::HeaderMap) {
95-
if let Some(val) = headers.remove(header::CONNECTION) {
96-
if let Ok(conn_header) = val.to_str() {
97-
// A `Connection` header may have a comma-separated list of
98-
// names of other headers that are meant for only this specific
99-
// connection.
100-
//
101-
// Iterate these names and remove them as headers.
102-
for name in conn_header.split(',') {
103-
let name = name.trim();
104-
headers.remove(name);
105-
}
106-
}
107-
}
108-
109-
// Additionally, strip these "connection-level" headers always, since
110-
// they are otherwise illegal if upgraded to HTTP2.
111-
headers.remove(header::UPGRADE);
112-
headers.remove("proxy-connection");
113-
headers.remove("keep-alive");
114-
}

linkerd/proxy/http/src/orig_proto.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{h1, h2, upgrade};
1+
use super::{h1, h2};
22
use futures::prelude::*;
33
use http::header::{HeaderValue, TRANSFER_ENCODING};
44
use hyper::body::HttpBody;
@@ -71,7 +71,11 @@ where
7171

7272
fn call(&mut self, mut req: http::Request<B>) -> Self::Future {
7373
debug_assert!(req.version() != http::Version::HTTP_2);
74-
if req.extensions().get::<upgrade::Http11Upgrade>().is_some() {
74+
if req
75+
.extensions()
76+
.get::<linkerd_http_upgrade::upgrade::Http11Upgrade>()
77+
.is_some()
78+
{
7579
debug!("Skipping orig-proto upgrade due to HTTP/1.1 upgrade");
7680
return Box::pin(self.http1.request(req).map_ok(|rsp| rsp.map(BoxBody::new)));
7781
}

0 commit comments

Comments
 (0)