Neaf/Request/Apache2.pm

filter: Statementstmt 62.9% 68 / 108 Branchbran 41.6% 10 / 24 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 66.6% 18 / 27 Podpod 100% 16 / 16 totaltotal 62.1% 115 / 185 CC 18 SCAR 18.3
CC
18
Cov
59%
do_get_client_ip
30.0
BEGIN
7.5
do_get_upload
6.0
CRAP 6.2
« Neaf/Exception.pm ↑ Summary Neaf/X/Session/File.pm »
1 package MVC::Neaf::Request::Apache2;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '0.2901';
7  
8 =head1 NAME
9  
10 MVC::Neaf::Request::Apache2 - Apache2 (mod_perl) driver for Not Even A Framework.
11  
12 =head1 WARNING
13  
14 B<This module is DEPRECATED and will be removed in Neaf 0.30.>
15  
16 Use L<Plack::Handler::Apache2> instead.
17  
18 =head1 SYNOPSIS
19  
20 Apache2 request that will invoke MVC::Neaf core functions from under mod_perl.
21  
22 The following apache configuration should work with this module:
23  
24 LoadModule perl_module modules/mod_perl.so
25 PerlSwitches -I[% YOUR_LIB_DIRECTORY %]
26 LoadModule apreq_module [% modules %]/mod_apreq2.so
27  
28 # later...
29 PerlModule MVC::Neaf::Request::Apache2
30 PerlPostConfigRequire [% YOUR_APPLICATION %]
31 <Location /[% SOME_URL_PREFIX %]>
32 SetHandler perl-script
33 PerlResponseHandler MVC::Neaf::Request::Apache2
34 </Location>
35  
36 =head1 METHODS
37  
38 =cut
39  
40 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 ▶use HTTP::Headers::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 2 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 2 my %fail_apache;
46 2 ▶BEGIN {
Subroutine BEGIN: called CC=7 SCAR=20.1
47 2 foreach my $mod (qw(
48 Apache2::RequestRec
49 Apache2::RequestIO
50 Apache2::Connection
51 APR::SockAddr
52 Apache2::Request
53 Apache2::Upload
54 Apache2::Const
55 )) {
56 14 ▶ eval { load $mod; 1 } and next;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { &load($mod); 1 } } and next 7 7 14
57 # warn "Failed to load $mod: $@";
58 7 $fail_apache{$mod} = $@;
59 };
60  
61 2 ▶ if ($ENV{MOD_PERL} && %fail_apache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"MOD_PERL"} and %fail_apache) 0 2 2
Condition66%2/3
$ENV{"MOD_PERL"} and %fail_apache
!ll&&!rl&&r
110
MC/DC0%0/2
$ENV{"MOD_PERL"} and %fail_apache
$ENV{"MOD_PERL"} %fail_apache
Truth table66%2/3
A and B
A= $ENV{"MOD_PERL"}B= %fail_apache
ABresult
0X0
100
111
62 carp "$_ failed to load: $fail_apache{$_}"
63 0 for keys %fail_apache;
64 0 croak "Apache2 modules not loaded, refusing to run right away";
65 };
66  
67 2 ▶ if (!%fail_apache) {
Branch: 1 branch
BranchTrueFalseTotal
if (not %fail_apache) 1 1 2
68 1 Apache2::Const->import( -compile => 'OK' );
69 };
70 };
71  
72 # TODO 0.30 remove the whole module
73 carp __PACKAGE__." is DEPRECATED and will be REMOVED in 0.30. Use Plack::Handler::Apache2 instead."
74 2 ▶ if $ENV{MOD_PERL};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"MOD_PERL"} 1 1 2
75  
76 2 ▶use MVC::Neaf;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 2 ▶use parent qw(MVC::Neaf::Request);
Subroutine BEGIN: called CC=1 SCAR=0.0
78  
79 =head2 do_get_client_ip
80  
81 =cut
82  
83 2 my $client_ip_name;
84 0 ▶sub do_get_client_ip {
Subroutine do_get_client_ip: not called CC=5 SCAR=34.0
Pod documented
85 0 my $self = shift;
86  
87 0 my $conn = $self->{driver_raw}->connection;
88 0 ▶ if (!$client_ip_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($client_ip_name) 0 0 0
89 # Apache 2.4 breaks API violently, so autodetect on first run,
90 # fall back to localhost
91 0 foreach (qw(remote_ip client_ip)) {
92 0 ▶ $conn->can($_) or next;
Branch: 1 branch
BranchTrueFalseTotal
$conn->can($_) or next 0 0 0
93 0 $client_ip_name = $_;
94 0 last;
95 };
96 0 ▶ if (!$client_ip_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($client_ip_name) 0 0 0
97 0 carp("WARNING: No client_ip found under Apache2, inform MVC::Neaf author");
98 0 return '127.0.0.1';
99 };
100 };
101  
102 0 return $conn->$client_ip_name;
103 };
104  
105 =head2 do_get_http_version
106  
107 =cut
108  
109 0 ▶sub do_get_http_version {
Subroutine do_get_http_version: not called CC=1 SCAR=6.9
Pod documented
110 0 my $self = shift;
111 0 my $proto = $self->{driver_raw}->proto_num;
112 0 $proto =~ /^\D*(\d+?)\D*(\d\d?\d?)$/;
113 0 return join ".", 0+$1, 0+$2;
114 };
115  
116 =head2 do_get_scheme
117  
118 =cut
119  
120 1 ▶sub do_get_scheme {
Subroutine do_get_scheme: called CC=3 SCAR=12.5
Pod documented
121 1 my $self = shift;
122  
123 # Shamelessly stolen from Catalyst
124 1 my $https = $self->{driver_raw}->subprocess_env('HTTPS');
125 1 ▶ return( ($https && uc $https eq 'ON') ? "https" : "http" );
Branch: 1 branch
BranchTrueFalseTotal
$https && uc $https eq 'ON' ? : 0 1 1
Condition33%1/3
$https and uc $https eq "ON"
!ll&&!rl&&r
100
MC/DC0%0/2
$https and uc $https eq "ON"
$https uc $https eq "ON"
Truth table33%1/3
A and B
A= $httpsB= uc $https eq "ON"
ABresult
0X0
100
111
126 };
127  
128 =head2 do_get_hostname
129  
130 =cut
131  
132 0 ▶sub do_get_hostname {
Subroutine do_get_hostname: not called CC=1 SCAR=6.9
Pod documented
133 0 my $self = shift;
134 0 return $self->{driver_raw}->hostname;
135 };
136  
137 =head2 do_get_port()
138  
139 =cut
140  
141 0 ▶sub do_get_port {
Subroutine do_get_port: not called CC=1 SCAR=6.9
Pod documented
142 0 my $self = shift;
143  
144 0 my $conn = $self->{driver_raw}->connection;
145 0 return $conn->local_addr->port;
146 };
147  
148 =head2 do_get_method()
149  
150 =cut
151  
152 1 ▶sub do_get_method {
Subroutine do_get_method: called CC=1 SCAR=0.0
Pod documented
153 1 my $self = shift;
154  
155 1 return $self->{driver_raw}->method;
156 };
157  
158 =head2 do_get_path()
159  
160 =cut
161  
162 1 ▶sub do_get_path {
Subroutine do_get_path: called CC=1 SCAR=0.0
Pod documented
163 1 my $self = shift;
164  
165 1 return $self->{driver_raw}->uri;
166 };
167  
168 =head2 do_get_params()
169  
170 =cut
171  
172 1 ▶sub do_get_params {
Subroutine do_get_params: called CC=2 SCAR=6.9
Pod documented
173 1 my $self = shift;
174  
175 1 my %hash;
176 1 my $r = $self->{driver};
177 1 $hash{$_} = $r->param($_) for $r->param;
178  
179 1 return \%hash;
180 };
181  
182 =head2 do_get_param_as_array
183  
184 =cut
185  
186 0 ▶sub do_get_param_as_array {
Subroutine do_get_param_as_array: not called CC=1 SCAR=6.9
Pod documented
187 0 my ($self, $name) = @_;
188  
189 0 return $self->{driver}->param( $name );
190 };
191  
192 =head2 do_get_header_in()
193  
194 =cut
195  
196 1 ▶sub do_get_header_in {
Subroutine do_get_header_in: called CC=1 SCAR=0.8
Pod documented
197 1 my $self = shift;
198  
199 1 my %head;
200 0 ▶ $self->{driver_raw}->headers_in->do( sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
201 0 my ($key, $val) = @_;
202 0 push @{ $head{$key} }, $val;
203 1 });
204  
205 1 return HTTP::Headers::Fast->new( %head );
206 };
207  
208 =head2 do_get_upload( "name" )
209  
210 Convert apache upload object into MCV::Neaf::Upload.
211  
212 =cut
213  
214 0 ▶sub do_get_upload {
Subroutine do_get_upload: not called CC=2 SCAR=17.9
Pod documented
215 0 my ($self, $name) = @_;
216  
217 0 my $r = $self->{driver};
218 0 my $upload = $r->upload($name);
219  
220 0 ▶ return $upload ? {
Branch: 1 branch
BranchTrueFalseTotal
$upload ? : 0 0 0
221 handle => $upload->fh,
222 tempfile => $upload->tempname,
223 filename => $upload->filename,
224 } : ();
225 };
226  
227 =head2 do_get_body
228  
229 =cut
230  
231 0 ▶sub do_get_body {
Subroutine do_get_body: not called CC=2 SCAR=17.9
Pod documented
232 0 my $self = shift;
233  
234 # use Apache2::RequestIO
235 # read until there's EOF, then concatenate & return
236 0 my $r = $self->{driver_raw};
237  
238 0 my @buf = ('');
239 0 ▶ while ( $r->read( $buf[-1], 8192, 0 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->read($buf[-1], 8192, 0)) 0 0 0
240 0 push @buf, '';
241 };
242  
243 0 return join '', @buf;
244 };
245  
246 =head2 do_reply( $status, $content )
247  
248 =cut
249  
250 1 ▶sub do_reply {
Subroutine do_reply: called CC=1 SCAR=0.0
Pod documented
251 1 my ($self, $status, $content) = @_;
252  
253 1 my $r = $self->{driver_raw};
254  
255 1 my ($type) = $self->header_out->remove_header("content_type");
256 1 $r->status( $status );
257 1 $r->content_type( $type );
258  
259 1 my $head_backend = $r->headers_out;
260 1 ▶ $self->header_out->scan( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
261 1 $head_backend->add( $_[0], $_[1] );
262 1 });
263  
264 1 return $r->print( $content );
265 };
266  
267 =head2 do_write( $data )
268  
269 Write to socket if async content serving is in use.
270  
271 =cut
272  
273 0 ▶sub do_write {
Subroutine do_write: not called CC=1 SCAR=6.9
Pod documented
274 0 my ($self, $data) = @_;
275 0 return $self->{driver_raw}->print( $data );
276 };
277  
278 =head2 handler( $apache_request )
279  
280 A valid Apache2/mod_perl handler.
281  
282 This invokes MCV::Neaf->handle_request when called.
283  
284 Unfortunately, libapreq (in addition to mod_perl) is required currently.
285  
286 =cut
287  
288 1 ▶sub handler : method {
Subroutine handler: called CC=2 SCAR=7.2
Pod documented
289 1 my ($class, $r) = @_;
290  
291 1 my $self = $class->new(
292 driver_raw => $r,
293 driver => Apache2::Request->new($r),
294 query_string => $r->args,
295 );
296 1 ▶ if (!$MVC::Neaf::Request::query_allowed{ $r->method }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($MVC::Neaf::Request::query_allowed{$r->method}) 0 1 1
297 0 $r->args('');
298 };
299 1 my $reply = MVC::Neaf::neaf()->handle_request( $self );
300  
301 1 return Apache2::Const::OK();
302 };
303  
304 =head2 failed_startup()
305  
306 If Apache modules failed to load on startup, report error here.
307  
308 This is done so because adding Apache2::* as dependencies would impose
309 a HUGE headache on PSGI users.
310  
311 Ideally, this module should be mover out of the repository altogether.
312  
313 =cut
314  
315 1 ▶sub failed_startup {
Subroutine failed_startup: called CC=2 SCAR=7.6
Pod documented
316 1 ▶ return %fail_apache ? \%fail_apache : ();
Branch: 1 branch
BranchTrueFalseTotal
%fail_apache ? : 0 1 1
317 };
318  
319 =head1 LICENSE AND COPYRIGHT
320  
321 This module is part of L<MVC::Neaf> suite.
322  
323 Copyright 2016-2023 Konstantin S. Uvarin C<khedin@cpan.org>.
324  
325 This program is free software; you can redistribute it and/or modify it
326 under the terms of either: the GNU General Public License as published
327 by the Free Software Foundation; or the Artistic License.
328  
329 See L<http://dev.perl.org/licenses/> for more information.
330  
331 =cut
332  
333 2 1;
« Neaf/Exception.pm ↑ Summary Neaf/X/Session/File.pm »