| 1 | package HTTP::Engine::Middleware::ReverseProxy; | ||
| 2 | use HTTP::Engine::Middleware; | ||
| 3 | |||
| 4 | before_handle { | ||
| 5 | my ( $c, $self, $req ) = @_; | ||
| 6 | my $env = $req->_connection->{env} || {}; | ||
| 7 | |||
| 8 | # in apache httpd.conf (RequestHeader set X-Forwarded-HTTPS %{HTTPS}s) | ||
| 9 | $env->{HTTPS} = $req->headers->{'x-forwarded-https'} | ||
| 10 | if $req->headers->{'x-forwarded-https'}; | ||
| 11 | $env->{HTTPS} = 'ON' if $req->headers->{'x-forwarded-proto'}; # Pound | ||
| 12 | $req->secure(1) if $env->{HTTPS} && uc $env->{HTTPS} eq 'ON'; | ||
| 13 | my $default_port = $req->secure ? 443 : 80; | ||
| 14 | |||
| 15 | # If we are running as a backend server, the user will always appear | ||
| 16 | # as 127.0.0.1. Select the most recent upstream IP (last in the list) | ||
| 17 | if ( $req->headers->{'x-forwarded-for'} ) { | ||
| 18 | my ( $ip, ) = $req->headers->{'x-forwarded-for'} =~ /([^,\s]+)$/; | ||
| 19 | $req->address($ip); | ||
| 20 | } | ||
| 21 | |||
| 22 | if ( $req->headers->{'x-forwarded-host'} ) { | ||
| 23 | my $host = $req->headers->{'x-forwarded-host'}; | ||
| 24 | if ( $host =~ /^(.+):(\d+)$/ ) { | ||
| 25 | $host = $1; | ||
| 26 | $env->{SERVER_PORT} = $2; | ||
| 27 | } elsif ( $req->headers->{'x-forwarded-port'} ) { | ||
| 28 | # in apache httpd.conf (RequestHeader set X-Forwarded-Port 8443) | ||
| 29 | $env->{SERVER_PORT} = $req->headers->{'x-forwarded-port'}; | ||
| 30 | } else { | ||
| 31 | $env->{SERVER_PORT} = $default_port; | ||
| 32 | } | ||
| 33 | $env->{HTTP_HOST} = $host; | ||
| 34 | |||
| 35 | $req->headers->header( 'Host' => $env->{HTTP_HOST} ); | ||
| 36 | } elsif ($req->headers->{'host'}) { | ||
| 37 | my $host = $req->headers->{'host'}; | ||
| 38 | if ($host =~ /^(.+):(\d+)$/ ) { | ||
| 39 | $env->{HTTP_HOST} = $1; | ||
| 40 | $env->{SERVER_PORT} = $2; | ||
| 41 | } elsif ($host =~ /^(.+)$/ ) { | ||
| 42 | $env->{HTTP_HOST} = $1; | ||
| 43 | $env->{SERVER_PORT} = $default_port; | ||
| 44 | } | ||
| 45 | } else { | ||
| 46 | $env->{HTTP_HOST} = $req->uri->host; | ||
| 47 | $env->{SERVER_PORT} = $req->uri->port || $default_port; | ||
| 48 | } | ||
| 49 | $req->_connection->{env} = $env; | ||
| 50 | |||
| 51 | for my $attr (qw/uri base/) { | ||
| 52 | my $scheme = $req->secure ? 'https' : 'http'; | ||
| 53 | my $host = $env->{HTTP_HOST} || $env->{SERVER_NAME}; | ||
| 54 | my $port = $env->{SERVER_PORT} || undef; | ||
| 55 | # my $path_info = $env->{PATH_INFO} || '/'; | ||
| 56 | |||
| 57 | $req->$attr->scheme($scheme); | ||
| 58 | $req->$attr->host($host); | ||
| 59 | if (($port || '') eq $default_port) { | ||
| 60 | $req->$attr->port(undef); | ||
| 61 | } else { | ||
| 62 | $req->$attr->port($port); | ||
| 63 | } | ||
| 64 | |||
| 65 | # $req->$attr->path($path_info); | ||
| 66 | # $req->$attr( $req->$attr->canonical ); | ||
| 67 | } | ||
| 68 | $req; | ||
| 69 | }; | ||
| 70 | |||
| 71 | __MIDDLEWARE__ | ||
| 72 | |||
| 73 | __END__ | ||
| 74 | |||
| 75 | =head1 NAME | ||
| 76 | |||
| 77 | HTTP::Engine::Middleware::ReverseProxy - reverse-proxy support | ||
| 78 | |||
| 79 | =head1 SYNOPSIS | ||
| 80 | |||
| 81 | my $mw = HTTP::Engine::Middleware->new; | ||
| 82 | $mw->install(qw/ HTTP::Engine::Middleware::ReverseProxy /); | ||
| 83 | HTTP::Engine->new( | ||
| 84 | interface => { | ||
| 85 | module => 'YourFavoriteInterfaceHere', | ||
| 86 | request_handler => $mw->handler( \&handler ), | ||
| 87 | } | ||
| 88 | )->run(); | ||
| 89 | |||
| 90 | =head1 DESCRIPTION | ||
| 91 | |||
| 92 | This module resets some HTTP headers, which changed by reverse-proxy. | ||
| 93 | |||
| 94 | =head1 AUTHORS | ||
| 95 | |||
| 96 | yappo | ||
| 97 | |||
| 98 | =cut |