Mojo/Content.pm

filter: Statementstmt 92.8% 156 / 168 Branchbran 86.8% 99 / 114 Conditioncond 89.7% 70 / 78 MC/DCmcdc 80.0% 36 / 45 Subroutinesub 95.2% 40 / 42 Podpod 100% 25 / 25 totaltotal 90.2% 426 / 472 CC 91 SCAR 22.5
CC
91
Cov
90%
_parse_sse
30.0
parse
24.0
write_sse
12.0
CRAP 9.4
« Mojolicious/Command/Author/cpanify.pm ↑ Summary Mojo/Exception.pm »
1 package Mojo::Content;
2 30 ▶use Mojo::Base 'Mojo::EventEmitter';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 30 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 30 ▶use Compress::Raw::Zlib qw(WANT_GZIP Z_STREAM_END);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 30 ▶use Mojo::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 30 ▶use Mojo::SSE qw(build_event parse_event);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 30 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 30 has [qw(auto_decompress auto_relax relaxed skip_body)];
11 1180 ▶has headers => sub { Mojo::Headers->new };
Subroutine __ANON__: called CC=1 SCAR=0.0
12 62 ▶has max_buffer_size => sub { $ENV{MOJO_MAX_BUFFER_SIZE} || 262144 };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"MOJO_MAX_BUFFER_SIZE"} or 262144 3 59 62
13 4 ▶has max_leftover_size => sub { $ENV{MOJO_MAX_LEFTOVER_SIZE} || 262144 };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"MOJO_MAX_LEFTOVER_SIZE"} or 262144 0 4 4
14  
15 30 my $BOUNDARY_RE = qr!multipart.*boundary\s*=\s*(?:"([^"]+)"|([\w'(),.:?\-+/]+))!i;
16  
17 1 ▶sub body_contains { croak 'Method "body_contains" not implemented by subclass' }
Subroutine body_contains: called CC=1 SCAR=0.0
Pod documented
18 1 ▶sub body_size { croak 'Method "body_size" not implemented by subclass' }
Subroutine body_size: called CC=1 SCAR=0.0
Pod documented
19  
20 1172 ▶sub boundary { (shift->headers->content_type // '') =~ $BOUNDARY_RE ? $1 // $2 : undef }
Subroutine boundary: called CC=4 SCAR=13.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
((shift())->headers->content_type // '') =~ /$BOUNDARY_RE/u ? : 228 944 1172
Condition80%4/5
(shift())->headers->content_type // ""
l!l
555617
$1 // $2
l!l&&r!l&&!r
92190
MC/DC100%1/1
(shift())->headers->content_type // ""
(shift())->headers->content_type
MC/DC0%0/2
$1 // $2
$1 $2
Truth table100%2/2
A
A= (shift())->headers->content_type
Aresult
11
00
Truth table66%2/3
A // B
A= $1B= $2
ABresult
1X1
011
000
21  
22 232 ▶sub charset {
Subroutine charset: called CC=3 SCAR=11.0
Pod documented
23 232 ▶ my $type = shift->headers->content_type // '';
Condition100%2/2
(shift())->headers->content_type // ""
l!l
20527
MC/DC100%1/1
(shift())->headers->content_type // ""
(shift())->headers->content_type
Truth table100%2/2
A
A= (shift())->headers->content_type
Aresult
11
00
24 232 ▶ return $type =~ /charset\s*=\s*"?([^"\s;]+)"?/i ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$type =~ /charset\s*=\s*"?([^"\s;]+)"?/iu ? : 152 80 232
25 }
26  
27 19 ▶sub clone {
Subroutine clone: called CC=2 SCAR=6.9
Pod documented
28 19 my $self = shift;
29 19 ▶ return undef if $self->is_dynamic;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_dynamic 4 15 19
30 15 return $self->new(headers => $self->headers->clone);
31 }
32  
33 58 ▶sub generate_body_chunk {
Subroutine generate_body_chunk: called CC=7 SCAR=19.7
Pod documented
34 58 my ($self, $offset) = @_;
35  
36 58 ▶ $self->emit(drain => $offset) unless length($self->{body_buffer} //= '');
Branch: 1 branch
BranchTrueFalseTotal
unless length($self->{"body_buffer"} //= "") 47 11 58
Condition100%2/2
$self->{"body_buffer"} //= ""
l!l
1444
MC/DC100%1/1
$self->{"body_buffer"} //= ""
$self->{"body_buffer"}
Truth table100%2/2
A
A= $self->{"body_buffer"}
Aresult
11
00
37 57 ▶ return delete $self->{body_buffer} if length $self->{body_buffer};
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"body_buffer"} 46 11 57
38 11 ▶ return '' if $self->{eof};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"eof"} 10 1 11
39  
40 1 my $len = $self->headers->content_length;
41 1 ▶ return looks_like_number $len && $len == $offset ? '' : undef;
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($len) && $len == $offset ? : 0 1 1
Condition33%1/3
&looks_like_number($len) and $len == $offset
!ll&&!rl&&r
100
MC/DC0%0/2
&looks_like_number($len) and $len == $offset
&looks_like_number($len) $len == $offset
Truth table33%1/3
A and B
A= &looks_like_number($len)B= $len == $offset
ABresult
0X0
100
111
42 }
43  
44 1 ▶sub get_body_chunk { croak 'Method "get_body_chunk" not implemented by subclass' }
Subroutine get_body_chunk: called CC=1 SCAR=0.0
Pod documented
45  
46 427 ▶sub get_header_chunk { substr shift->_headers->{header_buffer}, shift, 131072 }
Subroutine get_header_chunk: called CC=1 SCAR=0.0
Pod documented
47  
48 416 ▶sub header_size { length shift->_headers->{header_buffer} }
Subroutine header_size: called CC=1 SCAR=0.0
Pod documented
49  
50 34 ▶sub headers_contain { index(shift->_headers->{header_buffer}, shift) >= 0 }
Subroutine headers_contain: called CC=1 SCAR=0.0
Pod documented
51  
52 2080 ▶sub is_chunked { !!shift->headers->transfer_encoding }
Subroutine is_chunked: called CC=1 SCAR=0.0
Pod documented
53  
54 276 ▶sub is_compressed { lc(shift->headers->content_encoding // '') eq 'gzip' }
Subroutine is_compressed: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
(shift())->headers->content_encoding // ""
l!l
34242
MC/DC100%1/1
(shift())->headers->content_encoding // ""
(shift())->headers->content_encoding
Truth table100%2/2
A
A= (shift())->headers->content_encoding
Aresult
11
00
55  
56 1677 ▶sub is_dynamic { !!$_[0]{dynamic} }
Subroutine is_dynamic: called CC=1 SCAR=0.0
Pod documented
57  
58 1765 ▶sub is_finished { (shift->{state} // '') eq 'finished' }
Subroutine is_finished: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
+(shift())->{"state"} // ""
l!l
172540
MC/DC100%1/1
+(shift())->{"state"} // ""
+(shift())->{"state"}
Truth table100%2/2
A
A= +(shift())->{"state"}
Aresult
11
00
59  
60 1011 ▶sub is_limit_exceeded { !!shift->{limit} }
Subroutine is_limit_exceeded: called CC=1 SCAR=0.0
Pod documented
61  
62 1025 ▶sub is_multipart {undef}
Subroutine is_multipart: called CC=1 SCAR=0.0
Pod documented
63  
64 21 ▶sub is_parsing_body { (shift->{state} // '') eq 'body' }
Subroutine is_parsing_body: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
+(shift())->{"state"} // ""
l!l
183
MC/DC100%1/1
+(shift())->{"state"} // ""
+(shift())->{"state"}
Truth table100%2/2
A
A= +(shift())->{"state"}
Aresult
11
00
65  
66 758 ▶sub is_sse { (shift->headers->content_type // '') eq 'text/event-stream' }
Subroutine is_sse: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
(shift())->headers->content_type // ""
l!l
438320
MC/DC100%1/1
(shift())->headers->content_type // ""
(shift())->headers->content_type
Truth table100%2/2
A
A= (shift())->headers->content_type
Aresult
11
00
67  
68 179 ▶sub leftovers { shift->{buffer} }
Subroutine leftovers: called CC=1 SCAR=0.0
Pod documented
69  
70 1133 ▶sub parse {
Subroutine parse: called CC=24 SCAR=31.8
Pod documented
71 1133 my $self = shift;
72  
73 # Headers
74 1133 $self->_parse_until_body(@_);
75 1133 ▶ return $self if $self->{state} eq 'headers';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"state"} eq "headers" 273 860 1133
76  
77 # Chunked content
78 860 ▶ $self->{real_size} //= 0;
Condition100%2/2
$self->{"real_size"} //= 0
l!l
319541
MC/DC100%1/1
$self->{"real_size"} //= 0
$self->{"real_size"}
Truth table100%2/2
A
A= $self->{"real_size"}
Aresult
11
00
79 860 ▶ if ($self->is_chunked) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_chunked) { } 102 758 860
elsif ($self->is_sse) { } 0 758 758
80 102 $self->_parse_chunked;
81 102 ▶ $self->{state} = 'finished' if ($self->{chunk_state} // '') eq 'finished';
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"chunk_state"} // "") eq "finished" 17 85 102
Condition100%2/2
$self->{"chunk_state"} // ""
l!l
1983
MC/DC100%1/1
$self->{"chunk_state"} // ""
$self->{"chunk_state"}
Truth table100%2/2
A
A= $self->{"chunk_state"}
Aresult
11
00
82 }
83  
84 # SSE
85 0 elsif ($self->is_sse) { $self->_parse_sse }
86  
87 # Not chunked, pass through to second buffer
88 else {
89 758 $self->{real_size} += length $self->{pre_buffer};
90 758 ▶ my $limit = $self->is_finished && length($self->{buffer}) > $self->max_leftover_size;
Condition100%3/3
$self->is_finished && length $self->{'buffer'} > $self->max_leftover_size
!ll&&!rl&&r
740144
MC/DC100%2/2
$self->is_finished && length $self->{'buffer'} > $self->max_leftover_size
$self->is_finished length $self->{'buffer'} > $self->max_leftover_size
Truth table100%3/3
A && B
A= $self->is_finishedB= length $self->{'buffer'} > $self->max_leftover_size
ABresult
0X0
100
111
91 758 ▶ $self->{buffer} .= $self->{pre_buffer} unless $limit;
Branch: 1 branch
BranchTrueFalseTotal
unless $limit 754 4 758
92 758 $self->{pre_buffer} = '';
93 }
94  
95 # No content
96 860 ▶ if ($self->skip_body) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->skip_body) 10 850 860
97 10 $self->{state} = 'finished';
98 10 return $self;
99 }
100  
101 # Relaxed parsing
102 850 my $headers = $self->headers;
103 850 ▶ my $len = $headers->content_length // '';
Condition100%2/2
$headers->content_length // ""
l!l
485365
MC/DC100%1/1
$headers->content_length // ""
$headers->content_length
Truth table100%2/2
A
A= $headers->content_length
Aresult
11
00
104 850 ▶ if ($self->auto_relax && !length $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->auto_relax and not length $len) 62 788 850
Condition100%3/3
$self->auto_relax and not length $len
!ll&&!rl&&r
56921962
MC/DC100%2/2
$self->auto_relax and not length $len
$self->auto_relax not length $len
Truth table100%3/3
A and not(B)
A= $self->auto_relaxB= not length $len
ABresult
0X0
100
111
105 62 ▶ my $connection = lc($headers->connection // '');
Condition100%2/2
$headers->connection // ""
l!l
755
MC/DC100%1/1
$headers->connection // ""
$headers->connection
Truth table100%2/2
A
A= $headers->connection
Aresult
11
00
106 62 ▶ $self->relaxed(1) if $connection eq 'close' || !$connection;
Branch: 1 branch
BranchTrueFalseTotal
if $connection eq "close" or not $connection 60 2 62
Condition100%3/3
$connection eq "close" or not $connection
l!l&&r!l&&!r
5552
MC/DC100%2/2
$connection eq "close" or not $connection
$connection eq "close" not $connection
Truth table100%3/3
A or not(B)
A= $connection eq "close"B= not $connection
ABresult
1X1
011
000
107 }
108  
109 # Chunked or relaxed content
110 850 ▶ if ($self->is_chunked || $self->relaxed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_chunked or $self->relaxed) 193 657 850
Condition100%3/3
$self->is_chunked or $self->relaxed
l!l&&r!l&&!r
85108657
MC/DC100%2/2
$self->is_chunked or $self->relaxed
$self->is_chunked $self->relaxed
Truth table100%3/3
A or B
A= $self->is_chunkedB= $self->relaxed
ABresult
1X1
011
000
111 193 ▶ $self->_decompress($self->{buffer} //= '');
Condition100%2/2
$self->{"buffer"} //= ""
l!l
18211
MC/DC100%1/1
$self->{"buffer"} //= ""
$self->{"buffer"}
Truth table100%2/2
A
A= $self->{"buffer"}
Aresult
11
00
112 193 $self->{size} += length $self->{buffer};
113 193 $self->{buffer} = '';
114 193 return $self;
115 }
116  
117 # Normal content
118 657 ▶ $len = 0 unless looks_like_number $len;
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($len) 181 476 657
119 657 ▶ if ((my $need = $len - ($self->{size} ||= 0)) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $need = $len - ($self->{"size"} ||= 0)) > 0) 430 227 657
Condition100%2/2
$self->{"size"} ||= 0
l!l
180477
MC/DC100%1/1
$self->{"size"} ||= 0
$self->{"size"}
Truth table100%2/2
A
A= $self->{"size"}
Aresult
11
00
120 430 my $len = length $self->{buffer};
121 430 ▶ my $chunk = substr $self->{buffer}, 0, $need > $len ? $len : $need, '';
Branch: 1 branch
BranchTrueFalseTotal
$need > $len ? : 200 230 430
122 430 $self->_decompress($chunk);
123 430 $self->{size} += length $chunk;
124 }
125 657 ▶ $self->{state} = 'finished' if $len <= $self->progress;
Branch: 1 branch
BranchTrueFalseTotal
if $len <= $self->progress 457 200 657
126  
127 657 return $self;
128 }
129  
130 40 ▶sub parse_body {
Subroutine parse_body: called CC=1 SCAR=0.0
Pod documented
131 40 my $self = shift;
132 40 $self->{state} = 'body';
133 40 return $self->parse(@_);
134 }
135  
136 699 ▶sub progress {
Subroutine progress: called CC=5 SCAR=16.1
Pod documented
137 699 my $self = shift;
138 699 ▶ return 0 unless my $state = $self->{state};
Branch: 1 branch
BranchTrueFalseTotal
unless my $state = $self->{"state"} 7 692 699
139 692 ▶ return 0 unless $state eq 'body' || $state eq 'finished';
Branch: 1 branch
BranchTrueFalseTotal
unless $state eq "body" or $state eq "finished" 6 686 692
Condition100%3/3
$state eq "body" or $state eq "finished"
l!l&&r!l&&!r
647396
MC/DC100%2/2
$state eq "body" or $state eq "finished"
$state eq "body" $state eq "finished"
Truth table100%3/3
A or B
A= $state eq "body"B= $state eq "finished"
ABresult
1X1
011
000
140 686 ▶ return $self->{raw_size} - ($self->{header_size} || 0);
Condition100%2/2
$self->{'header_size'} || 0
l!l
63551
MC/DC100%1/1
$self->{'header_size'} || 0
$self->{'header_size'}
Truth table100%2/2
A
A= $self->{'header_size'}
Aresult
11
00
141 }
142  
143 29 ▶sub write {
Subroutine write: called CC=5 SCAR=16.2
Pod documented
144 29 my ($self, $chunk, $cb) = @_;
145  
146 29 $self->{dynamic} = 1;
147 29 ▶ $self->{body_buffer} .= $chunk if defined $chunk;
Branch: 1 branch
BranchTrueFalseTotal
if defined $chunk 29 0 29
148 29 ▶ $self->once(drain => $cb) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 24 5 29
149 29 ▶ $self->{eof} = 1 if defined $chunk && !length $chunk;
Branch: 1 branch
BranchTrueFalseTotal
if defined $chunk and not length $chunk 6 23 29
Condition66%2/3
defined $chunk and not length $chunk
!ll&&!rl&&r
0236
MC/DC50%1/2
defined $chunk and not length $chunk
defined $chunk not length $chunk
Truth table66%2/3
A and not(B)
A= defined $chunkB= not length $chunk
ABresult
0X0
100
111
150  
151 29 return $self;
152 }
153  
154 38 ▶sub write_chunk {
Subroutine write_chunk: called CC=6 SCAR=17.9
Pod documented
155 38 my ($self, $chunk, $cb) = @_;
156  
157 38 ▶ $self->headers->transfer_encoding('chunked') unless $self->{chunked};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"chunked"} 12 26 38
158 38 @{$self}{qw(chunked dynamic)} = (1, 1);
159  
160 38 ▶ $self->{body_buffer} .= $self->_build_chunk($chunk) if defined $chunk;
Branch: 1 branch
BranchTrueFalseTotal
if defined $chunk 37 1 38
161 38 ▶ $self->once(drain => $cb) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 23 15 38
162 38 ▶ $self->{eof} = 1 if defined $chunk && !length $chunk;
Branch: 1 branch
BranchTrueFalseTotal
if defined $chunk and not length $chunk 8 30 38
Condition100%3/3
defined $chunk and not length $chunk
!ll&&!rl&&r
1298
MC/DC100%2/2
defined $chunk and not length $chunk
defined $chunk not length $chunk
Truth table100%3/3
A and not(B)
A= defined $chunkB= not length $chunk
ABresult
0X0
100
111
163  
164 38 return $self;
165 }
166  
167 0 ▶sub write_sse {
Subroutine write_sse: not called CC=3 SCAR=24.8
Pod documented
168 0 my ($self, $event, $cb) = @_;
169  
170 0 ▶ $self->headers->content_type('text/event-stream') unless $self->{sse};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"sse"} 0 0 0
171 0 $self->{sse} = 1;
172  
173 0 ▶ return $self->write unless defined $event;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $event 0 0 0
174 0 return $self->write(build_event($event), $cb);
175 }
176  
177 37 ▶sub _build_chunk {
Subroutine _build_chunk: called CC=3 SCAR=11.0
178 37 my ($self, $chunk) = @_;
179  
180 # End
181 37 ▶ return "\x0d\x0a0\x0d\x0a\x0d\x0a" unless length $chunk;
Branch: 1 branch
BranchTrueFalseTotal
unless length $chunk 8 29 37
182  
183 # First chunk has no leading CRLF
184 29 ▶ my $crlf = $self->{chunks}++ ? "\x0d\x0a" : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'chunks'}++ ? : 17 12 29
185 29 return $crlf . sprintf('%x', length $chunk) . "\x0d\x0a$chunk";
186 }
187  
188 623 ▶sub _decompress {
Subroutine _decompress: called CC=8 SCAR=20.9
189 623 my ($self, $chunk) = @_;
190  
191 # No compression
192 623 ▶ return $self->emit(read => $chunk) unless $self->auto_decompress && $self->is_compressed;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->auto_decompress and $self->is_compressed 593 30 623
Condition100%3/3
$self->auto_decompress and $self->is_compressed
!ll&&!rl&&r
35324030
MC/DC100%2/2
$self->auto_decompress and $self->is_compressed
$self->auto_decompress $self->is_compressed
Truth table100%3/3
A and B
A= $self->auto_decompressB= $self->is_compressed
ABresult
0X0
100
111
193  
194 # Decompress
195 30 $self->{post_buffer} .= $chunk;
196 30 ▶ my $gz = $self->{gz} //= Compress::Raw::Zlib::Inflate->new(WindowBits => WANT_GZIP);
Condition66%2/3
$self->{"gz"} //= "Compress::Raw::Zlib::Inflate"->new("WindowBits", 16)
l!l&&r!l&&!r
10200
MC/DC0%0/2
$self->{"gz"} //= "Compress::Raw::Zlib::Inflate"->new("WindowBits", 16)
$self->{"gz"} "Compress::Raw::Zlib::Inflate"->new("WindowBits", 16)
Truth table66%2/3
A //= B
A= $self->{"gz"}B= "Compress::Raw::Zlib::Inflate"->new("WindowBits", 16)
ABresult
1X1
011
000
197 30 my $status = $gz->inflate(\$self->{post_buffer}, my $out);
198 30 ▶ $self->emit(read => $out) if defined $out;
Branch: 1 branch
BranchTrueFalseTotal
if defined $out 30 0 30
199  
200 # Replace Content-Encoding with Content-Length
201 30 ▶ $self->headers->content_length($gz->total_out)->remove('Content-Encoding') if $status == Z_STREAM_END;
Branch: 1 branch
BranchTrueFalseTotal
if $status == Z_STREAM_END() 19 11 30
202  
203 # Check buffer size
204 30 ▶ @$self{qw(state limit)} = ('finished', 1) if length($self->{post_buffer} // '') > $self->max_buffer_size;
Branch: 1 branch
BranchTrueFalseTotal
if length($self->{"post_buffer"} // "") > $self->max_buffer_size 1 29 30
Condition50%1/2
$self->{"post_buffer"} // ""
l!l
300
MC/DC0%0/1
$self->{"post_buffer"} // ""
$self->{"post_buffer"}
Truth table50%1/2
A
A= $self->{"post_buffer"}
Aresult
11
00
205 }
206  
207 877 ▶sub _headers {
Subroutine _headers: called CC=3 SCAR=11.0
208 877 my $self = shift;
209 877 ▶ return $self if defined $self->{header_buffer};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"header_buffer"} 491 386 877
210 386 my $headers = $self->headers->to_string;
211 386 ▶ $self->{header_buffer} = $headers ? "$headers\x0d\x0a\x0d\x0a" : "\x0d\x0a";
Branch: 1 branch
BranchTrueFalseTotal
$headers ? : 381 5 386
212 386 return $self;
213 }
214  
215 102 ▶sub _parse_chunked {
Subroutine _parse_chunked: called CC=12 SCAR=24.8
216 102 my $self = shift;
217  
218 # Trailing headers
219 102 ▶ return $self->_parse_chunked_trailing_headers if ($self->{chunk_state} // '') eq 'trailing_headers';
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"chunk_state"} // "") eq "trailing_headers" 2 100 102
Condition100%2/2
$self->{"chunk_state"} // ""
l!l
2100
MC/DC100%1/1
$self->{"chunk_state"} // ""
$self->{"chunk_state"}
Truth table100%2/2
A
A= $self->{"chunk_state"}
Aresult
11
00
220  
221 100 ▶ while (my $len = length $self->{pre_buffer}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $len = length $self->{"pre_buffer"}) 137 63 200
222  
223 # Start new chunk (ignore the chunk extension)
224 137 ▶ unless ($self->{chunk_len}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"chunk_len"}) 87 50 137
225 87 ▶ last unless $self->{pre_buffer} =~ s/^(?:\x0d?\x0a)?([0-9a-fA-F]+).*\x0a//;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"pre_buffer"} =~ s/^(?:\x0d?\x0a)?([0-9a-fA-F]+).*\x0a//u 20 67 87
226 67 ▶ next if $self->{chunk_len} = hex $1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"chunk_len"} = hex $1 50 17 67
227  
228 # Last chunk
229 17 $self->{chunk_state} = 'trailing_headers';
230 17 last;
231 }
232  
233 # Remove as much as possible from payload
234 50 ▶ $len = $self->{chunk_len} if $self->{chunk_len} < $len;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"chunk_len"} < $len 24 26 50
235 50 $self->{buffer} .= substr $self->{pre_buffer}, 0, $len, '';
236 50 $self->{real_size} += $len;
237 50 $self->{chunk_len} -= $len;
238 }
239  
240 # Trailing headers
241 100 ▶ $self->_parse_chunked_trailing_headers if ($self->{chunk_state} // '') eq 'trailing_headers';
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"chunk_state"} // "") eq "trailing_headers" 17 83 100
Condition100%2/2
$self->{"chunk_state"} // ""
l!l
1783
MC/DC100%1/1
$self->{"chunk_state"} // ""
$self->{"chunk_state"}
Truth table100%2/2
A
A= $self->{"chunk_state"}
Aresult
11
00
242  
243 # Check buffer size
244 100 ▶ @$self{qw(state limit)} = ('finished', 1) if length($self->{pre_buffer} // '') > $self->max_buffer_size;
Branch: 1 branch
BranchTrueFalseTotal
if length($self->{"pre_buffer"} // "") > $self->max_buffer_size 1 99 100
Condition100%2/2
$self->{"pre_buffer"} // ""
l!l
8317
MC/DC100%1/1
$self->{"pre_buffer"} // ""
$self->{"pre_buffer"}
Truth table100%2/2
A
A= $self->{"pre_buffer"}
Aresult
11
00
245 }
246  
247 19 ▶sub _parse_chunked_trailing_headers {
Subroutine _parse_chunked_trailing_headers: called CC=3 SCAR=11.0
248 19 my $self = shift;
249  
250 19 my $headers = $self->headers->parse(delete $self->{pre_buffer});
251 19 ▶ return unless $headers->is_finished;
Branch: 1 branch
BranchTrueFalseTotal
unless $headers->is_finished 2 17 19
252 17 $self->{chunk_state} = 'finished';
253  
254 # Take care of leftover and replace Transfer-Encoding with Content-Length
255 17 $self->{buffer} .= $headers->leftovers;
256 17 $headers->remove('Transfer-Encoding');
257 17 ▶ $headers->content_length($self->{real_size}) unless $headers->content_length;
Branch: 1 branch
BranchTrueFalseTotal
unless $headers->content_length 16 1 17
258 }
259  
260 1068 ▶sub _parse_headers {
Subroutine _parse_headers: called CC=2 SCAR=6.9
261 1068 my $self = shift;
262  
263 1068 my $headers = $self->headers->parse(delete $self->{pre_buffer});
264 1068 ▶ return unless $headers->is_finished;
Branch: 1 branch
BranchTrueFalseTotal
unless $headers->is_finished 546 522 1068
265 522 $self->{state} = 'body';
266  
267 # Take care of leftovers
268 522 my $leftovers = $self->{pre_buffer} = $headers->leftovers;
269 522 $self->{header_size} = $self->{raw_size} - length $leftovers;
270 }
271  
272 0 ▶sub _parse_sse {
Subroutine _parse_sse: not called CC=5 SCAR=34.0
273 0 my $self = shift;
274  
275 # Connection established
276 0 ▶ $self->emit('sse') unless $self->{sse};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"sse"} 0 0 0
277 0 $self->{sse} = 1;
278  
279 # Parse SSE
280 0 ▶ while (my $event = parse_event(\$self->{pre_buffer})) { $self->emit(sse => $event) }
Branch: 1 branch
BranchTrueFalseTotal
if (my $event = parse_event(\$self->{"pre_buffer"})) 0 0 0
281  
282 # Check buffer size
283 0 ▶ @$self{qw(state limit)} = ('finished', 1) if length($self->{pre_buffer} // '') > $self->max_buffer_size;
Branch: 1 branch
BranchTrueFalseTotal
if length($self->{"pre_buffer"} // "") > $self->max_buffer_size 0 0 0
Condition0%0/2
$self->{"pre_buffer"} // ""
l!l
00
MC/DC0%0/1
$self->{"pre_buffer"} // ""
$self->{"pre_buffer"}
Truth table0%0/2
A
A= $self->{"pre_buffer"}
Aresult
11
00
284 }
285  
286 2117 ▶sub _parse_until_body {
Subroutine _parse_until_body: called CC=6 SCAR=17.9
287 2117 my ($self, $chunk) = @_;
288  
289 2117 ▶ $self->{raw_size} += length($chunk //= '');
Condition100%2/2
$chunk //= ""
l!l
1133984
MC/DC100%1/1
$chunk //= ""
$chunk
Truth table100%2/2
A
A= $chunk
Aresult
11
00
290 2117 $self->{pre_buffer} .= $chunk;
291 2117 ▶ $self->_parse_headers if ($self->{state} ||= 'headers') eq 'headers';
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"state"} ||= "headers") eq "headers" 1068 1049 2117
Condition100%2/2
$self->{"state"} ||= "headers"
l!l
1592525
MC/DC100%1/1
$self->{"state"} ||= "headers"
$self->{"state"}
Truth table100%2/2
A
A= $self->{"state"}
Aresult
11
00
292 2117 ▶ $self->emit('body') if $self->{state} ne 'headers' && !$self->{body}++;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"state"} ne "headers" and not $self->{"body"}++ 541 1576 2117
Condition100%3/3
$self->{"state"} ne "headers" and not $self->{"body"}++
!ll&&!rl&&r
5461030541
MC/DC100%2/2
$self->{"state"} ne "headers" and not $self->{"body"}++
$self->{"state"} ne "headers" not $self->{"body"}++
Truth table100%3/3
A and not(B)
A= $self->{"state"} ne "headers"B= not $self->{"body"}++
ABresult
0X0
100
111
293 }
294  
295 30 1;
296  
297 =encoding utf8
298  
299 =head1 NAME
300  
301 Mojo::Content - HTTP content base class
302  
303 =head1 SYNOPSIS
304  
305 package Mojo::Content::MyContent;
306 use Mojo::Base 'Mojo::Content';
307  
308 sub body_contains {...}
309 sub body_size {...}
310 sub get_body_chunk {...}
311  
312 =head1 DESCRIPTION
313  
314 L<Mojo::Content> is an abstract base class for HTTP content containers, based on L<RFC
315 7230|https://tools.ietf.org/html/rfc7230> and L<RFC 7231|https://tools.ietf.org/html/rfc7231>, like
316 L<Mojo::Content::MultiPart> and L<Mojo::Content::Single>.
317  
318 =head1 EVENTS
319  
320 L<Mojo::Content> inherits all events from L<Mojo::EventEmitter> and can emit the following new ones.
321  
322 =head2 body
323  
324 $content->on(body => sub ($content) {...});
325  
326 Emitted once all headers have been parsed and the body starts.
327  
328 $content->on(body => sub ($content) {
329 $content->auto_upgrade(0) if $content->headers->header('X-No-MultiPart');
330 });
331  
332 =head2 drain
333  
334 $content->on(drain => sub ($content, $offset) {...});
335  
336 Emitted once all data has been written.
337  
338 $content->on(drain => sub ($content) {
339 $content->write_chunk(time);
340 });
341  
342 =head2 read
343  
344 $content->on(read => sub ($content, $bytes) {...});
345  
346 Emitted when a new chunk of content arrives.
347  
348 $content->on(read => sub ($content, $bytes) {
349 say "Streaming: $bytes";
350 });
351  
352 =head2 sse
353  
354 $content->on(sse => sub ($content, $event) {...});
355  
356 Emitted when a new Server-Sent Event (SSE) connection has been established and for each new event that arrives. Note
357 that this event is B<EXPERIMENTAL> and may change without warning!
358  
359 $content->on(sse => sub ($content, $event) {
360 if ($event) { say "Type: $event->{type}, Data: $event->{data}" }
361 else { say "SSE connection established" }
362 });
363  
364 =head1 ATTRIBUTES
365  
366 L<Mojo::Content> implements the following attributes.
367  
368 =head2 auto_decompress
369  
370 my $bool = $content->auto_decompress;
371 $content = $content->auto_decompress($bool);
372  
373 Decompress content automatically if L</"is_compressed"> is true.
374  
375 =head2 auto_relax
376  
377 my $bool = $content->auto_relax;
378 $content = $content->auto_relax($bool);
379  
380 Try to detect when relaxed parsing is necessary.
381  
382 =head2 headers
383  
384 my $headers = $content->headers;
385 $content = $content->headers(Mojo::Headers->new);
386  
387 Content headers, defaults to a L<Mojo::Headers> object.
388  
389 =head2 max_buffer_size
390  
391 my $size = $content->max_buffer_size;
392 $content = $content->max_buffer_size(1024);
393  
394 Maximum size in bytes of buffer for content parser, defaults to the value of the C<MOJO_MAX_BUFFER_SIZE> environment
395 variable or C<262144> (256KiB).
396  
397 =head2 max_leftover_size
398  
399 my $size = $content->max_leftover_size;
400 $content = $content->max_leftover_size(1024);
401  
402 Maximum size in bytes of buffer for pipelined HTTP requests, defaults to the value of the C<MOJO_MAX_LEFTOVER_SIZE>
403 environment variable or C<262144> (256KiB).
404  
405 =head2 relaxed
406  
407 my $bool = $content->relaxed;
408 $content = $content->relaxed($bool);
409  
410 Activate relaxed parsing for responses that are terminated with a connection close.
411  
412 =head2 skip_body
413  
414 my $bool = $content->skip_body;
415 $content = $content->skip_body($bool);
416  
417 Skip body parsing and finish after headers.
418  
419 =head1 METHODS
420  
421 L<Mojo::Content> inherits all methods from L<Mojo::EventEmitter> and implements the following new ones.
422  
423 =head2 body_contains
424  
425 my $bool = $content->body_contains('foo bar baz');
426  
427 Check if content contains a specific string. Meant to be overloaded in a subclass.
428  
429 =head2 body_size
430  
431 my $size = $content->body_size;
432  
433 Content size in bytes. Meant to be overloaded in a subclass.
434  
435 =head2 boundary
436  
437 my $boundary = $content->boundary;
438  
439 Extract multipart boundary from C<Content-Type> header.
440  
441 =head2 charset
442  
443 my $charset = $content->charset;
444  
445 Extract charset from C<Content-Type> header.
446  
447 =head2 clone
448  
449 my $clone = $content->clone;
450  
451 Return a new L<Mojo::Content> object cloned from this content if possible, otherwise return C<undef>.
452  
453 =head2 generate_body_chunk
454  
455 my $bytes = $content->generate_body_chunk(0);
456  
457 Generate dynamic content.
458  
459 =head2 get_body_chunk
460  
461 my $bytes = $content->get_body_chunk(0);
462  
463 Get a chunk of content starting from a specific position. Meant to be overloaded in a subclass.
464  
465 =head2 get_header_chunk
466  
467 my $bytes = $content->get_header_chunk(13);
468  
469 Get a chunk of the headers starting from a specific position. Note that this method finalizes the content.
470  
471 =head2 header_size
472  
473 my $size = $content->header_size;
474  
475 Size of headers in bytes. Note that this method finalizes the content.
476  
477 =head2 headers_contain
478  
479 my $bool = $content->headers_contain('foo bar baz');
480  
481 Check if headers contain a specific string. Note that this method finalizes the content.
482  
483 =head2 is_chunked
484  
485 my $bool = $content->is_chunked;
486  
487 Check if C<Transfer-Encoding> header indicates chunked transfer encoding.
488  
489 =head2 is_compressed
490  
491 my $bool = $content->is_compressed;
492  
493 Check C<Content-Encoding> header for C<gzip> value.
494  
495 =head2 is_dynamic
496  
497 my $bool = $content->is_dynamic;
498  
499 Check if content will be dynamically generated, which prevents L</"clone"> from working.
500  
501 =head2 is_finished
502  
503 my $bool = $content->is_finished;
504  
505 Check if parser is finished.
506  
507 =head2 is_limit_exceeded
508  
509 my $bool = $content->is_limit_exceeded;
510  
511 Check if buffer has exceeded L</"max_buffer_size">.
512  
513 =head2 is_multipart
514  
515 my $bool = $content->is_multipart;
516  
517 False, this is not a L<Mojo::Content::MultiPart> object.
518  
519 =head2 is_parsing_body
520  
521 my $bool = $content->is_parsing_body;
522  
523 Check if body parsing started yet.
524  
525 =head2 is_sse
526  
527 my $bool = $content->is_sse;
528  
529 Check if C<Content-Type> header indicates Server-Sent Events (SSE). Note that this method is B<EXPERIMENTAL> and may
530 change without warning!
531  
532 =head2 leftovers
533  
534 my $bytes = $content->leftovers;
535  
536 Get leftover data from content parser.
537  
538 =head2 parse
539  
540 $content
541 = $content->parse("Content-Length: 12\x0d\x0a\x0d\x0aHello World!");
542  
543 Parse content chunk.
544  
545 =head2 parse_body
546  
547 $content = $content->parse_body('Hi!');
548  
549 Parse body chunk and skip headers.
550  
551 =head2 progress
552  
553 my $size = $content->progress;
554  
555 Size of content already received from message in bytes.
556  
557 =head2 write
558  
559 $content = $content->write;
560 $content = $content->write('');
561 $content = $content->write($bytes);
562 $content = $content->write($bytes => sub {...});
563  
564 Write dynamic content non-blocking, the optional drain callback will be executed once all data has been written.
565 Calling this method without a chunk of data will finalize the L</"headers"> and allow for dynamic content to be written
566 later. You can write an empty chunk of data at any time to end the stream.
567  
568 # Make sure previous chunk of data has been written before continuing
569 $content->write('He' => sub ($content) {
570 $content->write('llo!' => sub ($content) {
571 $content->write('');
572 });
573 });
574  
575 =head2 write_chunk
576  
577 $content = $content->write_chunk;
578 $content = $content->write_chunk('');
579 $content = $content->write_chunk($bytes);
580 $content = $content->write_chunk($bytes => sub {...});
581  
582 Write dynamic content non-blocking with chunked transfer encoding, the optional drain callback will be executed once
583 all data has been written. Calling this method without a chunk of data will finalize the L</"headers"> and allow for
584 dynamic content to be written later. You can write an empty chunk of data at any time to end the stream.
585  
586 # Make sure previous chunk of data has been written before continuing
587 $content->write_chunk('He' => sub ($content) {
588 $content->write_chunk('llo!' => sub ($content) {
589 $content->write_chunk('');
590 });
591 });
592  
593 =head2 write_sse
594  
595 $content = $content->write_sse;
596 $content = $content->write_sse($event);
597 $content = $content->write_sse($event => sub {...});
598  
599 Write Server-Sent Event (SSE) non-blocking, the optional drain callback will be executed once all data has been
600 written. Calling this method without an event will finalize the response headers and allow for events to be written
601 later. Note that this method is B<EXPERIMENTAL> and may change without warning!
602  
603 =head1 SEE ALSO
604  
605 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
606  
607 =cut
« Mojolicious/Command/Author/cpanify.pm ↑ Summary Mojo/Exception.pm »