File Coverage

blib/lib/HTTP/Engine/Role/RequestBuilder/ParseEnv.pm
Criterion Covered Total %
statement 40 40 100.0
branch 18 18 100.0
condition 9 9 100.0
subroutine 5 5 100.0
pod n/a
total 72 72 100.0


line stmt bran cond sub pod time code
1             package HTTP::Engine::Role::RequestBuilder::ParseEnv;
2 51     51   35535 use Any::Moose '::Role';
  51         117  
  51         377  
3              
4             with 'HTTP::Engine::Role::RequestBuilder::Standard' => {
5             -alias => { _build_hostname => "_resolve_hostname" }, # we might be able to get it from the env
6             };
7              
8             sub _build_connection_info {
9 34     34   83 my($self, $req) = @_;
10              
11 34         635 my $env = $req->_connection->{env};
12              
13             return {
14 34         1303 address => $env->{REMOTE_ADDR},
15             protocol => $env->{SERVER_PROTOCOL},
16             method => $env->{REQUEST_METHOD},
17             port => $env->{SERVER_PORT},
18             user => $env->{REMOTE_USER},
19             _https_info => $env->{HTTPS},
20             request_uri => $env->{REQUEST_URI},
21             }
22             }
23              
24             sub _build_headers {
25 9     9   25 my ($self, $req) = @_;
26              
27 9         44 my $env = $req->_connection->{env};
28              
29 9         32 HTTP::Headers::Fast->new(
30             map {
31 189         501 (my $field = $_) =~ s/^HTTPS?_//;
32 9         70 ( $field => $env->{$_} );
33             }
34 9         82 grep { /^(?:HTTP|CONTENT|COOKIE)/i } keys %$env
35             );
36             }
37              
38             sub _build_hostname {
39 2     2   4 my ( $self, $req ) = @_;
40 2 100       25 $req->_connection->{env}{REMOTE_HOST} || $self->_resolve_hostname($req);
41             }
42              
43             sub _build_uri {
44 27     27   91 my($self, $req) = @_;
45              
46 27         117 my $env = $req->_connection->{env};
47              
48 27 100       204 my $scheme = $req->secure ? 'https' : 'http';
49 27   100     132 my $host = $env->{HTTP_HOST} || $env->{SERVER_NAME};
50             # my $port = $env->{SERVER_PORT} || ( $req->secure ? 443 : 80 );
51 27         59 my $port = $env->{SERVER_PORT};
52 27 100       133 $port = ( $req->secure ? 443 : 80 ) unless $port; # dirty code for coverage_test
    100          
53              
54 27         52 my $base_path;
55 27 100       96 if (exists $env->{REDIRECT_URL}) {
56 2         6 $base_path = $env->{REDIRECT_URL};
57 2 100       32 $base_path =~ s/$env->{PATH_INFO}$// if exists $env->{PATH_INFO};
58             } else {
59 25   100     141 $base_path = $env->{SCRIPT_NAME} || '/';
60             }
61              
62 27   100     407 my $path = $base_path . ($env->{PATH_INFO} || '');
63 27         190 $path =~ s{^/+}{};
64              
65             # for proxy request
66 27 100       230 $path = $base_path = '/' if $req->proxy_request;
67              
68 27         209 my $uri = URI->new;
69 27         20201 $uri->scheme($scheme);
70 27         19104 $uri->host($host);
71 27         2368 $uri->port($port);
72 27   100     1579 $uri->path($path || '/');
73 27 100       931 $uri->query($env->{QUERY_STRING}) if $env->{QUERY_STRING};
74              
75             # sanitize the URI
76 27         154 $uri = $uri->canonical;
77              
78             # set the base URI
79             # base must end in a slash
80 27         5108 $base_path =~ s{^/+}{};
81 27 100       110 $base_path .= '/' unless $base_path =~ /\/$/;
82 27         101 my $base = $uri->clone;
83 27         261 $base->path_query($base_path);
84              
85 27         945 return URI::WithBase->new($uri, $base);
86             }
87              
88             1;
89