File Coverage

blib/lib/Plack/Middleware/ReverseProxy.pm
Criterion Covered Total %
statement 41 41 100.0
branch 27 30 90.0
condition 6 6 100.0
subroutine 5 5 100.0
pod 1 1 100.0
total 80 83 96.3


line stmt bran cond sub pod time code
1             package Plack::Middleware::ReverseProxy;
2              
3 2     2   70414 use strict;
  2         5  
  2         59  
4 2     2   10 use warnings;
  2         4  
  2         48  
5 2     2   45 use 5.008_001;
  2         10  
6 2     2   468 use parent qw(Plack::Middleware);
  2         313  
  2         11  
7             our $VERSION = '0.16';
8              
9             sub call {
10 17     17 1 79704 my $self = shift;
11 17         31 my $env = shift;
12              
13             # in apache2 httpd.conf (RequestHeader set X-Forwarded-HTTPS %{HTTPS}s)
14             $env->{HTTPS} = $env->{'HTTP_X_FORWARDED_HTTPS'}
15 17 100       51 if $env->{'HTTP_X_FORWARDED_HTTPS'};
16             $env->{HTTPS} = 'ON'
17 17 100 100     49 if $env->{'HTTP_X_FORWARDED_PROTO'} && $env->{'HTTP_X_FORWARDED_PROTO'} eq 'https'; # Pound
18 17 100 100     60 $env->{'psgi.url_scheme'} = 'https' if $env->{HTTPS} && uc $env->{HTTPS} eq 'ON';
19 17 100       46 my $default_port = $env->{'psgi.url_scheme'} eq 'https' ? 443 : 80;
20              
21              
22             # If we are running as a backend server, the user will always appear
23             # as 127.0.0.1. Select the most recent upstream IP (last in the list)
24 17 100       37 if ( $env->{'HTTP_X_FORWARDED_FOR'} ) {
25 2         13 my ( $ip, ) = $env->{HTTP_X_FORWARDED_FOR} =~ /([^,\s]+)$/;
26 2         8 $env->{REMOTE_ADDR} = $ip;
27             }
28              
29 17 100       50 if ( $env->{HTTP_X_FORWARDED_HOST} ) {
    50          
30              
31             # in apache1 ServerName example.com:443
32 8 100       19 if ( $env->{HTTP_X_FORWARDED_SERVER} ) {
33 2         12 my ( $host, ) = $env->{HTTP_X_FORWARDED_SERVER} =~ /([^,\s]+)$/;
34 2 100       10 if ( $host =~ /^(.+):(\d+)$/ ) {
35             # $host = $1;
36 1         4 $env->{SERVER_PORT} = $2;
37 1 50       5 $env->{'psgi.url_scheme'} = 'https' if $env->{SERVER_PORT} == 443;
38             }
39 2         6 $env->{HTTP_HOST} = $host;
40             }
41              
42 8         42 my ( $host, ) = $env->{HTTP_X_FORWARDED_HOST} =~ /([^,\s]+)$/;
43 8 100       31 if ( $host =~ /^(.+):(\d+)$/ ) {
    100          
44             # $host = $1;
45 2         8 $env->{SERVER_PORT} = $2;
46             } elsif ( $env->{HTTP_X_FORWARDED_PORT} ) {
47             # in apache2 httpd.conf (RequestHeader set X-Forwarded-Port 8443)
48 2         18 $env->{SERVER_PORT} = $env->{HTTP_X_FORWARDED_PORT};
49 2         7 $host .= ":$env->{SERVER_PORT}";
50             $env->{'psgi.url_scheme'} = 'https'
51 2 100       15 if $env->{SERVER_PORT} == 443;
52             } else {
53 4         9 $env->{SERVER_PORT} = $default_port;
54             }
55 8         17 $env->{HTTP_HOST} = $host;
56              
57             } elsif ( $env->{HTTP_HOST} ) {
58 9         19 my $host = $env->{HTTP_HOST};
59 9 100       49 if ($host =~ /^(.+):(\d+)$/ ) {
    50          
60             # $env->{HTTP_HOST} = $1;
61 1         5 $env->{SERVER_PORT} = $2;
62             } elsif ($host =~ /^(.+)$/ ) {
63 8         23 $env->{HTTP_HOST} = $1;
64 8         14 $env->{SERVER_PORT} = $default_port;
65             }
66             }
67              
68 17         54 $self->app->($env);
69             }
70              
71             1;
72              
73             __END__