Mojo/Message/Response.pm

filter: Statementstmt 100% 59 / 59 Branchbran 96.1% 25 / 26 Conditioncond 90.4% 19 / 21 MC/DCmcdc 72.7% 8 / 11 Subroutinesub 100% 19 / 19 Podpod 100% 13 / 13 totaltotal 95.9% 143 / 149 CC 24 SCAR 12.1
CC
24
Cov
97%
extract_start_line
6.0
_start_line
4.0
cookies
4.0
CRAP 3.3
« Mojo/Asset/Memory.pm ↑ Summary Mojolicious/Plugin/EPRenderer.pm »
1 package Mojo::Message::Response;
2 27 ▶use Mojo::Base 'Mojo::Message';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 27 ▶use Mojo::Cookie::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 27 ▶use Mojo::Date;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 27 has [qw(code message)];
8 201 ▶has max_message_size => sub { $ENV{MOJO_MAX_MESSAGE_SIZE} // 2147483648 };
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%2/2
$ENV{"MOJO_MAX_MESSAGE_SIZE"} // 2147483648
l!l
1200
MC/DC100%1/1
$ENV{"MOJO_MAX_MESSAGE_SIZE"} // 2147483648
$ENV{"MOJO_MAX_MESSAGE_SIZE"}
Truth table100%2/2
A
A= $ENV{"MOJO_MAX_MESSAGE_SIZE"}
Aresult
11
00
9  
10 # Unmarked codes are from RFC 7231
11 27 my %MESSAGES = (
12 100 => 'Continue',
13 101 => 'Switching Protocols',
14 102 => 'Processing', # RFC 2518 (WebDAV)
15 103 => 'Early Hints', # RFC 8297
16 200 => 'OK',
17 201 => 'Created',
18 202 => 'Accepted',
19 203 => 'Non-Authoritative Information',
20 204 => 'No Content',
21 205 => 'Reset Content',
22 206 => 'Partial Content',
23 207 => 'Multi-Status', # RFC 2518 (WebDAV)
24 208 => 'Already Reported', # RFC 5842
25 226 => 'IM Used', # RFC 3229
26 300 => 'Multiple Choices',
27 301 => 'Moved Permanently',
28 302 => 'Found',
29 303 => 'See Other',
30 304 => 'Not Modified',
31 305 => 'Use Proxy',
32 307 => 'Temporary Redirect',
33 308 => 'Permanent Redirect', # RFC 7538
34 400 => 'Bad Request',
35 401 => 'Unauthorized',
36 402 => 'Payment Required',
37 403 => 'Forbidden',
38 404 => 'Not Found',
39 405 => 'Method Not Allowed',
40 406 => 'Not Acceptable',
41 407 => 'Proxy Authentication Required',
42 408 => 'Request Timeout',
43 409 => 'Conflict',
44 410 => 'Gone',
45 411 => 'Length Required',
46 412 => 'Precondition Failed',
47 413 => 'Request Entity Too Large',
48 414 => 'Request-URI Too Long',
49 415 => 'Unsupported Media Type',
50 416 => 'Request Range Not Satisfiable',
51 417 => 'Expectation Failed',
52 418 => "I'm a teapot", # RFC 2324 :)
53 421 => 'Misdirected Request', # RFC 7540
54 422 => 'Unprocessable Entity', # RFC 2518 (WebDAV)
55 423 => 'Locked', # RFC 2518 (WebDAV)
56 424 => 'Failed Dependency', # RFC 2518 (WebDAV)
57 425 => 'Too Early', # RFC 8470
58 426 => 'Upgrade Required', # RFC 2817
59 428 => 'Precondition Required', # RFC 6585
60 429 => 'Too Many Requests', # RFC 6585
61 431 => 'Request Header Fields Too Large', # RFC 6585
62 451 => 'Unavailable For Legal Reasons', # RFC 7725
63 500 => 'Internal Server Error',
64 501 => 'Not Implemented',
65 502 => 'Bad Gateway',
66 503 => 'Service Unavailable',
67 504 => 'Gateway Timeout',
68 505 => 'HTTP Version Not Supported',
69 506 => 'Variant Also Negotiates', # RFC 2295
70 507 => 'Insufficient Storage', # RFC 2518 (WebDAV)
71 508 => 'Loop Detected', # RFC 5842
72 509 => 'Bandwidth Limit Exceeded', # Unofficial
73 510 => 'Not Extended', # RFC 2774
74 511 => 'Network Authentication Required' # RFC 6585
75 );
76  
77 189 ▶sub cookies {
Subroutine cookies: called CC=4 SCAR=13.9
Pod documented
78 189 my $self = shift;
79  
80 # Parse cookies
81 189 my $headers = $self->headers;
82 189 ▶ return [@{Mojo::Cookie::Response->parse($headers->set_cookie)}] unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 174 15 189
83  
84 # Add cookies
85 15 ▶ $headers->add('Set-Cookie' => "$_") for map { ref $_ eq 'HASH' ? Mojo::Cookie::Response->new($_) : $_ } @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 5 25 30
86  
87 15 return $self;
88 }
89  
90 393 ▶sub default_message { $MESSAGES{$_[1] || $_[0]->code // 404} || '' }
Subroutine default_message: called CC=4 SCAR=13.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$MESSAGES{($_[1] || $_[0]->code) // 404} or "" 392 1 393
Condition100%5/5
($_[1] || $_[0]->code) // 404
l!l
3921
$_[1] or $_[0]->code
l!l&&r!l&&!r
1552371
MC/DC100%2/2
($_[1] || $_[0]->code) // 404
$_[1] $_[0]->code
Truth table100%3/3
A or B
A= $_[1]B= $_[0]->code
ABresult
1X1
011
000
91  
92 216 ▶sub extract_start_line {
Subroutine extract_start_line: called CC=6 SCAR=17.9
Pod documented
93 216 my ($self, $bufref) = @_;
94  
95 # We have a full response line
96 216 ▶ return undef unless $$bufref =~ s/^(.*?)\x0d?\x0a//;
Branch: 1 branch
BranchTrueFalseTotal
unless $$bufref =~ s/^(.*?)\x0d?\x0a//u 16 200 216
97 200 ▶ return !$self->error({message => 'Bad response start-line'}) unless $1 =~ m!^\s*HTTP/(\d\.\d)\s+(\d\d\d)\s*(.+)?$!;
Branch: 1 branch
BranchTrueFalseTotal
unless $1 =~ m[^\s*HTTP/(\d\.\d)\s+(\d\d\d)\s*(.+)?$]u 1 199 200
98  
99 199 my $content = $self->content;
100 199 ▶ $content->skip_body(1) if $self->code($2)->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->code($2)->is_empty 10 189 199
101 199 ▶ defined $content->$_ or $content->$_(1) for qw(auto_decompress auto_relax);
Branch: 1 branch
BranchTrueFalseTotal
defined $content->$_ or $content->$_(1) 0 398 398
102 199 return !!$self->version($1)->message($3);
103 }
104  
105 348 ▶sub fix_headers {
Subroutine fix_headers: called CC=4 SCAR=13.9
Pod documented
106 348 my $self = shift;
107 348 ▶ $self->{fix} ? return $self : $self->SUPER::fix_headers(@_);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'fix'} ? : 174 174 348
108  
109 # Date
110 174 my $headers = $self->headers;
111 174 ▶ $headers->date(Mojo::Date->new->to_string) unless $headers->date;
Branch: 1 branch
BranchTrueFalseTotal
unless $headers->date 166 8 174
112  
113 # RFC 7230 3.3.2
114 174 ▶ $headers->remove('Content-Length') if $self->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_empty 5 169 174
115  
116 174 return $self;
117 }
118  
119 183 ▶sub get_start_line_chunk {
Subroutine get_start_line_chunk: called CC=1 SCAR=0.0
Pod documented
120 183 my ($self, $offset) = @_;
121 183 $self->_start_line->emit(progress => 'start_line', $offset);
122 183 return substr $self->{start_buffer}, $offset, 131072;
123 }
124  
125 3 ▶sub is_client_error { shift->_status_class(400) }
Subroutine is_client_error: called CC=1 SCAR=0.0
Pod documented
126  
127 543 ▶sub is_empty {
Subroutine is_empty: called CC=4 SCAR=13.9
Pod documented
128 543 my $self = shift;
129 543 ▶ return undef unless my $code = $self->code;
Branch: 1 branch
BranchTrueFalseTotal
unless my $code = $self->code 3 540 543
130 540 ▶ return $self->is_info || $code == 204 || $code == 304;
Condition100%6/6
$self->is_info || $code == 204 || $code == 304
l!l&&r!l&&!r
1311516
$self->is_info or $code == 204
l!l&&r!l&&!r
112527
MC/DC100%3/3
$self->is_info || $code == 204 || $code == 304
$self->is_info $code == 204 $code == 304
Truth table100%4/4
A or B || C
A= $self->is_infoB= $code == 204C= $code == 304
ABCresult
1XX1
01X1
0011
0000
131 }
132  
133 162 ▶sub is_error { shift->_status_class(400, 500) }
Subroutine is_error: called CC=1 SCAR=0.0
Pod documented
134 703 ▶sub is_info { shift->_status_class(100) }
Subroutine is_info: called CC=1 SCAR=0.0
Pod documented
135 3 ▶sub is_redirect { shift->_status_class(300) }
Subroutine is_redirect: called CC=1 SCAR=0.0
Pod documented
136 3 ▶sub is_server_error { shift->_status_class(500) }
Subroutine is_server_error: called CC=1 SCAR=0.0
Pod documented
137  
138 6 ▶sub is_success { shift->_status_class(200) }
Subroutine is_success: called CC=1 SCAR=0.0
Pod documented
139  
140 163 ▶sub start_line_size { length shift->_start_line->{start_buffer} }
Subroutine start_line_size: called CC=1 SCAR=0.0
Pod documented
141  
142 346 ▶sub _start_line {
Subroutine _start_line: called CC=4 SCAR=14.0
143 346 my $self = shift;
144  
145 346 ▶ return $self if defined $self->{start_buffer};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"start_buffer"} 174 172 346
146 172 ▶ my $code = $self->code || 404;
Condition50%1/2
$self->code || 404
l!l
1720
MC/DC0%0/1
$self->code || 404
$self->code
Truth table50%1/2
A
A= $self->code
Aresult
11
00
147 172 ▶ my $msg = $self->message || $self->default_message;
Condition66%2/3
$self->message || $self->default_message
l!l&&r!l&&!r
11710
MC/DC0%0/2
$self->message || $self->default_message
$self->message $self->default_message
Truth table66%2/3
A || B
A= $self->messageB= $self->default_message
ABresult
1X1
011
000
148 172 $self->{start_buffer} = "HTTP/@{[$self->version]} $code $msg\x0d\x0a";
149  
150 172 return $self;
151 }
152  
153 880 ▶sub _status_class {
Subroutine _status_class: called CC=3 SCAR=11.0
154 880 my ($self, @classes) = @_;
155 880 ▶ return undef unless my $code = $self->code;
Branch: 1 branch
BranchTrueFalseTotal
unless my $code = $self->code 1 879 880
156 879 ▶ return !!grep { $code >= $_ && $code < ($_ + 100) } @classes;
Condition100%3/3
$code >= $_ && $code < $_ + 100
!ll&&!rl&&r
25371573
MC/DC100%2/2
$code >= $_ && $code < $_ + 100
$code >= $_ $code < $_ + 100
Truth table100%3/3
A && B
A= $code >= $_B= $code < $_ + 100
ABresult
0X0
100
111
157 }
158  
159 27 1;
160  
161 =encoding utf8
162  
163 =head1 NAME
164  
165 Mojo::Message::Response - HTTP response
166  
167 =head1 SYNOPSIS
168  
169 use Mojo::Message::Response;
170  
171 # Parse
172 my $res = Mojo::Message::Response->new;
173 $res->parse("HTTP/1.0 200 OK\x0d\x0a");
174 $res->parse("Content-Length: 12\x0d\x0a");
175 $res->parse("Content-Type: text/plain\x0d\x0a\x0d\x0a");
176 $res->parse('Hello World!');
177 say $res->code;
178 say $res->headers->content_type;
179 say $res->body;
180  
181 # Build
182 my $res = Mojo::Message::Response->new;
183 $res->code(200);
184 $res->headers->content_type('text/plain');
185 $res->body('Hello World!');
186 say $res->to_string;
187  
188 =head1 DESCRIPTION
189  
190 L<Mojo::Message::Response> is a container for HTTP responses, based on L<RFC 7230|https://tools.ietf.org/html/rfc7230>
191 and L<RFC 7231|https://tools.ietf.org/html/rfc7231>.
192  
193 =head1 EVENTS
194  
195 L<Mojo::Message::Response> inherits all events from L<Mojo::Message>.
196  
197 =head1 ATTRIBUTES
198  
199 L<Mojo::Message::Response> inherits all attributes from L<Mojo::Message> and implements the following new ones.
200  
201 =head2 code
202  
203 my $code = $res->code;
204 $res = $res->code(200);
205  
206 HTTP response status code.
207  
208 =head2 max_message_size
209  
210 my $size = $res->max_message_size;
211 $res = $res->max_message_size(1024);
212  
213 Maximum message size in bytes, defaults to the value of the C<MOJO_MAX_MESSAGE_SIZE> environment variable or
214 C<2147483648> (2GiB). Setting the value to C<0> will allow messages of indefinite size.
215  
216 =head2 message
217  
218 my $msg = $res->message;
219 $res = $res->message('OK');
220  
221 HTTP response status message.
222  
223 =head1 METHODS
224  
225 L<Mojo::Message::Response> inherits all methods from L<Mojo::Message> and implements the following new ones.
226  
227 =head2 cookies
228  
229 my $cookies = $res->cookies;
230 $res = $res->cookies(Mojo::Cookie::Response->new);
231 $res = $res->cookies({name => 'foo', value => 'bar'});
232  
233 Access response cookies, usually L<Mojo::Cookie::Response> objects.
234  
235 # Names of all cookies
236 say $_->name for @{$res->cookies};
237  
238 =head2 default_message
239  
240 my $msg = $res->default_message;
241 my $msg = $res->default_message(418);
242  
243 Generate default response message for status code, defaults to using L</"code">.
244  
245 =head2 extract_start_line
246  
247 my $bool = $res->extract_start_line(\$str);
248  
249 Extract status-line from string.
250  
251 =head2 fix_headers
252  
253 $res = $res->fix_headers;
254  
255 Make sure response has all required headers.
256  
257 =head2 get_start_line_chunk
258  
259 my $bytes = $res->get_start_line_chunk($offset);
260  
261 Get a chunk of status-line data starting from a specific position. Note that this method finalizes the response.
262  
263 =head2 is_client_error
264  
265 my $bool = $res->is_client_error;
266  
267 Check if this response has a C<4xx> status L</"code">.
268  
269 =head2 is_empty
270  
271 my $bool = $res->is_empty;
272  
273 Check if this response has a C<1xx>, C<204> or C<304> status L</"code">.
274  
275 =head2 is_error
276  
277 my $bool = $res->is_error;
278  
279 Check if this response has a C<4xx> or C<5xx> status L</"code">.
280  
281 =head2 is_info
282  
283 my $bool = $res->is_info;
284  
285 Check if this response has a C<1xx> status L</"code">.
286  
287 =head2 is_redirect
288  
289 my $bool = $res->is_redirect;
290  
291 Check if this response has a C<3xx> status L</"code">.
292  
293 =head2 is_server_error
294  
295 my $bool = $res->is_server_error;
296  
297 Check if this response has a C<5xx> status L</"code">.
298  
299 =head2 is_success
300  
301 my $bool = $res->is_success;
302  
303 Check if this response has a C<2xx> status L</"code">.
304  
305 =head2 start_line_size
306  
307 my $size = $req->start_line_size;
308  
309 Size of the status-line in bytes. Note that this method finalizes the response.
310  
311 =head1 SEE ALSO
312  
313 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
314  
315 =cut
« Mojo/Asset/Memory.pm ↑ Summary Mojolicious/Plugin/EPRenderer.pm »