@@ -8,31 +8,28 @@ use hyper::http::header::{InvalidHeaderValue, ToStrError};
88use hyper:: http:: uri:: InvalidUri ;
99use hyper:: upgrade:: OnUpgrade ;
1010use hyper:: { Body , Client , Error , Request , Response , StatusCode } ;
11- use lazy_static:: lazy_static;
1211use std:: net:: IpAddr ;
1312use tokio:: io:: copy_bidirectional;
1413
15- lazy_static ! {
16- static ref TE_HEADER : HeaderName = HeaderName :: from_static( "te" ) ;
17- static ref CONNECTION_HEADER : HeaderName = HeaderName :: from_static( "connection" ) ;
18- static ref UPGRADE_HEADER : HeaderName = HeaderName :: from_static( "upgrade" ) ;
19- static ref TRAILER_HEADER : HeaderName = HeaderName :: from_static( "trailer" ) ;
20- static ref TRAILERS_HEADER : HeaderName = HeaderName :: from_static( "trailers" ) ;
21- // A list of the headers, using hypers actual HeaderName comparison
22- static ref HOP_HEADERS : [ HeaderName ; 9 ] = [
23- CONNECTION_HEADER . clone( ) ,
24- TE_HEADER . clone( ) ,
25- TRAILER_HEADER . clone( ) ,
26- HeaderName :: from_static( "keep-alive" ) ,
27- HeaderName :: from_static( "proxy-connection" ) ,
28- HeaderName :: from_static( "proxy-authenticate" ) ,
29- HeaderName :: from_static( "proxy-authorization" ) ,
30- HeaderName :: from_static( "transfer-encoding" ) ,
31- HeaderName :: from_static( "upgrade" ) ,
32- ] ;
33-
34- static ref X_FORWARDED_FOR : HeaderName = HeaderName :: from_static( "x-forwarded-for" ) ;
35- }
14+ static TE_HEADER : HeaderName = HeaderName :: from_static ( "te" ) ;
15+ static CONNECTION_HEADER : HeaderName = HeaderName :: from_static ( "connection" ) ;
16+ static UPGRADE_HEADER : HeaderName = HeaderName :: from_static ( "upgrade" ) ;
17+ static TRAILERS_HEADER : HeaderName = HeaderName :: from_static ( "trailers" ) ;
18+ static X_FORWARDED_FOR : HeaderName = HeaderName :: from_static ( "x-forwarded-for" ) ;
19+
20+ // A list of the headers, using hypers actual HeaderName comparison
21+ #[ cfg_attr( feature = "__bench" , visibility:: make( pub ) ) ]
22+ static HOP_HEADERS : [ HeaderName ; 9 ] = [
23+ HeaderName :: from_static ( "connection" ) ,
24+ HeaderName :: from_static ( "te" ) ,
25+ HeaderName :: from_static ( "trailer" ) ,
26+ HeaderName :: from_static ( "keep-alive" ) ,
27+ HeaderName :: from_static ( "proxy-connection" ) ,
28+ HeaderName :: from_static ( "proxy-authenticate" ) ,
29+ HeaderName :: from_static ( "proxy-authorization" ) ,
30+ HeaderName :: from_static ( "transfer-encoding" ) ,
31+ HeaderName :: from_static ( "upgrade" ) ,
32+ ] ;
3633
3734#[ derive( Debug ) ]
3835pub enum ProxyError {
@@ -69,25 +66,25 @@ impl From<InvalidHeaderValue> for ProxyError {
6966fn remove_hop_headers ( headers : & mut HeaderMap ) {
7067 debug ! ( "Removing hop headers" ) ;
7168
72- for header in & * HOP_HEADERS {
69+ for header in & HOP_HEADERS {
7370 headers. remove ( header) ;
7471 }
7572}
7673
7774fn get_upgrade_type ( headers : & HeaderMap ) -> Option < String > {
7875 #[ allow( clippy:: blocks_in_if_conditions) ]
7976 if headers
80- . get ( & * CONNECTION_HEADER )
77+ . get ( & CONNECTION_HEADER )
8178 . map ( |value| {
8279 value
8380 . to_str ( )
8481 . unwrap ( )
8582 . split ( ',' )
86- . any ( |e| e. trim ( ) == * UPGRADE_HEADER )
83+ . any ( |e| e. trim ( ) == UPGRADE_HEADER )
8784 } )
8885 . unwrap_or ( false )
8986 {
90- if let Some ( upgrade_value) = headers. get ( & * UPGRADE_HEADER ) {
87+ if let Some ( upgrade_value) = headers. get ( & UPGRADE_HEADER ) {
9188 debug ! (
9289 "Found upgrade header with value: {}" ,
9390 upgrade_value. to_str( ) . unwrap( ) . to_owned( )
@@ -101,10 +98,10 @@ fn get_upgrade_type(headers: &HeaderMap) -> Option<String> {
10198}
10299
103100fn remove_connection_headers ( headers : & mut HeaderMap ) {
104- if headers. get ( & * CONNECTION_HEADER ) . is_some ( ) {
101+ if headers. get ( & CONNECTION_HEADER ) . is_some ( ) {
105102 debug ! ( "Removing connection headers" ) ;
106103
107- let value = headers. get ( & * CONNECTION_HEADER ) . cloned ( ) . unwrap ( ) ;
104+ let value = headers. get ( & CONNECTION_HEADER ) . cloned ( ) . unwrap ( ) ;
108105
109106 for name in value. to_str ( ) . unwrap ( ) . split ( ',' ) {
110107 if !name. trim ( ) . is_empty ( ) {
@@ -114,6 +111,7 @@ fn remove_connection_headers(headers: &mut HeaderMap) {
114111 }
115112}
116113
114+ #[ cfg_attr( feature = "__bench" , visibility:: make( pub ) ) ]
117115fn create_proxied_response < B > ( mut response : Response < B > ) -> Response < B > {
118116 info ! ( "Creating proxied response" ) ;
119117
@@ -123,6 +121,7 @@ fn create_proxied_response<B>(mut response: Response<B>) -> Response<B> {
123121 response
124122}
125123
124+ #[ cfg_attr( feature = "__bench" , visibility:: make( pub ) ) ]
126125fn forward_uri < B > ( forward_url : & str , req : & Request < B > ) -> String {
127126 debug ! ( "Building forward uri" ) ;
128127
@@ -201,6 +200,7 @@ fn forward_uri<B>(forward_url: &str, req: &Request<B>) -> String {
201200 url. parse ( ) . unwrap ( )
202201}
203202
203+ #[ cfg_attr( feature = "__bench" , visibility:: make( pub ) ) ]
204204fn create_proxied_request < B > (
205205 client_ip : IpAddr ,
206206 forward_url : & str ,
@@ -211,13 +211,13 @@ fn create_proxied_request<B>(
211211
212212 let contains_te_trailers_value = request
213213 . headers ( )
214- . get ( & * TE_HEADER )
214+ . get ( & TE_HEADER )
215215 . map ( |value| {
216216 value
217217 . to_str ( )
218218 . unwrap ( )
219219 . split ( ',' )
220- . any ( |e| e. trim ( ) == * TRAILERS_HEADER )
220+ . any ( |e| e. trim ( ) == TRAILERS_HEADER )
221221 } )
222222 . unwrap_or ( false ) ;
223223
@@ -239,22 +239,22 @@ fn create_proxied_request<B>(
239239
240240 request
241241 . headers_mut ( )
242- . insert ( & * TE_HEADER , HeaderValue :: from_static ( "trailers" ) ) ;
242+ . insert ( & TE_HEADER , HeaderValue :: from_static ( "trailers" ) ) ;
243243 }
244244
245245 if let Some ( value) = upgrade_type {
246246 debug ! ( "Repopulate upgrade headers" ) ;
247247
248248 request
249249 . headers_mut ( )
250- . insert ( & * UPGRADE_HEADER , value. parse ( ) . unwrap ( ) ) ;
250+ . insert ( & UPGRADE_HEADER , value. parse ( ) . unwrap ( ) ) ;
251251 request
252252 . headers_mut ( )
253- . insert ( & * CONNECTION_HEADER , HeaderValue :: from_static ( "UPGRADE" ) ) ;
253+ . insert ( & CONNECTION_HEADER , HeaderValue :: from_static ( "UPGRADE" ) ) ;
254254 }
255255
256256 // Add forwarding information in the headers
257- match request. headers_mut ( ) . entry ( & * X_FORWARDED_FOR ) {
257+ match request. headers_mut ( ) . entry ( & X_FORWARDED_FOR ) {
258258 hyper:: header:: Entry :: Vacant ( entry) => {
259259 debug ! ( "X-Fowraded-for header was vacant" ) ;
260260 entry. insert ( client_ip. to_string ( ) . parse ( ) ?) ;
@@ -362,27 +362,3 @@ impl<T: hyper::client::connect::Connect + Clone + Send + Sync + 'static> Reverse
362362 call :: < T > ( client_ip, forward_uri, request, & self . client ) . await
363363 }
364364}
365-
366- #[ cfg( feature = "__bench" ) ]
367- pub mod benches {
368- pub fn hop_headers ( ) -> & ' static [ crate :: HeaderName ] {
369- & * super :: HOP_HEADERS
370- }
371-
372- pub fn create_proxied_response < T > ( response : crate :: Response < T > ) {
373- super :: create_proxied_response ( response) ;
374- }
375-
376- pub fn forward_uri < B > ( forward_url : & str , req : & crate :: Request < B > ) {
377- super :: forward_uri ( forward_url, req) ;
378- }
379-
380- pub fn create_proxied_request < B > (
381- client_ip : crate :: IpAddr ,
382- forward_url : & str ,
383- request : crate :: Request < B > ,
384- upgrade_type : Option < & String > ,
385- ) {
386- super :: create_proxied_request ( client_ip, forward_url, request, upgrade_type) . unwrap ( ) ;
387- }
388- }
0 commit comments