blib/lib/HTTP/Message/PSGI.pm

filter: Statementstmt 76.7% 89 / 116 Branchbran 65.3% 34 / 52 Conditioncond 68.4% 13 / 19 MC/DCmcdc 54.5% 6 / 11 Subroutinesub 80.0% 16 / 20 Podpod 50.0% 2 / 4 totaltotal 72.0% 160 / 222 CC 36 SCAR 27.8
CC
36
Cov
73%
req_to_psgi
30.9
__ANON__
20.0
read
12.0
CRAP 16.2
« blib/lib/Plack/Middleware/Chunked.pm ↑ Summary blib/lib/Plack/Middleware/ContentMD5.pm »
1 package HTTP::Message::PSGI;
2 72 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 72 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 72 ▶use parent qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 72 our @EXPORT = qw( req_to_psgi res_from_psgi );
6  
7 72 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 72 ▶use HTTP::Status qw(status_message);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 72 ▶use URI::Escape ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 72 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 72 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 72 my $TRUE = (1 == 1);
14 72 my $FALSE = !$TRUE;
15  
16 348 ▶sub req_to_psgi {
Subroutine req_to_psgi: called CC=23 SCAR=34.3
Pod documented
17 348 my $req = shift;
18  
19 348 ▶ unless (Scalar::Util::blessed($req) && $req->isa('HTTP::Request')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($req) and $req->isa("HTTP::Request")) 0 348 348
Condition33%1/3
builtin::blessed($req) and $req->isa("HTTP::Request")
!ll&&!rl&&r
00348
MC/DC0%0/2
builtin::blessed($req) and $req->isa("HTTP::Request")
builtin::blessed($req) $req->isa("HTTP::Request")
Truth table33%1/3
A and B
A= builtin::blessed($req)B= $req->isa("HTTP::Request")
ABresult
0X0
100
111
20 0 Carp::croak("Request is not HTTP::Request: $req");
21 }
22  
23 # from HTTP::Request::AsCGI
24 348 my $host = $req->header('Host');
25 348 my $uri = $req->uri->clone;
26 348 ▶ $uri->scheme('http') unless $uri->scheme;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri->scheme 9 339 348
27 348 ▶ $uri->host('localhost') unless $uri->host;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri->host 9 339 348
28 348 ▶ $uri->port(80) unless $uri->port;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri->port 0 348 348
29 348 ▶ $uri->host_port($host) unless !$host || ( $host eq $uri->host_port );
Branch: 1 branch
BranchTrueFalseTotal
unless not $host or $host eq $uri->host_port 2 346 348
Condition66%2/3
not $host or $host eq $uri->host_port
l!l&&r!l&&!r
34602
MC/DC50%1/2
not $host or $host eq $uri->host_port
not $host $host eq $uri->host_port
Truth table66%2/3
not(A) or B
A= not $hostB= $host eq $uri->host_port
ABresult
1X1
011
000
30  
31 348 my $input;
32 348 my $content = $req->content;
33 348 ▶ if (ref $content eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content eq 'CODE') { } 0 348 348
34 0 ▶ if (defined $req->content_length) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $req->content_length) { } 0 0 0
35 0 $input = HTTP::Message::PSGI::ChunkedInput->new($content);
36 } else {
37 0 $req->header("Transfer-Encoding" => "chunked");
38 0 $input = HTTP::Message::PSGI::ChunkedInput->new($content, 1);
39 }
40 } else {
41 348 open $input, "<", \$content;
42 348 ▶ $req->content_length(length $content)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $req->content_length 331 17 348
43 unless defined $req->content_length;
44 }
45  
46 348 ▶ my $env = {
Branch: 1 branch
BranchTrueFalseTotal
$uri->scheme eq 'https' ? : 0 348 348
Condition87%7/8
$uri->path || '/'
l!l
3471
$uri->query || ''
l!l
25323
$req->protocol || 'HTTP/1.1'
l!l
0348
$uri->path_query || '/'
l!l
3471
MC/DC100%1/1
$uri->path || '/'
$uri->path
MC/DC100%1/1
$uri->query || ''
$uri->query
MC/DC0%0/1
$req->protocol || 'HTTP/1.1'
$req->protocol
MC/DC100%1/1
$uri->path_query || '/'
$uri->path_query
Truth table100%2/2
A
A= $uri->path
Aresult
11
00
Truth table100%2/2
A
A= $uri->query
Aresult
11
00
Truth table50%1/2
A
A= $req->protocol
Aresult
11
00
Truth table100%2/2
A
A= $uri->path_query
Aresult
11
00
47 PATH_INFO => URI::Escape::uri_unescape($uri->path || '/'),
48 QUERY_STRING => $uri->query || '',
49 SCRIPT_NAME => '',
50 SERVER_NAME => $uri->host,
51 SERVER_PORT => $uri->port,
52 SERVER_PROTOCOL => $req->protocol || 'HTTP/1.1',
53 REMOTE_ADDR => '127.0.0.1',
54 REMOTE_HOST => 'localhost',
55 REMOTE_PORT => int( rand(64000) + 1000 ), # not in RFC 3875
56 REQUEST_URI => $uri->path_query || '/', # not in RFC 3875
57 REQUEST_METHOD => $req->method,
58 'psgi.version' => [ 1, 1 ],
59 'psgi.url_scheme' => $uri->scheme eq 'https' ? 'https' : 'http',
60 'psgi.input' => $input,
61 'psgi.errors' => *STDERR,
62 'psgi.multithread' => $FALSE,
63 'psgi.multiprocess' => $FALSE,
64 'psgi.run_once' => $TRUE,
65 'psgi.streaming' => $TRUE,
66 'psgi.nonblocking' => $FALSE,
67 @_,
68 };
69  
70 348 for my $field ( $req->headers->header_field_names ) {
71 397 my $key = uc("HTTP_$field");
72 397 $key =~ tr/-/_/;
73 397 ▶ $key =~ s/^HTTP_// if $field =~ /^Content-(Length|Type)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $field =~ /^Content-(Length|Type)$/ 365 32 397
74  
75 397 ▶ unless ( exists $env->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $env->{$key}) 397 0 397
76 397 $env->{$key} = $req->headers->header($field);
77 }
78 }
79  
80 348 ▶ if ($env->{SCRIPT_NAME}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"SCRIPT_NAME"}) 0 348 348
81 0 $env->{PATH_INFO} =~ s/^\Q$env->{SCRIPT_NAME}\E/\//;
82 0 $env->{PATH_INFO} =~ s/^\/+/\//;
83 }
84  
85 348 ▶ if (!defined($env->{HTTP_HOST}) && $req->uri->can('host')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $env->{"HTTP_HOST"} and $req->uri->can("host")) 338 10 348
Condition100%3/3
not defined $env->{"HTTP_HOST"} and $req->uri->can("host")
!ll&&!rl&&r
28338
MC/DC100%2/2
not defined $env->{"HTTP_HOST"} and $req->uri->can("host")
not defined $env->{"HTTP_HOST"} $req->uri->can("host")
Truth table100%3/3
not(A) and B
A= not defined $env->{"HTTP_HOST"}B= $req->uri->can("host")
ABresult
0X0
100
111
86 338 $env->{HTTP_HOST} = $req->uri->host;
87 338 ▶ $env->{HTTP_HOST} .= ':' . $req->uri->port
Branch: 1 branch
BranchTrueFalseTotal
if $req->uri->port ne $req->uri->default_port 2 336 338
88 if $req->uri->port ne $req->uri->default_port;
89 }
90  
91 348 return $env;
92 }
93  
94 330 ▶sub res_from_psgi {
Subroutine res_from_psgi: called CC=4 SCAR=13.9
Pod documented
95 330 my ($psgi_res) = @_;
96  
97 330 require HTTP::Response;
98  
99 330 my $res;
100 330 ▶ if (ref $psgi_res eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $psgi_res eq 'ARRAY') { } 293 37 330
elsif (ref $psgi_res eq 'CODE') { } 35 2 37
101 293 _res_from_psgi($psgi_res, \$res);
102 } elsif (ref $psgi_res eq 'CODE') {
103 33 ▶ $psgi_res->(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
104 33 _res_from_psgi($_[0], \$res);
105 35 });
106 } else {
107 2 ▶ Carp::croak("Bad response: ", defined $psgi_res ? $psgi_res : 'undef');
Branch: 1 branch
BranchTrueFalseTotal
defined $psgi_res ? : 1 1 2
108 }
109  
110 326 return $res;
111 }
112  
113 326 ▶sub _res_from_psgi {
Subroutine _res_from_psgi: called CC=2 SCAR=6.9
114 326 my ($status, $headers, $body) = @{+shift};
115 326 my $res_ref = shift;
116  
117 326 ▶ my $convert_resp = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
118 326 my $res = HTTP::Response->new($status);
119 326 $res->message(status_message($status));
120 326 ▶ $res->headers->header(@$headers) if @$headers;
Branch: 1 branch
BranchTrueFalseTotal
if @$headers 305 21 326
121  
122 326 ▶ if (ref $body eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $body eq 'ARRAY') { } 294 32 326
123 294 $res->content(join '', grep defined, @$body);
124 } else {
125 32 local $/ = \4096;
126 32 my $content = '';
127 32 ▶ while (defined(my $buf = $body->getline)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $buf = $body->getline)) 98 32 130
128 98 $content .= $buf;
129 }
130 32 $body->close;
131 32 $res->content($content);
132 }
133  
134 326 ${ $res_ref } = $res;
135  
136 326 return;
137 326 };
138  
139 326 ▶ if (!defined $body) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $body) 15 311 326
140 15 $body = [];
141 my $o = Plack::Util::inline_object
142 21 ▶ write => sub { push @$body, @_ },
Subroutine __ANON__: called CC=1 SCAR=0.0
143 15 close => $convert_resp;
144  
145 15 return $o;
146 }
147  
148 311 $convert_resp->();
149 }
150  
151 292 ▶sub HTTP::Request::to_psgi {
Subroutine to_psgi: called CC=1 SCAR=0.0
Pod undocumented
152 292 req_to_psgi(@_);
153 }
154  
155 293 ▶sub HTTP::Response::from_psgi {
Subroutine from_psgi: called CC=1 SCAR=0.0
Pod undocumented
156 293 my $class = shift;
157 293 res_from_psgi(@_);
158 }
159  
160 package
161 HTTP::Message::PSGI::ChunkedInput;
162  
163 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
164 0 my($class, $content, $chunked) = @_;
165  
166 0 my $content_cb;
167 0 ▶ if ($chunked) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunked) { } 0 0 0
168 0 my $done;
169 0 ▶ $content_cb = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
170 0 my $chunk = $content->();
171 0 ▶ return if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 0 0 0
172 0 ▶ unless (defined $chunk) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $chunk) 0 0 0
173 0 $done = 1;
174 0 return "0\015\012\015\012";
175 }
176 0 ▶ return '' unless length $chunk;
Branch: 1 branch
BranchTrueFalseTotal
unless length $chunk 0 0 0
177 0 return sprintf('%x', length $chunk) . "\015\012$chunk\015\012";
178 0 };
179 } else {
180 0 $content_cb = $content;
181 }
182  
183 0 bless { content => $content_cb }, $class;
184 }
185  
186 0 ▶sub read {
Subroutine read: not called CC=3 SCAR=24.8
187 0 my $self = shift;
188  
189 0 my $chunk = $self->{content}->();
190 0 ▶ return 0 unless defined $chunk;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $chunk 0 0 0
191  
192 0 $_[0] = '';
193 0 ▶ substr($_[0], $_[2] || 0, length $chunk) = $chunk;
Condition0%0/2
$_[2] || 0
l!l
00
MC/DC0%0/1
$_[2] || 0
$_[2]
Truth table0%0/2
A
A= $_[2]
Aresult
11
00
194  
195 0 return length $chunk;
196 }
197  
198 0 ▶sub close { }
Subroutine close: not called CC=1 SCAR=0.0
199  
200 package HTTP::Message::PSGI;
201  
202 72 1;
203  
204 __END__
« blib/lib/Plack/Middleware/Chunked.pm ↑ Summary blib/lib/Plack/Middleware/ContentMD5.pm »