Skip to content

feat(transport): Add disable user-agent #2334

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion tonic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ codegen = ["dep:async-trait"]
gzip = ["dep:flate2"]
deflate = ["dep:flate2"]
zstd = ["dep:zstd"]
default = ["router", "transport", "codegen", "prost"]
default = ["router", "transport", "codegen", "prost", "user-agent"]
user-agent = []
prost = ["dep:prost"]
_tls-any = ["dep:tokio-rustls", "dep:tokio", "tokio?/rt", "tokio?/macros"] # Internal. Please choose one of `tls-ring` or `tls-aws-lc`
tls-ring = ["_tls-any", "tokio-rustls/ring"]
Expand Down
28 changes: 19 additions & 9 deletions tonic/src/transport/channel/endpoint.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
use std::{fmt, future::Future, net::IpAddr, pin::Pin, str, str::FromStr, time::Duration};

use bytes::Bytes;
use http::uri::Uri;
#[cfg(feature = "user-agent")]
use http::HeaderValue;
use hyper::rt;
use hyper_util::client::legacy::connect::HttpConnector;
use tower_service::Service;

#[cfg(feature = "_tls-any")]
use super::service::TlsConnector;
use super::service::{self, Executor, SharedExec};
use super::uds_connector::UdsConnector;
use super::Channel;
#[cfg(feature = "_tls-any")]
use super::ClientTlsConfig;
use super::{
service::{self, Executor, SharedExec},
uds_connector::UdsConnector,
Channel,
};
#[cfg(feature = "_tls-any")]
use crate::transport::error;
use crate::transport::Error;
use bytes::Bytes;
use http::{uri::Uri, HeaderValue};
use hyper::rt;
use hyper_util::client::legacy::connect::HttpConnector;
use std::{fmt, future::Future, net::IpAddr, pin::Pin, str, str::FromStr, time::Duration};
use tower_service::Service;

#[derive(Clone, PartialEq, Eq, Hash)]
pub(crate) enum EndpointType {
Expand All @@ -29,6 +35,7 @@ pub struct Endpoint {
pub(crate) uri: EndpointType,
fallback_uri: Uri,
pub(crate) origin: Option<Uri>,
#[cfg(feature = "user-agent")]
pub(crate) user_agent: Option<HeaderValue>,
pub(crate) timeout: Option<Duration>,
pub(crate) concurrency_limit: Option<usize>,
Expand Down Expand Up @@ -76,6 +83,7 @@ impl Endpoint {
uri: EndpointType::Uri(uri.clone()),
fallback_uri: uri,
origin: None,
#[cfg(feature = "user-agent")]
user_agent: None,
concurrency_limit: None,
rate_limit: None,
Expand Down Expand Up @@ -105,6 +113,7 @@ impl Endpoint {
uri: EndpointType::Uds(uds_filepath.to_string()),
fallback_uri: Uri::from_static("http://tonic"),
origin: None,
#[cfg(feature = "user-agent")]
user_agent: None,
concurrency_limit: None,
rate_limit: None,
Expand Down Expand Up @@ -185,6 +194,7 @@ impl Endpoint {
/// builder.user_agent("Greeter").expect("Greeter should be a valid header value");
/// // user-agent: "Greeter tonic/x.x.x"
/// ```
#[cfg(feature = "user-agent")]
pub fn user_agent<T>(self, user_agent: T) -> Result<Self, Error>
where
T: TryInto<HeaderValue>,
Expand Down
38 changes: 22 additions & 16 deletions tonic/src/transport/channel/service/connection.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
use super::{AddOrigin, Reconnect, SharedExec, UserAgent};
use crate::{
body::Body,
transport::{channel::BoxFuture, service::GrpcTimeout, Endpoint},
};
use http::{Request, Response, Uri};
use hyper::rt;
use hyper::{client::conn::http2::Builder, rt::Executor};
use hyper_util::rt::TokioTimer;
use std::{
fmt,
task::{Context, Poll},
};
use tower::load::Load;

use http::{Request, Response, Uri};
use hyper::{client::conn::http2::Builder, rt, rt::Executor};
use hyper_util::rt::TokioTimer;
use tower::{
layer::Layer,
limit::{concurrency::ConcurrencyLimitLayer, rate::RateLimitLayer},
load::Load,
util::BoxService,
ServiceBuilder, ServiceExt,
};
use tower_service::Service;

#[cfg(feature = "user-agent")]
use super::UserAgent;
use super::{AddOrigin, Reconnect, SharedExec};
use crate::{
body::Body,
transport::{channel::BoxFuture, service::GrpcTimeout, Endpoint},
};

pub(crate) struct Connection {
inner: BoxService<Request<Body>, Response<Body>, crate::BoxError>,
}
Expand Down Expand Up @@ -55,13 +58,16 @@ impl Connection {
settings.max_header_list_size(val);
}

let stack = ServiceBuilder::new()
.layer_fn(|s| {
let origin = endpoint.origin.as_ref().unwrap_or(endpoint.uri()).clone();
let stack = ServiceBuilder::new().layer_fn(|s| {
let origin = endpoint.origin.as_ref().unwrap_or(endpoint.uri()).clone();

AddOrigin::new(s, origin)
});

#[cfg(feature = "user-agent")]
let stack = stack.layer_fn(|s| UserAgent::new(s, endpoint.user_agent.clone()));

AddOrigin::new(s, origin)
})
.layer_fn(|s| UserAgent::new(s, endpoint.user_agent.clone()))
let stack = stack
.layer_fn(|s| GrpcTimeout::new(s, endpoint.timeout))
.option_layer(endpoint.concurrency_limit.map(ConcurrencyLimitLayer::new))
.option_layer(endpoint.rate_limit.map(|(l, d)| RateLimitLayer::new(l, d)))
Expand Down
2 changes: 2 additions & 0 deletions tonic/src/transport/channel/service/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
mod add_origin;
use self::add_origin::AddOrigin;

#[cfg(feature = "user-agent")]
mod user_agent;
#[cfg(feature = "user-agent")]
use self::user_agent::UserAgent;

mod reconnect;
Expand Down
6 changes: 3 additions & 3 deletions tonic/src/transport/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub(crate) enum Kind {
Transport,
#[cfg(feature = "channel")]
InvalidUri,
#[cfg(feature = "channel")]
#[cfg(all(feature = "channel", feature = "user-agent"))]
InvalidUserAgent,
#[cfg(all(feature = "_tls-any", feature = "channel"))]
InvalidTlsConfigForUds,
Expand All @@ -44,7 +44,7 @@ impl Error {
Error::new(Kind::InvalidUri)
}

#[cfg(feature = "channel")]
#[cfg(all(feature = "channel", feature = "user-agent"))]
pub(crate) fn new_invalid_user_agent() -> Self {
Error::new(Kind::InvalidUserAgent)
}
Expand All @@ -54,7 +54,7 @@ impl Error {
Kind::Transport => "transport error",
#[cfg(feature = "channel")]
Kind::InvalidUri => "invalid URI",
#[cfg(feature = "channel")]
#[cfg(all(feature = "channel", feature = "user-agent"))]
Kind::InvalidUserAgent => "user agent is not a valid header value",
#[cfg(all(feature = "_tls-any", feature = "channel"))]
Kind::InvalidTlsConfigForUds => "cannot apply TLS config for unix domain socket",
Expand Down
Loading