blib/lib/WebDAO/CV.pm

filter: Statementstmt 71.1% 111 / 156 Branchbran 48.3% 30 / 62 Conditioncond 48.2% 14 / 29 MC/DCmcdc 25.0% 4 / 16 Subroutinesub 85.0% 17 / 20 Podpod 83.3% 10 / 12 totaltotal 63.0% 186 / 295 SCAR 50.8
CC
47
Cov
63%
_parse_body
42.1
param
15.4
body
12.0
CRAP 161.1
« blib/lib/WebDAO/FCGI/ProcManager.pm ↑ Summary blib/lib/WebDAO/Element.pm »
1 #===============================================================================
2 #
3 # DESCRIPTION: controller
4 #
5 # AUTHOR: Aliaksandr P. Zahatski, <zag@cpan.org>
6 #===============================================================================
7 package WebDAO::CV;
8 5 our $VERSION = '0.02';
9 5 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use HTTP::Body;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use WebDAO::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use base qw( WebDAO::Base );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 5 __PACKAGE__->mk_attr(status=>200, _parsed_cookies=>undef);
18  
19 sub new {
20 13 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=12.0
Pod documented
21 13 ▶ my $self = bless( ( $#_ == 0 ) ? shift : {@_}, ref($class) || $class );
Branch: 1 branch
BranchTrueFalseTotal
$#_ == 0 ? : 0 13 13
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0130
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
22 13 $self->{headers} = {};
23 13 $self
24 }
25  
26 =head2 url (-options1=>1)
27  
28 from url: http://testwd.zag:82/Envs/partsh.sd?23=23
29 where options:
30
31 -path_info -> /Envs/partsh.sd
32 -base -> http://example.com:82
33  
34 defaul http://testwd.zag:82/Envs/partsh.sd?23=23
35
36 =cut
37  
38 sub url {
39 20 ▶ my $self = shift;
Subroutine url: called CC=9 SCAR=22.0
Pod documented
40 20 my %args = @_;
41 20 my $env = $self->{env};
42  
43 20 ▶ if ( exists $env->{FCGI_ROLE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $env->{"FCGI_ROLE"}) 4 16 20
44 ( $env->{PATH_INFO}, $env->{QUERY_STRING} ) =
45 4 $env->{REQUEST_URI} =~ /([^?]*)(?:\?(.*)$)?/s;
46 }
47 20 ▶ my $path = $env->{PATH_INFO} || ''; # 'PATH_INFO' => '/Env'
Condition100%2/2
$env->{'PATH_INFO'} || ''
l!l
416
MC/DC100%1/1
$env->{'PATH_INFO'} || ''
$env->{'PATH_INFO'}
Truth table100%2/2
A
A= $env->{'PATH_INFO'}
Aresult
11
00
48 20 ▶ my $host = $env->{HTTP_HOST} || 'example.org'; # 'HTTP_HOST' => '127.0.0.1:5000'
Condition100%2/2
$env->{'HTTP_HOST'} || 'example.org'
l!l
416
MC/DC100%1/1
$env->{'HTTP_HOST'} || 'example.org'
$env->{'HTTP_HOST'}
Truth table100%2/2
A
A= $env->{'HTTP_HOST'}
Aresult
11
00
49 20 ▶ my $query = $env->{QUERY_STRING}|| ''; # 'QUERY_STRING' => '434=34&erer=2'
Condition100%2/2
$env->{'QUERY_STRING'} || ''
l!l
416
MC/DC100%1/1
$env->{'QUERY_STRING'} || ''
$env->{'QUERY_STRING'}
Truth table100%2/2
A
A= $env->{'QUERY_STRING'}
Aresult
11
00
50 20 ▶ my $proto = $env->{'psgi.url_scheme'} || 'http';
Condition50%1/2
$env->{'psgi.url_scheme'} || 'http'
l!l
020
MC/DC0%0/1
$env->{'psgi.url_scheme'} || 'http'
$env->{'psgi.url_scheme'}
Truth table50%1/2
A
A= $env->{'psgi.url_scheme'}
Aresult
11
00
51 20 my $full_path = "$proto://${host}${path}?$query";
52 #clear / at end
53 20 ▶ $full_path =~ s!/$!! if $path =~ m!^/!;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ m[^/] 4 16 20
54 20 my $uri = URI->new($full_path);
55  
56 20 ▶ if ( exists $args{-path_info} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args{'-path_info'}) { } 5 15 20
elsif (exists $args{'-base'}) { } 9 6 15
57 5 return $uri->path();
58 }
59 elsif ( exists $args{-base} ) {
60 9 return "$proto://$host";
61 }
62 6 return URI->new($full_path)->canonical;
63 }
64  
65 =head2 method - HTTP method
66  
67 retrun HTTP method
68  
69 =cut
70  
71 sub method {
72 1 ▶ my $self = shift;
Subroutine method: called CC=2 SCAR=7.2
Pod documented
73 1 ▶ $self->{env}->{REQUEST_METHOD} || "GET";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"env"}{"REQUEST_METHOD"} or "GET" 0 1 1
74 }
75  
76 =head2 accept
77  
78 return hashref
79  
80 {
81 'application/xhtml+xml' => undef,
82 'application/xml' => undef,
83 'text/html' => undef
84 };
85  
86 =cut
87  
88 sub accept {
89 5 ▶ my $self = shift;
Subroutine accept: called CC=2 SCAR=6.9
Pod documented
90 5 ▶ my $accept = $self->{env}->{HTTP_ACCEPT} || return {};
Condition100%2/2
$self->{'env'}{'HTTP_ACCEPT'} || (return {})
l!l
14
MC/DC100%1/1
$self->{'env'}{'HTTP_ACCEPT'} || (return {})
$self->{'env'}{'HTTP_ACCEPT'}
Truth table100%2/2
A
A= $self->{'env'}{'HTTP_ACCEPT'}
Aresult
11
00
91 1 my ($types) = split( ';', $accept );
92 1 my %res;
93 1 @res{ split( ',', $types ) } = ();
94 1 \%res;
95 }
96  
97 =head2 param - return GET and POST params
98  
99 return params
100  
101 =cut
102  
103 sub param {
104 9 ▶ my $self = shift;
Subroutine param: called CC=10 SCAR=27.4
Pod documented
105 9 my $params = $self->{parsed_params};
106 9 ▶ unless ($params) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params) 5 4 9
107 #init by POST params
108 5 $params = $self->_parse_body;
109 5 my @get_params = $self->url()->query_form;
110 5 ▶ while (my ($k, $v) = splice(@get_params,0,2 )) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@get_params, 0, 2)) 1 5 6
111 1 ▶ unless ( exists $params->{ $k } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{$k}) { } 0 1 1
112 1 $params->{ $k } = $v
113 } else {
114 0 my $val = $params->{ $k };
115 #if array ?
116 0 ▶ if ( ref $val ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val) { } 0 0 0
117 0 push @$val, $v
118 } else {
119 0 ▶ $params->{ $k } = [$val, ref($v) ? @$v : $v]
Branch: 1 branch
BranchTrueFalseTotal
ref $v ? : 0 0 0
120 }
121 }
122 }
123 5 $self->{parsed_params} = $params;
124 }
125 9 ▶ return keys %$params unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 8 1 9
126 1 ▶ return undef unless exists $params->{$_[0]};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$params{$_[0]} 0 1 1
127 1 my $res = $params->{$_[0]};
128 1 ▶ if ( ref($res) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $res) 0 1 1
129 0 ▶ return wantarray ? @$res : $res->[0]
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
130 }
131 1 return $res;
132 }
133  
134 #parse body
135 sub _parse_body {
136 5 ▶ my $self = shift;
Subroutine _parse_body: called CC=8 SCAR=37.4
137  
138 5 my $content_type = $self->{env}->{CONTENT_TYPE};
139 5 my $content_length = $self->{env}->{CONTENT_LENGTH};
140 5 ▶ if (!$content_type && !$content_length) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($content_type or $content_length) 5 0 5
Condition33%1/3
$content_type or $content_length
l!l&&r!l&&!r
005
MC/DC0%0/2
$content_type or $content_length
$content_type $content_length
Truth table33%1/3
A or B
A= $content_typeB= $content_length
ABresult
1X1
011
000
141 5 return {};
142 }
143  
144 0 my $body = HTTP::Body->new($content_type, $content_length);
145 0 $body->cleanup(1);
146  
147 0 my $input = $self->{env}->{'psgi.input'};
148 0 ▶ if ( $input ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input) { } 0 0 0
149 #reset IO
150 0 $input->seek(0,0);
151 }
152 else {
153 # for FCGI, Shell
154 0 $input = \*STDIN
155 }
156 0 my $spin = 0;
157  
158 0 ▶ while ($content_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_length) 0 0 0
159 0 ▶ $input->read(my $chunk, $content_length < 8192 ? $content_length : 8192);
Branch: 1 branch
BranchTrueFalseTotal
$content_length < 8192 ? : 0 0 0
160 0 my $read = length $chunk;
161 0 $content_length -= $read;
162 0 $body->add($chunk);
163 0 ▶ if ($read == 0 && $spin++ > 2000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($read == 0 and $spin++ > 2000) 0 0 0
Condition0%0/3
$read == 0 and $spin++ > 2000
!ll&&!rl&&r
000
MC/DC0%0/2
$read == 0 and $spin++ > 2000
$read == 0 $spin++ > 2000
Truth table0%0/3
A and B
A= $read == 0B= $spin++ > 2000
ABresult
0X0
100
111
164 0 Carp::croak "Bad Content-Length: maybe client disconnect? ($content_length bytes remaining)";
165 }
166 }
167 0 $self->{'http.body'} = $body;
168 0 return $body->param
169 }
170  
171 =head2 body - HTTP body file descriptor ( see get-body for get content)
172  
173 Return HTTP body file descriptor
174  
175 my $body;
176 {
177 local $/;
178 my $fd = $request->body;
179 $body = <$fd>;
180 }
181  
182 =cut
183  
184 sub body {
185 0 ▶ my $self = shift;
Subroutine body: not called CC=3 SCAR=24.8
Pod documented
186 0 ▶ unless ( exists $self->{'http.body'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"http.body"}) 0 0 0
187 0 $self->_parse_body();
188 }
189  
190 0 ▶ my $http_body = $self->{'http.body'} || return undef;
Condition0%0/2
$self->{'http.body'} || (return undef)
l!l
00
MC/DC0%0/1
$self->{'http.body'} || (return undef)
$self->{'http.body'}
Truth table0%0/2
A
A= $self->{'http.body'}
Aresult
11
00
191 0 return $http_body->body;
192 }
193  
194 =head2 get-body - HTTP body content
195  
196 Return HTTP body text
197  
198 my $body= $r->get_body;
199  
200 =cut
201  
202 sub get_body {
203 0 ▶ my $self = shift;
Subroutine get_body: not called CC=2 SCAR=17.9
Pod undocumented
204 0 my $body;
205 {
206 0 local $/;
207 0 ▶ if ( my $fd = $self->body ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fd = $self->body) 0 0 0
208 0 $body = <$fd>
209 }
210 }
211 0 return $body
212 }
213  
214  
215 =head2 upload - return upload content
216  
217 print Dumper $request->upload;
218  
219 For command:
220  
221 curl -i -X POST -H "Content-Type: multipart/form-data"\
222 -F "data=@UserSettings.txt"\
223 http://example.org/Upload
224  
225 output:
226  
227 {
228 'data' => {
229 'headers' => {
230 'Content-Type' => 'text/plain',
231 'Content-Disposition' => 'form-data; name="data"; filename="UserSettings.txt"'
232 },
233 'tempname' => '/tmp/txBmaz5Bpf.txt',
234 'size' => 6704,
235 'filename' => 'UserSettings.txt',
236 'name' => 'data'
237 }
238 };
239  
240 =cut
241  
242 sub upload {
243 0 ▶ my $self = shift;
Subroutine upload: not called CC=3 SCAR=24.8
Pod documented
244 0 ▶ unless ( exists $self->{'http.body'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"http.body"}) 0 0 0
245 0 $self->_parse_body();
246 }
247  
248 0 ▶ my $http_body = $self->{'http.body'} || return {};
Condition0%0/2
$self->{'http.body'} || (return {})
l!l
00
MC/DC0%0/1
$self->{'http.body'} || (return {})
$self->{'http.body'}
Truth table0%0/2
A
A= $self->{'http.body'}
Aresult
11
00
249 0 return $http_body->upload;
250 }
251  
252 =head2 set_header
253  
254 $cv->set_header("Content-Type" => 'text/html; charset=utf-8')
255  
256 =cut
257  
258 sub set_header {
259 5 ▶ my ( $self, $name, $par ) = @_;
Subroutine set_header: called CC=2 SCAR=8.4
Pod documented
260  
261 #collect -cookies
262 5 ▶ if ( $name eq 'Set-Cookie' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq 'Set-Cookie') { } 0 5 5
263 0 push @{ $self->{headers}->{$name} }, $par;
264 }
265 else {
266 5 $self->{headers}->{$name} = $par;
267 }
268 }
269  
270 =head3 print_headers [ header1=>value, ...]
271  
272 Method for output headers
273  
274 =cut
275  
276 sub print_headers {
277 4 ▶ my $self = shift;
Subroutine print_headers: called CC=8 SCAR=21.3
Pod documented
278 #save cookie
279 4 my $cookie = delete $self->{headers}->{"Set-Cookie"};
280 #merge in and exists headers
281 4 my %headers = ( %{ $self->{headers} } , @_ );
282 #merge cookies
283 4 ▶ if ( $cookie ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cookie) 0 4 4
284 0 push @{ $headers{"Set-Cookie"} }, @$cookie;
285 }
286 4 my @cookies_headers = ();
287 #format cookies
288 4 ▶ if ( my $cookies = delete $headers{"Set-Cookie"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cookies = delete $headers{"Set-Cookie"}) 1 3 4
289 1 foreach my $c ( @$cookies ) {
290 2 my $hvalue;
291 2 ▶ if (ref($c) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c eq 'HASH') { } 2 0 2
292 2 ▶ my $path = $c->{path} || '/';
Condition50%1/2
$c->{'path'} || '/'
l!l
02
MC/DC0%0/1
$c->{'path'} || '/'
$c->{'path'}
Truth table50%1/2
A
A= $c->{'path'}
Aresult
11
00
293 # Set-Cookie: srote=ewe&1&1&2; path=$path
294 2 $hvalue = "$c->{name}=$c->{value}; path=$path";
295 2 ▶ if (my $expires = $c->{expires}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $expires = $c->{"expires"}) 1 1 2
296 1 my @MON = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
297 1 my @WDAY = qw( Sun Mon Tue Wed Thu Fri Sat );
298 1 my($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($expires);
299 1 $year += 1900;
300 1 $expires = sprintf("%s, %02d-%s-%04d %02d:%02d:%02d GMT",
301 $WDAY[$wday], $mday, $MON[$mon], $year, $hour, $min, $sec);
302 1 $hvalue .=" ;expires=$expires";
303 }
304 0 } else { $hvalue = $c }
305 2 push @cookies_headers, "Set-Cookie", $hvalue;
306 }
307 }
308 4 my $status = $self->status;
309 4 ▶ my $fd = $self->{writer}->([$status||"200", [%headers, @cookies_headers], undef]);
Condition50%1/2
$status || '200'
l!l
40
MC/DC0%0/1
$status || '200'
$status
Truth table50%1/2
A
A= $status
Aresult
11
00
310 4 $self->{fd} = $fd;
311 }
312  
313 sub print {
314 1 ▶ my $self = shift;
Subroutine print: called CC=4 SCAR=14.9
Pod undocumented
315 1 ▶ if (exists $self->{fd}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'fd'}) { } 1 0 1
316 1 foreach my $line (@_) {
317 1 ▶ utf8::encode( $line) if utf8::is_utf8($line);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($line) 0 1 1
318 1 $self->{fd}->write($line);
319 }
320 } else {
321 0 print @_;
322 }
323 }
324  
325 =head2 get_cookie
326  
327 return hashref to {key=>value}
328  
329 =cut
330  
331 sub get_cookie {
332 1 ▶ my $self = shift;
Subroutine get_cookie: called CC=3 SCAR=11.3
Pod documented
333 1 ▶ my $str = $self->{env}->{HTTP_COOKIE} || return {};
Condition50%1/2
$self->{'env'}{'HTTP_COOKIE'} || (return {})
l!l
10
MC/DC0%0/1
$self->{'env'}{'HTTP_COOKIE'} || (return {})
$self->{'env'}{'HTTP_COOKIE'}
Truth table50%1/2
A
A= $self->{'env'}{'HTTP_COOKIE'}
Aresult
11
00
334 1 ▶ if ($self->_parsed_cookies) { return $self->_parsed_cookies };
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_parsed_cookies) 0 1 1
335 1 my %res;
336 %res =
337 1 map { URI::Escape::uri_unescape($_) } map { split '=',$_,2 } split(/\s*[;]\s*/,
338 $str);
339 1 $self->_parsed_cookies(\%res);
340 1 \%res;
341 }
342  
343  
344 5 1;
345  
346  
« blib/lib/WebDAO/FCGI/ProcManager.pm ↑ Summary blib/lib/WebDAO/Element.pm »