|
| 1 | +use super::h1; |
| 2 | +use futures::{try_ready, Future, Poll}; |
| 3 | +use http::{self, header::AsHeaderName, uri::Authority}; |
| 4 | +use std::fmt; |
| 5 | +use tracing::debug; |
| 6 | + |
| 7 | +pub trait CanOverrideAuthority { |
| 8 | + fn override_authority(&self) -> Option<Authority>; |
| 9 | +} |
| 10 | + |
| 11 | +#[derive(Debug, Clone)] |
| 12 | +pub struct Layer<H> { |
| 13 | + headers_to_strip: Vec<H>, |
| 14 | +} |
| 15 | + |
| 16 | +#[derive(Clone, Debug)] |
| 17 | +pub struct MakeSvc<H, M> { |
| 18 | + headers_to_strip: Vec<H>, |
| 19 | + inner: M, |
| 20 | +} |
| 21 | + |
| 22 | +pub struct MakeSvcFut<M, H> { |
| 23 | + authority: Option<Authority>, |
| 24 | + headers_to_strip: Vec<H>, |
| 25 | + inner: M, |
| 26 | +} |
| 27 | + |
| 28 | +#[derive(Clone, Debug)] |
| 29 | +pub struct Service<S, H> { |
| 30 | + authority: Option<Authority>, |
| 31 | + headers_to_strip: Vec<H>, |
| 32 | + inner: S, |
| 33 | +} |
| 34 | + |
| 35 | +// === impl Layer === |
| 36 | + |
| 37 | +impl<H> Layer<H> |
| 38 | +where |
| 39 | + H: AsHeaderName + Clone, |
| 40 | +{ |
| 41 | + pub fn new(headers_to_strip: Vec<H>) -> Self { |
| 42 | + Self { headers_to_strip } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl<H> Default for Layer<H> { |
| 47 | + fn default() -> Self { |
| 48 | + Self { |
| 49 | + headers_to_strip: Vec::default(), |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl<H, M> tower::layer::Layer<M> for Layer<H> |
| 55 | +where |
| 56 | + H: AsHeaderName + Clone, |
| 57 | +{ |
| 58 | + type Service = MakeSvc<H, M>; |
| 59 | + |
| 60 | + fn layer(&self, inner: M) -> Self::Service { |
| 61 | + Self::Service { |
| 62 | + headers_to_strip: self.headers_to_strip.clone(), |
| 63 | + inner, |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +impl<H, T, M> tower::Service<T> for MakeSvc<H, M> |
| 69 | +where |
| 70 | + T: CanOverrideAuthority + Clone + Send + Sync + 'static, |
| 71 | + M: tower::Service<T>, |
| 72 | + H: AsHeaderName + Clone, |
| 73 | +{ |
| 74 | + type Response = Service<M::Response, H>; |
| 75 | + type Error = M::Error; |
| 76 | + type Future = MakeSvcFut<M::Future, H>; |
| 77 | + |
| 78 | + fn poll_ready(&mut self) -> Poll<(), M::Error> { |
| 79 | + self.inner.poll_ready() |
| 80 | + } |
| 81 | + |
| 82 | + fn call(&mut self, t: T) -> Self::Future { |
| 83 | + let authority = t.override_authority(); |
| 84 | + let inner = self.inner.call(t); |
| 85 | + MakeSvcFut { |
| 86 | + authority, |
| 87 | + headers_to_strip: self.headers_to_strip.clone(), |
| 88 | + inner, |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +impl<F, H> Future for MakeSvcFut<F, H> |
| 94 | +where |
| 95 | + F: Future, |
| 96 | + H: AsHeaderName + Clone, |
| 97 | +{ |
| 98 | + type Item = Service<F::Item, H>; |
| 99 | + type Error = F::Error; |
| 100 | + |
| 101 | + fn poll(&mut self) -> Poll<Self::Item, Self::Error> { |
| 102 | + let inner = try_ready!(self.inner.poll()); |
| 103 | + Ok(Service { |
| 104 | + authority: self.authority.clone(), |
| 105 | + headers_to_strip: self.headers_to_strip.clone(), |
| 106 | + inner, |
| 107 | + } |
| 108 | + .into()) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// === impl Service === |
| 113 | + |
| 114 | +impl<S, H, B> tower::Service<http::Request<B>> for Service<S, H> |
| 115 | +where |
| 116 | + S: tower::Service<http::Request<B>>, |
| 117 | + H: AsHeaderName + fmt::Display + Clone, |
| 118 | +{ |
| 119 | + type Response = S::Response; |
| 120 | + type Error = S::Error; |
| 121 | + type Future = S::Future; |
| 122 | + |
| 123 | + fn poll_ready(&mut self) -> Poll<(), Self::Error> { |
| 124 | + self.inner.poll_ready() |
| 125 | + } |
| 126 | + |
| 127 | + fn call(&mut self, mut req: http::Request<B>) -> Self::Future { |
| 128 | + if let Some(authority) = self.authority.clone() { |
| 129 | + for header in self.headers_to_strip.iter() { |
| 130 | + if let Some(value) = req.headers_mut().remove(header.clone()) { |
| 131 | + debug!( |
| 132 | + %header, |
| 133 | + ?value, |
| 134 | + "Stripped header", |
| 135 | + ); |
| 136 | + }; |
| 137 | + } |
| 138 | + |
| 139 | + debug!(%authority, "Overriding"); |
| 140 | + h1::set_authority(req.uri_mut(), authority); |
| 141 | + } |
| 142 | + |
| 143 | + self.inner.call(req) |
| 144 | + } |
| 145 | +} |
0 commit comments