From 16f235b9fd9ae7f19ed00816bb74b79990fbf29e Mon Sep 17 00:00:00 2001 From: Christof Weickhardt Date: Sun, 3 Apr 2022 19:20:17 +0200 Subject: [PATCH] feat: declare headers globally (#20) --- Cargo.toml | 1 - src/lib.rs | 40 ++++++++++++++++++---------------------- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e87d1c9..462c670 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,6 @@ include = [ [dependencies] hyper = { version = "0.14", features = ["full"] } lazy_static = "1.4" -unicase = "2.6" hyper-tls = { version = "0.5", optional = true } [dev-dependencies] diff --git a/src/lib.rs b/src/lib.rs index 8612c63..e4c082e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -97,13 +97,29 @@ //! use hyper::client::{connect::dns::GaiResolver, HttpConnector}; -use hyper::header::{HeaderMap, HeaderValue, HOST}; +use hyper::header::{HeaderMap, HeaderValue, HeaderName, HOST}; use hyper::http::header::{InvalidHeaderValue, ToStrError}; use hyper::http::uri::InvalidUri; use hyper::{Body, Client, Error, Request, Response}; use lazy_static::lazy_static; use std::net::IpAddr; +lazy_static! { + // A list of the headers, using hypers actual HeaderName comparison + static ref HOP_HEADERS: [HeaderName; 8] = [ + HeaderName::from_static("connection"), + HeaderName::from_static("keep-alive"), + HeaderName::from_static("proxy-authenticate"), + HeaderName::from_static("proxy-authorization"), + HeaderName::from_static("te"), + HeaderName::from_static("trailers"), + HeaderName::from_static("transfer-encoding"), + HeaderName::from_static("upgrade"), + ]; + + static ref X_FORWARDED_FOR: HeaderName = HeaderName::from_static("x-forwarded-for"); +} + #[derive(Debug)] pub enum ProxyError { InvalidUri(InvalidUri), @@ -136,24 +152,6 @@ impl From for ProxyError { } 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> = 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) } @@ -211,10 +209,8 @@ fn create_proxied_request( *request.headers_mut() = remove_hop_headers(request.headers()); *request.uri_mut() = uri; - let x_forwarded_for_header_name = "x-forwarded-for"; - // Add forwarding information in the headers - match request.headers_mut().entry(x_forwarded_for_header_name) { + match request.headers_mut().entry(&*X_FORWARDED_FOR) { hyper::header::Entry::Vacant(entry) => { entry.insert(client_ip.to_string().parse()?); }