Changeset 22638 for lang/perl/HTTP-Engine-Middleware
- Timestamp:
- 11/03/08 22:16:13 (5 years ago)
- Location:
- lang/perl/HTTP-Engine-Middleware/branches/functional
- Files:
-
- 2 modified
-
lib/HTTP/Engine/Middleware/ReverseProxy.pm (modified) (1 diff)
-
t/15_middleware_reverseproxy.t (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/HTTP-Engine-Middleware/branches/functional/lib/HTTP/Engine/Middleware/ReverseProxy.pm
r11881 r22638 3 3 4 4 sub wrap { 5 my ($ next, $c) = @_;5 my ($class, $next) = @_; 6 6 7 # in apache httpd.conf (RequestHeader set X-Forwarded-HTTPS %{HTTPS}s) 8 $ENV{HTTPS} = $ENV{HTTP_X_FORWARDED_HTTPS} if $ENV{HTTP_X_FORWARDED_HTTPS}; 9 $ENV{HTTPS} = 'ON' if $ENV{HTTP_X_FORWARDED_PROTO}; # Pound 10 $c->req->secure(1) if $ENV{HTTPS} && uc $ENV{HTTPS} eq 'ON'; 7 sub { 8 my $req = shift; 11 9 12 # If we are running as a backend server, the user will always appear13 # as 127.0.0.1. Select the most recent upstream IP (last in the list)14 if ($ENV{HTTP_X_FORWARDED_FOR}) {15 my ($ip, ) = $ENV{HTTP_X_FORWARDED_FOR} =~ /([^,\s]+)$/;16 $c->req->address($ip);17 }10 my $env = $req->_connection->{env}; 11 if ( $env ) { 12 # in apache httpd.conf (RequestHeader set X-Forwarded-HTTPS %{HTTPS}s) 13 $env->{HTTPS} = $env->{HTTP_X_FORWARDED_HTTPS} if $env->{HTTP_X_FORWARDED_HTTPS}; 14 $env->{HTTPS} = 'ON' if $env->{HTTP_X_FORWARDED_PROTO}; # Pound 15 $req->secure(1) if $env->{HTTPS} && uc $env->{HTTPS} eq 'ON'; 18 16 19 if ($ENV{HTTP_X_FORWARDED_HOST}) { 20 my $host = $ENV{HTTP_X_FORWARDED_HOST}; 21 if ($host =~ /^(.+):(\d+)$/ ) { 22 $host = $1; 23 $ENV{SERVER_PORT} = $2; 24 } elsif ($ENV{HTTP_X_FORWARDED_PORT}) { 25 # in apache httpd.conf (RequestHeader set X-Forwarded-Port 8443) 26 $ENV{SERVER_PORT} = $ENV{HTTP_X_FORWARDED_PORT}; 17 # If we are running as a backend server, the user will always appear 18 # as 127.0.0.1. Select the most recent upstream IP (last in the list) 19 if ($env->{HTTP_X_FORWARDED_FOR}) { 20 my ($ip, ) = $env->{HTTP_X_FORWARDED_FOR} =~ /([^,\s]+)$/; 21 $req->address($ip); 22 } 23 24 if ($env->{HTTP_X_FORWARDED_HOST}) { 25 my $host = $env->{HTTP_X_FORWARDED_HOST}; 26 if ($host =~ /^(.+):(\d+)$/ ) { 27 $host = $1; 28 $env->{SERVER_PORT} = $2; 29 } elsif ($env->{HTTP_X_FORWARDED_PORT}) { 30 # in apache httpd.conf (RequestHeader set X-Forwarded-Port 8443) 31 $env->{SERVER_PORT} = $env->{HTTP_X_FORWARDED_PORT}; 32 } 33 $env->{HTTP_HOST} = $host; 34 35 $req->headers->header( 'Host' => $env->{HTTP_HOST} ); 36 } 37 38 for my $attr (qw/uri base/) { 39 my $scheme = $req->secure ? 'https' : 'http'; 40 my $host = $env->{HTTP_HOST} || $env->{SERVER_NAME}; 41 my $port = $env->{SERVER_PORT} || ( $req->secure ? 443 : 80 ); 42 # my $path_info = $env->{PATH_INFO} || '/'; 43 44 $req->$attr->scheme($scheme); 45 $req->$attr->host($host); 46 $req->$attr->port($port); 47 # $req->$attr->path($path_info); 48 # $req->$attr( $req->$attr->canonical ); 49 } 27 50 } 28 $ENV{HTTP_HOST} = $host; 29 30 $c->req->headers->header( 'Host' => $ENV{HTTP_HOST} ); 31 } 32 33 for my $attr (qw/uri base/) { 34 my $scheme = $c->req->secure ? 'https' : 'http'; 35 my $host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME}; 36 my $port = $ENV{SERVER_PORT} || ( $c->req->secure ? 443 : 80 ); 37 38 $c->req->$attr->scheme($scheme); 39 $c->req->$attr->host($host); 40 $c->req->$attr->port($port); 41 $c->req->$attr( $c->req->$attr->canonical ); 42 } 43 44 $next->($c); 51 $next->($req); 52 }; 45 53 } 46 54 -
lang/perl/HTTP-Engine-Middleware/branches/functional/t/15_middleware_reverseproxy.t
r11960 r22638 2 2 use warnings; 3 3 use Test::Base; 4 use HTTP::Engine middlewares => [ 5 qw/ReverseProxy/ 6 ]; 4 use HTTP::Engine; 5 use HTTP::Engine::Middleware::ReverseProxy; 7 6 8 7 filters { input => [qw/yaml/] }; 9 8 10 plan tests => 23;9 plan tests => 17; 11 10 12 11 run { … … 21 20 HTTP::Engine->new( 22 21 interface => { 23 module => 'CGI', 24 request_handler => sub { 25 my $c = shift; 26 eval $block->expected; 27 die $@ if $@; 28 }, 22 module => 'Test', 23 request_handler => HTTP::Engine::Middleware::ReverseProxy->wrap(sub { 24 my $req = shift; 25 26 for my $attr ( qw/secure address/ ) { 27 if ( $block->$attr ) { 28 is($req->$attr, $block->$attr, $block->name . " of $attr"); 29 } 30 } 31 for my $url ( qw/uri base / ) { 32 if ( $block->$url ) { 33 is($req->$url->as_string, $block->$url, $block->name . " of $url"); 34 } 35 } 36 37 HTTP::Engine::Response->new(body => 'OK'); 38 }), 29 39 }, 30 )->run ;40 )->run(HTTP::Request->new(GET => 'http://example.com/?foo=bar'), env => \%ENV); 31 41 }; 32 42 33 43 __END__ 34 44 35 === 45 === with https 36 46 --- input 37 47 HTTP_X_FORWARDED_HTTPS: ON 38 --- expected 39 is $c->req->secure, 1; 40 is $c->req->uri->as_string, "https://example.com:80?foo=bar"; 41 is $c->req->base->as_string, "https://example.com:80/"; 48 --- secure: 1 49 --- base: https://example.com:80/ 50 --- uri: https://example.com:80/?foo=bar 42 51 43 === 52 === without https 44 53 --- input 45 54 HTTP_X_FORWARDED_HTTPS: OFF 46 --- expected 47 is $c->req->secure, 0; 48 is $c->req->uri->as_string, "http://example.com?foo=bar"; 49 is $c->req->base->as_string, "http://example.com/"; 55 --- secure: 0 56 --- base: http://example.com:80/ 57 --- uri: http://example.com:80/?foo=bar 50 58 51 59 === 52 60 --- input 53 61 DUMMY: 1 54 --- expected 55 is $c->req->secure, 0; 56 is $c->req->uri->as_string, "http://example.com?foo=bar"; 57 is $c->req->base->as_string, "http://example.com/"; 62 --- secure: 0 63 --- base: http://example.com:80/ 64 --- uri: http://example.com:80/?foo=bar 58 65 59 === 66 === https with HTTP_X_FORWARDED_PROTO 60 67 --- input 61 68 HTTP_X_FORWARDED_PROTO: https 62 --- expected 63 is $c->req->secure, 1; 64 is $c->req->uri->as_string, "https://example.com:80?foo=bar"; 65 is $c->req->base->as_string, "https://example.com:80/"; 69 --- secure: 1 70 --- base: https://example.com:80/ 71 --- uri: https://example.com:80/?foo=bar 66 72 67 === 73 === with HTTP_X_FORWARDED_FOR 68 74 --- input 69 75 HTTP_X_FORWARDED_FOR: 192.168.3.2 70 --- expected 71 is $c->req->address, '192.168.3.2'; 72 is $c->req->uri->as_string, "http://example.com?foo=bar"; 73 is $c->req->base->as_string, "http://example.com/"; 76 --- address: 192.168.3.2 77 --- base: http://example.com:80/ 78 --- uri: http://example.com:80/?foo=bar 74 79 75 === 80 === with HTTP_X_FORWARDED_HOST 76 81 --- input 77 82 HTTP_X_FORWARDED_HOST: 192.168.1.2:5235 78 --- expected 79 is $ENV{HTTP_HOST}, '192.168.1.2'; 80 is $ENV{SERVER_PORT}, 5235; 81 is $c->req->uri->as_string, "http://192.168.1.2:5235?foo=bar"; 82 is $c->req->base->as_string, "http://192.168.1.2:5235/"; 83 --- base: http://192.168.1.2:5235/ 84 --- uri: http://192.168.1.2:5235/?foo=bar 83 85 84 === 86 === with HTTP_X_FORWARDED_HOST and HTTP_X_FORWARDED_PORT 85 87 --- input 86 88 HTTP_X_FORWARDED_HOST: 192.168.1.5 87 89 HTTP_X_FORWARDED_PORT: 1984 88 --- expected 89 is $ENV{HTTP_HOST}, '192.168.1.5'; 90 is $ENV{SERVER_PORT}, 1984; 91 is $c->req->uri->as_string, "http://192.168.1.5:1984?foo=bar"; 92 is $c->req->base->as_string, "http://192.168.1.5:1984/"; 90 --- base: http://192.168.1.5:1984/ 91 --- uri: http://192.168.1.5:1984/?foo=bar 93 92
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)