Skip to content

Commit 7a90e4e

Browse files
committed
can disable user agent
1 parent cde96ac commit 7a90e4e

File tree

5 files changed

+48
-29
lines changed

5 files changed

+48
-29
lines changed

tonic/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ codegen = ["dep:async-trait"]
2424
gzip = ["dep:flate2"]
2525
deflate = ["dep:flate2"]
2626
zstd = ["dep:zstd"]
27-
default = ["router", "transport", "codegen", "prost"]
27+
default = ["router", "transport", "codegen", "prost", "user-agent"]
28+
user-agent = []
2829
prost = ["dep:prost"]
2930
_tls-any = ["dep:tokio-rustls", "dep:tokio", "tokio?/rt", "tokio?/macros"] # Internal. Please choose one of `tls-ring` or `tls-aws-lc`
3031
tls-ring = ["_tls-any", "tokio-rustls/ring"]

tonic/src/transport/channel/endpoint.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
1+
use std::{fmt, future::Future, net::IpAddr, pin::Pin, str, str::FromStr, time::Duration};
2+
3+
use bytes::Bytes;
4+
#[cfg(feature = "user-agent")]
5+
use http::uri::HeaderValue;
6+
use http::uri::Uri;
7+
use hyper::rt;
8+
use hyper_util::client::legacy::connect::HttpConnector;
9+
use tower_service::Service;
10+
111
#[cfg(feature = "_tls-any")]
212
use super::service::TlsConnector;
3-
use super::service::{self, Executor, SharedExec};
4-
use super::uds_connector::UdsConnector;
5-
use super::Channel;
613
#[cfg(feature = "_tls-any")]
714
use super::ClientTlsConfig;
15+
use super::{
16+
service::{self, Executor, SharedExec},
17+
uds_connector::UdsConnector,
18+
Channel,
19+
};
820
#[cfg(feature = "_tls-any")]
921
use crate::transport::error;
1022
use crate::transport::Error;
11-
use bytes::Bytes;
12-
use http::{uri::Uri, HeaderValue};
13-
use hyper::rt;
14-
use hyper_util::client::legacy::connect::HttpConnector;
15-
use std::{fmt, future::Future, net::IpAddr, pin::Pin, str, str::FromStr, time::Duration};
16-
use tower_service::Service;
1723

1824
#[derive(Clone, PartialEq, Eq, Hash)]
1925
pub(crate) enum EndpointType {
@@ -29,6 +35,7 @@ pub struct Endpoint {
2935
pub(crate) uri: EndpointType,
3036
fallback_uri: Uri,
3137
pub(crate) origin: Option<Uri>,
38+
#[cfg(feature = "user-agent")]
3239
pub(crate) user_agent: Option<HeaderValue>,
3340
pub(crate) timeout: Option<Duration>,
3441
pub(crate) concurrency_limit: Option<usize>,
@@ -76,6 +83,7 @@ impl Endpoint {
7683
uri: EndpointType::Uri(uri.clone()),
7784
fallback_uri: uri,
7885
origin: None,
86+
#[cfg(feature = "user-agent")]
7987
user_agent: None,
8088
concurrency_limit: None,
8189
rate_limit: None,
@@ -105,6 +113,7 @@ impl Endpoint {
105113
uri: EndpointType::Uds(uds_filepath.to_string()),
106114
fallback_uri: Uri::from_static("http://tonic"),
107115
origin: None,
116+
#[cfg(feature = "user-agent")]
108117
user_agent: None,
109118
concurrency_limit: None,
110119
rate_limit: None,
@@ -185,6 +194,7 @@ impl Endpoint {
185194
/// builder.user_agent("Greeter").expect("Greeter should be a valid header value");
186195
/// // user-agent: "Greeter tonic/x.x.x"
187196
/// ```
197+
#[cfg(feature = "user-agent")]
188198
pub fn user_agent<T>(self, user_agent: T) -> Result<Self, Error>
189199
where
190200
T: TryInto<HeaderValue>,

tonic/src/transport/channel/service/connection.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
1-
use super::{AddOrigin, Reconnect, SharedExec, UserAgent};
2-
use crate::{
3-
body::Body,
4-
transport::{channel::BoxFuture, service::GrpcTimeout, Endpoint},
5-
};
6-
use http::{Request, Response, Uri};
7-
use hyper::rt;
8-
use hyper::{client::conn::http2::Builder, rt::Executor};
9-
use hyper_util::rt::TokioTimer;
101
use std::{
112
fmt,
123
task::{Context, Poll},
134
};
14-
use tower::load::Load;
5+
6+
use http::{Request, Response, Uri};
7+
use hyper::{client::conn::http2::Builder, rt, rt::Executor};
8+
use hyper_util::rt::TokioTimer;
159
use tower::{
1610
layer::Layer,
1711
limit::{concurrency::ConcurrencyLimitLayer, rate::RateLimitLayer},
12+
load::Load,
1813
util::BoxService,
1914
ServiceBuilder, ServiceExt,
2015
};
2116
use tower_service::Service;
2217

18+
#[cfg(feature = "user-agent")]
19+
use super::UserAgent;
20+
use super::{AddOrigin, Reconnect, SharedExec};
21+
use crate::{
22+
body::Body,
23+
transport::{channel::BoxFuture, service::GrpcTimeout, Endpoint},
24+
};
25+
2326
pub(crate) struct Connection {
2427
inner: BoxService<Request<Body>, Response<Body>, crate::BoxError>,
2528
}
@@ -55,13 +58,16 @@ impl Connection {
5558
settings.max_header_list_size(val);
5659
}
5760

58-
let stack = ServiceBuilder::new()
59-
.layer_fn(|s| {
60-
let origin = endpoint.origin.as_ref().unwrap_or(endpoint.uri()).clone();
61+
let stack = ServiceBuilder::new().layer_fn(|s| {
62+
let origin = endpoint.origin.as_ref().unwrap_or(endpoint.uri()).clone();
63+
64+
AddOrigin::new(s, origin)
65+
});
66+
67+
#[cfg(feature = "user-agent")]
68+
let stack = stack.layer_fn(|s| UserAgent::new(s, endpoint.user_agent.clone()));
6169

62-
AddOrigin::new(s, origin)
63-
})
64-
.layer_fn(|s| UserAgent::new(s, endpoint.user_agent.clone()))
70+
let stack = stack
6571
.layer_fn(|s| GrpcTimeout::new(s, endpoint.timeout))
6672
.option_layer(endpoint.concurrency_limit.map(ConcurrencyLimitLayer::new))
6773
.option_layer(endpoint.rate_limit.map(|(l, d)| RateLimitLayer::new(l, d)))

tonic/src/transport/channel/service/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
mod add_origin;
22
use self::add_origin::AddOrigin;
33

4+
#[cfg(feature = "user-agent")]
45
mod user_agent;
6+
#[cfg(feature = "user-agent")]
57
use self::user_agent::UserAgent;
68

79
mod reconnect;

tonic/src/transport/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub(crate) enum Kind {
1717
Transport,
1818
#[cfg(feature = "channel")]
1919
InvalidUri,
20-
#[cfg(feature = "channel")]
20+
#[cfg(all(feature = "channel", feature = "user-agent"))]
2121
InvalidUserAgent,
2222
#[cfg(all(feature = "_tls-any", feature = "channel"))]
2323
InvalidTlsConfigForUds,
@@ -44,7 +44,7 @@ impl Error {
4444
Error::new(Kind::InvalidUri)
4545
}
4646

47-
#[cfg(feature = "channel")]
47+
#[cfg(all(feature = "channel", feature = "user-agent"))]
4848
pub(crate) fn new_invalid_user_agent() -> Self {
4949
Error::new(Kind::InvalidUserAgent)
5050
}
@@ -54,7 +54,7 @@ impl Error {
5454
Kind::Transport => "transport error",
5555
#[cfg(feature = "channel")]
5656
Kind::InvalidUri => "invalid URI",
57-
#[cfg(feature = "channel")]
57+
#[cfg(all(feature = "channel", feature = "user-agent"))]
5858
Kind::InvalidUserAgent => "user agent is not a valid header value",
5959
#[cfg(all(feature = "_tls-any", feature = "channel"))]
6060
Kind::InvalidTlsConfigForUds => "cannot apply TLS config for unix domain socket",

0 commit comments

Comments
 (0)