diff options
Diffstat (limited to 'src/reverse_proxy.rs')
-rw-r--r-- | src/reverse_proxy.rs | 61 |
1 files changed, 25 insertions, 36 deletions
diff --git a/src/reverse_proxy.rs b/src/reverse_proxy.rs index 72644b7..445f6ef 100644 --- a/src/reverse_proxy.rs +++ b/src/reverse_proxy.rs @@ -12,33 +12,25 @@ use log::*; use http::header::HeaderName; use hyper::header::{HeaderMap, HeaderValue}; -use hyper::{Body, Client, Request, Response, Uri}; -use lazy_static::lazy_static; +use hyper::{header, Body, Client, Request, Response, Uri}; use rustls::client::{ServerCertVerified, ServerCertVerifier}; use rustls::{Certificate, ServerName}; use crate::tls_util::HttpsConnectorFixedDnsname; -fn is_hop_header(name: &str) -> bool { - use unicase::Ascii; - - // A list of the headers, using `unicase` to help us compare without - // worrying about the case, and `lazy_static!` to prevent reallocation - // of the vector. - lazy_static! { - static ref HOP_HEADERS: Vec<Ascii<&'static str>> = vec![ - Ascii::new("Connection"), - Ascii::new("Keep-Alive"), - Ascii::new("Proxy-Authenticate"), - Ascii::new("Proxy-Authorization"), - Ascii::new("Te"), - Ascii::new("Trailers"), - Ascii::new("Transfer-Encoding"), - Ascii::new("Upgrade"), - ]; - } - - HOP_HEADERS.iter().any(|h| h == &name) +const HOP_HEADERS: &[HeaderName] = &[ + header::CONNECTION, + //header::KEEP_ALIVE, + header::PROXY_AUTHENTICATE, + header::PROXY_AUTHORIZATION, + header::TE, + header::TRAILER, + header::TRANSFER_ENCODING, + header::UPGRADE, +]; + +fn is_hop_header(name: &HeaderName) -> bool { + HOP_HEADERS.iter().any(|h| h == name) } /// Returns a clone of the headers without the [hop-by-hop headers]. @@ -47,7 +39,7 @@ fn is_hop_header(name: &str) -> bool { fn remove_hop_headers(headers: &HeaderMap<HeaderValue>) -> HeaderMap<HeaderValue> { let mut result = HeaderMap::new(); for (k, v) in headers.iter() { - if !is_hop_header(k.as_str()) { + if !is_hop_header(&k) { result.append(k.clone(), v.clone()); } } @@ -80,10 +72,7 @@ fn create_proxied_request<B>( *builder.headers_mut().unwrap() = remove_hop_headers(request.headers()); // If request does not have host header, add it from original URI authority - let host_header_name = "host"; - if let hyper::header::Entry::Vacant(entry) = - builder.headers_mut().unwrap().entry(host_header_name) - { + if let header::Entry::Vacant(entry) = builder.headers_mut().unwrap().entry(header::HOST) { if let Some(authority) = request.uri().authority() { entry.insert(authority.as_str().parse()?); } @@ -96,11 +85,11 @@ fn create_proxied_request<B>( .unwrap() .entry(x_forwarded_for_header_name) { - hyper::header::Entry::Vacant(entry) => { + header::Entry::Vacant(entry) => { entry.insert(client_ip.to_string().parse()?); } - hyper::header::Entry::Occupied(mut entry) => { + header::Entry::Occupied(mut entry) => { let addr = format!("{}, {}", entry.get().to_str()?, client_ip); entry.insert(addr.parse()?); } @@ -112,17 +101,17 @@ fn create_proxied_request<B>( ); // Proxy upgrade requests properly - if let Some(conn) = request.headers().get("connection") { + if let Some(conn) = request.headers().get(header::CONNECTION) { if conn.to_str()?.to_lowercase() == "upgrade" { - if let Some(upgrade) = request.headers().get("upgrade") { - builder.headers_mut().unwrap().insert( - HeaderName::from_bytes(b"connection")?, - "Upgrade".try_into()?, - ); + if let Some(upgrade) = request.headers().get(header::UPGRADE) { + builder + .headers_mut() + .unwrap() + .insert(header::CONNECTION, "Upgrade".try_into()?); builder .headers_mut() .unwrap() - .insert(HeaderName::from_bytes(b"upgrade")?, upgrade.clone()); + .insert(header::UPGRADE, upgrade.clone()); } } } |