Mojo/Message/Request.pm

filter: Statementstmt 98.5% 133 / 135 Branchbran 92.2% 83 / 90 Conditioncond 70.4% 43 / 61 MC/DCmcdc 56.2% 18 / 32 Subroutinesub 92.3% 24 / 26 Podpod 100% 14 / 14 totaltotal 87.9% 315 / 358 CC 70 SCAR 23.6
CC
70
Cov
91%
_parse_env
20.7
parse
15.3
fix_headers
13.0
CRAP 10.6
« Mojolicious/Plugin/TagHelpers.pm ↑ Summary Mojo/UserAgent/Transactor.pm »
1 package Mojo::Message::Request;
2 28 ▶use Mojo::Base 'Mojo::Message';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 28 ▶use Digest::SHA qw(sha1_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 28 ▶use Mojo::Cookie::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 28 ▶use Mojo::Util qw(b64_encode b64_decode sha1_sum url_unescape);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 28 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 0 ▶has env => sub { {} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
10 28 has method => 'GET';
11 28 has [qw(proxy reverse_proxy)];
12 164 ▶has request_id => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
13 164 state $seed = $$ . time . rand;
14 164 state $counter = int rand 0xffffff;
15 164 my $b64 = substr(sha1_base64($seed . ($counter = ($counter + 1) % 0xffffff)), 0, 12);
16 164 $b64 =~ tr!+/!-_!;
17 164 return $b64;
18 28 };
19 163 ▶has trusted_proxies => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
20 600 ▶has url => sub { Mojo::URL->new };
Subroutine __ANON__: called CC=1 SCAR=0.0
21 28 has via_proxy => 1;
22  
23 19 ▶sub clone {
Subroutine clone: called CC=3 SCAR=11.0
Pod documented
24 19 my $self = shift;
25  
26 # Dynamic requests cannot be cloned
27 19 ▶ return undef unless my $content = $self->content->clone;
Branch: 1 branch
BranchTrueFalseTotal
unless my $content = $self->content->clone 4 15 19
28 15 my $clone
29 = $self->new(content => $content, method => $self->method, url => $self->url->clone, version => $self->version);
30 15 ▶ $clone->{proxy} = $self->{proxy}->clone if $self->{proxy};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"proxy"} 1 14 15
31  
32 15 return $clone;
33 }
34  
35 54 ▶sub cookies {
Subroutine cookies: called CC=4 SCAR=13.9
Pod documented
36 54 my $self = shift;
37  
38 # Parse cookies
39 54 my $headers = $self->headers;
40 54 ▶ return [map { @{Mojo::Cookie::Request->parse($_)} } $headers->cookie] unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 40 14 54
41  
42 # Add cookies
43 14 ▶ my @cookies = map { ref $_ eq 'HASH' ? Mojo::Cookie::Request->new($_) : $_ } $headers->cookie || (), @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 6 18 24
Condition66%2/3
$headers->cookie || ()
l!l&&r!l&&!r
1013
MC/DC50%1/2
$headers->cookie || ()
$headers->cookie ()
Truth table66%2/3
A || B
A= $headers->cookieB= ()
ABresult
1X1
011
000
44 14 $headers->cookie(join '; ', @cookies);
45  
46 14 return $self;
47 }
48  
49 59 ▶sub every_param { shift->params->every_param(@_) }
Subroutine every_param: called CC=1 SCAR=0.0
Pod documented
50  
51 275 ▶sub extract_start_line {
Subroutine extract_start_line: called CC=5 SCAR=16.1
Pod documented
52 275 my ($self, $bufref) = @_;
53  
54 # Ignore any leading empty lines
55 275 ▶ return undef unless $$bufref =~ s/^\s*(.*?)\x0d?\x0a//;
Branch: 1 branch
BranchTrueFalseTotal
unless $$bufref =~ s/^\s*(.*?)\x0d?\x0a//u 25 250 275
56  
57 # We have a (hopefully) full request-line
58 250 ▶ return !$self->error({message => 'Bad request start-line'}) unless $1 =~ /^(\S+)\s+(\S+)\s+HTTP\/(\d\.\d)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $1 =~ m[^(\S+)\s+(\S+)\s+HTTP/(\d\.\d)$]u 1 249 250
59 249 my $url = $self->method($1)->version($3)->url;
60 249 my $target = $2;
61 249 ▶ return !!$url->host_port($target) if $1 eq 'CONNECT';
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq "CONNECT" 2 247 249
62 247 ▶ return !!$url->parse($target)->fragment(undef) if $target =~ /^[^:\/?#]+:/;
Branch: 1 branch
BranchTrueFalseTotal
if $target =~ m[^[^:/?#]+:]u 5 242 247
63 242 return !!$url->path_query($target);
64 }
65  
66 390 ▶sub fix_headers {
Subroutine fix_headers: called CC=13 SCAR=25.7
Pod documented
67 390 my $self = shift;
68 390 ▶ $self->{fix} ? return $self : $self->SUPER::fix_headers(@_);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'fix'} ? : 194 196 390
69  
70 # Empty
71 196 my $headers = $self->headers;
72 196 ▶ $headers->remove('Content-Length') if ($headers->content_length // '') eq '0' && $self->method eq 'GET';
Branch: 1 branch
BranchTrueFalseTotal
if ($headers->content_length // "") eq 0 and $self->method eq "GET" 156 40 196
Condition100%5/5
($headers->content_length // "") eq 0 and $self->method eq "GET"
!ll&&!rl&&r
2713156
$headers->content_length // ""
l!l
1942
MC/DC100%2/2
($headers->content_length // "") eq 0 and $self->method eq "GET"
$headers->content_length $self->method eq "GET"
Truth table100%3/3
A and B
A= $headers->content_lengthB= $self->method eq "GET"
ABresult
0X0
100
111
73  
74 # Host
75 196 my $url = $self->url;
76 196 ▶ $headers->host($url->host_port // '') unless defined $headers->host;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $headers->host 190 6 196
Condition100%2/2
$url->host_port // ""
l!l
1891
MC/DC100%1/1
$url->host_port // ""
$url->host_port
Truth table100%2/2
A
A= $url->host_port
Aresult
11
00
77  
78 # Basic authentication
79 196 ▶ if ((my $info = $url->userinfo) && !$headers->authorization) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $info = $url->userinfo and not $headers->authorization) 5 191 196
Condition66%2/3
my $info = $url->userinfo and not $headers->authorization
!ll&&!rl&&r
19105
MC/DC50%1/2
my $info = $url->userinfo and not $headers->authorization
my $info = $url->userinfo not $headers->authorization
Truth table66%2/3
A and not(B)
A= my $info = $url->userinfoB= not $headers->authorization
ABresult
0X0
100
111
80 5 $headers->authorization('Basic ' . b64_encode($info, ''));
81 }
82  
83 # Basic proxy authentication
84 196 ▶ return $self unless (my $proxy = $self->proxy) && $self->via_proxy;
Branch: 1 branch
BranchTrueFalseTotal
unless my $proxy = $self->proxy and $self->via_proxy 187 9 196
Condition100%3/3
my $proxy = $self->proxy and $self->via_proxy
!ll&&!rl&&r
18619
MC/DC100%2/2
my $proxy = $self->proxy and $self->via_proxy
my $proxy = $self->proxy $self->via_proxy
Truth table100%3/3
A and B
A= my $proxy = $self->proxyB= $self->via_proxy
ABresult
0X0
100
111
85 9 ▶ return $self unless my $info = $proxy->userinfo;
Branch: 1 branch
BranchTrueFalseTotal
unless my $info = $proxy->userinfo 3 6 9
86 6 ▶ $headers->proxy_authorization('Basic ' . b64_encode($info, '')) unless $headers->proxy_authorization;
Branch: 1 branch
BranchTrueFalseTotal
unless $headers->proxy_authorization 6 0 6
87 6 return $self;
88 }
89  
90 226 ▶sub get_start_line_chunk {
Subroutine get_start_line_chunk: called CC=1 SCAR=0.0
Pod documented
91 226 my ($self, $offset) = @_;
92 226 $self->_start_line->emit(progress => 'start_line', $offset);
93 226 return substr $self->{start_buffer}, $offset, 131072;
94 }
95  
96 851 ▶sub is_handshake { lc($_[0]->headers->upgrade // '') eq 'websocket' }
Subroutine is_handshake: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
$_[0]->headers->upgrade // ""
l!l
15836
MC/DC100%1/1
$_[0]->headers->upgrade // ""
$_[0]->headers->upgrade
Truth table100%2/2
A
A= $_[0]->headers->upgrade
Aresult
11
00
97  
98 3 ▶sub is_secure {
Subroutine is_secure: called CC=2 SCAR=8.1
Pod documented
99 3 my $url = shift->url;
100 3 ▶ return ($url->protocol || $url->base->protocol) eq 'https';
Condition33%1/3
$url->protocol || $url->base->protocol
l!l&&r!l&&!r
030
MC/DC0%0/2
$url->protocol || $url->base->protocol
$url->protocol $url->base->protocol
Truth table33%1/3
A || B
A= $url->protocolB= $url->base->protocol
ABresult
1X1
011
000
101 }
102  
103 0 ▶sub is_xhr { (shift->headers->header('X-Requested-With') // '') =~ /XMLHttpRequest/i }
Subroutine is_xhr: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
(shift())->headers->header("X-Requested-With") // ""
l!l
00
MC/DC0%0/1
(shift())->headers->header("X-Requested-With") // ""
(shift())->headers->header("X-Requested-With")
Truth table0%0/2
A
A= (shift())->headers->header("X-Requested-With")
Aresult
11
00
104  
105 57 ▶sub param { shift->params->param(@_) }
Subroutine param: called CC=1 SCAR=0.0
Pod documented
106  
107 141 ▶sub params { $_[0]->{params} ||= $_[0]->body_params->clone->append($_[0]->query_params) }
Subroutine params: called CC=2 SCAR=7.2
Pod documented
Condition66%2/3
$_[0]{"params"} ||= $_[0]->body_params->clone->append($_[0]->query_params)
l!l&&r!l&&!r
95460
MC/DC0%0/2
$_[0]{"params"} ||= $_[0]->body_params->clone->append($_[0]->query_params)
$_[0]{"params"} $_[0]->body_params->clone->append($_[0]->query_params)
Truth table66%2/3
A ||= B
A= $_[0]{"params"}B= $_[0]->body_params->clone->append($_[0]->query_params)
ABresult
1X1
011
000
108  
109 653 ▶sub parse {
Subroutine parse: called CC=15 SCAR=27.2
Pod documented
110 653 ▶ my ($self, $env, $chunk) = (shift, ref $_[0] ? (shift, '') : (undef, shift));
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 19 634 653
111  
112 # Parse CGI environment
113 653 ▶ $self->env($env)->_parse_env($env) if $env;
Branch: 1 branch
BranchTrueFalseTotal
if $env 19 634 653
114  
115 # Parse normal message
116 653 ▶ if (($self->{state} // '') ne 'cgi') { $self->SUPER::parse($chunk) }
Branch: 1 branch
BranchTrueFalseTotal
if (($self->{'state'} // '') ne 'cgi') { } 613 40 653
Condition100%2/2
$self->{"state"} // ""
l!l
375278
MC/DC100%1/1
$self->{"state"} // ""
$self->{"state"}
Truth table100%2/2
A
A= $self->{"state"}
Aresult
11
00
117  
118 # Parse CGI content
119 else {
120 40 ▶ $self->{raw_size} += length $chunk unless defined $env;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $env 21 19 40
121 40 $self->content($self->content->parse_body($chunk))->SUPER::parse('');
122 }
123  
124 # Check if we can fix things that require all headers
125 653 ▶ return $self unless $self->is_finished;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_finished 361 292 653
126  
127 # Base URL
128 292 my $base = $self->url->base;
129 292 ▶ $base->scheme('http') unless $base->scheme;
Branch: 1 branch
BranchTrueFalseTotal
unless $base->scheme 254 38 292
130 292 my $headers = $self->headers;
131 292 ▶ if (!$base->host && (my $host = $headers->host)) { $base->host_port($host) }
Branch: 1 branch
BranchTrueFalseTotal
if (not $base->host and my $host = $headers->host) 204 88 292
Condition100%3/3
not $base->host and my $host = $headers->host
!ll&&!rl&&r
2068204
MC/DC100%2/2
not $base->host and my $host = $headers->host
not $base->host my $host = $headers->host
Truth table100%3/3
not(A) and B
A= not $base->hostB= my $host = $headers->host
ABresult
0X0
100
111
132  
133 # Basic authentication
134 292 ▶ if (my $basic = _basic($headers->authorization)) { $base->userinfo($basic) }
Branch: 1 branch
BranchTrueFalseTotal
if (my $basic = _basic($headers->authorization)) 6 286 292
135  
136 # Basic proxy authentication
137 292 my $basic = _basic($headers->proxy_authorization);
138 292 ▶ $self->proxy(Mojo::URL->new->userinfo($basic)) if $basic;
Branch: 1 branch
BranchTrueFalseTotal
if $basic 7 285 292
139  
140 # "X-Forwarded-Proto"
141 292 ▶ $base->scheme('https') if $self->reverse_proxy && ($headers->header('X-Forwarded-Proto') // '') eq 'https';
Branch: 1 branch
BranchTrueFalseTotal
if $self->reverse_proxy and ($headers->header("X-Forwarded-Proto") // "") eq "https" 0 292 292
Condition20%1/5
$self->reverse_proxy and ($headers->header("X-Forwarded-Proto") // "") eq "https"
!ll&&!rl&&r
29200
$headers->header("X-Forwarded-Proto") // ""
l!l
00
MC/DC0%0/2
$self->reverse_proxy and ($headers->header("X-Forwarded-Proto") // "") eq "https"
$self->reverse_proxy $headers->header("X-Forwarded-Proto")
Truth table33%1/3
A and B
A= $self->reverse_proxyB= $headers->header("X-Forwarded-Proto")
ABresult
0X0
100
111
142  
143 292 return $self;
144 }
145  
146 55 ▶sub query_params { shift->url->query }
Subroutine query_params: called CC=1 SCAR=0.0
Pod documented
147  
148 160 ▶sub start_line_size { length shift->_start_line->{start_buffer} }
Subroutine start_line_size: called CC=1 SCAR=0.0
Pod documented
149  
150 584 ▶sub _basic { $_[0] && $_[0] =~ /Basic (.+)$/ ? b64_decode $1 : undef }
Subroutine _basic: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
$_[0] && $_[0] =~ /Basic (.+)$/u ? : 13 571 584
Condition66%2/3
$_[0] and $_[0] =~ /Basic (.+)$/u
!ll&&!rl&&r
571013
MC/DC50%1/2
$_[0] and $_[0] =~ /Basic (.+)$/u
$_[0] $_[0] =~ /Basic (.+)$/u
Truth table66%2/3
A and B
A= $_[0]B= $_[0] =~ /Basic (.+)$/u
ABresult
0X0
100
111
151  
152 19 ▶sub _parse_env {
Subroutine _parse_env: called CC=20 SCAR=30.3
153 19 my ($self, $env) = @_;
154  
155 # Bypass normal message parser
156 19 $self->{state} = 'cgi';
157  
158 # Extract headers
159 19 my $headers = $self->headers;
160 19 my $url = $self->url;
161 19 my $base = $url->base;
162 19 for my $name (keys %$env) {
163 183 my $value = $env->{$name};
164 183 ▶ next unless $name =~ s/^HTTP_//i;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ s/^HTTP_//iu 148 35 183
165 35 $name =~ y/_/-/;
166 35 $headers->header($name => $value);
167  
168 # Host/Port
169 35 ▶ $value =~ s/:(\d+)$// ? $base->host($value)->port($1) : $base->host($value) if $name eq 'HOST';
Branch: 2 branches
BranchTrueFalseTotal
if $name eq "HOST" 19 16 35
$value =~ s/:(\d+)$//u ? : 10 9 19
170 }
171  
172 # Content-Type is a special case on some servers
173 19 ▶ $headers->content_type($env->{CONTENT_TYPE}) if $env->{CONTENT_TYPE};
Branch: 1 branch
BranchTrueFalseTotal
if $env->{"CONTENT_TYPE"} 12 7 19
174  
175 # Content-Length is a special case on some servers
176 19 ▶ $headers->content_length($env->{CONTENT_LENGTH}) if $env->{CONTENT_LENGTH};
Branch: 1 branch
BranchTrueFalseTotal
if $env->{"CONTENT_LENGTH"} 12 7 19
177  
178 # Query
179 19 ▶ $url->query->parse($env->{QUERY_STRING}) if length $env->{QUERY_STRING};
Branch: 1 branch
BranchTrueFalseTotal
if length $env->{"QUERY_STRING"} 7 12 19
180  
181 # Method
182 19 ▶ $self->method($env->{REQUEST_METHOD}) if defined $env->{REQUEST_METHOD};
Branch: 1 branch
BranchTrueFalseTotal
if defined $env->{"REQUEST_METHOD"} 19 0 19
183  
184 # Scheme/Version
185 19 ▶ $base->scheme($1) and $self->version($2) if ($env->{SERVER_PROTOCOL} // '') =~ m!^([^/]+)/([^/]+)$!;
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"SERVER_PROTOCOL"} // "") =~ m[^([^/]+)/([^/]+)$]u 19 0 19
Condition37%3/8
($env->{'SERVER_PROTOCOL'} // '') =~ m[^([^/]+)/([^/]+)$]u && ($base->scheme($1) && $self->version($2))
!ll&&!rl&&r
0190
$env->{"SERVER_PROTOCOL"} // ""
l!l
190
$base->scheme($1) and $self->version($2)
!ll&&!rl&&r
0190
MC/DC0%0/3
($env->{'SERVER_PROTOCOL'} // '') =~ m[^([^/]+)/([^/]+)$]u && ($base->scheme($1) && $self->version($2))
$env->{"SERVER_PROTOCOL"} $base->scheme($1) $self->version($2)
Truth table0%0/4
A && B and C
A= $env->{"SERVER_PROTOCOL"}B= $base->scheme($1)C= $self->version($2)
ABCresult
0XX0
10X0
1100
1111
186  
187 # HTTPS
188 19 ▶ $base->scheme('https') if uc($env->{HTTPS} // '') eq 'ON';
Branch: 1 branch
BranchTrueFalseTotal
if uc($env->{"HTTPS"} // "") eq "ON" 1 18 19
Condition100%2/2
$env->{"HTTPS"} // ""
l!l
217
MC/DC100%1/1
$env->{"HTTPS"} // ""
$env->{"HTTPS"}
Truth table100%2/2
A
A= $env->{"HTTPS"}
Aresult
11
00
189  
190 # Path
191 19 ▶ my $path = $url->path->parse($env->{PATH_INFO} ? $env->{PATH_INFO} : '');
Branch: 1 branch
BranchTrueFalseTotal
$env->{'PATH_INFO'} ? : 15 4 19
192  
193 # Base path
194 19 ▶ if (my $value = $env->{SCRIPT_NAME}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $value = $env->{"SCRIPT_NAME"}) 17 2 19
195  
196 # Make sure there is a trailing slash (important for merging)
197 17 ▶ $base->path->parse($value =~ m!/$! ? $value : "$value/");
Branch: 1 branch
BranchTrueFalseTotal
$value =~ m[/$]u ? : 0 17 17
198  
199 # Remove SCRIPT_NAME prefix if necessary (unless the server already did it)
200 17 my $buffer = $path->to_string;
201 17 $value =~ s!^/|/$!!g;
202 17 ▶ my $uri = url_unescape(($env->{REQUEST_URI} // '') =~ s/\?.*$//r);
Condition100%2/2
$env->{"REQUEST_URI"} // ""
l!l
413
MC/DC100%1/1
$env->{"REQUEST_URI"} // ""
$env->{"REQUEST_URI"}
Truth table100%2/2
A
A= $env->{"REQUEST_URI"}
Aresult
11
00
203 17 ▶ $buffer =~ s!^/?\Q$value\E(?:/|$)!! unless index($uri, "/$value" . ($env->{PATH_INFO} // '')) == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless index($uri, "/$value" . ($env->{"PATH_INFO"} // "")) == 0 14 3 17
Condition100%2/2
$env->{"PATH_INFO"} // ""
l!l
152
MC/DC100%1/1
$env->{"PATH_INFO"} // ""
$env->{"PATH_INFO"}
Truth table100%2/2
A
A= $env->{"PATH_INFO"}
Aresult
11
00
204 17 $buffer =~ s!^/!!;
205 17 $path->parse($buffer);
206 }
207 }
208  
209 386 ▶sub _start_line {
Subroutine _start_line: called CC=10 SCAR=23.1
210 386 my $self = shift;
211  
212 386 ▶ return $self if defined $self->{start_buffer};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"start_buffer"} 194 192 386
213  
214 # Path
215 192 my $url = $self->url;
216 192 my $path = $url->path_query;
217 192 ▶ $path = "/$path" unless $path =~ m!^/!;
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ m[^/]u 1 191 192
218  
219 # CONNECT
220 192 my $method = uc $self->method;
221 192 ▶ if ($method eq 'CONNECT') {
Branch: 2 branches
BranchTrueFalseTotal
if ($method eq 'CONNECT') { } 1 191 192
elsif ($self->proxy and $self->via_proxy and $url->protocol ne 'https') { } 5 371 376
222 1 ▶ my $port = $url->port // ($url->protocol eq 'https' ? '443' : '80');
Branch: 1 branch
BranchTrueFalseTotal
$url->protocol eq 'https' ? : 0 0 0
Condition50%1/2
$url->port // ($url->protocol eq "https" ? 443 : 80)
l!l
10
MC/DC0%0/1
$url->port // ($url->protocol eq "https" ? 443 : 80)
$url->port
Truth table50%1/2
A
A= $url->port
Aresult
11
00
223 1 $path = $url->ihost . ":$port";
224 }
225  
226 # Proxy
227 ▶ elsif ($self->proxy && $self->via_proxy && $url->protocol ne 'https') {
Condition100%6/6
$self->proxy and $self->via_proxy and $url->protocol ne "https"
!ll&&!rl&&r
18515
$self->proxy and $self->via_proxy
!ll&&!rl&&r
18416
MC/DC100%3/3
$self->proxy and $self->via_proxy and $url->protocol ne "https"
$self->proxy $self->via_proxy $url->protocol ne "https"
Truth table100%4/4
A and B and C
A= $self->proxyB= $self->via_proxyC= $url->protocol ne "https"
ABCresult
0XX0
10X0
1100
1111
228 5 ▶ $path = $url->clone->userinfo(undef) unless $self->is_handshake;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_handshake 4 1 5
229 }
230  
231 192 $self->{start_buffer} = "$method $path HTTP/@{[$self->version]}\x0d\x0a";
232  
233 192 return $self;
234 }
235  
236 28 1;
237  
238 =encoding utf8
239  
240 =head1 NAME
241  
242 Mojo::Message::Request - HTTP request
243  
244 =head1 SYNOPSIS
245  
246 use Mojo::Message::Request;
247  
248 # Parse
249 my $req = Mojo::Message::Request->new;
250 $req->parse("GET /foo HTTP/1.0\x0d\x0a");
251 $req->parse("Content-Length: 12\x0d\x0a");
252 $req->parse("Content-Type: text/plain\x0d\x0a\x0d\x0a");
253 $req->parse('Hello World!');
254 say $req->method;
255 say $req->headers->content_type;
256 say $req->body;
257  
258 # Build
259 my $req = Mojo::Message::Request->new;
260 $req->url->parse('http://127.0.0.1/foo/bar');
261 $req->method('GET');
262 say $req->to_string;
263  
264 =head1 DESCRIPTION
265  
266 L<Mojo::Message::Request> is a container for HTTP requests, based on L<RFC 7230|https://tools.ietf.org/html/rfc7230>,
267 L<RFC 7231|https://tools.ietf.org/html/rfc7231>, L<RFC 7235|https://tools.ietf.org/html/rfc7235> and L<RFC
268 2817|https://tools.ietf.org/html/rfc2817>.
269  
270 =head1 EVENTS
271  
272 L<Mojo::Message::Request> inherits all events from L<Mojo::Message>.
273  
274 =head1 ATTRIBUTES
275  
276 L<Mojo::Message::Request> inherits all attributes from L<Mojo::Message> and implements the following new ones.
277  
278 =head2 env
279  
280 my $env = $req->env;
281 $req = $req->env({PATH_INFO => '/'});
282  
283 Direct access to the C<CGI> or C<PSGI> environment hash if available.
284  
285 # Check CGI version
286 my $version = $req->env->{GATEWAY_INTERFACE};
287  
288 # Check PSGI version
289 my $version = $req->env->{'psgi.version'};
290  
291 =head2 method
292  
293 my $method = $req->method;
294 $req = $req->method('POST');
295  
296 HTTP request method, defaults to C<GET>.
297  
298 =head2 proxy
299  
300 my $url = $req->proxy;
301 $req = $req->proxy(Mojo::URL->new('http://127.0.0.1:3000'));
302  
303 Proxy URL for request.
304  
305 =head2 reverse_proxy
306  
307 my $bool = $req->reverse_proxy;
308 $req = $req->reverse_proxy($bool);
309  
310 Request has been performed through a reverse proxy.
311  
312 =head2 trusted_proxies
313  
314 my $proxies = $req->trusted_proxies;
315 $req = $req->trusted_proxies(['10.0.0.0/8', '127.0.0.1', '172.16.0.0/12', '192.168.0.0/16', 'fc00::/7']);
316  
317 Trusted reverse proxies, addresses or networks in CIDR form.
318  
319 =head2 request_id
320  
321 my $id = $req->request_id;
322 $req = $req->request_id('aee7d5d8');
323  
324 Request ID, defaults to a reasonably unique value.
325  
326 =head2 url
327  
328 my $url = $req->url;
329 $req = $req->url(Mojo::URL->new);
330  
331 HTTP request URL, defaults to a L<Mojo::URL> object.
332  
333 # Get request information
334 my $info = $req->url->to_abs->userinfo;
335 my $host = $req->url->to_abs->host;
336 my $path = $req->url->to_abs->path;
337  
338 =head2 via_proxy
339  
340 my $bool = $req->via_proxy;
341 $req = $req->via_proxy($bool);
342  
343 Request can be performed through a proxy server.
344  
345 =head1 METHODS
346  
347 L<Mojo::Message::Request> inherits all methods from L<Mojo::Message> and implements the following new ones.
348  
349 =head2 clone
350  
351 my $clone = $req->clone;
352  
353 Return a new L<Mojo::Message::Request> object cloned from this request if possible, otherwise return C<undef>.
354  
355 =head2 cookies
356  
357 my $cookies = $req->cookies;
358 $req = $req->cookies(Mojo::Cookie::Request->new);
359 $req = $req->cookies({name => 'foo', value => 'bar'});
360  
361 Access request cookies, usually L<Mojo::Cookie::Request> objects.
362  
363 # Names of all cookies
364 say $_->name for @{$req->cookies};
365  
366 =head2 every_param
367  
368 my $values = $req->every_param('foo');
369  
370 Similar to L</"param">, but returns all values sharing the same name as an array reference.
371  
372 # Get first value
373 say $req->every_param('foo')->[0];
374  
375 =head2 extract_start_line
376  
377 my $bool = $req->extract_start_line(\$str);
378  
379 Extract request-line from string.
380  
381 =head2 fix_headers
382  
383 $req = $req->fix_headers;
384  
385 Make sure request has all required headers.
386  
387 =head2 get_start_line_chunk
388  
389 my $bytes = $req->get_start_line_chunk($offset);
390  
391 Get a chunk of request-line data starting from a specific position. Note that this method finalizes the request.
392  
393 =head2 is_handshake
394  
395 my $bool = $req->is_handshake;
396  
397 Check C<Upgrade> header for C<websocket> value.
398  
399 =head2 is_secure
400  
401 my $bool = $req->is_secure;
402  
403 Check if connection is secure.
404  
405 =head2 is_xhr
406  
407 my $bool = $req->is_xhr;
408  
409 Check C<X-Requested-With> header for C<XMLHttpRequest> value.
410  
411 =head2 param
412  
413 my $value = $req->param('foo');
414  
415 Access C<GET> and C<POST> parameters extracted from the query string and C<application/x-www-form-urlencoded> or
416 C<multipart/form-data> message body. If there are multiple values sharing the same name, and you want to access more
417 than just the last one, you can use L</"every_param">. Note that this method caches all data, so it should not be
418 called before the entire request body has been received. Parts of the request body need to be loaded into memory to
419 parse C<POST> parameters, so you have to make sure it is not excessively large. There's a 16MiB limit for requests by
420 default.
421  
422 =head2 params
423  
424 my $params = $req->params;
425  
426 All C<GET> and C<POST> parameters extracted from the query string and C<application/x-www-form-urlencoded> or
427 C<multipart/form-data> message body, usually a L<Mojo::Parameters> object. Note that this method caches all data, so it
428 should not be called before the entire request body has been received. Parts of the request body need to be loaded into
429 memory to parse C<POST> parameters, so you have to make sure it is not excessively large. There's a 16MiB limit for
430 requests by default.
431  
432 # Get parameter names and values
433 my $hash = $req->params->to_hash;
434  
435 =head2 parse
436  
437 $req = $req->parse('GET /foo/bar HTTP/1.1');
438 $req = $req->parse({PATH_INFO => '/'});
439  
440 Parse HTTP request chunks or environment hash.
441  
442 =head2 query_params
443  
444 my $params = $req->query_params;
445  
446 All C<GET> parameters, usually a L<Mojo::Parameters> object.
447  
448 # Turn GET parameters to hash and extract value
449 say $req->query_params->to_hash->{foo};
450  
451 =head2 start_line_size
452  
453 my $size = $req->start_line_size;
454  
455 Size of the request-line in bytes. Note that this method finalizes the request.
456  
457 =head1 SEE ALSO
458  
459 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
460  
461 =cut
« Mojolicious/Plugin/TagHelpers.pm ↑ Summary Mojo/UserAgent/Transactor.pm »