Request.pm

filter: Statementstmt 94.3% 218 / 231 Branchbran 82.1% 46 / 56 Conditioncond 91.3% 21 / 23 MC/DCmcdc 84.6% 11 / 13 Subroutinesub 95.0% 38 / 40 Podpod 92.8% 13 / 14 totaltotal 92.0% 347 / 377 CC 45 SCAR 13.5
CC
45
Cov
92%
__ANON__
11.3
path
6.0
__ANON__
5.0
CRAP 3.9
↑ Summary Response.pm »
1 package Web::Request;
2 BEGIN {
3 24 ▶ $Web::Request::AUTHORITY = 'cpan:DOY';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 }
5 {
6 24 $Web::Request::VERSION = '0.11';
7 }
8 24 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 # ABSTRACT: common request class for web frameworks
10  
11 24 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 24 ▶use HTTP::Body ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 24 ▶use HTTP::Headers ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 24 ▶use HTTP::Message::PSGI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 24 ▶use Module::Runtime ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 24 ▶use Stream::Buffered ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 24 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 24 ▶use URI::Escape ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20  
21 24 has env => (
22 traits => ['Hash'],
23 is => 'ro',
24 isa => 'HashRef',
25 required => 1,
26 handles => {
27 address => [ get => 'REMOTE_ADDR' ],
28 remote_host => [ get => 'REMOTE_HOST' ],
29 protocol => [ get => 'SERVER_PROTOCOL' ],
30 method => [ get => 'REQUEST_METHOD' ],
31 port => [ get => 'SERVER_PORT' ],
32 request_uri => [ get => 'REQUEST_URI' ],
33 path_info => [ get => 'PATH_INFO' ],
34 script_name => [ get => 'SCRIPT_NAME' ],
35 scheme => [ get => 'psgi.url_scheme' ],
36 _input => [ get => 'psgi.input' ],
37 content_length => [ get => 'CONTENT_LENGTH' ],
38 content_type => [ get => 'CONTENT_TYPE' ],
39 session => [ get => 'psgix.session' ],
40 session_options => [ get => 'psgix.session.options' ],
41 logger => [ get => 'psgix.logger' ],
42 },
43 );
44  
45 has _base_uri => (
46 is => 'ro',
47 isa => 'Str',
48 lazy => 1,
49 default => sub {
50 32 ▶ my $self = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
51  
52 32 my $env = $self->env;
53  
54 32 ▶ my $scheme = $self->scheme || "http";
Condition100%2/2
$self->scheme || 'http'
l!l
1616
MC/DC100%1/1
$self->scheme || 'http'
$self->scheme
Truth table100%2/2
A
A= $self->scheme
Aresult
11
00
55 32 my $server = $self->host;
56 32 ▶ my $path = $self->script_name || '/';
Condition100%2/2
$self->script_name || '/'
l!l
923
MC/DC100%1/1
$self->script_name || '/'
$self->script_name
Truth table100%2/2
A
A= $self->script_name
Aresult
11
00
57  
58 32 return "${scheme}://${server}${path}";
59 },
60 24 );
61  
62 has base_uri => (
63 is => 'ro',
64 isa => 'URI',
65 lazy => 1,
66 9 ▶ default => sub { URI->new(shift->_base_uri)->canonical },
Subroutine __ANON__: called CC=1 SCAR=0.0
67 24 );
68  
69 has uri => (
70 is => 'ro',
71 isa => 'URI',
72 lazy => 1,
73 default => sub {
74 23 ▶ my $self = shift;
Subroutine __ANON__: called CC=5 SCAR=16.1
75  
76 23 my $base = $self->_base_uri;
77  
78 # We have to escape back PATH_INFO in case they include stuff
79 # like ? or # so that the URI parser won't be tricked. However
80 # we should preserve '/' since encoding them into %2f doesn't
81 # make sense. This means when a request like /foo%2fbar comes
82 # in, we recognize it as /foo/bar which is not ideal, but that's
83 # how the PSGI PATH_INFO spec goes and we can't do anything
84 # about it. See PSGI::FAQ for details.
85 23 my $path_escape_class = q{^/;:@&=A-Za-z0-9\$_.+!*'(),-};
86  
87 23 ▶ my $path = URI::Escape::uri_escape(
Condition100%2/2
$self->path_info || ''
l!l
815
MC/DC100%1/1
$self->path_info || ''
$self->path_info
Truth table100%2/2
A
A= $self->path_info
Aresult
11
00
88 $self->path_info || '',
89 $path_escape_class
90 );
91 $path .= '?' . $self->env->{QUERY_STRING}
92 if defined $self->env->{QUERY_STRING}
93 23 ▶ && $self->env->{QUERY_STRING} ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->env->{"QUERY_STRING"} and $self->env->{"QUERY_STRING"} ne "" 11 12 23
Condition100%3/3
defined $self->env->{"QUERY_STRING"} and $self->env->{"QUERY_STRING"} ne ""
!ll&&!rl&&r
10211
MC/DC100%2/2
defined $self->env->{"QUERY_STRING"} and $self->env->{"QUERY_STRING"} ne ""
defined $self->env->{"QUERY_STRING"} $self->env->{"QUERY_STRING"} ne ""
Truth table100%3/3
A and B
A= defined $self->env->{"QUERY_STRING"}B= $self->env->{"QUERY_STRING"} ne ""
ABresult
0X0
100
111
94  
95 23 ▶ $base =~ s!/$!! if $path =~ m!^/!;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ m[^/] 8 15 23
96  
97 23 return URI->new($base . $path)->canonical;
98 },
99 24 );
100  
101 has headers => (
102 is => 'ro',
103 isa => 'HTTP::Headers',
104 lazy => 1,
105 default => sub {
106 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
107 0 my $env = $self->env;
108 return HTTP::Headers->new(
109 map {
110 0 (my $field = $_) =~ s/^HTTPS?_//;
111 0 $field => $env->{$_}
112 } grep {
113 0 /^(?:HTTP|CONTENT)/i
114 } keys %$env
115 );
116 },
117 24 handles => ['header', 'content_encoding', 'referer', 'user_agent'],
118 );
119  
120 has cookies => (
121 is => 'ro',
122 isa => 'HashRef',
123 lazy => 1,
124 default => sub {
125 3 ▶ my $self = shift;
Subroutine __ANON__: called CC=4 SCAR=13.9
126  
127 3 my $cookie_str = $self->env->{HTTP_COOKIE};
128 3 ▶ return {} unless defined $cookie_str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cookie_str 1 2 3
129  
130 2 my %results;
131 2 for my $pair (grep { /=/ } split /[;,] ?/, $cookie_str) {
132 7 $pair =~ s/^\s+|\s+$//g;
133 my ($key, $value) = map {
134 7 URI::Escape::uri_unescape($_)
135 } split(/=/, $pair, 2);
136 # XXX $self->decode too?
137 7 ▶ $results{$key} = $value unless exists $results{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $results{$key} 6 1 7
138 }
139  
140 2 return \%results;
141 },
142 24 );
143  
144 24 has _http_body => (
145 is => 'rw',
146 isa => 'HTTP::Body',
147 );
148  
149 has _parsed_body => (
150 traits => ['Hash'],
151 is => 'ro',
152 isa => 'HashRef',
153 lazy => 1,
154 default => sub {
155 19 ▶ my $self = shift;
Subroutine __ANON__: called CC=11 SCAR=24.2
156  
157 19 my $ct = $self->content_type;
158 19 my $cl = $self->content_length;
159 19 ▶ if (!$ct && !$cl) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ct or $cl) 8 11 19
Condition66%2/3
$ct or $cl
l!l&&r!l&&!r
1108
MC/DC50%1/2
$ct or $cl
$ct $cl
Truth table66%2/3
A or B
A= $ctB= $cl
ABresult
1X1
011
000
160 8 ▶ if (!$self->env->{'psgix.input.buffered'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->env->{"psgix.input.buffered"}) 8 0 8
161 8 $self->env->{'psgix.input.buffered'} = 1;
162 8 $self->env->{'psgi.input'} = Stream::Buffered->new(0)->rewind;
163 }
164 return {
165 8 body => {},
166 uploads => {},
167 };
168 }
169  
170 11 my $body = HTTP::Body->new($ct, $cl);
171 # automatically clean up, but wait until the request object is gone
172 11 $body->cleanup(1);
173 11 $self->_http_body($body);
174  
175 11 my $input = $self->_input;
176  
177 11 my $buffer;
178 11 ▶ if ($self->env->{'psgix.input.buffered'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->env->{'psgix.input.buffered'}) { } 0 11 11
179 0 $input->seek(0, 0);
180 }
181 else {
182 11 $buffer = Stream::Buffered->new($cl);
183 }
184  
185 11 my $spin = 0;
186 11 ▶ while ($cl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cl) 2022 10 2032
187 2022 ▶ $input->read(my $chunk, $cl < 8192 ? $cl : 8192);
Branch: 1 branch
BranchTrueFalseTotal
$cl < 8192 ? : 2013 9 2022
188 2022 my $read = length($chunk);
189 2022 $cl -= $read;
190 2022 $body->add($chunk);
191 2022 ▶ $buffer->print($chunk) if $buffer;
Branch: 1 branch
BranchTrueFalseTotal
if $buffer 2022 0 2022
192  
193 2022 ▶ if ($read == 0 && $spin++ > 2000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($read == 0 and $spin++ > 2000) 1 2021 2022
Condition100%3/3
$read == 0 and $spin++ > 2000
!ll&&!rl&&r
2020011
MC/DC100%2/2
$read == 0 and $spin++ > 2000
$read == 0 $spin++ > 2000
Truth table100%3/3
A and B
A= $read == 0B= $spin++ > 2000
ABresult
0X0
100
111
194 1 confess "Bad Content-Length ($cl bytes remaining)";
195 }
196 }
197  
198 10 ▶ if ($buffer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buffer) { } 10 0 10
199 10 $self->env->{'psgix.input.buffered'} = 1;
200 10 $self->env->{'psgi.input'} = $buffer->rewind;
201 }
202 else {
203 0 $input->seek(0, 0);
204 }
205  
206 return {
207 10 body => $body->param,
208 uploads => $body->upload,
209 }
210 },
211 24 handles => {
212 _body => [ get => 'body' ],
213 _uploads => [ get => 'uploads' ],
214 },
215 );
216  
217 has query_parameters => (
218 is => 'ro',
219 isa => 'HashRef[Str]',
220 lazy => 1,
221 clearer => '_clear_query_parameters',
222 default => sub {
223 23 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
224  
225 my %params = (
226 $self->uri->query_form,
227 23 (map { $_ => '' } $self->uri->query_keywords),
228 );
229 return {
230 23 map { $self->_decode($_) } map { $_ => $params{$_} } keys %params
231 };
232 },
233 24 );
234  
235 has all_query_parameters => (
236 is => 'ro',
237 isa => 'HashRef[ArrayRef[Str]]',
238 lazy => 1,
239 clearer => '_clear_all_query_parameters',
240 default => sub {
241 6 ▶ my $self = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
242  
243 6 my @params = $self->uri->query_form;
244 6 my $ret = {};
245  
246 6 ▶ while (my ($k, $v) = splice @params, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@params, 0, 2)) 8 6 14
247 8 $k = $self->_decode($k);
248 8 ▶ push @{ $ret->{$k} ||= [] }, $self->_decode($v);
Condition100%2/2
$ret->{$k} ||= []
l!l
26
MC/DC100%1/1
$ret->{$k} ||= []
$ret->{$k}
Truth table100%2/2
A
A= $ret->{$k}
Aresult
11
00
249 }
250  
251 6 return $ret;
252 },
253 24 );
254  
255 has body_parameters => (
256 is => 'ro',
257 isa => 'HashRef[Str]',
258 lazy => 1,
259 clearer => '_clear_body_parameters',
260 default => sub {
261 13 ▶ my $self = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
262  
263 13 my $body = $self->_body;
264  
265 12 my $ret = {};
266 12 for my $key (keys %$body) {
267 7 my $val = $body->{$key};
268 7 $key = $self->_decode($key);
269 7 ▶ $ret->{$key} = $self->_decode(ref($val) ? $val->[-1] : $val);
Branch: 1 branch
BranchTrueFalseTotal
ref $val ? : 1 6 7
270 }
271  
272 12 return $ret;
273 },
274 24 );
275  
276 has all_body_parameters => (
277 is => 'ro',
278 isa => 'HashRef[ArrayRef[Str]]',
279 lazy => 1,
280 clearer => '_clear_all_body_parameters',
281 default => sub {
282 6 ▶ my $self = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
283  
284 6 my $body = $self->_body;
285  
286 6 my $ret = {};
287 6 for my $key (keys %$body) {
288 5 my $val = $body->{$key};
289 5 $key = $self->_decode($key);
290 $ret->{$key} = ref($val)
291 5 ▶ ? [ map { $self->_decode($_) } @$val ]
Branch: 1 branch
BranchTrueFalseTotal
ref $val ? : 1 4 5
292 : [ $self->_decode($val) ];
293 }
294  
295 6 return $ret;
296 },
297 24 );
298  
299 has uploads => (
300 is => 'ro',
301 isa => 'HashRef[Web::Request::Upload]',
302 lazy => 1,
303 default => sub {
304 3 ▶ my $self = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
305  
306 3 my $uploads = $self->_uploads;
307  
308 3 my $ret = {};
309 3 for my $key (keys %$uploads) {
310 7 my $val = $uploads->{$key};
311 7 ▶ $ret->{$key} = ref($val) eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 3 4 7
312 ? $self->_new_upload($val->[-1])
313 : $self->_new_upload($val);
314 }
315  
316 3 return $ret;
317 },
318 24 );
319  
320 has all_uploads => (
321 is => 'ro',
322 isa => 'HashRef[ArrayRef[Web::Request::Upload]]',
323 lazy => 1,
324 default => sub {
325 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
326  
327 1 my $uploads = $self->_uploads;
328  
329 1 my $ret = {};
330 1 for my $key (keys %$uploads) {
331 4 my $val = $uploads->{$key};
332 $ret->{$key} = ref($val) eq 'ARRAY'
333 4 ▶ ? [ map { $self->_new_upload($_) } @$val ]
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 2 2 4
334 : [ $self->_new_upload($val) ];
335 }
336  
337 1 return $ret;
338 },
339 24 );
340  
341 24 has _encoding_obj => (
342 is => 'rw',
343 isa => 'Object', # no idea what this should be
344 clearer => '_clear_encoding_obj',
345 predicate => 'has_encoding',
346 );
347  
348 sub BUILD {
349 68 ▶ my $self = shift;
Subroutine BUILD: called CC=2 SCAR=7.4
Pod undocumented
350 68 my ($params) = @_;
351 68 ▶ if (defined $params->{encoding}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params->{'encoding'}) { } 0 68 68
352 0 $self->encoding($params->{encoding});
353 }
354 else {
355 68 $self->encoding($self->default_encoding);
356 }
357 }
358  
359 sub new_from_env {
360 68 ▶ my $class = shift;
Subroutine new_from_env: called CC=1 SCAR=0.0
Pod documented
361 68 my ($env) = @_;
362  
363 68 return $class->new(env => $env);
364 }
365  
366 sub new_from_request {
367 1 ▶ my $class = shift;
Subroutine new_from_request: called CC=1 SCAR=0.0
Pod documented
368 1 my ($req) = @_;
369  
370 1 return $class->new_from_env(HTTP::Message::PSGI::req_to_psgi($req));
371 }
372  
373 sub new_response {
374 32 ▶ my $self = shift;
Subroutine new_response: called CC=2 SCAR=6.9
Pod documented
375  
376 32 Module::Runtime::use_package_optimistically($self->response_class);
377 32 my $res = $self->response_class->new(@_);
378 32 ▶ $res->_encoding_obj($self->_encoding_obj)
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_encoding 31 1 32
379 if $self->has_encoding;
380 32 return $res;
381 }
382  
383 sub _new_upload {
384 13 ▶ my $self = shift;
Subroutine _new_upload: called CC=1 SCAR=0.0
385  
386 13 Module::Runtime::use_package_optimistically($self->upload_class);
387 13 $self->upload_class->new(@_);
388 }
389  
390 sub host {
391 41 ▶ my $self = shift;
Subroutine host: called CC=4 SCAR=13.9
Pod documented
392  
393 41 my $env = $self->env;
394 41 my $host = $env->{HTTP_HOST};
395 $host = ($env->{SERVER_NAME} || '') . ':'
396 41 ▶ . ($env->{SERVER_PORT} || 80)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host 11 30 41
Condition100%4/4
$env->{'SERVER_NAME'} || ''
l!l
65
$env->{'SERVER_PORT'} || 80
l!l
74
MC/DC100%1/1
$env->{'SERVER_NAME'} || ''
$env->{'SERVER_NAME'}
MC/DC100%1/1
$env->{'SERVER_PORT'} || 80
$env->{'SERVER_PORT'}
Truth table100%2/2
A
A= $env->{'SERVER_NAME'}
Aresult
11
00
Truth table100%2/2
A
A= $env->{'SERVER_PORT'}
Aresult
11
00
397 unless defined $host;
398  
399 41 return $host;
400 }
401  
402 sub path {
403 0 ▶ my $self = shift;
Subroutine path: not called CC=2 SCAR=17.9
Pod documented
404  
405 0 my $path = $self->path_info;
406 0 ▶ return $path if length($path);
Branch: 1 branch
BranchTrueFalseTotal
if length $path 0 0 0
407 0 return '/';
408 }
409  
410 sub parameters {
411 14 ▶ my $self = shift;
Subroutine parameters: called CC=1 SCAR=0.0
Pod documented
412  
413 return {
414 14 %{ $self->query_parameters },
415 14 %{ $self->body_parameters },
416 };
417 }
418  
419 sub all_parameters {
420 3 ▶ my $self = shift;
Subroutine all_parameters: called CC=3 SCAR=11.0
Pod documented
421  
422 3 my $ret = { %{ $self->all_query_parameters } };
423 3 my $body_parameters = $self->all_body_parameters;
424  
425 3 for my $key (keys %$body_parameters) {
426 2 ▶ push @{ $ret->{$key} ||= [] }, @{ $body_parameters->{$key} };
Condition50%1/2
$ret->{$key} ||= []
l!l
02
MC/DC0%0/1
$ret->{$key} ||= []
$ret->{$key}
Truth table50%1/2
A
A= $ret->{$key}
Aresult
11
00
427 }
428  
429 3 return $ret;
430 }
431  
432 sub param {
433 3 ▶ my $self = shift;
Subroutine param: called CC=1 SCAR=0.0
Pod documented
434 3 my ($key) = @_;
435  
436 3 $self->parameters->{$key};
437 }
438  
439 sub content {
440 11 ▶ my $self = shift;
Subroutine content: called CC=4 SCAR=13.9
Pod documented
441  
442 11 ▶ unless ($self->env->{'psgix.input.buffered'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->env->{"psgix.input.buffered"}) 5 6 11
443 # the builder for this attribute also sets up psgi.input
444 5 $self->_parsed_body;
445 }
446  
447 11 ▶ my $fh = $self->_input or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $fh = $self->_input or return "" 0 11 11
448 11 ▶ my $cl = $self->content_length or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $cl = $self->content_length or return "" 2 9 11
449  
450 9 $fh->seek(0, 0); # just in case middleware/apps read it without seeking back
451  
452 9 $fh->read(my $content, $cl, 0);
453 9 $fh->seek(0, 0);
454  
455 9 return $self->_decode($content);
456 }
457  
458 sub _decode {
459 80 ▶ my $self = shift;
Subroutine _decode: called CC=2 SCAR=7.0
460 80 my ($content) = @_;
461 80 ▶ return $content unless $self->has_encoding;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_encoding 0 80 80
462 80 return $self->_encoding_obj->decode($content);
463 }
464  
465 sub encoding {
466 84 ▶ my $self = shift;
Subroutine encoding: called CC=4 SCAR=13.9
Pod documented
467  
468 84 ▶ if (@_ > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 0) 84 0 84
469 84 my ($encoding) = @_;
470 84 $self->_clear_encoded_data;
471 84 ▶ if (defined($encoding)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $encoding) { } 83 1 84
472 83 $self->_encoding_obj(Encode::find_encoding($encoding));
473 }
474 else {
475 1 $self->_clear_encoding_obj;
476 }
477 }
478  
479 84 ▶ return $self->_encoding_obj ? $self->_encoding_obj->name : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->_encoding_obj ? : 83 1 84
480 }
481  
482 sub _clear_encoded_data {
483 84 ▶ my $self = shift;
Subroutine _clear_encoded_data: called CC=1 SCAR=0.0
484 84 $self->_clear_encoding_obj;
485 84 $self->_clear_query_parameters;
486 84 $self->_clear_all_query_parameters;
487 84 $self->_clear_body_parameters;
488 84 $self->_clear_all_body_parameters;
489 }
490  
491 64 ▶sub response_class { 'Web::Response' }
Subroutine response_class: called CC=1 SCAR=0.0
Pod documented
492 26 ▶sub upload_class { 'Web::Request::Upload' }
Subroutine upload_class: called CC=1 SCAR=0.0
Pod documented
493 68 ▶sub default_encoding { 'iso8859-1' }
Subroutine default_encoding: called CC=1 SCAR=0.0
Pod documented
494  
495 24 __PACKAGE__->meta->make_immutable;
496 24 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
497  
498  
499  
500 24 1;
501  
502 __END__
503  
504 =pod
505  
506 =head1 NAME
507  
508 Web::Request - common request class for web frameworks
509  
510 =head1 VERSION
511  
512 version 0.11
513  
514 =head1 SYNOPSIS
515  
516 use Web::Request;
517  
518 my $app = sub {
519 my ($env) = @_;
520 my $req = Web::Request->new_from_env($env);
521 # ...
522 };
523  
524 =head1 DESCRIPTION
525  
526 Web::Request is a request class for L<PSGI> applications. It provides access to
527 all of the information received in a request, generated from the PSGI
528 environment. The available methods are listed below.
529  
530 Note that Web::Request objects are intended to be (almost) entirely read-only -
531 although some methods (C<headers>, C<uri>, etc) may return mutable objects,
532 changing those objects will have no effect on the actual environment, or the
533 return values of any of the other methods. Doing this is entirely unsupported.
534 In addition, the return values of most methods that aren't direct accesses to
535 C<env> are cached, so if you do modify the actual environment hashref, you
536 should create a new Web::Request object for it.
537  
538 The one exception is the C<encoding> attribute, which is allowed to be
539 modified. Changing the encoding will change the return value of any subsequent
540 calls to C<content>, C<query_parameters>, C<all_query_parameters>,
541 C<body_parameters>, and C<all_body_parameters>.
542  
543 Web::Request is based heavily on L<Plack::Request>, but with the intention of
544 growing to become more generally useful to end users (rather than just
545 framework and middleware developers). In the future, it is expected to grow in
546 functionality to support a lot more convenient functionality, while
547 Plack::Request has a more minimalist goal.
548  
549 =head1 METHODS
550  
551 =head2 address
552  
553 Returns the IP address of the remote client.
554  
555 =head2 remote_host
556  
557 Returns the hostname of the remote client. May be empty.
558  
559 =head2 protocol
560  
561 Returns the protocol (HTTP/1.0, HTTP/1.1, etc.) used in the current request.
562  
563 =head2 method
564  
565 Returns the HTTP method (GET, POST, etc.) used in the current request.
566  
567 =head2 port
568  
569 Returns the local port that this request was made on.
570  
571 =head2 host
572  
573 Returns the contents of the HTTP C<Host> header. If it doesn't exist, falls
574 back to recreating the host from the C<SERVER_NAME> and C<SERVER_PORT>
575 variables.
576  
577 =head2 path
578  
579 Returns the request path for the current request. Unlike C<path_info>, this
580 will never be empty, it will always start with C</>. This is most likely what
581 you want to use to dispatch on.
582  
583 =head2 path_info
584  
585 Returns the request path for the current request. This can be C<''> if
586 C<script_name> ends in a C</>. This can be appended to C<script_name> to get
587 the full (absolute) path that was requested from the server.
588  
589 =head2 script_name
590  
591 Returns the absolute path where your application is mounted. It may be C<''>
592 (in which case, C<path_info> will start with a C</>).
593  
594 =head2 request_uri
595  
596 Returns the raw, undecoded URI path (the literal path provided in the request,
597 so C</foo%20bar> in C<GET /foo%20bar HTTP/1.1>). You most likely want to use
598 C<path>, C<path_info>, or C<script_name> instead.
599  
600 =head2 scheme
601  
602 Returns C<http> or C<https> depending on the scheme used in the request.
603  
604 =head2 session
605  
606 Returns the session object, if a middleware is used which provides one. See
607 L<PSGI::Extensions>.
608  
609 =head2 session_options
610  
611 Returns the session options hashref, if a middleware is used which provides
612 one. See L<PSGI::Extensions>.
613  
614 =head2 logger
615  
616 Returns the logger object, if a middleware is used which provides one. See
617 L<PSGI::Extensions>.
618  
619 =head2 uri
620  
621 Returns the full URI used in the current request, as a L<URI> object.
622  
623 =head2 base_uri
624  
625 Returns the base URI for the current request (only the components up through
626 C<script_name>) as a L<URI> object.
627  
628 =head2 headers
629  
630 Returns a L<HTTP::Headers> object containing the headers for the current
631 request.
632  
633 =head2 content_length
634  
635 The length of the content, in bytes. Corresponds to the C<Content-Length>
636 header.
637  
638 =head2 content_type
639  
640 The MIME type of the content. Corresponds to the C<Content-Type> header.
641  
642 =head2 content_encoding
643  
644 The encoding of the content. Corresponds to the C<Content-Encoding> header.
645  
646 =head2 referer
647  
648 Returns the value of the C<Referer> header.
649  
650 =head2 user_agent
651  
652 Returns the value of the C<User-Agent> header.
653  
654 =head2 header($name)
655  
656 Shortcut for C<< $req->headers->header($name) >>.
657  
658 =head2 cookies
659  
660 Returns a hashref of cookies received in this request. The values are URI
661 decoded.
662  
663 =head2 content
664  
665 Returns the content received in this request, decoded based on the value of
666 C<encoding>.
667  
668 =head2 param($param)
669  
670 Returns the parameter value for the parameter named C<$param>. Returns the last
671 parameter given if more than one are passed.
672  
673 =head2 parameters
674  
675 Returns a hashref of parameter names to values. If a name is given more than
676 once, the last value is provided.
677  
678 =head2 all_parameters
679  
680 Returns a hashref where the keys are parameter names and the values are
681 arrayrefs holding every value given for that parameter name. All parameters are
682 stored in an arrayref, even if there is only a single value.
683  
684 =head2 query_parameters
685  
686 Like C<parameters>, but only return the parameters that were given in the query
687 string.
688  
689 =head2 all_query_parameters
690  
691 Like C<all_parameters>, but only return the parameters that were given in the
692 query string.
693  
694 =head2 body_parameters
695  
696 Like C<parameters>, but only return the parameters that were given in the
697 request body.
698  
699 =head2 all_body_parameters
700  
701 Like C<all_parameters>, but only return the parameters that were given in the
702 request body.
703  
704 =head2 uploads
705  
706 Returns a hashref of upload objects (instances of C<upload_class>). If more
707 than one upload is provided with a given name, returns the last one given.
708  
709 =head2 all_uploads
710  
711 Returns a hashref where the keys are upload names and the values are arrayrefs
712 holding an upload object (instance of C<upload_class>) for every upload given
713 for that name. All uploads are stored in an arrayref, even if there is only a
714 single value.
715  
716 =head2 new_response(@params)
717  
718 Returns a new response object, passing C<@params> to its constructor.
719  
720 =head2 env
721  
722 Returns the L<PSGI> environment that was provided in the constructor (or
723 generated from the L<HTTP::Request>, if C<new_from_request> was used).
724  
725 =head2 encoding($enc)
726  
727 Returns the encoding that was provided in the constructor. You can also pass an
728 encoding name to this method to set the encoding that will be used to decode
729 the content and encode the response. For instance, you can set the encoding to
730 UTF-8 in order to read the body content and parameters, and then set the
731 encoding to C<undef> at the end of the handler in order to indicate that the
732 response should not be encoded (for instance, if it is a binary file).
733  
734 =head2 response_class
735  
736 Returns the name of the class to use when creating a new response object via
737 C<new_response>. Defaults to L<Web::Response>. This can be overridden in
738 a subclass.
739  
740 =head2 upload_class
741  
742 Returns the name of the class to use when creating a new upload object for
743 C<uploads> or C<all_uploads>. Defaults to L<Web::Request::Upload>. This can be
744 overridden in a subclass.
745  
746 =head2 default_encoding
747  
748 Returns the name of the default encoding to use for decoding. Defaults to
749 iso8859-1. This can be overridden in a subclass.
750  
751 =head1 CONSTRUCTORS
752  
753 =head2 new_from_env($env)
754  
755 Create a new Web::Request object from a L<PSGI> environment hashref.
756  
757 =head2 new_from_request($request)
758  
759 Create a new Web::Request object from a L<HTTP::Request> object.
760  
761 =head2 new(%params)
762  
763 Create a new Web::Request object with named parameters. Valid parameters are:
764  
765 =over 4
766  
767 =item env
768  
769 A L<PSGI> environment hashref. Required.
770  
771 =item encoding
772  
773 The encoding to use for decoding all input in the request and encoding all
774 output in the response. Defaults to the value of C<default_encoding>. If
775 C<undef> is passed, no encoding or decoding will be done.
776  
777 =back
778  
779 =head1 BUGS
780  
781 No known bugs.
782  
783 Please report any bugs through RT: email
784 C<bug-web-request at rt.cpan.org>, or browse to
785 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Web-Request>.
786  
787 =head1 SEE ALSO
788  
789 L<Plack::Request> - Much of this module's API and implementation were taken
790 from Plack::Request.
791  
792 =head1 SUPPORT
793  
794 You can find this documentation for this module with the perldoc command.
795  
796 perldoc Web::Request
797  
798 You can also look for information at:
799  
800 =over 4
801  
802 =item * MetaCPAN
803  
804 L<https://metacpan.org/release/Web-Request>
805  
806 =item * Github
807  
808 L<https://github.com/doy/web-request>
809  
810 =item * RT: CPAN's request tracker
811  
812 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Web-Request>
813  
814 =item * CPAN Ratings
815  
816 L<http://cpanratings.perl.org/d/Web-Request>
817  
818 =back
819  
820 =for Pod::Coverage BUILD
821  
822 =head1 AUTHOR
823  
824 Jesse Luehrs <doy@tozt.net>
825  
826 =head1 COPYRIGHT AND LICENSE
827  
828 This software is copyright (c) 2013 by Jesse Luehrs.
829  
830 This is free software; you can redistribute it and/or modify it under
831 the same terms as the Perl 5 programming language system itself.
832  
833 =cut
↑ Summary Response.pm »