Mojo/Transaction/HTTP.pm

filter: Statementstmt 89.7% 61 / 68 Branchbran 72.2% 39 / 54 Conditioncond 88.2% 30 / 34 MC/DCmcdc 73.6% 14 / 19 Subroutinesub 92.3% 12 / 13 Podpod 100% 8 / 8 totaltotal 83.6% 164 / 196 CC 42 SCAR 19.5
CC
42
Cov
83%
_write
11.0
_body
9.3
client_read
8.7
CRAP 7.0
« Mojo/IOLoop/Client.pm ↑ Summary Mojolicious/Plugin/EPLRenderer.pm »
1 package Mojo::Transaction::HTTP;
2 25 ▶use Mojo::Base 'Mojo::Transaction';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 25 has 'previous';
5  
6 173 ▶sub client_read {
Subroutine client_read: called CC=6 SCAR=21.6
Pod documented
7 173 my ($self, $chunk) = @_;
8  
9 # Skip body for HEAD request
10 173 my $res = $self->res;
11 173 ▶ $res->content->skip_body(1) if uc $self->req->method eq 'HEAD';
Branch: 1 branch
BranchTrueFalseTotal
if uc $self->req->method eq "HEAD" 0 173 173
12 173 ▶ return undef unless $res->parse($chunk)->is_finished;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->parse($chunk)->is_finished 13 160 173
13  
14 # Unexpected 1xx response
15 160 ▶ return $self->completed if !$res->is_info || $res->headers->upgrade;
Branch: 1 branch
BranchTrueFalseTotal
if not $res->is_info or $res->headers->upgrade 160 0 160
Condition66%2/3
not $res->is_info or $res->headers->upgrade
l!l&&r!l&&!r
15910
MC/DC0%0/2
not $res->is_info or $res->headers->upgrade
not $res->is_info $res->headers->upgrade
Truth table66%2/3
not(A) or B
A= not $res->is_infoB= $res->headers->upgrade
ABresult
1X1
011
000
16 0 $self->res($res->new)->emit(unexpected => $res);
17 0 ▶ return undef unless length(my $leftovers = $res->content->leftovers);
Branch: 1 branch
BranchTrueFalseTotal
unless length(my $leftovers = $res->content->leftovers) 0 0 0
18 0 $self->client_read($leftovers);
19 }
20  
21 339 ▶sub client_write { shift->_write(0) }
Subroutine client_write: called CC=1 SCAR=0.0
Pod documented
22  
23 165 ▶sub is_empty { !!(uc $_[0]->req->method eq 'HEAD' || $_[0]->res->is_empty) }
Subroutine is_empty: called CC=2 SCAR=6.9
Pod documented
Condition100%3/3
uc $_[0]->req->method eq 'HEAD' || $_[0]->res->is_empty
l!l&&r!l&&!r
14160
MC/DC100%2/2
uc $_[0]->req->method eq 'HEAD' || $_[0]->res->is_empty
uc $_[0]->req->method eq 'HEAD' $_[0]->res->is_empty
Truth table100%3/3
A || B
A= uc $_[0]->req->method eq 'HEAD'B= $_[0]->res->is_empty
ABresult
1X1
011
000
24  
25 328 ▶sub keep_alive {
Subroutine keep_alive: called CC=7 SCAR=19.5
Pod documented
26 328 my $self = shift;
27  
28 # Close
29 328 my $req = $self->req;
30 328 my $res = $self->res;
31 328 ▶ my $req_conn = lc($req->headers->connection // '');
Condition100%2/2
$req->headers->connection // ""
l!l
6322
MC/DC100%1/1
$req->headers->connection // ""
$req->headers->connection
Truth table100%2/2
A
A= $req->headers->connection
Aresult
11
00
32 328 ▶ my $res_conn = lc($res->headers->connection // '');
Condition100%2/2
$res->headers->connection // ""
l!l
3325
MC/DC100%1/1
$res->headers->connection // ""
$res->headers->connection
Truth table100%2/2
A
A= $res->headers->connection
Aresult
11
00
33 328 ▶ return undef if $req_conn eq 'close' || $res_conn eq 'close';
Branch: 1 branch
BranchTrueFalseTotal
if $req_conn eq "close" or $res_conn eq "close" 9 319 328
Condition100%3/3
$req_conn eq "close" or $res_conn eq "close"
l!l&&r!l&&!r
63319
MC/DC100%2/2
$req_conn eq "close" or $res_conn eq "close"
$req_conn eq "close" $res_conn eq "close"
Truth table100%3/3
A or B
A= $req_conn eq "close"B= $res_conn eq "close"
ABresult
1X1
011
000
34  
35 # Keep-alive is optional for 1.0
36 319 ▶ return $res_conn eq 'keep-alive' if $res->version eq '1.0';
Branch: 1 branch
BranchTrueFalseTotal
if $res->version eq "1.0" 0 319 319
37 319 ▶ return $req_conn eq 'keep-alive' if $req->version eq '1.0';
Branch: 1 branch
BranchTrueFalseTotal
if $req->version eq "1.0" 3 316 319
38  
39 # Keep-alive is the default for 1.1
40 316 return 1;
41 }
42  
43 0 ▶sub redirects {
Subroutine redirects: not called CC=2 SCAR=17.9
Pod documented
44 0 my $previous = shift;
45 0 my @redirects;
46 0 ▶ unshift @redirects, $previous while $previous = $previous->previous;
Branch: 1 branch
BranchTrueFalseTotal
$previous = $previous->previous and do { unshift @redirects, $previous } 0 0 0
47 0 return \@redirects;
48 }
49  
50 187 ▶sub resume { ++$_[0]{writing} and return $_[0]->emit('resume') }
Subroutine resume: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
++$_[0]{"writing"} and return $_[0]->emit("resume") 187 0 187
51  
52 181 ▶sub server_read {
Subroutine server_read: called CC=4 SCAR=14.1
Pod documented
53 181 my ($self, $chunk) = @_;
54  
55 # Parse request
56 181 my $req = $self->req;
57 181 ▶ $req->parse($chunk) unless $req->error;
Branch: 1 branch
BranchTrueFalseTotal
unless $req->error 181 0 181
58  
59 # Generate response
60 181 ▶ $self->emit('request') if $req->is_finished && !$self->{handled}++;
Branch: 1 branch
BranchTrueFalseTotal
if $req->is_finished and not $self->{"handled"}++ 163 18 181
Condition66%2/3
$req->is_finished and not $self->{"handled"}++
!ll&&!rl&&r
180163
MC/DC50%1/2
$req->is_finished and not $self->{"handled"}++
$req->is_finished not $self->{"handled"}++
Truth table66%2/3
A and not(B)
A= $req->is_finishedB= not $self->{"handled"}++
ABresult
0X0
100
111
61 }
62  
63 181 ▶sub server_write { shift->_write(1) }
Subroutine server_write: called CC=1 SCAR=0.0
Pod documented
64  
65 355 ▶sub _body {
Subroutine _body: called CC=9 SCAR=22.3
66 355 my ($self, $msg, $finish) = @_;
67  
68 # Prepare body chunk
69 355 my $buffer = $msg->get_body_chunk($self->{offset});
70 355 ▶ $self->{offset} += defined $buffer ? length $buffer : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $buffer ? : 355 0 355
71  
72 # Delayed
73 355 ▶ $self->{writing} = 0 unless defined $buffer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $buffer 0 355 355
74  
75 # Finished (static content does not need an extra round trip for the empty chunk)
76 355 ▶ if (defined $buffer && (!length $buffer || (!$msg->content->is_dynamic && $self->{offset} >= $msg->body_size))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $buffer and !length($buffer) || !$msg->content->is_dynamic && $self->{"offset"} >= $msg->body_size) 318 37 355
Condition88%8/9
defined $buffer and !length($buffer) || !$msg->content->is_dynamic && $self->{"offset"} >= $msg->body_size
!ll&&!rl&&r
037318
not length $buffer or not $msg->content->is_dynamic and $self->{"offset"} >= $msg->body_size
l!l&&r!l&&!r
15915937
not $msg->content->is_dynamic and $self->{"offset"} >= $msg->body_size
!ll&&!rl&&r
1720159
MC/DC75%3/4
defined $buffer and !length($buffer) || !$msg->content->is_dynamic && $self->{"offset"} >= $msg->body_size
defined $buffer not length $buffer not $msg->content->is_dynamic $self->{"offset"} >= $msg->body_size
Truth table80%4/5
A and not(B) or not(C) and D
A= defined $bufferB= not length $bufferC= not $msg->content->is_dynamicD= $self->{"offset"} >= $msg->body_size
ABCDresult
0XXX0
100X0
10100
11XX1
10111
77 318 $msg->finish;
78 318 ▶ $finish ? $self->completed : ($self->{writing} = 0);
Branch: 1 branch
BranchTrueFalseTotal
$finish ? : 158 160 318
79 }
80  
81 355 ▶ return $buffer // '';
Condition50%1/2
$buffer // ""
l!l
3550
MC/DC0%0/1
$buffer // ""
$buffer
Truth table50%1/2
A
A= $buffer
Aresult
11
00
82 }
83  
84 323 ▶sub _headers {
Subroutine _headers: called CC=5 SCAR=16.2
85 323 my ($self, $msg, $head) = @_;
86  
87 # Prepare header chunk
88 323 my $buffer = $msg->get_header_chunk($self->{offset});
89 323 ▶ my $written = defined $buffer ? length $buffer : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $buffer ? : 323 0 323
90 323 $self->{write} -= $written;
91 323 $self->{offset} += $written;
92  
93 # Switch to body
94 323 ▶ if ($self->{write} <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"write"} <= 0) 323 0 323
95 323 @$self{qw(http_state offset)} = ('body', 0);
96  
97 # Response without body
98 323 ▶ $self->completed->{http_state} = 'empty' if $head && $self->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $head and $self->is_empty 4 319 323
Condition100%3/3
$head and $self->is_empty
!ll&&!rl&&r
1601594
MC/DC100%2/2
$head and $self->is_empty
$head $self->is_empty
Truth table100%3/3
A and B
A= $headB= $self->is_empty
ABresult
0X0
100
111
99 }
100  
101 323 return $buffer;
102 }
103  
104 323 ▶sub _start_line {
Subroutine _start_line: called CC=3 SCAR=11.2
105 323 my ($self, $msg) = @_;
106  
107 # Prepare start-line chunk
108 323 my $buffer = $msg->get_start_line_chunk($self->{offset});
109 323 ▶ my $written = defined $buffer ? length $buffer : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $buffer ? : 323 0 323
110 323 $self->{write} -= $written;
111 323 $self->{offset} += $written;
112  
113 # Switch to headers
114 323 ▶ @$self{qw(http_state write offset)} = ('headers', $msg->header_size, 0) if $self->{write} <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"write"} <= 0 323 0 323
115  
116 323 return $buffer;
117 }
118  
119 520 ▶sub _write {
Subroutine _write: called CC=11 SCAR=24.0
120 520 my ($self, $server) = @_;
121  
122 # Client starts writing right away
123 520 ▶ return '' unless $server ? $self->{writing} : ($self->{writing} //= 1);
Branch: 2 branches
BranchTrueFalseTotal
unless $server ? $self->{"writing"} : ($self->{"writing"} //= 1) 161 359 520
$server ? : 181 339 520
Condition100%2/2
$self->{"writing"} //= 1
l!l
179160
MC/DC100%1/1
$self->{"writing"} //= 1
$self->{"writing"}
Truth table100%2/2
A
A= $self->{"writing"}
Aresult
11
00
124  
125 # Nothing written yet
126 359 ▶ $self->{$_} ||= 0 for qw(offset write);
Condition100%2/2
$self->{$_} ||= 0
l!l
36682
MC/DC100%1/1
$self->{$_} ||= 0
$self->{$_}
Truth table100%2/2
A
A= $self->{$_}
Aresult
11
00
127 359 ▶ my $msg = $server ? $self->res : $self->req;
Branch: 1 branch
BranchTrueFalseTotal
$server ? : 180 179 359
128 359 ▶ @$self{qw(http_state write)} = ('start_line', $msg->start_line_size) unless $self->{http_state};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"http_state"} 323 36 359
129  
130 # Start-line
131 359 my $chunk = '';
132 359 ▶ $chunk .= $self->_start_line($msg) if $self->{http_state} eq 'start_line';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"http_state"} eq "start_line" 323 36 359
133  
134 # Headers
135 359 ▶ $chunk .= $self->_headers($msg, $server) if $self->{http_state} eq 'headers';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"http_state"} eq "headers" 323 36 359
136  
137 # Body
138 359 ▶ $chunk .= $self->_body($msg, $server) if $self->{http_state} eq 'body';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"http_state"} eq "body" 355 4 359
139  
140 359 return $chunk;
141 }
142  
143 25 1;
144  
145 =encoding utf8
146  
147 =head1 NAME
148  
149 Mojo::Transaction::HTTP - HTTP transaction
150  
151 =head1 SYNOPSIS
152  
153 use Mojo::Transaction::HTTP;
154  
155 # Client
156 my $tx = Mojo::Transaction::HTTP->new;
157 $tx->req->method('GET');
158 $tx->req->url->parse('http://example.com');
159 $tx->req->headers->accept('application/json');
160 say $tx->res->code;
161 say $tx->res->headers->content_type;
162 say $tx->res->body;
163 say $tx->remote_address;
164  
165 # Server
166 my $tx = Mojo::Transaction::HTTP->new;
167 say $tx->req->method;
168 say $tx->req->url->to_abs;
169 say $tx->req->headers->accept;
170 say $tx->remote_address;
171 $tx->res->code(200);
172 $tx->res->headers->content_type('text/plain');
173 $tx->res->body('Hello World!');
174  
175 =head1 DESCRIPTION
176  
177 L<Mojo::Transaction::HTTP> is a container for HTTP transactions, based on L<RFC
178 7230|https://tools.ietf.org/html/rfc7230> and L<RFC 7231|https://tools.ietf.org/html/rfc7231>.
179  
180 =head1 EVENTS
181  
182 L<Mojo::Transaction::HTTP> inherits all events from L<Mojo::Transaction> and can emit the following new ones.
183  
184 =head2 request
185  
186 $tx->on(request => sub ($tx) {...});
187  
188 Emitted when a request is ready and needs to be handled.
189  
190 $tx->on(request => sub ($tx) { $tx->res->headers->header('X-Bender' => 'Bite my shiny metal ass!') });
191  
192 =head2 resume
193  
194 $tx->on(resume => sub ($tx) {...});
195  
196 Emitted when transaction is resumed.
197  
198 =head2 unexpected
199  
200 $tx->on(unexpected => sub ($tx, $res) {...});
201  
202 Emitted for unexpected C<1xx> responses that will be ignored.
203  
204 $tx->on(unexpected => sub ($tx) { $tx->res->on(finish => sub { say 'Follow-up response is finished.' }) });
205  
206 =head1 ATTRIBUTES
207  
208 L<Mojo::Transaction::HTTP> inherits all attributes from L<Mojo::Transaction> and implements the following new ones.
209  
210 =head2 previous
211  
212 my $previous = $tx->previous;
213 $tx = $tx->previous(Mojo::Transaction::HTTP->new);
214  
215 Previous transaction that triggered this follow-up transaction, usually a L<Mojo::Transaction::HTTP> object.
216  
217 # Paths of previous requests
218 say $tx->previous->previous->req->url->path;
219 say $tx->previous->req->url->path;
220  
221 =head1 METHODS
222  
223 L<Mojo::Transaction::HTTP> inherits all methods from L<Mojo::Transaction> and implements the following new ones.
224  
225 =head2 client_read
226  
227 $tx->client_read($bytes);
228  
229 Read data client-side, used to implement user agents such as L<Mojo::UserAgent>.
230  
231 =head2 client_write
232  
233 my $bytes = $tx->client_write;
234  
235 Write data client-side, used to implement user agents such as L<Mojo::UserAgent>.
236  
237 =head2 is_empty
238  
239 my $bool = $tx->is_empty;
240  
241 Check transaction for C<HEAD> request and C<1xx>, C<204> or C<304> response.
242  
243 =head2 keep_alive
244  
245 my $bool = $tx->keep_alive;
246  
247 Check if connection can be kept alive.
248  
249 =head2 redirects
250  
251 my $redirects = $tx->redirects;
252  
253 Return an array reference with all previous transactions that preceded this follow-up transaction.
254  
255 # Paths of all previous requests
256 say $_->req->url->path for @{$tx->redirects};
257  
258 =head2 resume
259  
260 $tx = $tx->resume;
261  
262 Resume transaction.
263  
264 =head2 server_read
265  
266 $tx->server_read($bytes);
267  
268 Read data server-side, used to implement web servers such as L<Mojo::Server::Daemon>.
269  
270 =head2 server_write
271  
272 my $bytes = $tx->server_write;
273  
274 Write data server-side, used to implement web servers such as L<Mojo::Server::Daemon>.
275  
276 =head1 SEE ALSO
277  
278 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
279  
280 =cut
« Mojo/IOLoop/Client.pm ↑ Summary Mojolicious/Plugin/EPLRenderer.pm »