Neaf/Request/PSGI.pm

filter: Statementstmt 92.8% 91 / 98 Branchbran 72.7% 16 / 22 Conditioncond 46.6% 7 / 15 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 89.6% 26 / 29 Podpod 100% 16 / 16 totaltotal 83.0% 157 / 189 CC 17 SCAR 7.7
CC
17
Cov
84%
new
5.2
do_get_upload
3.3
__ANON__
3.0
CRAP 2.2
« Neaf/X/Session.pm ↑ Summary Neaf/X/Session/Cookie.pm »
1 package MVC::Neaf::Request::PSGI;
2  
3 84 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 84 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 84 our $VERSION = '0.2901';
6  
7 =head1 NAME
8  
9 MVC::Neaf::Request::PSGI - Not Even A Framework: PSGI driver.
10  
11 =head1 METHODS
12  
13 =cut
14  
15 84 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.4
16 # NOTE HACK prevent 'Can't locate object method seek via package IO::Handle'
17 # try preloading it by hand (errors ignored)
18 0 ▶ eval { require FileHandle }
Branch: 1 branch
BranchTrueFalseTotal
if $] < "5.014" 0 84 84
19 if $] < 5.014;
20 # NOTE HACK - prevent load-time warnings from Cookie::Baker
21 # which we aren't even using
22 84 eval {
23 84 ▶ local $SIG{__WARN__} = sub {};
Subroutine __ANON__: not called CC=1 SCAR=0.0
24 84 require Cookie::Baker;
25 };
26 };
27  
28 84 ▶use URI::Escape qw(uri_unescape);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 84 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 84 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 84 ▶use HTTP::Headers::Fast; # we want 0.21, but will tolerate older ones
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 84 ▶use parent qw(MVC::Neaf::Request);
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 84 ▶if (!HTTP::Headers::Fast->can( "psgi_flatten_without_sort" ) || HTTP::XSHeaders->can("new")) {
Branch: 1 branch
BranchTrueFalseTotal
if (not "HTTP::Headers::Fast"->can("psgi_flatten_without_sort") or "HTTP::XSHeaders"->can("new")) 0 84 84
Condition33%1/3
not "HTTP::Headers::Fast"->can("psgi_flatten_without_sort") or "HTTP::XSHeaders"->can("new")
l!l&&r!l&&!r
0084
MC/DC0%0/2
not "HTTP::Headers::Fast"->can("psgi_flatten_without_sort") or "HTTP::XSHeaders"->can("new")
not "HTTP::Headers::Fast"->can("psgi_flatten_without_sort") "HTTP::XSHeaders"->can("new")
Truth table33%1/3
not(A) or B
A= not "HTTP::Headers::Fast"->can("psgi_flatten_without_sort")B= "HTTP::XSHeaders"->can("new")
ABresult
1X1
011
000
36 # NOTE HACK Versions below 0.21 don't support the method we call
37 # in do_reply() so fall back to failsafe emulation
38 # NOTE XSHeaders doesn't (yet) provide this method, so fallback as well
39 # See https://rt.cpan.org/Ticket/Display.html?id=123850
40 84 ▶ no warnings 'once', 'redefine'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
41 0 ▶ *HTTP::Headers::Fast::psgi_flatten_without_sort = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 0 my $self = shift;
43 0 my @all;
44 0 ▶ $self->scan( sub { push @all, $_[0]=>$_[1] } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 0 return \@all;
46 0 };
47 };
48  
49  
50 =head2 new( env => $psgi_input )
51  
52 Constructor. C<env> MUST follow L<PSGI> requirements.
53  
54 =cut
55  
56 84 my %default_env = (
57 REQUEST_METHOD => 'GET',
58 );
59  
60 # TODO 0.30 rewrite env copying for good.
61 # Maybe separate ::GET and ::POST to avoid if's
62 160 ▶sub new {
Subroutine new: called CC=5 SCAR=16.5
Pod documented
63 160 my $class = shift;
64  
65 160 my $self = $class->SUPER::new( @_ );
66  
67 # Don't modify env!
68 # Remove query string if not GET|HEAD
69 # so that GET params are not available inside POST by default
70 160 ▶ my $env = $self->{env} || \%default_env;
Condition50%1/2
$self->{'env'} || \%default_env
l!l
1600
MC/DC0%0/1
$self->{'env'} || \%default_env
$self->{'env'}
Truth table50%1/2
A
A= $self->{'env'}
Aresult
11
00
71 160 $self->{query_string} = $env->{QUERY_STRING};
72  
73 $self->{driver} ||= Plack::Request->new({
74 REQUEST_METHOD => 'GET',
75 %$env,
76 160 ▶ ($MVC::Neaf::Request::query_allowed{ $env->{REQUEST_METHOD} || 'GET' }
Branch: 1 branch
BranchTrueFalseTotal
$MVC::Neaf::Request::query_allowed{$env->{'REQUEST_METHOD'} or 'GET'} ? : 140 20 160
Condition60%3/5
$self->{"driver"} ||= "Plack::Request"->new({"REQUEST_METHOD", "GET", %$env, $MVC::Neaf::Request::query_allowed{$env->{"REQUEST_METHOD"} or "GET"} ? () : ("QUERY_STRING", "")})
l!l&&r!l&&!r
01600
$env->{'REQUEST_METHOD'} || 'GET'
l!l
14317
MC/DC0%0/2
$self->{"driver"} ||= "Plack::Request"->new({"REQUEST_METHOD", "GET", %$env, $MVC::Neaf::Request::query_allowed{$env->{"REQUEST_METHOD"} or "GET"} ? () : ("QUERY_STRING", "")})
$self->{"driver"} "Plack::Request"->new({"REQUEST_METHOD", "GET", %$env, $MVC::Neaf::Request::query_allowed{$env->{"REQUEST_METHOD"} or "GET"} ? () : ("QUERY_STRING", "")})
MC/DC100%1/1
$env->{'REQUEST_METHOD'} || 'GET'
$env->{'REQUEST_METHOD'}
Truth table33%1/3
A ||= B
A= $self->{"driver"}B= "Plack::Request"->new({"REQUEST_METHOD", "GET", %$env, $MVC::Neaf::Request::query_allowed{$env->{"REQUEST_METHOD"} or "GET"} ? () : ("QUERY_STRING", "")})
ABresult
1X1
011
000
Truth table100%2/2
A
A= $env->{'REQUEST_METHOD'}
Aresult
11
00
77 ? () : (QUERY_STRING => '')),
78 });
79  
80 160 return $self;
81 };
82  
83 =head2 do_get_client_ip
84  
85 =cut
86  
87 1 ▶sub do_get_client_ip {
Subroutine do_get_client_ip: called CC=1 SCAR=0.0
Pod documented
88 1 my $self = shift;
89  
90 1 return $self->{driver}->address;
91 };
92  
93 =head2 do_get_http_version()
94  
95 =cut
96  
97 1 ▶sub do_get_http_version {
Subroutine do_get_http_version: called CC=2 SCAR=7.0
Pod documented
98 1 my $self = shift;
99  
100 1 ▶ my $proto = $self->{driver}->protocol || '1.0';
Condition50%1/2
$self->{'driver'}->protocol || '1.0'
l!l
01
MC/DC0%0/1
$self->{'driver'}->protocol || '1.0'
$self->{'driver'}->protocol
Truth table50%1/2
A
A= $self->{'driver'}->protocol
Aresult
11
00
101 1 $proto =~ s#^HTTP/##;
102  
103 1 return $proto;
104 };
105  
106 =head2 do_get_scheme()
107  
108 =cut
109  
110 6 ▶sub do_get_scheme {
Subroutine do_get_scheme: called CC=1 SCAR=0.0
Pod documented
111 6 my $self = shift;
112 6 return $self->{driver}->scheme;
113 };
114  
115 =head2 do_get_hostname()
116  
117 =cut
118  
119 3 ▶sub do_get_hostname {
Subroutine do_get_hostname: called CC=2 SCAR=7.1
Pod documented
120 3 my $self = shift;
121 3 my $base = $self->{driver}->base;
122  
123 3 ▶ return $base =~ m#//([^:?/]+)# ? $1 : "localhost";
Branch: 1 branch
BranchTrueFalseTotal
$base =~ m[//([^:?/]+)] ? : 3 0 3
124 };
125  
126 =head2 do_get_port()
127  
128 =cut
129  
130 3 ▶sub do_get_port {
Subroutine do_get_port: called CC=2 SCAR=6.9
Pod documented
131 3 my $self = shift;
132 3 my $base = $self->{driver}->base;
133  
134 3 ▶ return $base =~ m#//([^:?/]+):(\d+)# ? $2 : "80";
Branch: 1 branch
BranchTrueFalseTotal
$base =~ m[//([^:?/]+):(\d+)] ? : 1 2 3
135 };
136  
137 =head2 do_get_method()
138  
139 Return GET/POST.
140  
141 =cut
142  
143 158 ▶sub do_get_method {
Subroutine do_get_method: called CC=1 SCAR=0.0
Pod documented
144 158 my $self = shift;
145 158 return $self->{driver}->method;
146 };
147  
148 =head2 do_get_path()
149  
150 Returns the path part of URI.
151  
152 =cut
153  
154 156 ▶sub do_get_path {
Subroutine do_get_path: called CC=2 SCAR=6.9
Pod documented
155 156 my $self = shift;
156  
157 156 my $path = $self->{env}{REQUEST_URI};
158 156 ▶ $path = '' unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 11 145 156
159  
160 156 $path =~ s#\?.*$##;
161 156 $path =~ s#^/*#/#;
162  
163 156 return $path;
164 };
165  
166 =head2 do_get_params()
167  
168 Returns GET/POST parameters as a hash.
169  
170 B<CAVEAT> Plack::Request's multivalue hash params are ignored for now.
171  
172 =cut
173  
174 35 ▶sub do_get_params {
Subroutine do_get_params: called CC=2 SCAR=6.9
Pod documented
175 35 my $self = shift;
176  
177 35 my %hash;
178 35 foreach ( $self->{driver}->param ) {
179 38 $hash{$_} = $self->{driver}->param( $_ );
180 };
181  
182 35 return \%hash;
183 };
184  
185 =head2 do_get_param_as_array
186  
187 =cut
188  
189 7 ▶sub do_get_param_as_array {
Subroutine do_get_param_as_array: called CC=1 SCAR=0.0
Pod documented
190 7 my ($self, $name) = @_;
191  
192 7 return $self->{driver}->param( $name );
193 };
194  
195 =head2 do_get_upload( "name" )
196  
197 B<NOTE> This garbles Hash::Multivalue.
198  
199 =cut
200  
201 1 ▶sub do_get_upload {
Subroutine do_get_upload: called CC=3 SCAR=12.0
Pod documented
202 1 my ($self, $id) = @_;
203  
204 1 ▶ $self->{driver_upload} ||= $self->{driver}->uploads;
Condition33%1/3
$self->{"driver_upload"} ||= $self->{"driver"}->uploads
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"driver_upload"} ||= $self->{"driver"}->uploads
$self->{"driver_upload"} $self->{"driver"}->uploads
Truth table33%1/3
A ||= B
A= $self->{"driver_upload"}B= $self->{"driver"}->uploads
ABresult
1X1
011
000
205 1 my $up = $self->{driver_upload}{$id}; # TODO 0.90 don't garble multivalues
206  
207 1 ▶ return $up ? { tempfile => $up->path, filename => $up->filename } : ();
Branch: 1 branch
BranchTrueFalseTotal
$up ? : 0 1 1
208 };
209  
210 =head2 do_get_header_in
211  
212 =cut
213  
214 28 ▶sub do_get_header_in {
Subroutine do_get_header_in: called CC=1 SCAR=0.0
Pod documented
215 28 my $self = shift;
216  
217 28 return $self->{driver}->headers;
218 };
219  
220 =head2 do_get_body
221  
222 =cut
223  
224 6 ▶sub do_get_body {
Subroutine do_get_body: called CC=1 SCAR=0.0
Pod documented
225 6 my $self = shift;
226  
227 6 return $self->{driver}->content;
228 };
229  
230 =head2 do_reply( $status_line, \%headers, $content )
231  
232 Send reply to client. Not to be used directly.
233  
234 B<NOTE> This function just returns its input and has no side effect,
235 rather relying on PSGI calling conventions.
236  
237 =cut
238  
239 156 ▶sub do_reply {
Subroutine do_reply: called CC=2 SCAR=6.9
Pod documented
240 156 my ($self, $status, $content) = @_;
241  
242 156 my $header_array = $self->header_out->psgi_flatten_without_sort;
243  
244 # HACK - we're being returned by handler in MVC::Neaf itself in case of
245 # PSGI being used.
246  
247 156 ▶ if ($self->{response}{postponed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"response"}{"postponed"}) 5 151 156
248 # Even hackier HACK. If we have a postponed action,
249 # we must use PSGI functional interface to ensure
250 # reply is sent to client BEFORE
251 # postponed calls get executed.
252  
253 5 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
254 5 my $responder = shift;
255  
256 # TODO 0.90 should handle responder's failure somehow
257 5 $self->{writer} = $responder->( [ $status, $header_array ] );
258 5 ▶ $self->{writer}->write( $content ) if defined $content;
Branch: 1 branch
BranchTrueFalseTotal
if defined $content 3 2 5
259  
260 # Now we may need to output more stuff
261 # So save writer inside self for callbacks to write to
262 5 $self->execute_postponed;
263 # close was not called by 1 of callbacks
264 5 ▶ $self->do_close if $self->{continue};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"continue"} 5 0 5
265 5 };
266 };
267  
268 # Otherwise just return plain data.
269 151 return [ $status, $header_array, [ $content ]];
270 };
271  
272 =head2 do_write( $data )
273  
274 Write to socket in async content mode.
275  
276 =cut
277  
278 46 ▶sub do_write {
Subroutine do_write: called CC=2 SCAR=7.0
Pod documented
279 46 my ($self, $data) = @_;
280  
281 46 ▶ return unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 46 46
282  
283 # NOTE "can't call method write on undefined value" here
284 # probably means that PSGI responder failed unexpectedly in do_reply()
285 # and we didn't handle it properly and got empty {writer}
286 # and the request is being destroyed.
287 46 $self->{writer}->write( $data );
288 46 return 1;
289 };
290  
291 =head2 do_close()
292  
293 Close client connection in async content mode.
294  
295 =cut
296  
297 7 ▶sub do_close {
Subroutine do_close: called CC=1 SCAR=0.0
Pod documented
298 7 my $self = shift;
299  
300 7 $self->{writer}->close;
301 };
302  
303 =head1 LICENSE AND COPYRIGHT
304  
305 This module is part of L<MVC::Neaf> suite.
306  
307 Copyright 2016-2023 Konstantin S. Uvarin C<khedin@cpan.org>.
308  
309 This program is free software; you can redistribute it and/or modify it
310 under the terms of either: the GNU General Public License as published
311 by the Free Software Foundation; or the Artistic License.
312  
313 See L<http://dev.perl.org/licenses/> for more information.
314  
315 =cut
316  
317 84 1;
« Neaf/X/Session.pm ↑ Summary Neaf/X/Session/Cookie.pm »