@@ -140,6 +140,7 @@ lazy_static! {
140
140
] ;
141
141
142
142
static ref X_FORWARDED_FOR : HeaderName = HeaderName :: from_static( "x-forwarded-for" ) ;
143
+ static ref X_FORWARDED_HOST : HeaderName = HeaderName :: from_static( "x-forwarded-host" ) ;
143
144
}
144
145
145
146
#[ derive( Debug ) ]
@@ -334,7 +335,7 @@ fn create_proxied_request<B>(
334
335
debug ! ( "Setting headers of proxied request" ) ;
335
336
336
337
// remove the original HOST header. It will be set by the client that sends the request
337
- request. headers_mut ( ) . remove ( HOST ) ;
338
+ let original_host = request. headers_mut ( ) . remove ( HOST ) ;
338
339
339
340
* request. uri_mut ( ) = uri;
340
341
@@ -361,22 +362,27 @@ fn create_proxied_request<B>(
361
362
}
362
363
363
364
// Add forwarding information in the headers
365
+ let forwarded_for_value = client_ip. to_string ( ) . parse ( ) ?;
364
366
match request. headers_mut ( ) . entry ( & * X_FORWARDED_FOR ) {
365
367
hyper:: header:: Entry :: Vacant ( entry) => {
366
- debug ! ( "X-Fowraded -for header was vacant" ) ;
367
- entry. insert ( client_ip . to_string ( ) . parse ( ) ? ) ;
368
+ debug ! ( "x-forwarded -for header was vacant" ) ;
369
+ entry. insert ( forwarded_for_value ) ;
368
370
}
371
+ hyper:: header:: Entry :: Occupied ( mut entry) => {
372
+ debug ! ( "x-forwarded-for header was occupied" ) ;
373
+ entry. append ( forwarded_for_value) ;
374
+ }
375
+ }
369
376
370
- hyper:: header:: Entry :: Occupied ( entry) => {
371
- debug ! ( "X-Fowraded-for header was occupied" ) ;
372
- let client_ip_str = client_ip. to_string ( ) ;
373
- let mut addr =
374
- String :: with_capacity ( entry. get ( ) . as_bytes ( ) . len ( ) + 2 + client_ip_str. len ( ) ) ;
375
-
376
- addr. push_str ( std:: str:: from_utf8 ( entry. get ( ) . as_bytes ( ) ) . unwrap ( ) ) ;
377
- addr. push ( ',' ) ;
378
- addr. push ( ' ' ) ;
379
- addr. push_str ( & client_ip_str) ;
377
+ if let Some ( host) = original_host {
378
+ match request. headers_mut ( ) . entry ( & * X_FORWARDED_HOST ) {
379
+ hyper:: header:: Entry :: Vacant ( entry) => {
380
+ debug ! ( "x-forwarded-host header was vacant" ) ;
381
+ entry. insert ( host. to_owned ( ) ) ;
382
+ }
383
+ hyper:: header:: Entry :: Occupied ( mut _entry) => {
384
+ debug ! ( "x-forwarded-host header was occupied" ) ;
385
+ }
380
386
}
381
387
}
382
388
0 commit comments