Skip to content
Browse files

Initial stab at fixing RT#74778

  • Loading branch information...
1 parent aed7dc3 commit 0c159483fd9c46c9dd934eaba5da0c53309b8a86 @bobtfish committed Feb 9, 2012
Showing with 24 additions and 2 deletions.
  1. +13 −1 lib/Plack/Middleware/ReverseProxy.pm
  2. +11 −1 t/reverseproxy.t
View
14 lib/Plack/Middleware/ReverseProxy.pm
@@ -21,9 +21,11 @@ sub call {
# If we are running as a backend server, the user will always appear
# as 127.0.0.1. Select the most recent upstream IP (last in the list)
+ my $have_set_remote_addr;
if ( $env->{'HTTP_X_FORWARDED_FOR'} ) {
my ( $ip, ) = $env->{HTTP_X_FORWARDED_FOR} =~ /([^,\s]+)$/;
$env->{REMOTE_ADDR} = $ip;
+ $have_set_remote_addr = 1;
}
if ( $env->{HTTP_X_FORWARDED_HOST} ) {
@@ -42,15 +44,21 @@ sub call {
my ( $host, ) = $env->{HTTP_X_FORWARDED_HOST} =~ /([^,\s]+)$/;
if ( $host =~ /^(.+):(\d+)$/ ) {
# $host = $1;
- $env->{SERVER_PORT} = $2;
+ $env->{SERVER_PORT} = $2;
+ $env->{REMOTE_ADDR} = $1
+ unless $have_set_remote_addr;
} elsif ( $env->{HTTP_X_FORWARDED_PORT} ) {
# in apache2 httpd.conf (RequestHeader set X-Forwarded-Port 8443)
$env->{SERVER_PORT} = $env->{HTTP_X_FORWARDED_PORT};
+ $env->{REMOTE_ADDR} = $host
+ unless $have_set_remote_addr;
$host .= ":$env->{SERVER_PORT}";
$env->{'psgi.url_scheme'} = 'https'
if $env->{SERVER_PORT} == 443;
} else {
$env->{SERVER_PORT} = $default_port;
+ $env->{REMOTE_ADDR} = $host
+ unless $have_set_remote_addr;
}
$env->{HTTP_HOST} = $host;
@@ -59,9 +67,13 @@ sub call {
if ($host =~ /^(.+):(\d+)$/ ) {
# $env->{HTTP_HOST} = $1;
$env->{SERVER_PORT} = $2;
+ $env->{REMOTE_ADDR} = $1
+ unless $have_set_remote_addr;
} elsif ($host =~ /^(.+)$/ ) {
$env->{HTTP_HOST} = $1;
$env->{SERVER_PORT} = $default_port;
+ $env->{REMOTE_ADDR} = $1
+ unless $have_set_remote_addr;
}
}
View
12 t/reverseproxy.t
@@ -2,7 +2,7 @@ use strict;
use warnings;
use Test::Base;
use lib 't/lib';
-plan tests => 39;
+plan tests => 47;
use Plack::Builder;
use Plack::Test;
@@ -102,39 +102,46 @@ x-forwarded-for: 192.168.3.2
x-forwarded-host: 192.168.1.2:5235
--- base: http://192.168.1.2:5235/
--- uri: http://192.168.1.2:5235/?foo=bar
+--- address: 192.168.1.2
=== default port with HTTP_X_FORWARDED_HOST
--- input
x-forwarded-host: 192.168.1.2
--- base: http://192.168.1.2/
--- uri: http://192.168.1.2/?foo=bar
+--- address: 192.168.1.2
=== default https port with HTTP_X_FORWARDED_HOST
--- input
x-forwarded-https: on
x-forwarded-host: 192.168.1.2
--- base: https://192.168.1.2/
--- uri: https://192.168.1.2/?foo=bar
+--- address: 192.168.1.2
=== default port with HOST
--- input
host: 192.168.1.2
--- base: http://192.168.1.2/
--- uri: http://192.168.1.2/?foo=bar
+--- address: 192.168.1.2
=== default https port with HOST
--- input
host: 192.168.1.2
https: ON
--- base: https://192.168.1.2/
--- uri: https://192.168.1.2/?foo=bar
+--- address: 192.168.1.2
=== with HTTP_X_FORWARDED_HOST and HTTP_X_FORWARDED_PORT
--- input
x-forwarded-host: 192.168.1.5
x-forwarded-port: 1984
--- base: http://192.168.1.5:1984/
--- uri: http://192.168.1.5:1984/?foo=bar
+--- address: 192.168.1.5
+
=== with multiple HTTP_X_FORWARDED_HOST and HTTP_X_FORWARDED_FOR
--- input
x-forwarded-host: outmost.proxy.example.com, middle.proxy.example.com
@@ -143,17 +150,20 @@ host: 192.168.1.7:5000
--- address: 192.168.1.6
--- base: http://middle.proxy.example.com/
--- uri: http://middle.proxy.example.com/?foo=bar
+
=== normal plackup status
--- input
host: 127.0.0.1:5000
--- base: http://127.0.0.1:5000/
--- uri: http://127.0.0.1:5000/?foo=bar
+--- address: 127.0.0.1
=== HTTP_X_FORWARDED_PORT to secure port
--- input
x-forwarded-host: 192.168.1.2
x-forwarded-port: 443
--- secure: 1
+--- address: 192.168.1.2
=== HTTP_X_FORWARDED_PORT to secure port (apache2)
--- input

0 comments on commit 0c15948

Please sign in to comment.
Something went wrong with that request. Please try again.