blib/lib/Plack/Handler/Apache2.pm untested

filter: Statementstmt 0.0% 0 / 120 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 212 CC 16 SCAR 34.2
CC
16
Cov
0%
call_app
90.0
_handle_response
42.0
fixup_path
12.0
CRAP 30.7
1 package Plack::Handler::Apache2;
2 use strict;
3 use warnings;
4 use Apache2::RequestRec;
5 use Apache2::RequestIO;
6 use Apache2::RequestUtil;
7 use Apache2::Response;
8 use Apache2::Const -compile => qw(OK);
9 use Apache2::Log;
10 use APR::Table;
11 use IO::Handle;
12 use Plack::Util;
13 use Scalar::Util;
14 use URI;
15 use URI::Escape;
16  
17 my %apps; # psgi file to $app mapping
18  
19 sub new { bless {}, shift }
20  
21 sub preload {
22 my $class = shift;
23 for my $app (@_) {
24 $class->load_app($app);
25 }
26 }
27  
28 sub load_app {
29 my($class, $app) = @_;
30 return $apps{$app} ||= do {
31 # Trick Catalyst, CGI.pm, CGI::Cookie and others that check
32 # for $ENV{MOD_PERL}.
33 #
34 # Note that we delete it instead of just localizing
35 # $ENV{MOD_PERL} because some users may check if the key
36 # exists, and we do it this way because "delete local" is new
37 # in 5.12:
38 # http://perldoc.perl.org/5.12.0/perldelta.html#delete-local
39 local $ENV{MOD_PERL};
40 delete $ENV{MOD_PERL};
41  
42 Plack::Util::load_psgi $app;
43 };
44 }
45  
46 sub call_app {
47 my ($class, $r, $app) = @_;
48  
49 $r->subprocess_env; # let Apache create %ENV for us :)
50  
51 my $env = {
52 %ENV,
53 'psgi.version' => [ 1, 1 ],
54 'psgi.url_scheme' => ($ENV{HTTPS}||'off') =~ /^(?:on|1)$/i ? 'https' : 'http',
55 'psgi.input' => $r,
56 'psgi.errors' => *STDERR,
57 'psgi.multithread' => Plack::Util::FALSE,
58 'psgi.multiprocess' => Plack::Util::TRUE,
59 'psgi.run_once' => Plack::Util::FALSE,
60 'psgi.streaming' => Plack::Util::TRUE,
61 'psgi.nonblocking' => Plack::Util::FALSE,
62 'psgix.harakiri' => Plack::Util::TRUE,
63 'psgix.cleanup' => Plack::Util::TRUE,
64 'psgix.cleanup.handlers' => [],
65 };
66  
67 if (defined(my $HTTP_AUTHORIZATION = $r->headers_in->{Authorization})) {
68 $env->{HTTP_AUTHORIZATION} = $HTTP_AUTHORIZATION;
69 }
70  
71 # If you supply more than one Content-Length header Apache will
72 # happily concat the values with ", ", e.g. "72, 72". This
73 # violates the PSGI spec so fix this up and just take the first
74 # one.
75 if (exists $env->{CONTENT_LENGTH} && $env->{CONTENT_LENGTH} =~ /,/) {
76 no warnings qw(numeric);
77 $env->{CONTENT_LENGTH} = int $env->{CONTENT_LENGTH};
78 }
79  
80 # Actually, we can not trust PATH_INFO from mod_perl because mod_perl squeezes multiple slashes into one slash.
81 my $uri = URI->new("http://".$r->hostname.$r->unparsed_uri);
82  
83 $env->{PATH_INFO} = uri_unescape($uri->path);
84  
85 $class->fixup_path($r, $env);
86  
87 my $res = $app->($env);
88  
89 if (ref $res eq 'ARRAY') {
90 _handle_response($r, $res);
91 }
92 elsif (ref $res eq 'CODE') {
93 $res->(sub {
94 _handle_response($r, $_[0]);
95 });
96 }
97 else {
98 die "Bad response $res";
99 }
100  
101 if (@{ $env->{'psgix.cleanup.handlers'} }) {
102 $r->push_handlers(
103 PerlCleanupHandler => sub {
104 for my $cleanup_handler (@{ $env->{'psgix.cleanup.handlers'} }) {
105 $cleanup_handler->($env);
106 }
107  
108 if ($env->{'psgix.harakiri.commit'}) {
109 $r->child_terminate;
110 }
111 },
112 );
113 } else {
114 if ($env->{'psgix.harakiri.commit'}) {
115 $r->child_terminate;
116 }
117 }
118  
119 return Apache2::Const::OK;
120 }
121  
122 sub handler {
123 my $class = __PACKAGE__;
124 my $r = shift;
125 my $psgi = $r->dir_config('psgi_app');
126 $class->call_app($r, $class->load_app($psgi));
127 }
128  
129 # The method for PH::Apache2::Registry to override.
130 sub fixup_path {
131 my ($class, $r, $env) = @_;
132  
133 # $env->{PATH_INFO} is created from unparsed_uri so it is raw.
134 my $path_info = $env->{PATH_INFO} || '';
135  
136 # Get argument of <Location> or <LocationMatch> directive
137 # This may be string or regexp and we can't know either.
138 my $location = $r->location;
139  
140 # Let's *guess* if we're in a LocationMatch directive
141 if ($location eq '/') {
142 # <Location /> could be handled as a 'root' case where we make
143 # everything PATH_INFO and empty SCRIPT_NAME as in the PSGI spec
144 $env->{SCRIPT_NAME} = '';
145 } elsif ($path_info =~ s{^($location)/?}{/}) {
146 $env->{SCRIPT_NAME} = $1 || '';
147 } else {
148 # Apache's <Location> is matched but here is not.
149 # This is something wrong. We can only respect original.
150 $r->server->log_error(
151 "Your request path is '$path_info' and it doesn't match your Location(Match) '$location'. " .
152 "This should be due to the configuration error. See perldoc Plack::Handler::Apache2 for details."
153 );
154 }
155  
156 $env->{PATH_INFO} = $path_info;
157 }
158  
159 sub _handle_response {
160 my ($r, $res) = @_;
161  
162 my ($status, $headers, $body) = @{ $res };
163  
164 my $hdrs = ($status >= 200 && $status < 300)
165 ? $r->headers_out : $r->err_headers_out;
166  
167 Plack::Util::header_iter($headers, sub {
168 my($h, $v) = @_;
169 if (lc $h eq 'content-type') {
170 $r->content_type($v);
171 } elsif (lc $h eq 'content-length') {
172 $r->set_content_length($v);
173 } else {
174 $hdrs->add($h => $v);
175 }
176 });
177  
178 $r->status($status);
179  
180 if (Scalar::Util::blessed($body) and $body->can('path') and my $path = $body->path) {
181 $r->sendfile($path);
182 } elsif (defined $body) {
183 Plack::Util::foreach($body, sub { $r->print(@_) });
184 $r->rflush;
185 }
186 else {
187 return Plack::Util::inline_object
188 write => sub { $r->print(@_); $r->rflush },
189 close => sub { $r->rflush };
190 }
191  
192 return Apache2::Const::OK;
193 }
194  
195 1;
196  
197 __END__