|
| 1 | +use http::{header::USER_AGENT, HeaderValue, Request, Uri}; |
| 2 | +use std::task::{Context, Poll}; |
| 3 | +use tower_service::Service; |
| 4 | +use crate::body::Body; |
| 5 | + |
| 6 | +#[derive(Debug)] |
| 7 | +pub(crate) struct Modifier<M, T> { |
| 8 | + modifier_fn: M, |
| 9 | + next: T, |
| 10 | +} |
| 11 | + |
| 12 | +impl<M, T> Modifier<M, T> { |
| 13 | + pub(crate) fn new(next: T, modifier_fn: M) -> Self { |
| 14 | + Self { next, modifier_fn } |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +impl<M, Body, T> Service<Request<Body>> for Modifier<M, T> |
| 19 | +where |
| 20 | + T: Service<Request<Body>>, |
| 21 | + M: FnOnce(Request<Body>) -> Request<Body> + Clone, |
| 22 | + Body: Send + 'static, |
| 23 | +{ |
| 24 | + type Response = T::Response; |
| 25 | + type Error = T::Error; |
| 26 | + type Future = T::Future; |
| 27 | + |
| 28 | + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { |
| 29 | + self.next.poll_ready(cx) |
| 30 | + } |
| 31 | + |
| 32 | + fn call(&mut self, req: Request<Body>) -> Self::Future { |
| 33 | + let modifier_fn = self.modifier_fn.clone(); |
| 34 | + self.next.call(modifier_fn(req)) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// We're borrowing to avoid cloning the Uri more than once in layer which expects Fn |
| 39 | +// and not FnOnce |
| 40 | +#[derive(Debug, Clone)] |
| 41 | +pub(crate) struct AddOrigin<'a> { |
| 42 | + origin: &'a Uri |
| 43 | +} |
| 44 | + |
| 45 | +impl<'a> AddOrigin<'a> { |
| 46 | + pub(crate) fn new(origin: &'a Uri) -> Result<Self, crate::BoxError> { |
| 47 | + // We catch error right at initiation... This single line |
| 48 | + // eliminates countless heap allocations at `runtime` |
| 49 | + if origin.scheme().is_none() || origin.authority().is_none() { |
| 50 | + return Err(crate::transport::Error::new_invalid_uri().into()); |
| 51 | + } |
| 52 | + |
| 53 | + Ok(Self { origin }) |
| 54 | + } |
| 55 | + |
| 56 | + pub(crate) fn into_fn( |
| 57 | + self, |
| 58 | + ) -> impl FnOnce(Request<Body>) -> Request<Body> + Clone { |
| 59 | + let http::uri::Parts { |
| 60 | + scheme, authority, .. |
| 61 | + } = self.origin.clone().into_parts(); |
| 62 | + |
| 63 | + // Both have been checked |
| 64 | + let scheme = scheme.unwrap(); |
| 65 | + let authority = authority.unwrap(); |
| 66 | + |
| 67 | + move |req| { |
| 68 | + // Split the request into the head and the body. |
| 69 | + let (mut head, body) = req.into_parts(); |
| 70 | + |
| 71 | + // Update the request URI |
| 72 | + head.uri = { |
| 73 | + // Split the request URI into parts. |
| 74 | + let mut uri: http::uri::Parts = head.uri.into(); |
| 75 | + // Update the URI parts, setting the scheme and authority |
| 76 | + uri.scheme = Some(scheme); |
| 77 | + uri.authority = Some(authority); |
| 78 | + |
| 79 | + http::Uri::from_parts(uri).expect("valid uri") |
| 80 | + }; |
| 81 | + |
| 82 | + Request::from_parts(head, body) |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +const TONIC_USER_AGENT: &str = concat!("tonic/", env!("CARGO_PKG_VERSION")); |
| 88 | + |
| 89 | +#[derive(Debug)] |
| 90 | +pub(crate) struct UserAgent { |
| 91 | + user_agent: HeaderValue, |
| 92 | +} |
| 93 | + |
| 94 | +impl UserAgent { |
| 95 | + pub(crate) fn new(user_agent: Option<HeaderValue>) -> Self { |
| 96 | + let user_agent = user_agent |
| 97 | + .map(|value| { |
| 98 | + let mut buf = Vec::new(); |
| 99 | + buf.extend(value.as_bytes()); |
| 100 | + buf.push(b' '); |
| 101 | + buf.extend(TONIC_USER_AGENT.as_bytes()); |
| 102 | + HeaderValue::from_bytes(&buf).expect("user-agent should be valid") |
| 103 | + }) |
| 104 | + .unwrap_or_else(|| HeaderValue::from_static(TONIC_USER_AGENT)); |
| 105 | + |
| 106 | + Self { user_agent } |
| 107 | + } |
| 108 | + |
| 109 | + pub(crate) fn into_fn( |
| 110 | + self, |
| 111 | + ) -> impl FnOnce(Request<Body>) -> Request<Body> + Clone { |
| 112 | + move |mut req| { |
| 113 | + use http::header::Entry; |
| 114 | + |
| 115 | + // The former code uses try_insert so we'll respect that |
| 116 | + if let Ok(entry) = req.headers_mut().try_entry(USER_AGENT) { |
| 117 | + // This is to avoid anticipative cloning which happened |
| 118 | + // in the former code |
| 119 | + match entry { |
| 120 | + Entry::Vacant(vacant_entry) => { |
| 121 | + vacant_entry.insert(self.user_agent); |
| 122 | + } |
| 123 | + Entry::Occupied(occupied_entry) => { |
| 124 | + // The User-Agent header has already been set on the request. Let's |
| 125 | + // append our user agent to the end. |
| 126 | + let occupied_entry = occupied_entry.into_mut(); |
| 127 | + |
| 128 | + let mut buf = |
| 129 | + Vec::with_capacity(occupied_entry.len() + 1 + self.user_agent.len()); |
| 130 | + buf.extend(occupied_entry.as_bytes()); |
| 131 | + buf.push(b' '); |
| 132 | + buf.extend(self.user_agent.as_bytes()); |
| 133 | + |
| 134 | + // with try_into http uses from_shared internally to probably minimize |
| 135 | + // allocations |
| 136 | + *occupied_entry = buf.try_into().expect("user-agent should be valid") |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + req |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +#[cfg(test)] |
| 147 | +mod tests { |
| 148 | + use super::*; |
| 149 | + |
| 150 | + #[test] |
| 151 | + fn sets_default_if_no_custom_user_agent() { |
| 152 | + assert_eq!( |
| 153 | + UserAgent::new(None).user_agent, |
| 154 | + HeaderValue::from_static(TONIC_USER_AGENT) |
| 155 | + ) |
| 156 | + } |
| 157 | + |
| 158 | + #[test] |
| 159 | + fn prepends_custom_user_agent_to_default() { |
| 160 | + assert_eq!( |
| 161 | + UserAgent::new(Some(HeaderValue::from_static("Greeter 1.1"))).user_agent, |
| 162 | + HeaderValue::from_str(&format!("Greeter 1.1 {TONIC_USER_AGENT}")).unwrap() |
| 163 | + ) |
| 164 | + } |
| 165 | + |
| 166 | + async fn assert_user_agent_modified( |
| 167 | + genesis_user_agent: Option<impl TryInto<HeaderValue>>, |
| 168 | + expected_user_agent: impl TryInto<HeaderValue>, |
| 169 | + request: Option<Request<Body>>, |
| 170 | + ) { |
| 171 | + let ua = UserAgent::new(genesis_user_agent.map(|v| { |
| 172 | + v.try_into() |
| 173 | + .unwrap_or_else(|_| panic!("invalid header value")) |
| 174 | + })) |
| 175 | + .into_fn(); |
| 176 | + |
| 177 | + let modified_request = ua(request.unwrap_or_default()); |
| 178 | + let user_agent = modified_request.headers().get(USER_AGENT).unwrap(); |
| 179 | + assert_eq!( |
| 180 | + user_agent, |
| 181 | + expected_user_agent |
| 182 | + .try_into() |
| 183 | + .unwrap_or_else(|_| panic!("invalid header value")) |
| 184 | + ); |
| 185 | + } |
| 186 | + |
| 187 | + #[tokio::test] |
| 188 | + async fn sets_default_user_agent_if_none_present() { |
| 189 | + let genesis_user_agent = Option::<&str>::None; |
| 190 | + let expected_user_agent = TONIC_USER_AGENT.to_string(); |
| 191 | + let request = None; |
| 192 | + |
| 193 | + assert_user_agent_modified(genesis_user_agent, expected_user_agent, request).await |
| 194 | + } |
| 195 | + |
| 196 | + #[tokio::test] |
| 197 | + async fn sets_custom_user_agent_if_none_present() { |
| 198 | + let genesis_user_agent = Some("Greeter 1.1"); |
| 199 | + let expected_user_agent = format!("Greeter 1.1 {TONIC_USER_AGENT}"); |
| 200 | + let request = None; |
| 201 | + |
| 202 | + assert_user_agent_modified(genesis_user_agent, expected_user_agent, request).await |
| 203 | + } |
| 204 | + |
| 205 | + #[tokio::test] |
| 206 | + async fn appends_default_user_agent_to_request_fn_user_agent() { |
| 207 | + let genesis_user_agent = Option::<&str>::None; |
| 208 | + let expected_user_agent = format!("request-ua/x.y {TONIC_USER_AGENT}"); |
| 209 | + let mut request = Request::default(); |
| 210 | + request |
| 211 | + .headers_mut() |
| 212 | + .insert(USER_AGENT, HeaderValue::from_static("request-ua/x.y")); |
| 213 | + |
| 214 | + assert_user_agent_modified(genesis_user_agent, expected_user_agent, Some(request)).await |
| 215 | + } |
| 216 | + |
| 217 | + #[tokio::test] |
| 218 | + async fn appends_custom_user_agent_to_request_fn_user_agent() { |
| 219 | + let genesis_user_agent = Some("Greeter 1.1"); |
| 220 | + let expected_user_agent = format!("request-ua/x.y Greeter 1.1 {TONIC_USER_AGENT}"); |
| 221 | + let mut request = Request::default(); |
| 222 | + request |
| 223 | + .headers_mut() |
| 224 | + .insert(USER_AGENT, HeaderValue::from_static("request-ua/x.y")); |
| 225 | + |
| 226 | + assert_user_agent_modified(genesis_user_agent, expected_user_agent, Some(request)).await |
| 227 | + } |
| 228 | +} |
0 commit comments