|
| 1 | +use http::{uri::Scheme, Method, Request, Uri}; |
| 2 | +use tower_service::Service; |
| 3 | +use tracing::warn; |
| 4 | + |
| 5 | +pub struct Http1RequestTarget<S> { |
| 6 | + inner: S, |
| 7 | +} |
| 8 | + |
| 9 | +impl<S, B> Service<Request<B>> for Http1RequestTarget<S> |
| 10 | +where |
| 11 | + S: Service<Request<B>>, |
| 12 | +{ |
| 13 | + type Response = S::Response; |
| 14 | + type Error = S::Error; |
| 15 | + type Future = S::Future; |
| 16 | + |
| 17 | + fn poll_ready( |
| 18 | + &mut self, |
| 19 | + cx: &mut std::task::Context<'_>, |
| 20 | + ) -> std::task::Poll<Result<(), Self::Error>> { |
| 21 | + self.inner.poll_ready(cx) |
| 22 | + } |
| 23 | + |
| 24 | + fn call(&mut self, mut req: Request<B>) -> Self::Future { |
| 25 | + // CONNECT always sends authority-form, so check it first... |
| 26 | + if req.method() == Method::CONNECT { |
| 27 | + authority_form(req.uri_mut()); |
| 28 | + // TODO: this middleware must be connection pool aware |
| 29 | + // } else if pooled.conn_info.is_proxied { |
| 30 | + // absolute_form(req.uri_mut()); |
| 31 | + } else { |
| 32 | + origin_form(req.uri_mut()); |
| 33 | + } |
| 34 | + self.inner.call(req) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +fn origin_form(uri: &mut Uri) { |
| 39 | + let path = match uri.path_and_query() { |
| 40 | + Some(path) if path.as_str() != "/" => { |
| 41 | + let mut parts = ::http::uri::Parts::default(); |
| 42 | + parts.path_and_query = Some(path.clone()); |
| 43 | + Uri::from_parts(parts).expect("path is valid uri") |
| 44 | + } |
| 45 | + _none_or_just_slash => { |
| 46 | + debug_assert!(Uri::default() == "/"); |
| 47 | + Uri::default() |
| 48 | + } |
| 49 | + }; |
| 50 | + *uri = path |
| 51 | +} |
| 52 | + |
| 53 | +fn absolute_form(uri: &mut Uri) { |
| 54 | + debug_assert!(uri.scheme().is_some(), "absolute_form needs a scheme"); |
| 55 | + debug_assert!( |
| 56 | + uri.authority().is_some(), |
| 57 | + "absolute_form needs an authority" |
| 58 | + ); |
| 59 | + // If the URI is to HTTPS, and the connector claimed to be a proxy, |
| 60 | + // then it *should* have tunneled, and so we don't want to send |
| 61 | + // absolute-form in that case. |
| 62 | + if uri.scheme() == Some(&Scheme::HTTPS) { |
| 63 | + origin_form(uri); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +fn authority_form(uri: &mut Uri) { |
| 68 | + if let Some(path) = uri.path_and_query() { |
| 69 | + // `https://hyper.rs` would parse with `/` path, don't |
| 70 | + // annoy people about that... |
| 71 | + if path != "/" { |
| 72 | + warn!("HTTP/1.1 CONNECT request stripping path: {:?}", path); |
| 73 | + } |
| 74 | + } |
| 75 | + *uri = match uri.authority() { |
| 76 | + Some(auth) => { |
| 77 | + let mut parts = ::http::uri::Parts::default(); |
| 78 | + parts.authority = Some(auth.clone()); |
| 79 | + Uri::from_parts(parts).expect("authority is valid") |
| 80 | + } |
| 81 | + None => { |
| 82 | + unreachable!("authority_form with relative uri"); |
| 83 | + } |
| 84 | + }; |
| 85 | +} |
0 commit comments