blib/lib/Plack/Handler/CGI.pm

filter: Statementstmt 45.0% 27 / 60 Branchbran 12.5% 3 / 24 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 41.6% 5 / 12 Podpod 33.3% 1 / 3 totaltotal 32.7% 36 / 110 CC 17 SCAR 30.6
CC
17
Cov
33%
setup_env
56.0
_handle_response
17.5
run
12.0
CRAP 21.4
« blib/lib/Plack/Middleware/Lint.pm ↑ Summary blib/lib/Plack/Handler/Apache1.pm »
1 package Plack::Handler::CGI;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # copied from HTTP::Status
7 1 my %StatusCode = (
8 100 => 'Continue',
9 101 => 'Switching Protocols',
10 102 => 'Processing', # RFC 2518 (WebDAV)
11 103 => 'Early Hints',
12 200 => 'OK',
13 201 => 'Created',
14 202 => 'Accepted',
15 203 => 'Non-Authoritative Information',
16 204 => 'No Content',
17 205 => 'Reset Content',
18 206 => 'Partial Content',
19 207 => 'Multi-Status', # RFC 2518 (WebDAV)
20 300 => 'Multiple Choices',
21 301 => 'Moved Permanently',
22 302 => 'Found',
23 303 => 'See Other',
24 304 => 'Not Modified',
25 305 => 'Use Proxy',
26 307 => 'Temporary Redirect',
27 400 => 'Bad Request',
28 401 => 'Unauthorized',
29 402 => 'Payment Required',
30 403 => 'Forbidden',
31 404 => 'Not Found',
32 405 => 'Method Not Allowed',
33 406 => 'Not Acceptable',
34 407 => 'Proxy Authentication Required',
35 408 => 'Request Timeout',
36 409 => 'Conflict',
37 410 => 'Gone',
38 411 => 'Length Required',
39 412 => 'Precondition Failed',
40 413 => 'Request Entity Too Large',
41 414 => 'Request-URI Too Large',
42 415 => 'Unsupported Media Type',
43 416 => 'Request Range Not Satisfiable',
44 417 => 'Expectation Failed',
45 422 => 'Unprocessable Entity', # RFC 2518 (WebDAV)
46 423 => 'Locked', # RFC 2518 (WebDAV)
47 424 => 'Failed Dependency', # RFC 2518 (WebDAV)
48 425 => 'No code', # WebDAV Advanced Collections
49 426 => 'Upgrade Required', # RFC 2817
50 449 => 'Retry with', # unofficial Microsoft
51 500 => 'Internal Server Error',
52 501 => 'Not Implemented',
53 502 => 'Bad Gateway',
54 503 => 'Service Unavailable',
55 504 => 'Gateway Timeout',
56 505 => 'HTTP Version Not Supported',
57 506 => 'Variant Also Negotiates', # RFC 2295
58 507 => 'Insufficient Storage', # RFC 2518 (WebDAV)
59 509 => 'Bandwidth Limit Exceeded', # unofficial
60 510 => 'Not Extended', # RFC 2774
61 );
62  
63 0 ▶sub new { bless {}, shift }
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
64  
65 0 ▶sub run {
Subroutine run: not called CC=3 SCAR=24.8
Pod undocumented
66 0 my ($self, $app) = @_;
67  
68 0 my $env = $self->setup_env();
69  
70 0 my $res = $app->($env);
71 0 ▶ if (ref $res eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $res eq 'ARRAY') { } 0 0 0
elsif (ref $res eq 'CODE') { } 0 0 0
72 0 $self->_handle_response($res);
73 }
74 elsif (ref $res eq 'CODE') {
75 0 ▶ $res->(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 0 $self->_handle_response($_[0]);
77 0 });
78 }
79 else {
80 0 die "Bad response $res";
81 }
82 }
83  
84 0 ▶sub setup_env {
Subroutine setup_env: not called CC=7 SCAR=40.3
Pod documented
85 0 my ( $self, $override_env ) = @_;
86  
87 0 ▶ $override_env ||= {};
Condition0%0/2
$override_env ||= {}
l!l
00
MC/DC0%0/1
$override_env ||= {}
$override_env
Truth table0%0/2
A
A= $override_env
Aresult
11
00
88  
89 0 binmode STDIN;
90 0 binmode STDERR;
91  
92 my $env = {
93 %ENV,
94 'psgi.version' => [ 1, 1 ],
95 'psgi.url_scheme' => ($ENV{HTTPS}||'off') =~ /^(?:on|1)$/i ? 'https' : 'http',
96 'psgi.input' => *STDIN,
97 'psgi.errors' => *STDERR,
98 'psgi.multithread' => 0,
99 'psgi.multiprocess' => 1,
100 'psgi.run_once' => 1,
101 'psgi.streaming' => 1,
102 'psgi.nonblocking' => 1,
103 0 ▶ %{ $override_env },
Branch: 1 branch
BranchTrueFalseTotal
($ENV{'HTTPS'} || 'off') =~ /^(?:on|1)$/i ? : 0 0 0
Condition0%0/2
$ENV{'HTTPS'} || 'off'
l!l
00
MC/DC0%0/1
$ENV{'HTTPS'} || 'off'
$ENV{'HTTPS'}
Truth table0%0/2
A
A= $ENV{'HTTPS'}
Aresult
11
00
104 };
105  
106 0 delete $env->{HTTP_CONTENT_TYPE};
107 0 delete $env->{HTTP_CONTENT_LENGTH};
108 0 ▶ $env->{'HTTP_COOKIE'} ||= $ENV{COOKIE}; # O'Reilly server bug
Condition0%0/3
$env->{"HTTP_COOKIE"} ||= $ENV{"COOKIE"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$env->{"HTTP_COOKIE"} ||= $ENV{"COOKIE"}
$env->{"HTTP_COOKIE"} $ENV{"COOKIE"}
Truth table0%0/3
A ||= B
A= $env->{"HTTP_COOKIE"}B= $ENV{"COOKIE"}
ABresult
1X1
011
000
109  
110 0 ▶ if (!exists $env->{PATH_INFO}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $env->{"PATH_INFO"}) 0 0 0
111 0 $env->{PATH_INFO} = '';
112 }
113  
114 0 ▶ if ($env->{SCRIPT_NAME} eq '/') {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"SCRIPT_NAME"} eq "/") 0 0 0
115 0 $env->{SCRIPT_NAME} = '';
116 0 $env->{PATH_INFO} = '/' . $env->{PATH_INFO};
117 }
118  
119 0 return $env;
120 }
121  
122  
123  
124 1 ▶sub _handle_response {
Subroutine _handle_response: called CC=9 SCAR=28.6
125 1 my ($self, $res) = @_;
126  
127 1 *STDOUT->autoflush(1);
128 1 binmode STDOUT;
129  
130 1 my $hdrs;
131 1 my $message = $StatusCode{$res->[0]};
132 1 $hdrs = "Status: $res->[0] $message\015\012";
133  
134 1 my $headers = $res->[1];
135 1 ▶ while (my ($k, $v) = splice(@$headers, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@$headers, 0, 2)) 0 1 1
136 0 $hdrs .= "$k: $v\015\012";
137 }
138 1 $hdrs .= "\015\012";
139  
140 1 print STDOUT $hdrs;
141  
142 1 my $body = $res->[2];
143 1 ▶ my $cb = sub { print STDOUT $_[0] };
Subroutine __ANON__: called CC=1 SCAR=0.0
144  
145 # inline Plack::Util::foreach here
146 1 ▶ if (ref $body eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $body eq 'ARRAY') { } 1 0 1
elsif (defined $body) { } 0 0 0
147 1 for my $line (@$body) {
148 1 ▶ $cb->($line) if length $line;
Branch: 1 branch
BranchTrueFalseTotal
if length $line 1 0 1
149 }
150 }
151 elsif (defined $body) {
152 0 ▶ local $/ = \65536 unless ref $/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $/ 0 0 0
153 0 ▶ while (defined(my $line = $body->getline)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = $body->getline)) 0 0 0
154 0 ▶ $cb->($line) if length $line;
Branch: 1 branch
BranchTrueFalseTotal
if length $line 0 0 0
155 }
156 0 $body->close;
157 }
158 else {
159 0 return Plack::Handler::CGI::Writer->new;
160 }
161 }
162  
163 package Plack::Handler::CGI::Writer;
164 0 ▶sub new { bless \do { my $x }, $_[0] }
Subroutine new: not called CC=1 SCAR=6.9
165 0 ▶sub write { print STDOUT $_[1] }
Subroutine write: not called CC=1 SCAR=6.9
166 0 ▶sub close { }
Subroutine close: not called CC=1 SCAR=0.0
167  
168 package Plack::Handler::CGI;
169  
170 1 1;
171 __END__
« blib/lib/Plack/Middleware/Lint.pm ↑ Summary blib/lib/Plack/Handler/Apache1.pm »