Show
Ignore:
Timestamp:
11/03/08 22:16:13 (5 years ago)
Author:
walf443
Message:

change HTTP::Engine::Middleware::ReverseProxy? to non-context format like App::Mobirc::Web::Middleware::Encoding

Location:
lang/perl/HTTP-Engine-Middleware/branches/functional
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/HTTP-Engine-Middleware/branches/functional/lib/HTTP/Engine/Middleware/ReverseProxy.pm

    r11881 r22638  
    33 
    44sub wrap { 
    5     my ($next, $c) = @_; 
     5    my ($class, $next) = @_; 
    66 
    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; 
    119 
    12     # If we are running as a backend server, the user will always appear 
    13     # 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'; 
    1816 
    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            } 
    2750        } 
    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    }; 
    4553} 
    4654 
  • lang/perl/HTTP-Engine-Middleware/branches/functional/t/15_middleware_reverseproxy.t

    r11960 r22638  
    22use warnings; 
    33use Test::Base; 
    4 use HTTP::Engine middlewares => [ 
    5     qw/ReverseProxy/ 
    6 ]; 
     4use HTTP::Engine; 
     5use HTTP::Engine::Middleware::ReverseProxy; 
    76 
    87filters { input => [qw/yaml/] }; 
    98 
    10 plan tests => 23; 
     9plan tests => 17; 
    1110 
    1211run { 
     
    2120    HTTP::Engine->new( 
    2221        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            }), 
    2939        }, 
    30     )->run; 
     40    )->run(HTTP::Request->new(GET => 'http://example.com/?foo=bar'), env => \%ENV); 
    3141}; 
    3242 
    3343__END__ 
    3444 
    35 === 
     45=== with https 
    3646--- input 
    3747HTTP_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 
    4251 
    43 === 
     52=== without https 
    4453--- input 
    4554HTTP_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 
    5058 
    5159=== 
    5260--- input 
    5361DUMMY: 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 
    5865 
    59 === 
     66=== https with HTTP_X_FORWARDED_PROTO 
    6067--- input 
    6168HTTP_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 
    6672 
    67 === 
     73=== with HTTP_X_FORWARDED_FOR 
    6874--- input 
    6975HTTP_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 
    7479 
    75 === 
     80=== with HTTP_X_FORWARDED_HOST 
    7681--- input 
    7782HTTP_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 
    8385 
    84 === 
     86=== with HTTP_X_FORWARDED_HOST and HTTP_X_FORWARDED_PORT 
    8587--- input 
    8688HTTP_X_FORWARDED_HOST: 192.168.1.5 
    8789HTTP_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 
    9392