Tiny.pm

filter: Statementstmt 96.2% 534 / 555 Branchbran 78.4% 284 / 362 Conditioncond 66.4% 99 / 149 MC/DCmcdc 40.7% 35 / 86 Subroutinesub 97.6% 82 / 84 Podpod 98.4% 64 / 65 totaltotal 84.4% 1098 / 1301 CC 244 SCAR 26.8
CC
244
Cov
86%
_response_headers
40.1
render
29.0
_body_params
27.3
CRAP 14.5
« Tiny/Multipart.pm ↑ Summary
1 package CGI::Tiny;
2 # ABSTRACT: Common Gateway Interface, with no frills
3  
4 # This file is part of CGI::Tiny which is released under:
5 # The Artistic License 2.0 (GPL Compatible)
6 # See the documentation for CGI::Tiny for full license details.
7  
8 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use IO::Handle ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 7 our $VERSION = '1.003';
15  
16 7 ▶use constant DEFAULT_REQUEST_BODY_LIMIT => 16777216;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use constant DEFAULT_REQUEST_BODY_BUFFER => 262144;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 7 ▶use constant DEFAULT_RESPONSE_BODY_BUFFER => 131072;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 7 our @EXPORT = 'cgi';
21  
22 # List from HTTP::Status 6.46
23 # Unmarked codes are from RFC 7231 (2017-12-20)
24 7 my %HTTP_STATUS = (
25 100 => 'Continue',
26 101 => 'Switching Protocols',
27 102 => 'Processing', # RFC 2518: WebDAV
28 103 => 'Early Hints', # RFC 8297: Indicating Hints
29 200 => 'OK',
30 201 => 'Created',
31 202 => 'Accepted',
32 203 => 'Non-Authoritative Information',
33 204 => 'No Content',
34 205 => 'Reset Content',
35 206 => 'Partial Content', # RFC 7233: Range Requests
36 207 => 'Multi-Status', # RFC 4918: WebDAV
37 208 => 'Already Reported', # RFC 5842: WebDAV bindings
38 226 => 'IM Used', # RFC 3229: Delta encoding
39 300 => 'Multiple Choices',
40 301 => 'Moved Permanently',
41 302 => 'Found',
42 303 => 'See Other',
43 304 => 'Not Modified', # RFC 7232: Conditional Request
44 305 => 'Use Proxy',
45 307 => 'Temporary Redirect',
46 308 => 'Permanent Redirect', # RFC 9110: HTTP Semantics
47 400 => 'Bad Request',
48 401 => 'Unauthorized', # RFC 7235: Authentication
49 402 => 'Payment Required',
50 403 => 'Forbidden',
51 404 => 'Not Found',
52 405 => 'Method Not Allowed',
53 406 => 'Not Acceptable',
54 407 => 'Proxy Authentication Required', # RFC 7235: Authentication
55 408 => 'Request Timeout',
56 409 => 'Conflict',
57 410 => 'Gone',
58 411 => 'Length Required',
59 412 => 'Precondition Failed', # RFC 7232: Conditional Request
60 413 => 'Content Too Large', # RFC 9110: HTTP Semantics
61 414 => 'URI Too Long',
62 415 => 'Unsupported Media Type',
63 416 => 'Range Not Satisfiable', # RFC 7233: Range Requests
64 417 => 'Expectation Failed',
65 418 => 'I\'m a teapot', # RFC 2324: HTCPC/1.0 1-april
66 421 => 'Misdirected Request', # RFC 9110: HTTP Semantics
67 422 => 'Unprocessable Content', # RFC 9110: HTTP Semantics
68 423 => 'Locked', # RFC 4918: WebDAV
69 424 => 'Failed Dependency', # RFC 4918: WebDAV
70 425 => 'Too Early', # RFC 8470: Using Early Data in HTTP
71 426 => 'Upgrade Required',
72 428 => 'Precondition Required', # RFC 6585: Additional Codes
73 429 => 'Too Many Requests', # RFC 6585: Additional Codes
74 431 => 'Request Header Fields Too Large', # RFC 6585: Additional Codes
75 451 => 'Unavailable For Legal Reasons', # RFC 7725: Legal Obstacles
76 500 => 'Internal Server Error',
77 501 => 'Not Implemented',
78 502 => 'Bad Gateway',
79 503 => 'Service Unavailable',
80 504 => 'Gateway Timeout',
81 505 => 'HTTP Version Not Supported',
82 506 => 'Variant Also Negotiates', # RFC 2295: Transparant Ngttn
83 507 => 'Insufficient Storage', # RFC 4918: WebDAV
84 508 => 'Loop Detected', # RFC 5842: WebDAV bindings
85 509 => 'Bandwidth Limit Exceeded', # Apache / cPanel
86 510 => 'Not Extended', # RFC 2774: Extension Framework
87 511 => 'Network Authentication Required', # RFC 6585: Additional Codes
88 );
89  
90 {
91 7 my $cgi;
92  
93 8 ▶ sub import {
Subroutine import: called CC=2 SCAR=7.2
94 # for cleanup in END in case of premature exit
95 8 ▶ $cgi ||= bless {pid => $$}, $_[0];
Condition50%1/2
$cgi ||= bless({"pid", $$}, $_[0])
l!l
08
MC/DC0%0/1
$cgi ||= bless({"pid", $$}, $_[0])
$cgi
Truth table50%1/2
A
A= $cgi
Aresult
11
00
96 8 goto &Exporter::import;
97 }
98  
99 75 ▶ sub cgi (&) {
Subroutine cgi: called CC=7 SCAR=19.7
Pod undocumented
100 75 my ($handler) = @_;
101 75 ▶ $cgi ||= bless {pid => $$}, __PACKAGE__;
Condition100%2/2
$cgi ||= bless({"pid", $$}, __PACKAGE__)
l!l
570
MC/DC100%1/1
$cgi ||= bless({"pid", $$}, __PACKAGE__)
$cgi
Truth table100%2/2
A
A= $cgi
Aresult
11
00
102 75 ▶ if (@ARGV and !defined $ENV{REQUEST_METHOD}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV and not defined $ENV{"REQUEST_METHOD"}) 0 75 75
Condition33%1/3
@ARGV and not defined $ENV{"REQUEST_METHOD"}
!ll&&!rl&&r
7500
MC/DC0%0/2
@ARGV and not defined $ENV{"REQUEST_METHOD"}
@ARGV not defined $ENV{"REQUEST_METHOD"}
Truth table33%1/3
A and not(B)
A= @ARGVB= not defined $ENV{"REQUEST_METHOD"}
ABresult
0X0
100
111
103 0 require CGI::Tiny::_Debug;
104 0 CGI::Tiny::_Debug::debug_command($cgi, [@ARGV]);
105 }
106 75 my ($error, $errored);
107 {
108 75 local $@;
109 75 ▶ eval { local $_ = $cgi; $handler->(); 1 } or do { $error = $@; $errored = 1 };
Branch: 1 branch
BranchTrueFalseTotal
eval { do { local $_ = $cgi; &$handler(); 1 } } or do { $error = $@; $errored = 1 } 65 8 73
110 }
111 73 ▶ if ($errored) {
Branch: 2 branches
BranchTrueFalseTotal
if ($errored) { } 8 65 73
elsif (not $cgi->{'headers_rendered'}) { } 20 45 65
112 8 _handle_error($cgi, $error);
113 } elsif (!$cgi->{headers_rendered}) {
114 20 _handle_error($cgi, "cgi completed without rendering a response\n");
115 }
116 73 undef $cgi;
117 73 1;
118 }
119  
120 # cleanup of premature exit, more reliable than potentially doing this in global destruction
121 # ModPerl::Registry or CGI::Compile won't run END after each request,
122 # but they override exit to throw an exception which we handle already
123 7 ▶ END {
Subroutine END: called CC=3 SCAR=11.1
124 7 ▶ if (defined $cgi) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cgi) 5 2 7
125 5 ▶ _handle_error($cgi, "cgi exited without rendering a response\n") unless $cgi->{headers_rendered};
Branch: 1 branch
BranchTrueFalseTotal
unless $cgi->{"headers_rendered"} 5 0 5
126 5 undef $cgi;
127 }
128 }
129 }
130  
131 7 ▶sub _handle_error {
Subroutine _handle_error: called CC=10 SCAR=23.6
132 33 my ($cgi, $error) = @_;
133 33 ▶ return unless $cgi->{pid} == $$; # in case of fork
Branch: 1 branch
BranchTrueFalseTotal
unless $cgi->{"pid"} == $$ 1 32 33
134 $cgi->{response_status} = "500 $HTTP_STATUS{500}" unless $cgi->{headers_rendered}
135 32 ▶ or (defined $cgi->{response_status} and $cgi->{response_status} =~ m/^[45][0-9]{2} /);
Branch: 1 branch
BranchTrueFalseTotal
unless $cgi->{"headers_rendered"} or defined $cgi->{"response_status"} and $cgi->{"response_status"} =~ /^[45][0-9]{2} / 22 10 32
Condition100%6/6
$cgi->{"headers_rendered"} or defined $cgi->{"response_status"} and $cgi->{"response_status"} =~ /^[45][0-9]{2} /
l!l&&r!l&&!r
1922
defined $cgi->{"response_status"} and $cgi->{"response_status"} =~ /^[45][0-9]{2} /
!ll&&!rl&&r
2119
MC/DC100%3/3
$cgi->{"headers_rendered"} or defined $cgi->{"response_status"} and $cgi->{"response_status"} =~ /^[45][0-9]{2} /
$cgi->{"headers_rendered"} defined $cgi->{"response_status"} $cgi->{"response_status"} =~ /^[45][0-9]{2} /
Truth table100%4/4
A or B and C
A= $cgi->{"headers_rendered"}B= defined $cgi->{"response_status"}C= $cgi->{"response_status"} =~ /^[45][0-9]{2} /
ABCresult
1XX1
0111
00X0
0100
136 32 ▶ if (defined(my $handler = $cgi->{on_error})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $handler = $cgi->{'on_error'})) { } 30 2 32
137 30 my ($error_error, $error_errored);
138 {
139 30 local $@;
140 0 ▶ eval { $handler->($cgi, $error, !!$cgi->{headers_rendered}); 1 } or do { $error_error = $@; $error_errored = 1 };
Branch: 1 branch
BranchTrueFalseTotal
eval { do { &$handler($cgi, $error, !!$cgi->{"headers_rendered"}); 1 } } or do { $error_error = $@; $error_errored = 1 } 30 0 30
141 }
142 30 ▶ return unless $cgi->{pid} == $$; # in case of fork in error handler
Branch: 1 branch
BranchTrueFalseTotal
unless $cgi->{"pid"} == $$ 0 30 30
143 30 ▶ if ($error_errored) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error_errored) 0 30 30
144 0 warn "Exception in error handler: $error_error";
145 0 warn "Original error: $error";
146 }
147 } else {
148 2 warn $error;
149 }
150 32 ▶ $cgi->set_response_type('text/plain')->render(data => $cgi->{response_status}) unless $cgi->{headers_rendered};
Branch: 1 branch
BranchTrueFalseTotal
unless $cgi->{"headers_rendered"} 31 1 32
151 }
152  
153 30 ▶sub set_error_handler { $_[0]{on_error} = $_[1]; $_[0] }
Subroutine set_error_handler: called CC=1 SCAR=0.0
Pod documented
154 0 ▶sub set_request_body_buffer { $_[0]{request_body_buffer} = $_[1]; $_[0] }
Subroutine set_request_body_buffer: not called CC=1 SCAR=6.9
Pod documented
155 1 ▶sub set_request_body_limit { $_[0]{request_body_limit} = $_[1]; $_[0] }
Subroutine set_request_body_limit: called CC=1 SCAR=0.0
Pod documented
156 5 ▶sub set_multipart_form_options { $_[0]{multipart_form_options} = $_[1]; $_[0] }
Subroutine set_multipart_form_options: called CC=1 SCAR=0.0
Pod documented
157 5 ▶sub set_multipart_form_charset { $_[0]{multipart_form_charset} = $_[1]; $_[0] }
Subroutine set_multipart_form_charset: called CC=1 SCAR=0.0
Pod documented
158 75 ▶sub set_input_handle { $_[0]{input_handle} = $_[1]; $_[0] }
Subroutine set_input_handle: called CC=1 SCAR=0.0
Pod documented
159 75 ▶sub set_output_handle { $_[0]{output_handle} = $_[1]; $_[0] }
Subroutine set_output_handle: called CC=1 SCAR=0.0
Pod documented
160  
161 1 ▶sub auth_type { defined $ENV{AUTH_TYPE} ? $ENV{AUTH_TYPE} : '' }
Subroutine auth_type: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'AUTH_TYPE'} ? : 1 0 1
162 1 ▶sub content_length { defined $ENV{CONTENT_LENGTH} ? $ENV{CONTENT_LENGTH} : '' }
Subroutine content_length: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'CONTENT_LENGTH'} ? : 1 0 1
163 1 ▶sub content_type { defined $ENV{CONTENT_TYPE} ? $ENV{CONTENT_TYPE} : '' }
Subroutine content_type: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'CONTENT_TYPE'} ? : 1 0 1
164 1 ▶sub gateway_interface { defined $ENV{GATEWAY_INTERFACE} ? $ENV{GATEWAY_INTERFACE} : '' }
Subroutine gateway_interface: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'GATEWAY_INTERFACE'} ? : 1 0 1
165 2 ▶sub path_info { defined $ENV{PATH_INFO} ? $ENV{PATH_INFO} : '' }
Subroutine path_info: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'PATH_INFO'} ? : 2 0 2
166 7 *path = \&path_info;
167 1 ▶sub path_translated { defined $ENV{PATH_TRANSLATED} ? $ENV{PATH_TRANSLATED} : '' }
Subroutine path_translated: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'PATH_TRANSLATED'} ? : 1 0 1
168 5 ▶sub query_string { defined $ENV{QUERY_STRING} ? $ENV{QUERY_STRING} : '' }
Subroutine query_string: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'QUERY_STRING'} ? : 5 0 5
169 7 *query = \&query_string;
170 1 ▶sub remote_addr { defined $ENV{REMOTE_ADDR} ? $ENV{REMOTE_ADDR} : '' }
Subroutine remote_addr: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'REMOTE_ADDR'} ? : 1 0 1
171 1 ▶sub remote_host { defined $ENV{REMOTE_HOST} ? $ENV{REMOTE_HOST} : '' }
Subroutine remote_host: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'REMOTE_HOST'} ? : 1 0 1
172 1 ▶sub remote_ident { defined $ENV{REMOTE_IDENT} ? $ENV{REMOTE_IDENT} : '' }
Subroutine remote_ident: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'REMOTE_IDENT'} ? : 1 0 1
173 1 ▶sub remote_user { defined $ENV{REMOTE_USER} ? $ENV{REMOTE_USER} : '' }
Subroutine remote_user: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'REMOTE_USER'} ? : 1 0 1
174 2 ▶sub request_method { defined $ENV{REQUEST_METHOD} ? $ENV{REQUEST_METHOD} : '' }
Subroutine request_method: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'REQUEST_METHOD'} ? : 2 0 2
175 7 *method = \&request_method;
176 1 ▶sub script_name { defined $ENV{SCRIPT_NAME} ? $ENV{SCRIPT_NAME} : '' }
Subroutine script_name: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'SCRIPT_NAME'} ? : 1 0 1
177 1 ▶sub server_name { defined $ENV{SERVER_NAME} ? $ENV{SERVER_NAME} : '' }
Subroutine server_name: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'SERVER_NAME'} ? : 1 0 1
178 1 ▶sub server_port { defined $ENV{SERVER_PORT} ? $ENV{SERVER_PORT} : '' }
Subroutine server_port: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'SERVER_PORT'} ? : 1 0 1
179 1 ▶sub server_protocol { defined $ENV{SERVER_PROTOCOL} ? $ENV{SERVER_PROTOCOL} : '' }
Subroutine server_protocol: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'SERVER_PROTOCOL'} ? : 1 0 1
180 1 ▶sub server_software { defined $ENV{SERVER_SOFTWARE} ? $ENV{SERVER_SOFTWARE} : '' }
Subroutine server_software: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'SERVER_SOFTWARE'} ? : 1 0 1
181  
182 1 ▶sub headers {
Subroutine headers: called CC=4 SCAR=13.9
Pod documented
183 1 my ($self) = @_;
184 1 ▶ unless (exists $self->{request_headers}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"request_headers"}) 1 0 1
185 1 my %headers;
186 1 foreach my $key (keys %ENV) {
187 54 my $name = $key;
188 54 ▶ next unless $name =~ s/^HTTP_//;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ s/^HTTP_// 51 3 54
189 3 $name =~ tr/_/-/;
190 3 $headers{lc $name} = $ENV{$key};
191 }
192 1 $self->{request_headers} = \%headers;
193 }
194 1 return {%{$self->{request_headers}}};
195 }
196  
197 2 ▶sub header { (my $name = $_[1]) =~ tr/-/_/; $ENV{"HTTP_\U$name"} }
Subroutine header: called CC=1 SCAR=0.0
Pod documented
198  
199 1 ▶sub cookies { [map { [@$_] } @{$_[0]->_cookies->{ordered}}] }
Subroutine cookies: called CC=1 SCAR=0.0
Pod documented
200 1 ▶sub cookie_names { [@{$_[0]->_cookies->{names}}] }
Subroutine cookie_names: called CC=1 SCAR=0.0
Pod documented
201 2 ▶sub cookie { my $c = $_[0]->_cookies->{keyed}; exists $c->{$_[1]} ? $c->{$_[1]}[-1] : undef }
Subroutine cookie: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
exists $$c{$_[1]} ? : 1 1 2
202 1 ▶sub cookie_array { my $c = $_[0]->_cookies->{keyed}; exists $c->{$_[1]} ? [@{$c->{$_[1]}}] : [] }
Subroutine cookie_array: called CC=2 SCAR=7.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
exists $$c{$_[1]} ? : 1 0 1
203  
204 5 ▶sub _cookies {
Subroutine _cookies: called CC=7 SCAR=19.6
205 5 my ($self) = @_;
206 5 ▶ unless (exists $self->{request_cookies}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"request_cookies"}) 1 4 5
207 1 $self->{request_cookies} = {names => \my @names, ordered => \my @ordered, keyed => \my %keyed};
208 1 ▶ if (defined $ENV{HTTP_COOKIE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"HTTP_COOKIE"}) 1 0 1
209 1 foreach my $pair (split /\s*;\s*/, $ENV{HTTP_COOKIE}) {
210 4 ▶ next unless length $pair;
Branch: 1 branch
BranchTrueFalseTotal
unless length $pair 0 4 4
211 4 my ($name, $value) = split /=/, $pair, 2;
212 4 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 4 4
213 4 ▶ push @names, $name unless exists $keyed{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $keyed{$name} 3 1 4
214 4 push @ordered, [$name, $value];
215 4 push @{$keyed{$name}}, $value;
216 }
217 }
218 }
219 5 return $self->{request_cookies};
220 }
221  
222 1 ▶sub params { [map { [@$_] } @{$_[0]->_query_params->{ordered}}, @{$_[0]->_body_params->{ordered}}] }
Subroutine params: called CC=1 SCAR=0.0
Pod documented
223 1 ▶sub param_names { my $q = $_[0]->_query_params; [@{$q->{names}}, grep { !exists $q->{keyed}{$_} } @{$_[0]->_body_params->{names}}] }
Subroutine param_names: called CC=1 SCAR=0.0
Pod documented
224 3 ▶sub param {
Subroutine param: called CC=3 SCAR=11.0
Pod documented
225 3 my ($self, $name) = @_;
226 3 my $p = $self->_body_params->{keyed};
227 3 ▶ return $p->{$name}[-1] if exists $p->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $p->{$name} 1 2 3
228 2 my $q = $self->_query_params->{keyed};
229 2 ▶ return exists $q->{$name} ? $q->{$name}[-1] : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $q->{$name} ? : 1 1 2
230 }
231 3 ▶sub param_array { [map { exists $_->{$_[1]} ? @{$_->{$_[1]}} : () } $_[0]->_query_params->{keyed}, $_[0]->_body_params->{keyed}] }
Subroutine param_array: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
exists $$_{$_[1]} ? : 4 2 6
232  
233 1 ▶sub query_params { [map { [@$_] } @{$_[0]->_query_params->{ordered}}] }
Subroutine query_params: called CC=1 SCAR=0.0
Pod documented
234 1 ▶sub query_param_names { [@{$_[0]->_query_params->{names}}] }
Subroutine query_param_names: called CC=1 SCAR=0.0
Pod documented
235 2 ▶sub query_param { my $p = $_[0]->_query_params->{keyed}; exists $p->{$_[1]} ? $p->{$_[1]}[-1] : undef }
Subroutine query_param: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
exists $$p{$_[1]} ? : 1 1 2
236 2 ▶sub query_param_array { my $p = $_[0]->_query_params->{keyed}; exists $p->{$_[1]} ? [@{$p->{$_[1]}}] : [] }
Subroutine query_param_array: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
exists $$p{$_[1]} ? : 1 1 2
237  
238 13 ▶sub _query_params {
Subroutine _query_params: called CC=6 SCAR=17.9
239 13 my ($self) = @_;
240 13 ▶ unless (exists $self->{query_params}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"query_params"}) 3 10 13
241 3 $self->{query_params} = {names => \my @names, ordered => \my @ordered, keyed => \my %keyed};
242 3 foreach my $pair (split /[&;]/, $self->query) {
243 10 my ($name, $value) = split /=/, $pair, 2;
244 10 ▶ $value = '' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 10 10
245 10 do { tr/+/ /; s/%([0-9a-fA-F]{2})/chr hex $1/ge; utf8::decode $_ } for $name, $value;
246 10 ▶ push @names, $name unless exists $keyed{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $keyed{$name} 8 2 10
247 10 push @ordered, [$name, $value];
248 10 push @{$keyed{$name}}, $value;
249 }
250 }
251 13 return $self->{query_params};
252 }
253  
254 6 ▶sub body {
Subroutine body: called CC=9 SCAR=24.1
Pod documented
255 6 my ($self) = @_;
256 6 ▶ unless (exists $self->{body_content} or exists $self->{body_parts}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"body_content"} or exists $self->{"body_parts"}) 6 0 6
Condition33%1/3
exists $self->{"body_content"} or exists $self->{"body_parts"}
l!l&&r!l&&!r
006
MC/DC0%0/2
exists $self->{"body_content"} or exists $self->{"body_parts"}
exists $self->{"body_content"} exists $self->{"body_parts"}
Truth table33%1/3
A or B
A= exists $self->{"body_content"}B= exists $self->{"body_parts"}
ABresult
1X1
011
000
257 6 $self->{body_content} = '';
258 6 my $length = $self->_body_length;
259 5 ▶ my $in_fh = defined $self->{input_handle} ? $self->{input_handle} : *STDIN;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'input_handle'} ? : 5 0 5
260 5 binmode $in_fh;
261 5 ▶ my $buffer_size = 0 + ($self->{request_body_buffer} || $ENV{CGI_TINY_REQUEST_BODY_BUFFER} || DEFAULT_REQUEST_BODY_BUFFER);
Condition40%2/5
$self->{'request_body_buffer'} || $ENV{'CGI_TINY_REQUEST_BODY_BUFFER'} || 262144
l!l
05
$self->{"request_body_buffer"} or $ENV{"CGI_TINY_REQUEST_BODY_BUFFER"}
l!l&&r!l&&!r
005
MC/DC0%0/2
$self->{'request_body_buffer'} || $ENV{'CGI_TINY_REQUEST_BODY_BUFFER'} || 262144
$self->{"request_body_buffer"} $ENV{"CGI_TINY_REQUEST_BODY_BUFFER"}
Truth table33%1/3
A or B
A= $self->{"request_body_buffer"}B= $ENV{"CGI_TINY_REQUEST_BODY_BUFFER"}
ABresult
1X1
011
000
262 5 ▶ while ($length > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length > 0) 5 5 10
263 5 ▶ my $chunk = $length < $buffer_size ? $length : $buffer_size;
Branch: 1 branch
BranchTrueFalseTotal
$length < $buffer_size ? : 5 0 5
264 5 ▶ last unless my $read = read $in_fh, $self->{body_content}, $chunk, length $self->{body_content};
Branch: 1 branch
BranchTrueFalseTotal
unless my $read = read($in_fh, $self->{"body_content"}, $chunk, length $self->{"body_content"}) 0 5 5
265 5 $length -= $read;
266 }
267 }
268 5 return $self->{body_content};
269 }
270  
271 4 ▶sub body_json {
Subroutine body_json: called CC=4 SCAR=13.9
Pod documented
272 4 my ($self) = @_;
273 4 ▶ unless (exists $self->{body_json}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"body_json"}) 3 1 4
274 3 $self->{body_json} = undef;
275 3 ▶ if ($ENV{CONTENT_TYPE} and $ENV{CONTENT_TYPE} =~ m/^application\/json\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"CONTENT_TYPE"} and $ENV{"CONTENT_TYPE"} =~ m[^application/json\b]i) 2 1 3
Condition66%2/3
$ENV{"CONTENT_TYPE"} and $ENV{"CONTENT_TYPE"} =~ m[^application/json\b]i
!ll&&!rl&&r
102
MC/DC50%1/2
$ENV{"CONTENT_TYPE"} and $ENV{"CONTENT_TYPE"} =~ m[^application/json\b]i
$ENV{"CONTENT_TYPE"} $ENV{"CONTENT_TYPE"} =~ m[^application/json\b]i
Truth table66%2/3
A and B
A= $ENV{"CONTENT_TYPE"}B= $ENV{"CONTENT_TYPE"} =~ m[^application/json\b]i
ABresult
0X0
100
111
276 2 $self->{body_json} = $self->_json->decode($self->body);
277 }
278 }
279 4 return $self->{body_json};
280 }
281  
282 8 ▶sub body_params { [map { [@$_] } @{$_[0]->_body_params->{ordered}}] }
Subroutine body_params: called CC=1 SCAR=0.0
Pod documented
283 7 ▶sub body_param_names { [@{$_[0]->_body_params->{names}}] }
Subroutine body_param_names: called CC=1 SCAR=0.0
Pod documented
284 8 ▶sub body_param { my $p = $_[0]->_body_params->{keyed}; exists $p->{$_[1]} ? $p->{$_[1]}[-1] : undef }
Subroutine body_param: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
exists $$p{$_[1]} ? : 7 1 8
285 6 ▶sub body_param_array { my $p = $_[0]->_body_params->{keyed}; exists $p->{$_[1]} ? [@{$p->{$_[1]}}] : [] }
Subroutine body_param_array: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
exists $$p{$_[1]} ? : 5 1 6
286  
287 37 ▶sub _body_params {
Subroutine _body_params: called CC=26 SCAR=33.1
288 37 my ($self) = @_;
289 37 ▶ unless (exists $self->{body_params}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"body_params"}) 9 28 37
290 9 $self->{body_params} = {names => \my @names, ordered => \my @ordered, keyed => \my %keyed};
291 9 ▶ if ($ENV{CONTENT_TYPE} and $ENV{CONTENT_TYPE} =~ m/^application\/x-www-form-urlencoded\b/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ENV{'CONTENT_TYPE'} and $ENV{'CONTENT_TYPE'} =~ m[^application/x-www-form-urlencoded\b]i) { } 2 7 9
elsif ($ENV{'CONTENT_TYPE'} and $ENV{'CONTENT_TYPE'} =~ m[^multipart/form-data\b]i) { } 6 2 8
Condition100%3/3
$ENV{"CONTENT_TYPE"} and $ENV{"CONTENT_TYPE"} =~ m[^application/x-www-form-urlencoded\b]i
!ll&&!rl&&r
162
MC/DC100%2/2
$ENV{"CONTENT_TYPE"} and $ENV{"CONTENT_TYPE"} =~ m[^application/x-www-form-urlencoded\b]i
$ENV{"CONTENT_TYPE"} $ENV{"CONTENT_TYPE"} =~ m[^application/x-www-form-urlencoded\b]i
Truth table100%3/3
A and B
A= $ENV{"CONTENT_TYPE"}B= $ENV{"CONTENT_TYPE"} =~ m[^application/x-www-form-urlencoded\b]i
ABresult
0X0
100
111
292 2 foreach my $pair (split /&/, $self->body) {
293 8 my ($name, $value) = split /=/, $pair, 2;
294 8 ▶ $value = '' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 8 8
295 8 do { tr/+/ /; s/%([0-9a-fA-F]{2})/chr hex $1/ge; utf8::decode $_ } for $name, $value;
296 8 ▶ push @names, $name unless exists $keyed{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $keyed{$name} 6 2 8
297 8 push @ordered, [$name, $value];
298 8 push @{$keyed{$name}}, $value;
299 }
300 ▶ } elsif ($ENV{CONTENT_TYPE} and $ENV{CONTENT_TYPE} =~ m/^multipart\/form-data\b/i) {
Condition66%2/3
$ENV{"CONTENT_TYPE"} and $ENV{"CONTENT_TYPE"} =~ m[^multipart/form-data\b]i
!ll&&!rl&&r
106
MC/DC50%1/2
$ENV{"CONTENT_TYPE"} and $ENV{"CONTENT_TYPE"} =~ m[^multipart/form-data\b]i
$ENV{"CONTENT_TYPE"} $ENV{"CONTENT_TYPE"} =~ m[^multipart/form-data\b]i
Truth table66%2/3
A and B
A= $ENV{"CONTENT_TYPE"}B= $ENV{"CONTENT_TYPE"} =~ m[^multipart/form-data\b]i
ABresult
0X0
100
111
301 6 my $default_charset = $self->{multipart_form_charset};
302 6 ▶ $default_charset = 'UTF-8' unless defined $default_charset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $default_charset 1 5 6
303 6 foreach my $part (@{$self->_body_multipart}) {
304 18 ▶ next if defined $part->{filename};
Branch: 1 branch
BranchTrueFalseTotal
if defined $part->{"filename"} 8 10 18
305 10 my ($name, $headers, $content, $file) = @$part{'name','headers','content','file'};
306 10 ▶ if (length $default_charset) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $default_charset) 10 0 10
307 10 require Encode;
308 10 $name = Encode::decode($default_charset, "$name");
309 }
310 10 my $value = '';
311 10 ▶ if (defined $content) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $content) { } 9 1 10
elsif (defined $file) { } 1 0 1
312 9 $value = $content;
313 } elsif (defined $file) {
314 1 binmode $file;
315 1 seek $file, 0, 0;
316 1 $value = do { local $/; readline $file };
317 1 seek $file, 0, 0;
318 }
319 10 my $value_charset;
320 10 ▶ if (defined $headers->{'content-type'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $headers->{"content-type"}) 5 5 10
321 5 ▶ if (my ($charset_quoted, $charset_unquoted) = $headers->{'content-type'} =~ m/;\s*charset=(?:"((?:\\[\\"]|[^"])+)"|([^";]+))/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $headers->{"content-type"} =~ /;\s*charset=(?:"((?:\\[\\"]|[^"])+)"|([^";]+))/i) 5 0 5
322 5 ▶ $charset_quoted =~ s/\\([\\"])/$1/g if defined $charset_quoted;
Branch: 1 branch
BranchTrueFalseTotal
if defined $charset_quoted 0 5 5
323 5 ▶ $value_charset = defined $charset_quoted ? $charset_quoted : $charset_unquoted;
Branch: 1 branch
BranchTrueFalseTotal
defined $charset_quoted ? : 0 5 5
324 }
325 }
326 10 ▶ if (defined $value_charset or !defined $headers->{'content-type'} or $headers->{'content-type'} =~ m/^text\/plain\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value_charset or not defined $headers->{"content-type"} or $headers->{"content-type"} =~ m[^text/plain\b]i) 10 0 10
Condition50%3/6
defined $value_charset or not defined $headers->{"content-type"} or $headers->{"content-type"} =~ m[^text/plain\b]i
l!l&&r!l&&!r
1000
defined $value_charset or not defined $headers->{"content-type"}
l!l&&r!l&&!r
550
MC/DC0%0/3
defined $value_charset or not defined $headers->{"content-type"} or $headers->{"content-type"} =~ m[^text/plain\b]i
defined $value_charset not defined $headers->{"content-type"} $headers->{"content-type"} =~ m[^text/plain\b]i
Truth table50%2/4
A or not(B) or C
A= defined $value_charsetB= not defined $headers->{"content-type"}C= $headers->{"content-type"} =~ m[^text/plain\b]i
ABCresult
1XX1
01X1
0011
0000
327 10 require Encode;
328 10 ▶ if (defined $value_charset) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $value_charset) { } 5 5 10
elsif (length $default_charset) { } 5 0 5
329 5 $value = Encode::decode($value_charset, "$value");
330 } elsif (length $default_charset) {
331 5 $value = Encode::decode($default_charset, "$value");
332 }
333 }
334 10 ▶ push @names, $name unless exists $keyed{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $keyed{$name} 8 2 10
335 10 push @ordered, [$name, $value];
336 10 push @{$keyed{$name}}, $value;
337 }
338 }
339 }
340 37 return $self->{body_params};
341 }
342  
343 9 ▶sub body_parts {
Subroutine body_parts: called CC=3 SCAR=11.0
Pod documented
344 9 my ($self) = @_;
345 9 ▶ return [] unless $ENV{CONTENT_TYPE} and $ENV{CONTENT_TYPE} =~ m/^multipart\/form-data\b/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"CONTENT_TYPE"} and $ENV{"CONTENT_TYPE"} =~ m[^multipart/form-data\b]i 1 8 9
Condition66%2/3
$ENV{"CONTENT_TYPE"} and $ENV{"CONTENT_TYPE"} =~ m[^multipart/form-data\b]i
!ll&&!rl&&r
108
MC/DC50%1/2
$ENV{"CONTENT_TYPE"} and $ENV{"CONTENT_TYPE"} =~ m[^multipart/form-data\b]i
$ENV{"CONTENT_TYPE"} $ENV{"CONTENT_TYPE"} =~ m[^multipart/form-data\b]i
Truth table66%2/3
A and B
A= $ENV{"CONTENT_TYPE"}B= $ENV{"CONTENT_TYPE"} =~ m[^multipart/form-data\b]i
ABresult
0X0
100
111
346 8 return [map { +{%$_} } @{$self->_body_multipart}];
347 }
348  
349 6 ▶sub uploads { [map { [@$_] } @{$_[0]->_body_uploads->{ordered}}] }
Subroutine uploads: called CC=1 SCAR=0.0
Pod documented
350 6 ▶sub upload_names { [@{$_[0]->_body_uploads->{names}}] }
Subroutine upload_names: called CC=1 SCAR=0.0
Pod documented
351 7 ▶sub upload { my $u = $_[0]->_body_uploads->{keyed}; exists $u->{$_[1]} ? $u->{$_[1]}[-1] : undef }
Subroutine upload: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
exists $$u{$_[1]} ? : 6 1 7
352 6 ▶sub upload_array { my $u = $_[0]->_body_uploads->{keyed}; exists $u->{$_[1]} ? [@{$u->{$_[1]}}] : [] }
Subroutine upload_array: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
exists $$u{$_[1]} ? : 5 1 6
353  
354 25 ▶sub _body_uploads {
Subroutine _body_uploads: called CC=11 SCAR=24.1
355 25 my ($self) = @_;
356 25 ▶ unless (exists $self->{body_uploads}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"body_uploads"}) 6 19 25
357 6 $self->{body_uploads} = {names => \my @names, ordered => \my @ordered, keyed => \my %keyed};
358 6 ▶ if ($ENV{CONTENT_TYPE} and $ENV{CONTENT_TYPE} =~ m/^multipart\/form-data\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"CONTENT_TYPE"} and $ENV{"CONTENT_TYPE"} =~ m[^multipart/form-data\b]i) 6 0 6
Condition33%1/3
$ENV{"CONTENT_TYPE"} and $ENV{"CONTENT_TYPE"} =~ m[^multipart/form-data\b]i
!ll&&!rl&&r
006
MC/DC0%0/2
$ENV{"CONTENT_TYPE"} and $ENV{"CONTENT_TYPE"} =~ m[^multipart/form-data\b]i
$ENV{"CONTENT_TYPE"} $ENV{"CONTENT_TYPE"} =~ m[^multipart/form-data\b]i
Truth table33%1/3
A and B
A= $ENV{"CONTENT_TYPE"}B= $ENV{"CONTENT_TYPE"} =~ m[^multipart/form-data\b]i
ABresult
0X0
100
111
359 6 my $default_charset = $self->{multipart_form_charset};
360 6 ▶ $default_charset = 'UTF-8' unless defined $default_charset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $default_charset 1 5 6
361 6 foreach my $part (@{$self->_body_multipart}) {
362 18 ▶ next unless defined $part->{filename};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $part->{"filename"} 10 8 18
363 8 my ($name, $filename, $size, $headers, $file, $content) = @$part{'name','filename','size','headers','file','content'};
364 8 ▶ if (length $default_charset) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $default_charset) 8 0 8
365 8 require Encode;
366 8 $name = Encode::decode($default_charset, "$name");
367 8 $filename = Encode::decode($default_charset, "$filename");
368 }
369 my $upload = {
370 filename => $filename,
371 size => $size,
372 8 content_type => $headers->{'content-type'},
373 };
374 8 ▶ $upload->{file} = $file if defined $file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $file 5 3 8
375 8 ▶ $upload->{content} = $content if defined $content;
Branch: 1 branch
BranchTrueFalseTotal
if defined $content 1 7 8
376 8 ▶ push @names, $name unless exists $keyed{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $keyed{$name} 7 1 8
377 8 push @ordered, [$name, $upload];
378 8 push @{$keyed{$name}}, $upload;
379 }
380 }
381 }
382 25 return $self->{body_uploads};
383 }
384  
385 12 ▶sub _body_length {
Subroutine _body_length: called CC=7 SCAR=19.6
386 12 my ($self) = @_;
387 12 my $limit = $self->{request_body_limit};
388 12 ▶ $limit = $ENV{CGI_TINY_REQUEST_BODY_LIMIT} unless defined $limit;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $limit 11 1 12
389 12 ▶ $limit = DEFAULT_REQUEST_BODY_LIMIT unless defined $limit;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $limit 11 1 12
390 12 ▶ my $length = $ENV{CONTENT_LENGTH} || 0;
Condition50%1/2
$ENV{'CONTENT_LENGTH'} || 0
l!l
120
MC/DC0%0/1
$ENV{'CONTENT_LENGTH'} || 0
$ENV{'CONTENT_LENGTH'}
Truth table50%1/2
A
A= $ENV{'CONTENT_LENGTH'}
Aresult
11
00
391 12 ▶ if ($limit and $length > $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit and $length > $limit) 1 11 12
Condition66%2/3
$limit and $length > $limit
!ll&&!rl&&r
0111
MC/DC50%1/2
$limit and $length > $limit
$limit $length > $limit
Truth table66%2/3
A and B
A= $limitB= $length > $limit
ABresult
0X0
100
111
392 1 ▶ $self->{response_status} = "413 $HTTP_STATUS{413}" unless $self->{headers_rendered};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"headers_rendered"} 1 0 1
393 1 die "Request body limit exceeded\n";
394 }
395 11 return 0 + $length;
396 }
397  
398 20 ▶sub _body_multipart {
Subroutine _body_multipart: called CC=10 SCAR=23.2
399 20 my ($self) = @_;
400 20 ▶ unless (exists $self->{body_parts}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"body_parts"}) 8 12 20
401 8 $self->{body_parts} = [];
402 8 require CGI::Tiny::Multipart;
403 8 my $boundary = CGI::Tiny::Multipart::extract_multipart_boundary($ENV{CONTENT_TYPE});
404 8 ▶ unless (defined $boundary) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $boundary) 1 7 8
405 1 ▶ $self->{response_status} = "400 $HTTP_STATUS{400}" unless $self->{headers_rendered};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"headers_rendered"} 1 0 1
406 1 die "Malformed multipart/form-data request\n";
407 }
408  
409 7 my ($input, $length);
410 7 ▶ if (exists $self->{body_content}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'body_content'}) { } 1 6 7
411 1 $length = length $self->{body_content};
412 1 $input = \$self->{body_content};
413 } else {
414 6 $length = $self->_body_length;
415 6 ▶ $input = defined $self->{input_handle} ? $self->{input_handle} : *STDIN;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'input_handle'} ? : 6 0 6
416 }
417  
418 my $parts = CGI::Tiny::Multipart::parse_multipart_form_data($input, $length, $boundary, {
419 buffer_size => $self->{request_body_buffer} || $ENV{CGI_TINY_REQUEST_BODY_BUFFER},
420 7 ▶ %{$self->{multipart_form_options} || {}},
Condition60%3/5
$self->{'request_body_buffer'} || $ENV{'CGI_TINY_REQUEST_BODY_BUFFER'}
l!l&&r!l&&!r
007
$self->{'multipart_form_options'} || {}
l!l
52
MC/DC0%0/2
$self->{'request_body_buffer'} || $ENV{'CGI_TINY_REQUEST_BODY_BUFFER'}
$self->{'request_body_buffer'} $ENV{'CGI_TINY_REQUEST_BODY_BUFFER'}
MC/DC100%1/1
$self->{'multipart_form_options'} || {}
$self->{'multipart_form_options'}
Truth table33%1/3
A || B
A= $self->{'request_body_buffer'}B= $ENV{'CGI_TINY_REQUEST_BODY_BUFFER'}
ABresult
1X1
011
000
Truth table100%2/2
A
A= $self->{'multipart_form_options'}
Aresult
11
00
421 });
422 7 ▶ unless (defined $parts) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $parts) 1 6 7
423 1 ▶ $self->{response_status} = "400 $HTTP_STATUS{400}" unless $self->{headers_rendered};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"headers_rendered"} 1 0 1
424 1 die "Malformed multipart/form-data request\n";
425 }
426  
427 6 $self->{body_parts} = $parts;
428 }
429 18 return $self->{body_parts};
430 }
431  
432 2 ▶sub set_nph {
Subroutine set_nph: called CC=3 SCAR=11.3
Pod documented
433 2 my ($self, $value) = @_;
434 2 ▶ if ($self->{headers_rendered}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'headers_rendered'}) { } 0 2 2
435 0 Carp::carp "Attempted to set NPH response mode but headers have already been rendered";
436 } else {
437 2 ▶ $self->{nph} = @_ < 2 ? 1 : $value;
Branch: 1 branch
BranchTrueFalseTotal
@_ < 2 ? : 1 1 2
438 }
439 2 return $self;
440 }
441  
442 0 ▶sub set_response_body_buffer { $_[0]{response_body_buffer} = $_[1]; $_[0] }
Subroutine set_response_body_buffer: not called CC=1 SCAR=6.9
Pod documented
443  
444 15 ▶sub set_response_status {
Subroutine set_response_status: called CC=6 SCAR=18.8
Pod documented
445 15 my ($self, $status) = @_;
446 15 ▶ if ($self->{headers_rendered}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'headers_rendered'}) { } 0 15 15
447 0 Carp::carp "Attempted to set HTTP response status but headers have already been rendered";
448 } else {
449 15 ▶ if (defined $status and $status =~ m/\A[0-9]+ [^\r\n]*\z/) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $status and $status =~ /\A[0-9]+ [^\r\n]*\z/) { } 1 14 15
elsif (defined $status) { } 14 0 14
Condition66%2/3
defined $status and $status =~ /\A[0-9]+ [^\r\n]*\z/
!ll&&!rl&&r
0141
MC/DC50%1/2
defined $status and $status =~ /\A[0-9]+ [^\r\n]*\z/
defined $status $status =~ /\A[0-9]+ [^\r\n]*\z/
Truth table66%2/3
A and B
A= defined $statusB= $status =~ /\A[0-9]+ [^\r\n]*\z/
ABresult
0X0
100
111
450 1 $self->{response_status} = $status;
451 } elsif (defined $status) {
452 14 ▶ Carp::croak "Attempted to set unknown HTTP response status $status" unless exists $HTTP_STATUS{$status};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $HTTP_STATUS{$status} 1 13 14
453 13 $self->{response_status} = "$status $HTTP_STATUS{$status}";
454 } else {
455 0 delete $self->{response_status};
456 }
457 }
458 14 return $self;
459 }
460  
461 {
462 7 my %DISPOSITIONS = (attachment => 1, inline => 1);
463 4 ▶ sub set_response_disposition {
Subroutine set_response_disposition: called CC=3 SCAR=11.6
Pod documented
464 4 my ($self, $disposition, $filename) = @_;
465 4 ▶ if ($self->{headers_rendered}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'headers_rendered'}) { } 0 4 4
466 0 Carp::carp "Attempted to set HTTP response content disposition but headers have already been rendered";
467 } else {
468 4 ▶ Carp::croak "Attempted to set unknown Content-Disposition value '$disposition'" unless exists $DISPOSITIONS{lc $disposition};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $DISPOSITIONS{lc $disposition} 0 4 4
469 4 $self->{response_disposition} = $disposition;
470 # filename will be quoted/escaped later
471 4 $self->{response_filename} = $filename;
472 }
473 4 return $self;
474 }
475 }
476  
477 7 ▶sub set_response_type {
Subroutine set_response_type: called CC=4 SCAR=14.8
Pod documented
478 36 my ($self, $content_type) = @_;
479 36 ▶ if ($self->{headers_rendered}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'headers_rendered'}) { } 0 36 36
480 0 Carp::carp "Attempted to set HTTP response content type but headers have already been rendered";
481 } else {
482 36 ▶ Carp::croak "Newline characters not allowed in HTTP response content type" if defined $content_type and $content_type =~ tr/\r\n//;
Branch: 1 branch
BranchTrueFalseTotal
if defined $content_type and $content_type =~ tr/\n\r// 0 36 36
Condition66%2/3
defined $content_type and $content_type =~ tr/\n\r//
!ll&&!rl&&r
1350
MC/DC0%0/2
defined $content_type and $content_type =~ tr/\n\r//
defined $content_type $content_type =~ tr/\n\r//
Truth table66%2/3
A and B
A= defined $content_typeB= $content_type =~ tr/\n\r//
ABresult
0X0
100
111
483 36 $self->{response_type} = $content_type;
484 }
485 36 return $self;
486 }
487  
488 2 ▶sub set_response_charset {
Subroutine set_response_charset: called CC=3 SCAR=12.0
Pod documented
489 2 my ($self, $charset) = @_;
490 2 ▶ Carp::croak "Invalid characters in HTTP response charset" if defined $charset and $charset =~ m/[^a-zA-Z0-9!#\$%&'*+\-.^_`|~]/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $charset and $charset =~ /[^a-zA-Z0-9!#\$%&'*+\-.^_`|~]/ 0 2 2
Condition33%1/3
defined $charset and $charset =~ /[^a-zA-Z0-9!#\$%&'*+\-.^_`|~]/
!ll&&!rl&&r
020
MC/DC0%0/2
defined $charset and $charset =~ /[^a-zA-Z0-9!#\$%&'*+\-.^_`|~]/
defined $charset $charset =~ /[^a-zA-Z0-9!#\$%&'*+\-.^_`|~]/
Truth table33%1/3
A and B
A= defined $charsetB= $charset =~ /[^a-zA-Z0-9!#\$%&'*+\-.^_`|~]/
ABresult
0X0
100
111
491 2 $self->{response_charset} = $charset;
492 2 return $self;
493 }
494  
495 4 ▶sub add_response_header {
Subroutine add_response_header: called CC=3 SCAR=11.6
Pod documented
496 4 my ($self, $name, $value) = @_;
497 4 ▶ if ($self->{headers_rendered}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'headers_rendered'}) { } 0 4 4
498 0 Carp::carp "Attempted to add HTTP response header '$name' but headers have already been rendered";
499 } else {
500 4 ▶ Carp::croak "Newline characters not allowed in HTTP response header '$name'" if $value =~ tr/\r\n//;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ tr/\n\r// 0 4 4
501 4 push @{$self->{response_headers}}, [$name, $value];
502 }
503 4 return $self;
504 }
505  
506 {
507 7 my %COOKIE_ATTR_VALUE = (expires => 1, domain => 1, path => 1, secure => 0, httponly => 0, samesite => 1, 'max-age' => 1);
508 3 ▶ sub add_response_cookie {
Subroutine add_response_cookie: called CC=8 SCAR=21.4
Pod documented
509 3 my ($self, $name, $value, @attrs) = @_;
510 3 ▶ if ($self->{headers_rendered}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'headers_rendered'}) { } 0 3 3
511 0 Carp::carp "Attempted to add HTTP response cookie '$name' but headers have already been rendered";
512 } else {
513 3 my $cookie_str = "$name=$value";
514 3 my $i = 0;
515 3 ▶ while ($i <= $#attrs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#attrs) 10 3 13
516 10 my ($key, $val) = @attrs[$i, $i+1];
517 10 my $has_value = $COOKIE_ATTR_VALUE{lc $key};
518 10 ▶ if (!defined $has_value) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $has_value) { } 0 10 10
elsif ($has_value) { } 6 4 10
519 0 Carp::croak "Attempted to set unknown cookie attribute '$key' for HTTP response cookie '$name'";
520 } elsif ($has_value) {
521 6 ▶ $cookie_str .= "; $key=$val" if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 6 0 6
522 } else {
523 4 ▶ $cookie_str .= "; $key" if $val;
Branch: 1 branch
BranchTrueFalseTotal
if $val 2 2 4
524 }
525 } continue {
526 10 $i += 2;
527 }
528 3 ▶ Carp::croak "Newline characters not allowed in HTTP response cookie '$name'" if $cookie_str =~ tr/\r\n//;
Branch: 1 branch
BranchTrueFalseTotal
if $cookie_str =~ tr/\n\r// 0 3 3
529 3 push @{$self->{response_headers}}, ['Set-Cookie', $cookie_str];
530 }
531 3 return $self;
532 }
533 }
534  
535 1 ▶sub reset_response_headers { delete $_[0]{response_headers}; $_[0] }
Subroutine reset_response_headers: called CC=1 SCAR=0.0
Pod documented
536  
537 15 ▶sub response_status_code {
Subroutine response_status_code: called CC=3 SCAR=13.3
Pod documented
538 15 my ($self) = @_;
539 15 ▶ if (defined $self->{response_status} and $self->{response_status} =~ m/\A([0-9]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"response_status"} and $self->{"response_status"} =~ /\A([0-9]+)/) 15 0 15
Condition33%1/3
defined $self->{"response_status"} and $self->{"response_status"} =~ /\A([0-9]+)/
!ll&&!rl&&r
0015
MC/DC0%0/2
defined $self->{"response_status"} and $self->{"response_status"} =~ /\A([0-9]+)/
defined $self->{"response_status"} $self->{"response_status"} =~ /\A([0-9]+)/
Truth table33%1/3
A and B
A= defined $self->{"response_status"}B= $self->{"response_status"} =~ /\A([0-9]+)/
ABresult
0X0
100
111
540 15 return 0+$1;
541 }
542 0 return 200;
543 }
544  
545 {
546 7 my %RENDER_TYPES = (text => 1, html => 1, xml => 1, json => 1, data => 1, file => 1, handle => 1, redirect => 1);
547  
548 65 ▶ sub render {
Subroutine render: called CC=28 SCAR=33.7
Pod documented
549 65 my ($self, $type, $data) = @_;
550 65 ▶ Carp::croak "Cannot render additional data with ->render; use ->render_chunk" if $self->{headers_rendered};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"headers_rendered"} 0 65 65
551 65 ▶ $type = '' unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 22 43 65
552 65 ▶ Carp::croak "Don't know how to render '$type'" if length $type and !exists $RENDER_TYPES{$type};
Branch: 1 branch
BranchTrueFalseTotal
if length $type and not exists $RENDER_TYPES{$type} 0 65 65
Condition66%2/3
length $type and not exists $RENDER_TYPES{$type}
!ll&&!rl&&r
22430
MC/DC0%0/2
length $type and not exists $RENDER_TYPES{$type}
length $type not exists $RENDER_TYPES{$type}
Truth table66%2/3
A and not(B)
A= length $typeB= not exists $RENDER_TYPES{$type}
ABresult
0X0
100
111
553 65 ▶ Carp::croak "Cannot render from an open filehandle with ->render; use ->render_chunk" if $type eq 'handle';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "handle" 0 65 65
554  
555 65 my ($response_body, $response_length, $redirect_url);
556 65 ▶ if ($type eq 'redirect') {
Branch: 6 branches
BranchTrueFalseTotal
if ($type eq 'redirect') { } 3 62 65
elsif (uc($ENV{'REQUEST_METHOD'} || '') eq 'HEAD') { } 1 61 62
elsif ($type eq 'text' or $type eq 'html' or $type eq 'xml') { } 5 56 61
elsif ($type eq 'json') { } 1 55 56
elsif ($type eq 'data') { } 32 23 55
elsif ($type eq 'file') { } 1 22 23
557 3 ▶ Carp::croak "Newline characters not allowed in HTTP redirect" if $data =~ tr/\r\n//;
Branch: 1 branch
BranchTrueFalseTotal
if $data =~ tr/\n\r// 0 3 3
558 3 $redirect_url = $data;
559 ▶ } elsif (uc($ENV{REQUEST_METHOD} || '') eq 'HEAD') {
Condition100%2/2
$ENV{'REQUEST_METHOD'} || ''
l!l
584
MC/DC100%1/1
$ENV{'REQUEST_METHOD'} || ''
$ENV{'REQUEST_METHOD'}
Truth table100%2/2
A
A= $ENV{'REQUEST_METHOD'}
Aresult
11
00
560 # no response content
561 ▶ } elsif ($type eq 'text' or $type eq 'html' or $type eq 'xml') {
Condition100%6/6
$type eq "text" or $type eq "html" or $type eq "xml"
l!l&&r!l&&!r
4156
$type eq "text" or $type eq "html"
l!l&&r!l&&!r
3157
MC/DC100%3/3
$type eq "text" or $type eq "html" or $type eq "xml"
$type eq "text" $type eq "html" $type eq "xml"
Truth table100%4/4
A or B or C
A= $type eq "text"B= $type eq "html"C= $type eq "xml"
ABCresult
1XX1
01X1
0011
0000
562 5 my $charset = $self->{response_charset};
563 5 ▶ $charset = 'UTF-8' unless defined $charset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $charset 4 1 5
564 5 ▶ if (uc $charset eq 'UTF-8' and do { local $@; eval { require Unicode::UTF8; 1 } }) {
Branch: 1 branch
BranchTrueFalseTotal
if (uc $charset eq 'UTF-8' and do { local $@; eval { do { require Unicode::UTF8; 1 } } }) { } 4 1 5
Condition66%2/3
uc $charset eq "UTF-8" and do { local $@; eval { do { require Unicode::UTF8; 1 } } }
!ll&&!rl&&r
104
MC/DC50%1/2
uc $charset eq "UTF-8" and do { local $@; eval { do { require Unicode::UTF8; 1 } } }
uc $charset eq "UTF-8" do { local $@; eval { do { require Unicode::UTF8; 1 } } }
Truth table66%2/3
A and B
A= uc $charset eq "UTF-8"B= do { local $@; eval { do { require Unicode::UTF8; 1 } } }
ABresult
0X0
100
111
565 4 $response_body = Unicode::UTF8::encode_utf8($data);
566 } else {
567 1 require Encode;
568 1 $response_body = Encode::encode($charset, "$data");
569 }
570 5 $response_length = length $response_body;
571 } elsif ($type eq 'json') {
572 1 $response_body = $self->_json->encode($data);
573 1 $response_length = length $response_body;
574 } elsif ($type eq 'data') {
575 32 $response_body = $data;
576 32 $response_length = length $response_body;
577 } elsif ($type eq 'file') {
578 1 $response_length = -s $data;
579 1 ▶ Carp::croak "Failed to retrieve size of file '$data': $!" unless defined $response_length;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $response_length 0 1 1
580 }
581 65 ▶ $response_length = 0 unless defined $response_length;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $response_length 26 39 65
582  
583 65 my $headers_str = $self->_response_headers($type, $response_length, $redirect_url);
584 65 ▶ my $out_fh = defined $self->{output_handle} ? $self->{output_handle} : *STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'output_handle'} ? : 63 2 65
585 65 binmode $out_fh;
586 65 $out_fh->printflush($headers_str);
587 65 $self->{headers_rendered} = 1;
588 65 $self->{response_fixed_length} = 1;
589 65 ▶ return $self unless $response_length;
Branch: 1 branch
BranchTrueFalseTotal
unless $response_length 27 38 65
590  
591 38 ▶ if ($type eq 'file') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'file') { } 1 37 38
592 1 ▶ open my $in_fh, '<', $data or Carp::croak "Failed to open file '$data' for rendering: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $in_fh, "<", $data or Carp::croak("Failed to open file '${data}' for rendering: $!") 1 0 1
593 1 binmode $in_fh;
594 1 ▶ my $buffer_size = 0 + ($self->{response_body_buffer} || $ENV{CGI_TINY_RESPONSE_BODY_BUFFER} || DEFAULT_RESPONSE_BODY_BUFFER);
Condition40%2/5
$self->{'response_body_buffer'} || $ENV{'CGI_TINY_RESPONSE_BODY_BUFFER'} || 131072
l!l
01
$self->{"response_body_buffer"} or $ENV{"CGI_TINY_RESPONSE_BODY_BUFFER"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->{'response_body_buffer'} || $ENV{'CGI_TINY_RESPONSE_BODY_BUFFER'} || 131072
$self->{"response_body_buffer"} $ENV{"CGI_TINY_RESPONSE_BODY_BUFFER"}
Truth table33%1/3
A or B
A= $self->{"response_body_buffer"}B= $ENV{"CGI_TINY_RESPONSE_BODY_BUFFER"}
ABresult
1X1
011
000
595 1 ▶ while (read $in_fh, my $buffer, $buffer_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $in_fh, my $buffer, $buffer_size) 1 1 2
596 1 $out_fh->print($buffer);
597 }
598 1 $out_fh->flush;
599 } else {
600 37 $out_fh->printflush($response_body);
601 }
602 38 return $self;
603 }
604  
605 17 ▶ sub render_chunk {
Subroutine render_chunk: called CC=25 SCAR=32.7
Pod documented
606 17 my ($self, $type, $data) = @_;
607 17 ▶ Carp::croak "Cannot render additional data after ->render" if $self->{response_fixed_length};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"response_fixed_length"} 0 17 17
608 17 ▶ $type = '' unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 5 12 17
609 17 ▶ Carp::croak "Don't know how to render '$type'" if length $type and !exists $RENDER_TYPES{$type};
Branch: 1 branch
BranchTrueFalseTotal
if length $type and not exists $RENDER_TYPES{$type} 0 17 17
Condition66%2/3
length $type and not exists $RENDER_TYPES{$type}
!ll&&!rl&&r
5120
MC/DC0%0/2
length $type and not exists $RENDER_TYPES{$type}
length $type not exists $RENDER_TYPES{$type}
Truth table66%2/3
A and not(B)
A= length $typeB= not exists $RENDER_TYPES{$type}
ABresult
0X0
100
111
610 17 ▶ Carp::croak "Cannot render a chunked redirect" if $type eq 'redirect';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "redirect" 0 17 17
611  
612 17 ▶ my $out_fh = defined $self->{output_handle} ? $self->{output_handle} : *STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'output_handle'} ? : 17 0 17
613 17 ▶ unless ($self->{headers_rendered}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"headers_rendered"}) 12 5 17
614 12 my $headers_str = $self->_response_headers($type);
615 12 binmode $out_fh;
616 12 $out_fh->printflush($headers_str);
617 12 $self->{headers_rendered} = 1;
618 }
619  
620 17 ▶ if (uc($ENV{REQUEST_METHOD} || '') eq 'HEAD') {
Branch: 5 branches
BranchTrueFalseTotal
if (uc($ENV{'REQUEST_METHOD'} || '') eq 'HEAD') { } 2 15 17
elsif ($type eq 'text' or $type eq 'html' or $type eq 'xml') { } 4 11 15
elsif ($type eq 'json') { } 2 9 11
elsif ($type eq 'data') { } 2 7 9
elsif ($type eq 'file' or $type eq 'handle') { } 2 5 7
Condition50%1/2
$ENV{'REQUEST_METHOD'} || ''
l!l
170
MC/DC0%0/1
$ENV{'REQUEST_METHOD'} || ''
$ENV{'REQUEST_METHOD'}
Truth table50%1/2
A
A= $ENV{'REQUEST_METHOD'}
Aresult
11
00
621 # no response content
622 ▶ } elsif ($type eq 'text' or $type eq 'html' or $type eq 'xml') {
Condition83%5/6
$type eq "text" or $type eq "html" or $type eq "xml"
l!l&&r!l&&!r
4011
$type eq "text" or $type eq "html"
l!l&&r!l&&!r
2211
MC/DC66%2/3
$type eq "text" or $type eq "html" or $type eq "xml"
$type eq "text" $type eq "html" $type eq "xml"
Truth table75%3/4
A or B or C
A= $type eq "text"B= $type eq "html"C= $type eq "xml"
ABCresult
1XX1
01X1
0011
0000
623 4 my $charset = $self->{response_charset};
624 4 ▶ $charset = 'UTF-8' unless defined $charset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $charset 3 1 4
625 4 my $response_body;
626 4 ▶ if (uc $charset eq 'UTF-8' and do { local $@; eval { require Unicode::UTF8; 1 } }) {
Branch: 1 branch
BranchTrueFalseTotal
if (uc $charset eq 'UTF-8' and do { local $@; eval { do { require Unicode::UTF8; 1 } } }) { } 3 1 4
Condition66%2/3
uc $charset eq "UTF-8" and do { local $@; eval { do { require Unicode::UTF8; 1 } } }
!ll&&!rl&&r
103
MC/DC50%1/2
uc $charset eq "UTF-8" and do { local $@; eval { do { require Unicode::UTF8; 1 } } }
uc $charset eq "UTF-8" do { local $@; eval { do { require Unicode::UTF8; 1 } } }
Truth table66%2/3
A and B
A= uc $charset eq "UTF-8"B= do { local $@; eval { do { require Unicode::UTF8; 1 } } }
ABresult
0X0
100
111
627 3 $response_body = Unicode::UTF8::encode_utf8($data);
628 } else {
629 1 require Encode;
630 1 $response_body = Encode::encode($charset, "$data");
631 }
632 4 $out_fh->printflush($response_body);
633 } elsif ($type eq 'json') {
634 2 my $response_body = $self->_json->encode($data);
635 2 $out_fh->printflush($response_body);
636 } elsif ($type eq 'data') {
637 2 $out_fh->printflush($data);
638 ▶ } elsif ($type eq 'file' or $type eq 'handle') {
Condition100%3/3
$type eq "file" or $type eq "handle"
l!l&&r!l&&!r
115
MC/DC100%2/2
$type eq "file" or $type eq "handle"
$type eq "file" $type eq "handle"
Truth table100%3/3
A or B
A= $type eq "file"B= $type eq "handle"
ABresult
1X1
011
000
639 2 my $in_fh;
640 2 ▶ if ($type eq 'file') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'file') { } 1 1 2
641 1 ▶ open $in_fh, '<', $data or Carp::croak "Failed to open file '$data' for rendering: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $in_fh, "<", $data or Carp::croak("Failed to open file '${data}' for rendering: $!") 1 0 1
642 } else {
643 1 $in_fh = $data;
644 }
645 2 binmode $in_fh;
646 2 ▶ my $buffer_size = 0 + ($self->{response_body_buffer} || $ENV{CGI_TINY_RESPONSE_BODY_BUFFER} || DEFAULT_RESPONSE_BODY_BUFFER);
Condition40%2/5
$self->{'response_body_buffer'} || $ENV{'CGI_TINY_RESPONSE_BODY_BUFFER'} || 131072
l!l
02
$self->{"response_body_buffer"} or $ENV{"CGI_TINY_RESPONSE_BODY_BUFFER"}
l!l&&r!l&&!r
002
MC/DC0%0/2
$self->{'response_body_buffer'} || $ENV{'CGI_TINY_RESPONSE_BODY_BUFFER'} || 131072
$self->{"response_body_buffer"} $ENV{"CGI_TINY_RESPONSE_BODY_BUFFER"}
Truth table33%1/3
A or B
A= $self->{"response_body_buffer"}B= $ENV{"CGI_TINY_RESPONSE_BODY_BUFFER"}
ABresult
1X1
011
000
647 2 ▶ while (read $in_fh, my $buffer, $buffer_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $in_fh, my $buffer, $buffer_size) 2 2 4
648 2 $out_fh->print($buffer);
649 }
650 2 $out_fh->flush;
651 }
652 17 return $self;
653 }
654 }
655  
656 7 ▶sub _response_headers {
Subroutine _response_headers: called CC=38 SCAR=36.9
657 77 my ($self, $type, $content_length, $location) = @_;
658 77 my $headers_str = '';
659 77 ▶ return $headers_str if defined $self->{debug_method} and !$self->{debug_verbose};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"debug_method"} and not $self->{"debug_verbose"} 0 77 77
Condition33%1/3
defined $self->{"debug_method"} and not $self->{"debug_verbose"}
!ll&&!rl&&r
7700
MC/DC0%0/2
defined $self->{"debug_method"} and not $self->{"debug_verbose"}
defined $self->{"debug_method"} not $self->{"debug_verbose"}
Truth table33%1/3
A and not(B)
A= defined $self->{"debug_method"}B= not $self->{"debug_verbose"}
ABresult
0X0
100
111
660 77 my %headers_set;
661 77 ▶ foreach my $header (@{$self->{response_headers} || []}) {
Condition100%2/2
$self->{'response_headers'} || []
l!l
176
MC/DC100%1/1
$self->{'response_headers'} || []
$self->{'response_headers'}
Truth table100%2/2
A
A= $self->{'response_headers'}
Aresult
11
00
662 4 my ($name, $value) = @$header;
663 4 $headers_str .= "$name: $value\r\n";
664 4 $headers_set{lc $name} = 1;
665 }
666 77 ▶ if (!$headers_set{'content-length'} and defined $content_length) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $headers_set{"content-length"} and defined $content_length) 65 12 77
Condition66%2/3
not $headers_set{"content-length"} and defined $content_length
!ll&&!rl&&r
01265
MC/DC50%1/2
not $headers_set{"content-length"} and defined $content_length
not $headers_set{"content-length"} defined $content_length
Truth table66%2/3
not(A) and B
A= not $headers_set{"content-length"}B= defined $content_length
ABresult
0X0
100
111
667 65 $headers_str = "Content-Length: $content_length\r\n$headers_str";
668 }
669 77 ▶ if (!$headers_set{'content-disposition'} and (defined $self->{response_disposition} or defined $self->{response_filename})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $headers_set{"content-disposition"} and defined $self->{"response_disposition"} || defined $self->{"response_filename"}) 3 74 77
Condition66%4/6
not $headers_set{"content-disposition"} and defined $self->{"response_disposition"} || defined $self->{"response_filename"}
!ll&&!rl&&r
0743
defined $self->{"response_disposition"} or defined $self->{"response_filename"}
l!l&&r!l&&!r
3074
MC/DC33%1/3
not $headers_set{"content-disposition"} and defined $self->{"response_disposition"} || defined $self->{"response_filename"}
not $headers_set{"content-disposition"} defined $self->{"response_disposition"} defined $self->{"response_filename"}
Truth table50%2/4
not(A) and B or C
A= not $headers_set{"content-disposition"}B= defined $self->{"response_disposition"}C= defined $self->{"response_filename"}
ABCresult
0XX0
1000
11X1
1011
670 3 ▶ my $value = defined $self->{response_disposition} ? $self->{response_disposition} : 'inline';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'response_disposition'} ? : 3 0 3
671 3 ▶ if (defined(my $filename = $self->{response_filename})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $filename = $self->{"response_filename"})) 2 1 3
672 2 require Encode;
673 2 my $quoted_filename = Encode::encode('ISO-8859-1', "$filename");
674 2 $quoted_filename =~ tr/\r\n/ /;
675 2 $quoted_filename =~ s/([\\"])/\\$1/g;
676 2 $value .= "; filename=\"$quoted_filename\"";
677 2 my $ext_filename = Encode::encode('UTF-8', "$filename");
678 2 $ext_filename =~ s/([^a-zA-Z0-9!#\$&+\-.^_`|~])/sprintf '%%%02X', ord $1/ge;
679 2 $value .= "; filename*=UTF-8''$ext_filename";
680 }
681 3 ▶ $headers_str = "Content-Disposition: $value\r\n$headers_str" unless lc $value eq 'inline';
Branch: 1 branch
BranchTrueFalseTotal
unless lc $value eq "inline" 2 1 3
682 }
683 77 ▶ if (!$headers_set{location} and $type eq 'redirect') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $headers_set{"location"} and $type eq "redirect") 3 74 77
Condition66%2/3
not $headers_set{"location"} and $type eq "redirect"
!ll&&!rl&&r
0743
MC/DC50%1/2
not $headers_set{"location"} and $type eq "redirect"
not $headers_set{"location"} $type eq "redirect"
Truth table66%2/3
not(A) and B
A= not $headers_set{"location"}B= $type eq "redirect"
ABresult
0X0
100
111
684 3 $headers_str = "Location: $location\r\n$headers_str";
685 }
686 77 ▶ if (!$headers_set{'content-type'} and $type ne 'redirect') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $headers_set{"content-type"} and $type ne "redirect") 74 3 77
Condition66%2/3
not $headers_set{"content-type"} and $type ne "redirect"
!ll&&!rl&&r
0374
MC/DC50%1/2
not $headers_set{"content-type"} and $type ne "redirect"
not $headers_set{"content-type"} $type ne "redirect"
Truth table66%2/3
not(A) and B
A= not $headers_set{"content-type"}B= $type ne "redirect"
ABresult
0X0
100
111
687 74 my $content_type = $self->{response_type};
688 74 my $charset = $self->{response_charset};
689 74 ▶ $charset = 'UTF-8' unless defined $charset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $charset 72 2 74
690 74 ▶ $content_type =
Branch: 5 branches
BranchTrueFalseTotal
unless defined $content_type or defined $content_length and $content_length == 0 18 56 74
$type eq 'text' ? : 3 15 18
$type eq 'html' ? : 2 13 15
$type eq 'xml' ? : 1 12 13
$type eq 'json' ? : 1 11 12
Condition100%6/6
defined $content_type or defined $content_length and $content_length == 0
l!l&&r!l&&!r
342218
defined $content_length and $content_length == 0
!ll&&!rl&&r
11722
MC/DC100%3/3
defined $content_type or defined $content_length and $content_length == 0
defined $content_type defined $content_length $content_length == 0
Truth table100%4/4
A or B and C
A= defined $content_typeB= defined $content_lengthC= $content_length == 0
ABCresult
1XX1
0111
00X0
0100
691 $type eq 'text' ? "text/plain;charset=$charset"
692 : $type eq 'html' ? "text/html;charset=$charset"
693 : $type eq 'xml' ? "application/xml;charset=$charset"
694 : $type eq 'json' ? 'application/json;charset=UTF-8'
695 : 'application/octet-stream'
696 unless defined $content_type or (defined $content_length and $content_length == 0);
697 74 ▶ $headers_str = "Content-Type: $content_type\r\n$headers_str" if defined $content_type;
Branch: 1 branch
BranchTrueFalseTotal
if defined $content_type 52 22 74
698 }
699 77 ▶ if (!$headers_set{date}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($headers_set{"date"}) 77 0 77
700 77 my $date_str = epoch_to_date(time);
701 77 $headers_str = "Date: $date_str\r\n$headers_str";
702 }
703 77 my $status = $self->{response_status};
704 77 ▶ $status = $self->{response_status} = "302 $HTTP_STATUS{302}" if $type eq 'redirect'
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "redirect" and not defined $status && $status =~ /^3[0-9]{2} / 2 75 77
Condition100%6/6
$type eq "redirect" and not defined $status && $status =~ /^3[0-9]{2} /
!ll&&!rl&&r
7412
defined $status && $status =~ /^3[0-9]{2} /
!ll&&!rl&&r
111
MC/DC100%3/3
$type eq "redirect" and not defined $status && $status =~ /^3[0-9]{2} /
$type eq "redirect" defined $status $status =~ /^3[0-9]{2} /
Truth table100%4/4
A and not(B && C)
A= $type eq "redirect"B= defined $statusC= $status =~ /^3[0-9]{2} /
ABCresult
0XX0
1110
10X1
1101
705 and !(defined $status and $status =~ m/^3[0-9]{2} /);
706 77 ▶ if ($self->{nph}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'nph'}) { } 2 75 77
elsif (not $headers_set{'status'} and defined $status) { } 37 38 75
707 2 ▶ $status = "200 $HTTP_STATUS{200}" unless defined $status;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $status 1 1 2
708 2 my $protocol = $ENV{SERVER_PROTOCOL};
709 2 ▶ $protocol = 'HTTP/1.0' unless defined $protocol and length $protocol;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $protocol and length $protocol 0 2 2
Condition33%1/3
defined $protocol and length $protocol
!ll&&!rl&&r
002
MC/DC0%0/2
defined $protocol and length $protocol
defined $protocol length $protocol
Truth table33%1/3
A and B
A= defined $protocolB= length $protocol
ABresult
0X0
100
111
710 2 $headers_str = "$protocol $status\r\n$headers_str";
711 2 my $server = $ENV{SERVER_SOFTWARE};
712 2 ▶ $headers_str .= "Server: $server\r\n" if defined $server and length $server;
Branch: 1 branch
BranchTrueFalseTotal
if defined $server and length $server 1 1 2
Condition66%2/3
defined $server and length $server
!ll&&!rl&&r
011
MC/DC50%1/2
defined $server and length $server
defined $server length $server
Truth table66%2/3
A and B
A= defined $serverB= length $server
ABresult
0X0
100
111
713 ▶ } elsif (!$headers_set{status} and defined $status) {
Condition66%2/3
not $headers_set{"status"} and defined $status
!ll&&!rl&&r
03837
MC/DC50%1/2
not $headers_set{"status"} and defined $status
not $headers_set{"status"} defined $status
Truth table66%2/3
not(A) and B
A= not $headers_set{"status"}B= defined $status
ABresult
0X0
100
111
714 37 $headers_str = "Status: $status\r\n$headers_str";
715 }
716 77 return "$headers_str\r\n";
717 }
718  
719 5 ▶sub _json {
Subroutine _json: called CC=3 SCAR=11.1
720 5 my ($self) = @_;
721 5 ▶ unless (exists $self->{json}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"json"}) 3 2 5
722 3 ▶ if (do { local $@; eval { require Cpanel::JSON::XS; Cpanel::JSON::XS->VERSION('4.09'); 1 } }) {
Branch: 1 branch
BranchTrueFalseTotal
if (do { local $@; eval { do { require Cpanel::JSON::XS; 'Cpanel::JSON::XS'->VERSION('4.09'); 1 } } }) { } 3 0 3
723 3 $self->{json} = Cpanel::JSON::XS->new->allow_dupkeys->stringify_infnan;
724 } else {
725 0 require JSON::PP;
726 0 $self->{json} = JSON::PP->new;
727 }
728 3 $self->{json}->utf8->canonical->allow_nonref->allow_unknown->allow_blessed->convert_blessed->escape_slash;
729 }
730 5 return $self->{json};
731 }
732  
733 {
734 7 my @DAYS_OF_WEEK = qw(Sun Mon Tue Wed Thu Fri Sat);
735 7 my @MONTH_NAMES = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
736 7 my %MONTH_NUMS;
737 7 @MONTH_NUMS{@MONTH_NAMES} = 0..11;
738  
739 79 ▶ sub epoch_to_date {
Subroutine epoch_to_date: called CC=1 SCAR=0.0
Pod documented
740 79 my ($sec,$min,$hour,$mday,$mon,$year,$wday) = gmtime $_[0];
741 79 return sprintf '%s, %02d %s %04d %02d:%02d:%02d GMT',
742 $DAYS_OF_WEEK[$wday], $mday, $MONTH_NAMES[$mon], $year + 1900, $hour, $min, $sec;
743 }
744  
745 19 ▶ sub date_to_epoch {
Subroutine date_to_epoch: called CC=6 SCAR=18.0
Pod documented
746 # RFC 1123 (Sun, 06 Nov 1994 08:49:37 GMT)
747 19 my ($mday,$mon,$year,$hour,$min,$sec) = $_[0] =~ m/^ (?:Sun|Mon|Tue|Wed|Thu|Fri|Sat),
748 [ ] ([0-9]{2}) [ ] (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) [ ] ([0-9]{4})
749 [ ] ([0-9]{2}) : ([0-9]{2}) : ([0-9]{2}) [ ] GMT $/x;
750  
751 # RFC 850 (Sunday, 06-Nov-94 08:49:37 GMT)
752 19 ▶ ($mday,$mon,$year,$hour,$min,$sec) = $_[0] =~ m/^ (?:Sun|Mon|Tues|Wednes|Thurs|Fri|Satur)day,
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mday 3 16 19
753 [ ] ([0-9]{2}) - (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) - ([0-9]{2})
754 [ ] ([0-9]{2}) : ([0-9]{2}) : ([0-9]{2}) [ ] GMT $/x unless defined $mday;
755  
756 # asctime (Sun Nov 6 08:49:37 1994)
757 19 ▶ ($mon,$mday,$hour,$min,$sec,$year) = $_[0] =~ m/^ (?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mday 2 17 19
758 [ ] (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) [ ]{1,2} ([0-9]{1,2})
759 [ ] ([0-9]{2}) : ([0-9]{2}) : ([0-9]{2}) [ ] ([0-9]{4}) $/x unless defined $mday;
760  
761 19 ▶ return undef unless defined $mday;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mday 1 18 19
762  
763 18 require Time::Local;
764 # 4 digit years interpreted literally, but may have leading zeroes
765 # 2 digit years interpreted with best effort heuristic
766 18 ▶ return scalar Time::Local::timegm($sec, $min, $hour, $mday, $MONTH_NUMS{$mon},
Branch: 1 branch
BranchTrueFalseTotal
length $year == 4 && $year < 1900 ? : 0 18 18
Condition66%2/3
length $year == 4 and $year < 1900
!ll&&!rl&&r
1170
MC/DC0%0/2
length $year == 4 and $year < 1900
length $year == 4 $year < 1900
Truth table66%2/3
A and B
A= length $year == 4B= $year < 1900
ABresult
0X0
100
111
767 (length($year) == 4 && $year < 1900) ? $year - 1900 : $year);
768 }
769 }
770  
771 {
772 7 my %ESCAPES = ('&' => '&amp;', '<' => '&lt;', '>' => '&gt;', '"' => '&quot;', '\'' => '&#39;');
773 3 ▶ sub escape_html { (my $escaped = $_[0]) =~ s/([&<>"'])/$ESCAPES{$1}/ge; $escaped }
Subroutine escape_html: called CC=1 SCAR=0.0
Pod documented
774 }
775  
776 7 1;
« Tiny/Multipart.pm ↑ Summary