| 1 | package Mojo::Message; | ||||||||||||||||||||||||||||
| 2 | 29 | ▶ | use Mojo::Base 'Mojo::EventEmitter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 29 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 29 | ▶ | use Mojo::Asset::Memory; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 29 | ▶ | use Mojo::Content::Single; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 29 | ▶ | use Mojo::DOM; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 29 | ▶ | use Mojo::JSON qw(j); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 29 | ▶ | use Mojo::JSON::Pointer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 29 | ▶ | use Mojo::Parameters; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 29 | ▶ | use Mojo::Upload; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 29 | ▶ | use Mojo::Util qw(decode); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1084 | ▶ | has content => sub { Mojo::Content::Single->new }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 29 | has default_charset => 'UTF-8'; | |||||||||||||||||||||||||||
| 16 | 454 | ▶ | has max_line_size => sub { $ENV{MOJO_MAX_LINE_SIZE} || 8192 }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | 270 | ▶ | has max_message_size => sub { $ENV{MOJO_MAX_MESSAGE_SIZE} // 16777216 }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Condition100%2/2
$ENV{"MOJO_MAX_MESSAGE_SIZE"} // 16777216
MC/DC100%1/1
$ENV{"MOJO_MAX_MESSAGE_SIZE"} // 16777216
$ENV{"MOJO_MAX_MESSAGE_SIZE"}
Truth table100%2/2
A
A= $ENV{"MOJO_MAX_MESSAGE_SIZE"}
| |||||||||||||||||||||||||||||
| 18 | 29 | has version => '1.1'; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 510 | ▶ | sub body { | ||||||||||||||||||||||||||
|
Subroutine
body: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | 510 | my $self = shift; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # Get | ||||||||||||||||||||||||||||
| 24 | 510 | my $content = $self->content; | |||||||||||||||||||||||||||
| 25 | 510 | ▶ | return $content->is_multipart ? '' : $content->asset->slurp unless @_; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # Set (multipart content needs to be downgraded) | ||||||||||||||||||||||||||||
| 28 | 203 | ▶ | $content = $self->content(Mojo::Content::Single->new)->content if $content->is_multipart; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 203 | $content->asset(Mojo::Asset::Memory->new->add_chunk(@_)); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 203 | return $self; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 59 | ▶ | sub body_params { | ||||||||||||||||||||||||||
|
Subroutine
body_params: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | 59 | my $self = shift; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 59 | ▶ | return $self->{body_params} if $self->{body_params}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 51 | my $params = $self->{body_params} = Mojo::Parameters->new; | |||||||||||||||||||||||||||
| 39 | 51 | ▶ | $params->charset($self->content->charset || $self->default_charset); | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->content->charset || $self->default_charset
MC/DC0%0/2
$self->content->charset || $self->default_charset
$self->content->charset
$self->default_charset
Truth table66%2/3
A || B
A= $self->content->charsetB= $self->default_charset
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # "application/x-www-form-urlencoded" | ||||||||||||||||||||||||||||
| 42 | 51 | ▶ | my $type = $self->headers->content_type // ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->headers->content_type // ""
MC/DC100%1/1
$self->headers->content_type // ""
$self->headers->content_type
Truth table100%2/2
A
A= $self->headers->content_type
| |||||||||||||||||||||||||||||
| 43 | 51 | ▶ | if ($type =~ m!application/x-www-form-urlencoded!i) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 44 | 14 | $params->parse($self->content->asset->slurp); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # "multipart/form-data" | ||||||||||||||||||||||||||||
| 48 | elsif ($type =~ m!multipart/form-data!i) { | ||||||||||||||||||||||||||||
| 49 | 16 | $params->append(@$_) for @{$self->_formdata->{params}}; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 51 | return $params; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 542 | ▶ | sub body_size { shift->content->body_size } | ||||||||||||||||||||||||||
|
Subroutine
body_size: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 45 | ▶ | sub build_body { shift->_build('get_body_chunk') } | ||||||||||||||||||||||||||
|
Subroutine
build_body: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | 43 | ▶ | sub build_headers { shift->_build('get_header_chunk') } | ||||||||||||||||||||||||||
|
Subroutine
build_headers: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 43 | ▶ | sub build_start_line { shift->_build('get_start_line_chunk') } | ||||||||||||||||||||||||||
|
Subroutine
build_start_line: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 62 | ▶ | sub cookie { shift->_cache('cookies', 0, @_) } | ||||||||||||||||||||||||||
|
Subroutine
cookie: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 1 | ▶ | sub cookies { croak 'Method "cookies" not implemented by subclass' } | ||||||||||||||||||||||||||
|
Subroutine
cookies: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 36 | ▶ | sub dom { | ||||||||||||||||||||||||||
|
Subroutine
dom: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||
| 66 | 36 | my $self = shift; | |||||||||||||||||||||||||||
| 67 | 36 | ▶ | return undef if $self->content->is_multipart; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 36 | ▶ | my $dom = $self->{dom} ||= Mojo::DOM->new($self->text); | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"dom"} ||= "Mojo::DOM"->new($self->text)
MC/DC0%0/2
$self->{"dom"} ||= "Mojo::DOM"->new($self->text)
$self->{"dom"}
"Mojo::DOM"->new($self->text)
Truth table66%2/3
A ||= B
A= $self->{"dom"}B= "Mojo::DOM"->new($self->text)
| |||||||||||||||||||||||||||||
| 69 | 36 | ▶ | return @_ ? $dom->find(@_) : $dom; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 1569 | ▶ | sub error { | ||||||||||||||||||||||||||
|
Subroutine
error: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | 1569 | my $self = shift; | |||||||||||||||||||||||||||
| 74 | 1569 | ▶ | return $self->{error} unless @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 75 | $self->{error} = shift; | |||||||||||||||||||||||||||
| 76 | 75 | return $self->finish; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 27 | ▶ | sub every_cookie { shift->_cache('cookies', 1, @_) } | ||||||||||||||||||||||||||
|
Subroutine
every_cookie: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | 62 | ▶ | sub every_upload { shift->_cache('uploads', 1, @_) } | ||||||||||||||||||||||||||
|
Subroutine
every_upload: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 1 | ▶ | sub extract_start_line { croak 'Method "extract_start_line" not implemented by subclass' } | ||||||||||||||||||||||||||
|
Subroutine
extract_start_line: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 1226 | ▶ | sub finish { | ||||||||||||||||||||||||||
|
Subroutine
finish: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 1226 | my $self = shift; | |||||||||||||||||||||||||||
| 86 | 1226 | $self->{state} = 'finished'; | |||||||||||||||||||||||||||
| 87 | 1226 | ▶ | return $self->{finished}++ ? $self : $self->emit('finish'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 370 | ▶ | sub fix_headers { | ||||||||||||||||||||||||||
|
Subroutine
fix_headers: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | 370 | my $self = shift; | |||||||||||||||||||||||||||
| 92 | 370 | ▶ | return $self if $self->{fix}++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # Content-Length or Connection (unless chunked transfer encoding is used) | ||||||||||||||||||||||||||||
| 95 | 370 | my $content = $self->content; | |||||||||||||||||||||||||||
| 96 | 370 | my $headers = $content->headers; | |||||||||||||||||||||||||||
| 97 | 370 | ▶ | if ($content->is_multipart) { $headers->remove('Content-Length') } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 98 | 9 | ▶ | elsif ($content->is_chunked || $headers->content_length) { return $self } | ||||||||||||||||||||||||||
|
Condition100%3/3
$content->is_chunked or $headers->content_length
MC/DC100%2/2
$content->is_chunked or $headers->content_length
$content->is_chunked
$headers->content_length
Truth table100%3/3
A or B
A= $content->is_chunkedB= $headers->content_length
| |||||||||||||||||||||||||||||
| 99 | 361 | ▶ | if ($content->is_dynamic) { $headers->connection('close') } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 360 | else { $headers->content_length($self->body_size) } | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 361 | return $self; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 485 | ▶ | sub get_body_chunk { | ||||||||||||||||||||||||||
|
Subroutine
get_body_chunk: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 106 | 485 | my ($self, $offset) = @_; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 485 | $self->emit('progress', 'body', $offset); | |||||||||||||||||||||||||||
| 109 | 485 | my $chunk = $self->content->get_body_chunk($offset); | |||||||||||||||||||||||||||
| 110 | 484 | ▶ | return $chunk if !defined $chunk || length $chunk; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $chunk or length $chunk
MC/DC100%2/2
not defined $chunk or length $chunk
not defined $chunk
length $chunk
Truth table100%3/3
not(A) or B
A= not defined $chunkB= length $chunk
| |||||||||||||||||||||||||||||
| 111 | 207 | $self->finish; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 207 | return $chunk; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 409 | ▶ | sub get_header_chunk { | ||||||||||||||||||||||||||
|
Subroutine
get_header_chunk: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | 409 | my ($self, $offset) = @_; | |||||||||||||||||||||||||||
| 118 | 409 | $self->emit('progress', 'headers', $offset); | |||||||||||||||||||||||||||
| 119 | 409 | return $self->fix_headers->content->get_header_chunk($offset); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 1 | ▶ | sub get_start_line_chunk { croak 'Method "get_start_line_chunk" not implemented by subclass' } | ||||||||||||||||||||||||||
|
Subroutine
get_start_line_chunk: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 323 | ▶ | sub header_size { shift->fix_headers->content->header_size } | ||||||||||||||||||||||||||
|
Subroutine
header_size: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 5169 | ▶ | sub headers { shift->content->headers } | ||||||||||||||||||||||||||
|
Subroutine
headers: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 1383 | ▶ | sub is_finished { (shift->{state} // '') eq 'finished' } | ||||||||||||||||||||||||||
|
Subroutine
is_finished: called CC=2 SCAR=6.9
Pod
documented
Condition100%2/2
+(shift())->{"state"} // ""
MC/DC100%1/1
+(shift())->{"state"} // ""
+(shift())->{"state"}
Truth table100%2/2
A
A= +(shift())->{"state"}
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 22 | ▶ | sub is_limit_exceeded { !!shift->{limit} } | ||||||||||||||||||||||||||
|
Subroutine
is_limit_exceeded: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 11 | ▶ | sub json { | ||||||||||||||||||||||||||
|
Subroutine
json: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||
| 133 | 11 | my ($self, $pointer) = @_; | |||||||||||||||||||||||||||
| 134 | 11 | ▶ | return undef if $self->content->is_multipart; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 11 | ▶ | my $data = $self->{json} //= j($self->body); | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"json"} //= j($self->body)
MC/DC0%0/2
$self->{"json"} //= j($self->body)
$self->{"json"}
j($self->body)
Truth table66%2/3
A //= B
A= $self->{"json"}B= j($self->body)
| |||||||||||||||||||||||||||||
| 136 | 11 | ▶ | return $pointer ? Mojo::JSON::Pointer->new($data)->get($pointer) : $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 1023 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=14 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||
| 140 | 1023 | my ($self, $chunk) = @_; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 1023 | ▶ | return $self if $self->{error}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 1020 | $self->{raw_size} += length $chunk; | |||||||||||||||||||||||||||
| 144 | 1020 | $self->{buffer} .= $chunk; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | # Start-line | ||||||||||||||||||||||||||||
| 147 | 1020 | ▶ | unless ($self->{state}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | # Check start-line size | ||||||||||||||||||||||||||||
| 150 | 494 | my $len = index $self->{buffer}, "\x0a"; | |||||||||||||||||||||||||||
| 151 | 494 | ▶ | $len = length $self->{buffer} if $len < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 494 | ▶ | return $self->_limit('Maximum start-line size exceeded') if $len > $self->max_line_size; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 491 | ▶ | $self->{state} = 'content' if $self->extract_start_line(\$self->{buffer}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | # Content | ||||||||||||||||||||||||||||
| 158 | 1017 | ▶ | my $state = $self->{state} // ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"state"} // ""
MC/DC100%1/1
$self->{"state"} // ""
$self->{"state"}
Truth table100%2/2
A
A= $self->{"state"}
| |||||||||||||||||||||||||||||
| 159 | 1017 | ▶ | $self->content($self->content->parse(delete $self->{buffer})) if $state eq 'content' || $state eq 'finished'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$state eq "content" or $state eq "finished"
MC/DC100%2/2
$state eq "content" or $state eq "finished"
$state eq "content"
$state eq "finished"
Truth table100%3/3
A or B
A= $state eq "content"B= $state eq "finished"
| |||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # Check message size | ||||||||||||||||||||||||||||
| 162 | 1017 | my $max = $self->max_message_size; | |||||||||||||||||||||||||||
| 163 | 1017 | ▶ | return $self->_limit('Maximum message size exceeded') if $max && $max < $self->{raw_size}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$max and $max < $self->{"raw_size"}
MC/DC100%2/2
$max and $max < $self->{"raw_size"}
$max
$max < $self->{"raw_size"}
Truth table100%3/3
A and B
A= $maxB= $max < $self->{"raw_size"}
| |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | # Check header size | ||||||||||||||||||||||||||||
| 166 | 1012 | ▶ | return $self->_limit('Maximum header size exceeded') if $self->headers->is_limit_exceeded; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | # Check buffer size | ||||||||||||||||||||||||||||
| 169 | 1007 | ▶ | return $self->_limit('Maximum buffer size exceeded') if $self->content->is_limit_exceeded; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 1004 | ▶ | return $self->emit('progress')->content->is_finished ? $self->finish : $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 2 | ▶ | sub save_to { | ||||||||||||||||||||||||||
|
Subroutine
save_to: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 175 | 2 | my ($self, $path) = @_; | |||||||||||||||||||||||||||
| 176 | 2 | my $content = $self->content; | |||||||||||||||||||||||||||
| 177 | 2 | ▶ | croak 'Multipart content cannot be saved to files' if $content->is_multipart; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 1 | $content->asset->move_to($path); | |||||||||||||||||||||||||||
| 179 | 1 | return $self; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 1 | ▶ | sub start_line_size { croak 'Method "start_line_size" not implemented by subclass' } | ||||||||||||||||||||||||||
|
Subroutine
start_line_size: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 150 | ▶ | sub text { | ||||||||||||||||||||||||||
|
Subroutine
text: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||||||||||||||||||||
| 185 | 150 | my $self = shift; | |||||||||||||||||||||||||||
| 186 | 150 | my $body = $self->body; | |||||||||||||||||||||||||||
| 187 | 150 | ▶ | my $charset = $self->content->charset || $self->default_charset; | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->content->charset || $self->default_charset
MC/DC0%0/2
$self->content->charset || $self->default_charset
$self->content->charset
$self->default_charset
Truth table66%2/3
A || B
A= $self->content->charsetB= $self->default_charset
| |||||||||||||||||||||||||||||
| 188 | 150 | ▶ | return $charset ? decode($charset, $body) // $body : $body; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
decode($charset, $body) // $body
MC/DC0%0/2
decode($charset, $body) // $body
decode($charset, $body)
$body
Truth table66%2/3
A // B
A= decode($charset, $body)B= $body
| |||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 41 | ▶ | sub to_string { | ||||||||||||||||||||||||||
|
Subroutine
to_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 192 | 41 | my $self = shift; | |||||||||||||||||||||||||||
| 193 | 41 | return $self->build_start_line . $self->build_headers . $self->build_body; | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 33 | ▶ | sub upload { shift->_cache('uploads', 0, @_) } | ||||||||||||||||||||||||||
|
Subroutine
upload: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 52 | ▶ | sub uploads { [@{shift->_formdata->{uploads}}] } | ||||||||||||||||||||||||||
|
Subroutine
uploads: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 131 | ▶ | sub _build { | ||||||||||||||||||||||||||
|
Subroutine
_build: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 201 | 131 | my ($self, $method) = @_; | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 131 | my ($buffer, $offset) = ('', 0); | |||||||||||||||||||||||||||
| 204 | 131 | while (1) { | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | # No chunk yet, try again | ||||||||||||||||||||||||||||
| 207 | 278 | ▶ | next unless defined(my $chunk = $self->$method($offset)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | # End of part | ||||||||||||||||||||||||||||
| 210 | 277 | ▶ | last unless my $len = length $chunk; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 146 | $offset += $len; | |||||||||||||||||||||||||||
| 213 | 146 | $buffer .= $chunk; | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 131 | return $buffer; | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 184 | ▶ | sub _cache { | ||||||||||||||||||||||||||
|
Subroutine
_cache: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 220 | 184 | my ($self, $method, $all, $name) = @_; | |||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | # Cache objects by name | ||||||||||||||||||||||||||||
| 223 | 184 | ▶ | unless ($self->{$method}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 224 | 74 | $self->{$method} = {}; | |||||||||||||||||||||||||||
| 225 | 74 | push @{$self->{$method}{$_->name}}, $_ for @{$self->$method}; | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 184 | ▶ | my $objects = $self->{$method}{$name} // []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{$method}{$name} // []
MC/DC100%1/1
$self->{$method}{$name} // []
$self->{$method}{$name}
Truth table100%2/2
A
A= $self->{$method}{$name}
| |||||||||||||||||||||||||||||
| 229 | 184 | ▶ | return $all ? $objects : $objects->[-1]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 16 | ▶ | sub _limit { ++$_[0]{limit} and return $_[0]->error({message => $_[1]}) } | ||||||||||||||||||||||||||
|
Subroutine
_limit: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 68 | ▶ | sub _formdata { | ||||||||||||||||||||||||||
|
Subroutine
_formdata: called CC=16 SCAR=28.4
| |||||||||||||||||||||||||||||
| 235 | 68 | my $self = shift; | |||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | 68 | ▶ | return $self->{formdata} if $self->{formdata}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | 45 | my $formdata = $self->{formdata} = {params => [], uploads => []}; | |||||||||||||||||||||||||||
| 239 | 45 | my $content = $self->content; | |||||||||||||||||||||||||||
| 240 | 45 | ▶ | return $formdata unless $content->is_multipart; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 241 | 24 | ▶ | my $charset = $content->charset || $self->default_charset; | ||||||||||||||||||||||||||
|
Condition66%2/3
$content->charset || $self->default_charset
MC/DC0%0/2
$content->charset || $self->default_charset
$content->charset
$self->default_charset
Truth table66%2/3
A || B
A= $content->charsetB= $self->default_charset
| |||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | # Check all parts recursively | ||||||||||||||||||||||||||||
| 244 | 24 | my @parts = ($content); | |||||||||||||||||||||||||||
| 245 | 24 | ▶ | while (my $part = shift @parts) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 87 | ▶ | if ($part->is_multipart) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 248 | 24 | unshift @parts, @{$part->parts}; | |||||||||||||||||||||||||||
| 249 | 24 | next; | |||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | 63 | ▶ | next unless my $disposition = $part->headers->content_disposition; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 253 | 63 | my ($filename) = $disposition =~ /[; ]filename="((?:\\"|[^"])*)"/; | |||||||||||||||||||||||||||
| 254 | 63 | my ($name) = $disposition =~ /[; ]name="((?:\\"|[^;"])*)"/; | |||||||||||||||||||||||||||
| 255 | 63 | ▶ | next if !defined $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | 62 | ▶ | if ($charset) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 258 | 62 | ▶ | $name = decode($charset, $name) // $name if $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
decode($charset, $name) // $name
MC/DC0%0/2
decode($charset, $name) // $name
decode($charset, $name)
$name
Truth table33%1/3
A // B
A= decode($charset, $name)B= $name
| |||||||||||||||||||||||||||||
| 259 | 62 | ▶ | $filename = decode($charset, $filename) // $filename if $filename; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
decode($charset, $filename) // $filename
MC/DC0%0/2
decode($charset, $filename) // $filename
decode($charset, $filename)
$filename
Truth table33%1/3
A // B
A= decode($charset, $filename)B= $filename
| |||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | # Upload | ||||||||||||||||||||||||||||
| 263 | 62 | ▶ | if (defined $filename) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | 17 | push @{$formdata->{uploads}}, | |||||||||||||||||||||||||||
| 265 | Mojo::Upload->new(name => $name, filename => $filename, asset => $part->asset, headers => $part->headers); | ||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | # Parameter | ||||||||||||||||||||||||||||
| 269 | else { | ||||||||||||||||||||||||||||
| 270 | 45 | my $value = $part->asset->slurp; | |||||||||||||||||||||||||||
| 271 | 45 | ▶ | $value = decode($charset, $value) // $value if $charset; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
decode($charset, $value) // $value
MC/DC0%0/2
decode($charset, $value) // $value
decode($charset, $value)
$value
Truth table33%1/3
A // B
A= decode($charset, $value)B= $value
| |||||||||||||||||||||||||||||
| 272 | 45 | push @{$formdata->{params}}, [$name, $value]; | |||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 24 | return $formdata; | |||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | 29 | 1; | |||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | =encoding utf8 | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | =head1 NAME | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | Mojo::Message - HTTP message base class | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | package Mojo::Message::MyMessage; | ||||||||||||||||||||||||||||
| 290 | use Mojo::Base 'Mojo::Message'; | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | sub cookies {...} | ||||||||||||||||||||||||||||
| 293 | sub extract_start_line {...} | ||||||||||||||||||||||||||||
| 294 | sub get_start_line_chunk {...} | ||||||||||||||||||||||||||||
| 295 | sub start_line_size {...} | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | L<Mojo::Message> is an abstract base class for HTTP message containers, based on L<RFC | ||||||||||||||||||||||||||||
| 300 | 7230|https://tools.ietf.org/html/rfc7230>, L<RFC 7231|https://tools.ietf.org/html/rfc7231> and L<RFC | ||||||||||||||||||||||||||||
| 301 | 2388|https://tools.ietf.org/html/rfc2388>, like L<Mojo::Message::Request> and L<Mojo::Message::Response>. | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | =head1 EVENTS | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | L<Mojo::Message> inherits all events from L<Mojo::EventEmitter> and can emit the following new ones. | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | =head2 finish | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | $msg->on(finish => sub ($msg) {...}); | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | Emitted after message building or parsing is finished. | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | my $before = time; | ||||||||||||||||||||||||||||
| 314 | $msg->on(finish => sub ($msg) { $msg->headers->header('X-Parser-Time' => time - $before) }); | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | =head2 progress | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | $msg->on(progress => sub ($msg) {...}); | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | Emitted when message building or parsing makes progress. | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | # Building | ||||||||||||||||||||||||||||
| 323 | $msg->on(progress => sub ($msg, $state, $offset) { say qq{Building "$state" at offset $offset} }); | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | # Parsing | ||||||||||||||||||||||||||||
| 326 | $msg->on(progress => sub ($msg) { | ||||||||||||||||||||||||||||
| 327 | return unless my $len = $msg->headers->content_length; | ||||||||||||||||||||||||||||
| 328 | my $size = $msg->content->progress; | ||||||||||||||||||||||||||||
| 329 | say 'Progress: ', $size == $len ? 100 : int($size / ($len / 100)), '%'; | ||||||||||||||||||||||||||||
| 330 | }); | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | L<Mojo::Message> implements the following attributes. | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | =head2 content | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | my $msg = $msg->content; | ||||||||||||||||||||||||||||
| 339 | $msg = $msg->content(Mojo::Content::Single->new); | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | Message content, defaults to a L<Mojo::Content::Single> object. | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | =head2 default_charset | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | my $charset = $msg->default_charset; | ||||||||||||||||||||||||||||
| 346 | $msg = $msg->default_charset('UTF-8'); | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | Default charset used by L</"text"> and to extract data from C<application/x-www-form-urlencoded> or | ||||||||||||||||||||||||||||
| 349 | C<multipart/form-data> message body, defaults to C<UTF-8>. | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | =head2 max_line_size | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | my $size = $msg->max_line_size; | ||||||||||||||||||||||||||||
| 354 | $msg = $msg->max_line_size(1024); | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | Maximum start-line size in bytes, defaults to the value of the C<MOJO_MAX_LINE_SIZE> environment variable or C<8192> | ||||||||||||||||||||||||||||
| 357 | (8KiB). | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | =head2 max_message_size | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | my $size = $msg->max_message_size; | ||||||||||||||||||||||||||||
| 362 | $msg = $msg->max_message_size(1024); | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | Maximum message size in bytes, defaults to the value of the C<MOJO_MAX_MESSAGE_SIZE> environment variable or | ||||||||||||||||||||||||||||
| 365 | C<16777216> (16MiB). Setting the value to C<0> will allow messages of indefinite size. | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | =head2 version | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | my $version = $msg->version; | ||||||||||||||||||||||||||||
| 370 | $msg = $msg->version('1.1'); | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | HTTP version of message, defaults to C<1.1>. | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | =head1 METHODS | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | L<Mojo::Message> inherits all methods from L<Mojo::EventEmitter> and implements the following new ones. | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | =head2 body | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | my $bytes = $msg->body; | ||||||||||||||||||||||||||||
| 381 | $msg = $msg->body('Hello!'); | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | Slurp or replace L</"content">. | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | =head2 body_params | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | my $params = $msg->body_params; | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | C<POST> parameters extracted from C<application/x-www-form-urlencoded> or C<multipart/form-data> message body, usually | ||||||||||||||||||||||||||||
| 390 | a L<Mojo::Parameters> object. Note that this method caches all data, so it should not be called before the entire | ||||||||||||||||||||||||||||
| 391 | message body has been received. Parts of the message body need to be loaded into memory to parse C<POST> parameters, so | ||||||||||||||||||||||||||||
| 392 | you have to make sure it is not excessively large. There's a 16MiB limit for requests and a 2GiB limit for responses by | ||||||||||||||||||||||||||||
| 393 | default. | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | # Get POST parameter names and values | ||||||||||||||||||||||||||||
| 396 | my $hash = $msg->body_params->to_hash; | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | =head2 body_size | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | my $size = $msg->body_size; | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | Content size in bytes. | ||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | =head2 build_body | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | my $bytes = $msg->build_body; | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | Render whole body with L</"get_body_chunk">. | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | =head2 build_headers | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | my $bytes = $msg->build_headers; | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | Render all headers with L</"get_header_chunk">. | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | =head2 build_start_line | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | my $bytes = $msg->build_start_line; | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | Render start-line with L</"get_start_line_chunk">. | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | =head2 cookie | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | my $cookie = $msg->cookie('foo'); | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | Access message cookies, usually L<Mojo::Cookie::Request> or L<Mojo::Cookie::Response> objects. If there are multiple | ||||||||||||||||||||||||||||
| 427 | cookies sharing the same name, and you want to access more than just the last one, you can use L</"every_cookie">. Note | ||||||||||||||||||||||||||||
| 428 | that this method caches all data, so it should not be called before all headers have been received. | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | # Get cookie value | ||||||||||||||||||||||||||||
| 431 | say $msg->cookie('foo')->value; | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | =head2 cookies | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | my $cookies = $msg->cookies; | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | Access message cookies. Meant to be overloaded in a subclass. | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | =head2 dom | ||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | my $dom = $msg->dom; | ||||||||||||||||||||||||||||
| 442 | my $collection = $msg->dom('a[href]'); | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | Retrieve message body from L</"text"> and turn it into a L<Mojo::DOM> object, an optional selector can be used to call | ||||||||||||||||||||||||||||
| 445 | the method L<Mojo::DOM/"find"> on it right away, which then returns a L<Mojo::Collection> object. Note that this method | ||||||||||||||||||||||||||||
| 446 | caches all data, so it should not be called before the entire message body has been received. The whole message body | ||||||||||||||||||||||||||||
| 447 | needs to be loaded into memory to parse it, so you have to make sure it is not excessively large. There's a 16MiB limit | ||||||||||||||||||||||||||||
| 448 | for requests and a 2GiB limit for responses by default. | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | # Perform "find" right away | ||||||||||||||||||||||||||||
| 451 | say $msg->dom('h1, h2, h3')->map('text')->join("\n"); | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | # Use everything else Mojo::DOM has to offer | ||||||||||||||||||||||||||||
| 454 | say $msg->dom->at('title')->text; | ||||||||||||||||||||||||||||
| 455 | say $msg->dom->at('body')->children->map('tag')->uniq->join("\n"); | ||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | =head2 error | ||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | my $err = $msg->error; | ||||||||||||||||||||||||||||
| 460 | $msg = $msg->error({message => 'Parser error'}); | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | Get or set message error, an C<undef> return value indicates that there is no error. | ||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | # Connection or parser error | ||||||||||||||||||||||||||||
| 465 | $msg->error({message => 'Connection refused'}); | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | # 4xx/5xx response | ||||||||||||||||||||||||||||
| 468 | $msg->error({message => 'Internal Server Error', code => 500}); | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | =head2 every_cookie | ||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||
| 472 | my $cookies = $msg->every_cookie('foo'); | ||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | Similar to L</"cookie">, but returns all message cookies sharing the same name as an array reference. | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | # Get first cookie value | ||||||||||||||||||||||||||||
| 477 | say $msg->every_cookie('foo')->[0]->value; | ||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||
| 479 | =head2 every_upload | ||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||
| 481 | my $uploads = $msg->every_upload('foo'); | ||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | Similar to L</"upload">, but returns all file uploads sharing the same name as an array reference. | ||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | # Get content of first uploaded file | ||||||||||||||||||||||||||||
| 486 | say $msg->every_upload('foo')->[0]->asset->slurp; | ||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||
| 488 | =head2 extract_start_line | ||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | my $bool = $msg->extract_start_line(\$str); | ||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||
| 492 | Extract start-line from string. Meant to be overloaded in a subclass. | ||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||
| 494 | =head2 finish | ||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||
| 496 | $msg = $msg->finish; | ||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||
| 498 | Finish message parser/generator. | ||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||
| 500 | =head2 fix_headers | ||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||
| 502 | $msg = $msg->fix_headers; | ||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||
| 504 | Make sure message has all required headers. | ||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | =head2 get_body_chunk | ||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||
| 508 | my $bytes = $msg->get_body_chunk($offset); | ||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||
| 510 | Get a chunk of body data starting from a specific position. Note that it might not be possible to get the same chunk | ||||||||||||||||||||||||||||
| 511 | twice if content was generated dynamically. | ||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||
| 513 | =head2 get_header_chunk | ||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | my $bytes = $msg->get_header_chunk($offset); | ||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||
| 517 | Get a chunk of header data, starting from a specific position. Note that this method finalizes the message. | ||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||
| 519 | =head2 get_start_line_chunk | ||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||
| 521 | my $bytes = $msg->get_start_line_chunk($offset); | ||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||
| 523 | Get a chunk of start-line data starting from a specific position. Meant to be overloaded in a subclass. | ||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||
| 525 | =head2 header_size | ||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||
| 527 | my $size = $msg->header_size; | ||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||
| 529 | Size of headers in bytes. Note that this method finalizes the message. | ||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||
| 531 | =head2 headers | ||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||
| 533 | my $headers = $msg->headers; | ||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||
| 535 | Message headers, usually a L<Mojo::Headers> object. | ||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||
| 537 | # Longer version | ||||||||||||||||||||||||||||
| 538 | my $headers = $msg->content->headers; | ||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||
| 540 | =head2 is_finished | ||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||
| 542 | my $bool = $msg->is_finished; | ||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||
| 544 | Check if message parser/generator is finished. | ||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||
| 546 | =head2 is_limit_exceeded | ||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||
| 548 | my $bool = $msg->is_limit_exceeded; | ||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||
| 550 | Check if message has exceeded L</"max_line_size">, L</"max_message_size">, L<Mojo::Content/"max_buffer_size"> or | ||||||||||||||||||||||||||||
| 551 | L<Mojo::Headers/"max_line_size">. | ||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||
| 553 | =head2 json | ||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||
| 555 | my $value = $msg->json; | ||||||||||||||||||||||||||||
| 556 | my $value = $msg->json('/foo/bar'); | ||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||
| 558 | Decode JSON message body directly using L<Mojo::JSON> if possible, an C<undef> return value indicates a bare C<null> or | ||||||||||||||||||||||||||||
| 559 | that decoding failed. An optional JSON Pointer can be used to extract a specific value with L<Mojo::JSON::Pointer>. | ||||||||||||||||||||||||||||
| 560 | Note that this method caches all data, so it should not be called before the entire message body has been received. The | ||||||||||||||||||||||||||||
| 561 | whole message body needs to be loaded into memory to parse it, so you have to make sure it is not excessively large. | ||||||||||||||||||||||||||||
| 562 | There's a 16MiB limit for requests and a 2GiB limit for responses by default. | ||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||
| 564 | # Extract JSON values | ||||||||||||||||||||||||||||
| 565 | say $msg->json->{foo}{bar}[23]; | ||||||||||||||||||||||||||||
| 566 | say $msg->json('/foo/bar/23'); | ||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||
| 568 | =head2 parse | ||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||
| 570 | $msg = $msg->parse('HTTP/1.1 200 OK...'); | ||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||
| 572 | Parse message chunk. | ||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||
| 574 | =head2 save_to | ||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||
| 576 | $msg = $msg->save_to('/some/path/index.html'); | ||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||
| 578 | Save message body to a file. | ||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||
| 580 | =head2 start_line_size | ||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||
| 582 | my $size = $msg->start_line_size; | ||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||
| 584 | Size of the start-line in bytes. Meant to be overloaded in a subclass. | ||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||
| 586 | =head2 text | ||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||
| 588 | my $str = $msg->text; | ||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||
| 590 | Retrieve L</"body"> and try to decode it with L<Mojo::Content/"charset"> or L</"default_charset">. | ||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||
| 592 | =head2 to_string | ||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||
| 594 | my $str = $msg->to_string; | ||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||
| 596 | Render whole message. Note that this method finalizes the message, and that it might not be possible to render the same | ||||||||||||||||||||||||||||
| 597 | message twice if content was generated dynamically. | ||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||
| 599 | =head2 upload | ||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||
| 601 | my $upload = $msg->upload('foo'); | ||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||
| 603 | Access C<multipart/form-data> file uploads, usually L<Mojo::Upload> objects. If there are multiple uploads sharing the | ||||||||||||||||||||||||||||
| 604 | same name, and you want to access more than just the last one, you can use L</"every_upload">. Note that this method | ||||||||||||||||||||||||||||
| 605 | caches all data, so it should not be called before the entire message body has been received. | ||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||
| 607 | # Get content of uploaded file | ||||||||||||||||||||||||||||
| 608 | say $msg->upload('foo')->asset->slurp; | ||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||
| 610 | =head2 uploads | ||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||
| 612 | my $uploads = $msg->uploads; | ||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||
| 614 | All C<multipart/form-data> file uploads, usually L<Mojo::Upload> objects. | ||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||
| 616 | # Names of all uploads | ||||||||||||||||||||||||||||
| 617 | say $_->name for @{$msg->uploads}; | ||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||
| 619 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||
| 621 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||
| 623 | =cut | ||||||||||||||||||||||||||||