| 1 | package Lemonldap::NG::Handler::ApacheMP2::Request; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use base 'Lemonldap::NG::Common::PSGI::Request'; | ||
| 5 | use Plack::Util; | ||
| 6 | use URI; | ||
| 7 | use URI::Escape; | ||
| 8 | |||
| 9 | our $VERSION = '2.21.0'; | ||
| 10 | |||
| 11 | # Build Plack::Request (inspired from Plack::Handler::Apache2) | ||
| 12 | sub new { | ||
| 13 | my ( $class, $r ) = @_; | ||
| 14 | |||
| 15 | # $r->subprocess_env breaks header modification. That's why it is not used | ||
| 16 | # here | ||
| 17 | my ( $uri, $args ) = ( $r->uri, $r->args ); | ||
| 18 | my $uri_full = $uri . ( $args ? "?$args" : '' ); | ||
| 19 | my $env = { | ||
| 20 | |||
| 21 | #%ENV, | ||
| 22 | HTTP_HOST => $r->hostname, | ||
| 23 | REMOTE_ADDR => $r->useragent_ip, | ||
| 24 | QUERY_STRING => $args, | ||
| 25 | REQUEST_URI => $uri_full, | ||
| 26 | PATH_INFO => '', | ||
| 27 | SERVER_PORT => $ENV{SERVER_PORT} || $r->get_server_port, | ||
| 28 | REQUEST_METHOD => $r->method, | ||
| 29 | UNIQUE_ID => $r->subprocess_env('UNIQUE_ID'), | ||
| 30 | 'psgi.version' => [ 1, 1 ], | ||
| 31 | 'psgi.url_scheme' => ( $ENV{HTTPS} || 'off' ) =~ /^(?:on|1)$/i | ||
| 32 | ? 'https' | ||
| 33 | : 'http', | ||
| 34 | 'psgi.input' => $r, | ||
| 35 | 'psgi.errors' => *STDERR, | ||
| 36 | 'psgi.multithread' => Plack::Util::FALSE, | ||
| 37 | 'psgi.multiprocess' => Plack::Util::TRUE, | ||
| 38 | 'psgi.run_once' => Plack::Util::FALSE, | ||
| 39 | 'psgi.streaming' => Plack::Util::TRUE, | ||
| 40 | 'psgi.nonblocking' => Plack::Util::FALSE, | ||
| 41 | 'psgix.harakiri' => Plack::Util::TRUE, | ||
| 42 | 'psgix.cleanup' => Plack::Util::TRUE, | ||
| 43 | 'psgix.cleanup.handlers' => [], | ||
| 44 | 'psgi.r' => $r, | ||
| 45 | }; | ||
| 46 | $r->headers_in->do( | ||
| 47 | sub { | ||
| 48 | my $h = shift; | ||
| 49 | my $k = uc($h); | ||
| 50 | if ( $k ne 'HOST' ) { | ||
| 51 | $k =~ s/-/_/g; | ||
| 52 | $env->{"HTTP_$k"} = $r->headers_in->{$h}; | ||
| 53 | } | ||
| 54 | return 1; | ||
| 55 | } | ||
| 56 | ); | ||
| 57 | my $uri = URI->new( "http://" . $r->hostname . $r->unparsed_uri ); | ||
| 58 | $env->{PATH_INFO} = uri_unescape( $uri->path ); | ||
| 59 | |||
| 60 | my $self = Lemonldap::NG::Common::PSGI::Request->new($env); | ||
| 61 | bless $self, $class; | ||
| 62 | return $self; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub data { | ||
| 66 | my ($self) = @_; | ||
| 67 | return $self->{data} ||= {}; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub wantJSON { | ||
| 71 | return 1 | ||
| 72 | if ( defined $_[0]->env->{HTTP_ACCEPT} | ||
| 73 | and $_[0]->env->{HTTP_ACCEPT} =~ m#(?:application|text)/json#i ); | ||
| 74 | return 0; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub request_id { return $_[0]->env->{UNIQUE_ID} } | ||
| 78 | |||
| 79 | 1; |