| 1 | package HTTP::Engine::Role::RequestBuilder::ParseEnv; | ||
| 2 | use Any::Moose '::Role'; | ||
| 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 | my($self, $req) = @_; | ||
| 10 | |||
| 11 | my $env = $req->_connection->{env}; | ||
| 12 | |||
| 13 | return { | ||
| 14 | 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 | my ($self, $req) = @_; | ||
| 26 | |||
| 27 | my $env = $req->_connection->{env}; | ||
| 28 | |||
| 29 | HTTP::Headers::Fast->new( | ||
| 30 | map { | ||
| 31 | (my $field = $_) =~ s/^HTTPS?_//; | ||
| 32 | ( $field => $env->{$_} ); | ||
| 33 | } | ||
| 34 | grep { /^(?:HTTP|CONTENT|COOKIE)/i } keys %$env | ||
| 35 | ); | ||
| 36 | } | ||
| 37 | |||
| 38 | sub _build_hostname { | ||
| 39 | my ( $self, $req ) = @_; | ||
| 40 | $req->_connection->{env}{REMOTE_HOST} || $self->_resolve_hostname($req); | ||
| 41 | } | ||
| 42 | |||
| 43 | sub _build_uri { | ||
| 44 | my($self, $req) = @_; | ||
| 45 | |||
| 46 | my $env = $req->_connection->{env}; | ||
| 47 | |||
| 48 | my $scheme = $req->secure ? 'https' : 'http'; | ||
| 49 | my $host = $env->{HTTP_HOST} || $env->{SERVER_NAME}; | ||
| 50 | # my $port = $env->{SERVER_PORT} || ( $req->secure ? 443 : 80 ); | ||
| 51 | my $port = $env->{SERVER_PORT}; | ||
| 52 | $port = ( $req->secure ? 443 : 80 ) unless $port; # dirty code for coverage_test | ||
| 53 | |||
| 54 | my $base_path; | ||
| 55 | if (exists $env->{REDIRECT_URL}) { | ||
| 56 | $base_path = $env->{REDIRECT_URL}; | ||
| 57 | $base_path =~ s/$env->{PATH_INFO}$// if exists $env->{PATH_INFO}; | ||
| 58 | } else { | ||
| 59 | $base_path = $env->{SCRIPT_NAME} || '/'; | ||
| 60 | } | ||
| 61 | |||
| 62 | my $path = $base_path . ($env->{PATH_INFO} || ''); | ||
| 63 | $path =~ s{^/+}{}; | ||
| 64 | |||
| 65 | # for proxy request | ||
| 66 | $path = $base_path = '/' if $req->proxy_request; | ||
| 67 | |||
| 68 | my $uri = URI->new; | ||
| 69 | $uri->scheme($scheme); | ||
| 70 | $uri->host($host); | ||
| 71 | $uri->port($port); | ||
| 72 | $uri->path($path); | ||
| 73 | $uri->query($env->{QUERY_STRING}) if $env->{QUERY_STRING}; | ||
| 74 | |||
| 75 | # sanitize the URI | ||
| 76 | $uri = $uri->canonical; | ||
| 77 | |||
| 78 | # set the base URI | ||
| 79 | # base must end in a slash | ||
| 80 | $base_path =~ s{^/+}{}; | ||
| 81 | $base_path .= '/' unless $base_path =~ /\/$/; | ||
| 82 | my $base = $uri->clone; | ||
| 83 | $base->path_query($base_path); | ||
| 84 | |||
| 85 | return URI::WithBase->new($uri, $base); | ||
| 86 | } | ||
| 87 | |||
| 88 | 1; | ||
| 89 |