ASP/Request.pm

filter: Statementstmt 99.0% 108 / 109 Branchbran 80.0% 32 / 40 Conditioncond 35.0% 7 / 20 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 100% 21 / 21 Podpod 88.8% 8 / 9 totaltotal 83.0% 176 / 212 CC 31 SCAR 12.1
CC
31
Cov
87%
__ANON__
5.0
FileUpload
4.2
Form
4.2
CRAP 3.3
« ASP/Response.pm ↑ Summary ASP/Session.pm »
1 package CatalystX::ASP::Request;
2  
3 9 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use List::Util qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 9 has 'asp' => (
8 is => 'ro',
9 isa => 'CatalystX::ASP',
10 required => 1,
11 weak_ref => 1,
12 );
13  
14 =head1 NAME
15  
16 CatalystX::ASP::Request - $Request Object
17  
18 =head1 SYNOPSIS
19  
20 use CatalystX::ASP::Request;
21  
22 my $req = CatalystX::ASP::Request->new(asp => $asp);
23 my $session_cookie = $req->Cookies('session');
24 my $host = $req->ServerVariables('HTTP_HOST');
25  
26 =head1 DESCRIPTION
27  
28 The request object manages the input from the client browser, like posts, query
29 strings, cookies, etc. Normal return results are values if an index is
30 specified, or a collection / perl hash ref if no index is specified. WARNING:
31 the latter property is not supported in ActiveState PerlScript, so if you use
32 the hashes returned by such a technique, it will not be portable.
33  
34 A normal use of this feature would be to iterate through the form variables in
35 the form hash...
36  
37 $form = $Request->Form();
38 for(keys %{$form}) {
39 $Response->Write("$_: $form->{$_}<br>\n");
40 }
41  
42 Note that if a form POST or query string contains duplicate values for a key,
43 those values will be returned through normal use of the C<$Request> object:
44  
45 @values = $Request->Form('key');
46  
47 but you can also access the internal storage, which is an array reference like
48 so:
49  
50 $array_ref = $Request->{Form}{'key'};
51 @values = @{$array_ref};
52  
53 Please read the PERLSCRIPT section for more information on how things like
54 C<< $Request->QueryString() >> & C<< $Request->Form() >> behave as collections.
55  
56 =cut
57  
58 # For some reason, for attributes that start with a capital letter, Moose seems
59 # to load the default value before the object is fully initialized. lazy => 1 is
60 # a workaround to build the defaults later
61 has 'Cookies' => (
62 is => 'ro',
63 isa => 'HashRef',
64 reader => '_get_Cookies',
65 lazy => 1,
66 14 ▶ default => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
67 14 my ( $self ) = @_;
68 14 my $c = $self->asp->c;
69 14 my %cookies;
70 14 for my $name ( keys %{ $c->request->cookies } ) {
71 10 ▶ my $value = $c->request->cookies->{$name}{value} || [];
Condition50%1/2
$c->request->cookies->{$name}{'value'} || []
l!l
100
MC/DC0%0/1
$c->request->cookies->{$name}{'value'} || []
$c->request->cookies->{$name}{'value'}
Truth table50%1/2
A
A= $c->request->cookies->{$name}{'value'}
Aresult
11
00
72 10 ▶ if ( all {/.=./} @$value ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&all(sub { /.=./; } , @$value)) { } 5 5 10
73 5 for ( @$value ) {
74 10 my ( $key, $val ) = split '=';
75 10 $cookies{$name}{$key} = $val;
76 }
77 } else {
78 5 $cookies{$name} = $value->[0];
79 }
80 }
81 14 return \%cookies;
82 },
83 9 traits => ['Hash'],
84 handles => {
85 _get_Cookie => 'get',
86 },
87 );
88  
89 has 'FileUpload' => (
90 is => 'ro',
91 isa => 'HashRef',
92 reader => '_get_FileUploads',
93 lazy => 1,
94 14 ▶ default => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
95 14 my ( $self ) = @_;
96 14 my %uploads;
97 14 ▶ while ( my ( $field, $value ) = each %{ $self->asp->c->request->uploads } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->asp->c->request->uploads;}) 5 14 19
98  
99 # Just assume the first upload field, because how Apache::ASP deals with
100 # multiple uploads per-field is beyond me.
101 5 ▶ my $upload = ref( $value ) eq 'ARRAY' ? $value->[0] : $value;
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 0 5 5
102 5 $uploads{$field} = {
103 ContentType => $upload->type,
104 FileHandle => $upload->fh,
105 BrowserFile => $upload->filename,
106 TempFile => $upload->tempname,
107 };
108 }
109 14 return \%uploads;
110 },
111 9 traits => ['Hash'],
112 handles => {
113 _get_FileUpload => 'get',
114 },
115 );
116  
117 has 'Form' => (
118 is => 'ro',
119 isa => 'HashRef',
120 reader => '_get_Form',
121 lazy => 1,
122 14 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
123 14 my ( $self ) = @_;
124  
125 # ASP includes uploads in its Form()
126 return {
127 14 %{ $self->asp->c->request->body_parameters },
128 14 %{ $self->asp->c->request->uploads },
129 };
130 },
131 9 traits => ['Hash'],
132 handles => {
133 _get_FormField => 'get',
134 },
135 );
136  
137 =head1 ATTRIBUTES
138  
139 =over
140  
141 =item $Request->{Method}
142  
143 API extension. Returns the client HTTP request method, as in GET or POST. Added
144 in version C<2.31>.
145  
146 =cut
147  
148 has 'Method' => (
149 is => 'ro',
150 isa => 'Str',
151 lazy => 1,
152 14 ▶ default => sub { shift->asp->c->request->method },
Subroutine __ANON__: called CC=1 SCAR=0.0
153 9 );
154  
155 has 'Params' => (
156 is => 'ro',
157 isa => 'HashRef',
158 reader => '_get_Params',
159 lazy => 1,
160 14 ▶ default => sub { shift->asp->c->request->parameters },
Subroutine __ANON__: called CC=1 SCAR=0.0
161 9 traits => ['Hash'],
162 handles => {
163 _get_Param => 'get',
164 },
165 );
166  
167 has 'QueryString' => (
168 is => 'ro',
169 isa => 'HashRef',
170 reader => '_get_QueryString',
171 lazy => 1,
172 14 ▶ default => sub { shift->asp->c->request->query_parameters },
Subroutine __ANON__: called CC=1 SCAR=0.0
173 9 traits => ['Hash'],
174 handles => {
175 _get_Query => 'get',
176 },
177 );
178  
179 has 'ServerVariables' => (
180 is => 'ro',
181 isa => 'HashRef',
182 reader => '_get_ServerVariables',
183 lazy => 1,
184 14 ▶ default => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
185 14 my ( $self ) = @_;
186  
187 # Populate %ENV freely because we assume some process upstream will
188 # localize ENV for the request.
189 14 my $env = $self->asp->c->request->env;
190 14 for ( keys %$env ) {
191 386 ▶ $ENV{$_} = $env->{$_} unless ref $env->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $env->{$_} 359 27 386
192 }
193  
194 # For backwards compatibility with Apache::ASP
195 14 $ENV{SCRIPT_NAME} = $ENV{PATH_INFO};
196  
197 14 return \%ENV;
198 },
199 9 traits => ['Hash'],
200 handles => {
201 _get_ServerVariable => 'get',
202 },
203 );
204  
205 =item $Request->{TotalBytes}
206  
207 The amount of data sent by the client in the body of the request, usually the
208 length of the form data. This is the same value as
209 C<< $Request->ServerVariables('CONTENT_LENGTH') >>
210  
211 =cut
212  
213 has 'TotalBytes' => (
214 is => 'ro',
215 isa => 'Int',
216 lazy => 1,
217 14 ▶ default => sub { shift->asp->c->request->content_length || 0 },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
(shift())->asp->c->request->content_length or 0 0 14 14
218 9 );
219  
220 15 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
221 15 my ( $self ) = @_;
222  
223 # Don't initiate below attributes unless past setup phase
224 15 ▶ return unless $self->asp->_setup_finished;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->asp->_setup_finished 1 14 15
225  
226 # Due to problem mentioned above in the builder methods, we are calling
227 # these attributes to populate the values for the hash key to be available
228 14 $self->Cookies;
229 14 $self->FileUpload;
230 14 $self->Form;
231 14 $self->Method;
232 14 $self->Params;
233 14 $self->QueryString;
234 14 $self->ServerVariables;
235 14 $self->TotalBytes;
236 }
237  
238 =back
239  
240 =head1 METHODS
241  
242 =over
243  
244 =item $Request->BinaryRead([$length])
245  
246 Returns a string whose contents are the first C<$length> bytes of the form data,
247 or body, sent by the client request. If C<$length> is not given, will return all
248 of the form data. This data is the raw data sent by the client, without any
249 parsing done on it by CatalystX::ASP.
250  
251 Note that C<BinaryRead> will not return any data for file uploads. Please see
252 the C<< $Request->FileUpload() >> interface for access to this data.
253 C<< $Request->Form() >> data will also be available as normal.
254  
255 =cut
256  
257 1 ▶sub BinaryRead {
Subroutine BinaryRead: called CC=3 SCAR=11.4
Pod documented
258 1 my ( $self, $length ) = @_;
259 1 my $c = $self->asp->c;
260 1 my $body = $c->request->body;
261 1 my @types = qw(application/x-www-form-urlencoded text/xml multipart/form-data);
262 1 ▶ if ( grep { $c->request->content_type eq $_ } @types ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$c->request->content_type eq $_;} @types) { } 1 0 1
263 1 my $buffer = '';
264 1 ▶ $length ||= $c->request->content_length;
Condition33%1/3
$length ||= $c->request->content_length
l!l&&r!l&&!r
100
MC/DC0%0/2
$length ||= $c->request->content_length
$length $c->request->content_length
Truth table33%1/3
A ||= B
A= $lengthB= $c->request->content_length
ABresult
1X1
011
000
265 1 $body->read( $buffer, $length );
266 1 return $buffer;
267 } else {
268 0 return substr( $body, 0, $length );
269 }
270 }
271  
272 =item $Request->ClientCertificate()
273  
274 Not implemented.
275  
276 =cut
277  
278 # TODO: will not implement
279 1 ▶sub ClientCertificate {
Subroutine ClientCertificate: called CC=1 SCAR=0.0
Pod documented
280 1 my ( $self ) = @_;
281 1 $self->asp->c->log->warn( "\$Request->ClientCertificate has not been implemented!" );
282 1 return;
283 }
284  
285 =item $Request->Cookies($name [,$key])
286  
287 Returns the value of the Cookie with name C<$name>. If a C<$key> is specified,
288 then a lookup will be done on the cookie as if it were a query string. So, a
289 cookie set by:
290  
291 Set-Cookie: test=data1=1&data2=2
292  
293 would have a value of C<2> returned by C<< $Request->Cookies('test','data2') >>.
294  
295 If no name is specified, a hash will be returned of cookie names as keys and
296 cookie values as values. If the cookie value is a query string, it will
297 automatically be parsed, and the value will be a hash reference to these values.
298  
299 When in doubt, try it out. Remember that unless you set the C<Expires> attribute
300 of a cookie with C<< $Response->Cookies('cookie', 'Expires', $xyz) >>, the
301 cookies that you set will only last until you close your browser, so you may
302 find your self opening & closing your browser a lot when debugging cookies.
303  
304 For more information on cookies in ASP, please read C<< $Response->Cookies() >>
305  
306 =cut
307  
308 19 ▶sub Cookies {
Subroutine Cookies: called CC=4 SCAR=13.9
Pod documented
309 19 my ( $self, $name, $key ) = @_;
310  
311 19 ▶ if ( $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) { } 5 14 19
312 5 ▶ if ( $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key) { } 3 2 5
313 3 my $cookie = $self->_get_Cookie( $name );
314 3 ▶ return ref $cookie eq 'HASH' ? $cookie->{$key} : $cookie;
Branch: 1 branch
BranchTrueFalseTotal
ref $cookie eq 'HASH' ? : 2 1 3
315 } else {
316 2 return $self->_get_Cookie( $name );
317 }
318 } else {
319 14 return $self->_get_Cookies;
320 }
321 }
322  
323 =item $Request->FileUpload($form_field, $key)
324  
325 API extension. The C<FileUpload> interface to file upload data is stabilized.
326 The internal representation of the file uploads is a hash of hashes, one hash
327 per file upload found in the C<< $Request->Form() >> collection. This collection
328 of collections may be queried through the normal interface like so:
329  
330 $Request->FileUpload('upload_file', 'ContentType');
331 $Request->FileUpload('upload_file', 'FileHandle');
332 $Request->FileUpload('upload_file', 'BrowserFile');
333 $Request->FileUpload('upload_file', 'Mime-Header');
334 $Request->FileUpload('upload_file', 'TempFile');
335  
336 * note that TempFile must be use with the UploadTempFile configuration setting.
337  
338 The above represents the old slow collection interface, but like all collections
339 in CatalystX::ASP, you can reference the internal hash representation more
340 easily.
341  
342 my $fileup = $Request->{FileUpload}{upload_file};
343 $fileup->{ContentType};
344 $fileup->{BrowserFile};
345 $fileup->{FileHandle};
346 $fileup->{Mime-Header};
347 $fileup->{TempFile};
348  
349 =cut
350  
351 16 ▶sub FileUpload {
Subroutine FileUpload: called CC=4 SCAR=14.5
Pod documented
352 16 my ( $self, $form_field, $key ) = @_;
353  
354 16 ▶ if ( $form_field ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($form_field) { } 2 14 16
355 2 my $upload = $self->_get_FileUpload( $form_field )->{$key};
356 2 ▶ return wantarray && ref $upload eq 'ARRAY' ? @$upload : $upload;
Branch: 1 branch
BranchTrueFalseTotal
wantarray && ref $upload eq 'ARRAY' ? : 0 2 2
Condition33%1/3
wantarray and ref $upload eq "ARRAY"
!ll&&!rl&&r
200
MC/DC0%0/2
wantarray and ref $upload eq "ARRAY"
wantarray ref $upload eq "ARRAY"
Truth table33%1/3
A and B
A= wantarrayB= ref $upload eq "ARRAY"
ABresult
0X0
100
111
357 } else {
358 14 return $self->_get_FileUploads;
359 }
360 }
361  
362 =item $Request->Form($name)
363  
364 Returns the value of the input of name C<$name> used in a form with POST method.
365 If C<$name> is not specified, returns a ref to a hash of all the form data. One
366 can use this hash to create a nice alias to the form data like:
367  
368 # in global.asa
369 use vars qw( $Form );
370 sub Script_OnStart {
371 $Form = $Request->Form;
372 }
373 # then in ASP scripts
374 <%= $Form->{var} %>
375  
376 File upload data will be loaded into C<< $Request->Form('file_field') >>, where
377 the value is the actual file name of the file uploaded, and the contents of the
378 file can be found by reading from the file name as a file handle as in:
379  
380 while(read($Request->Form('file_field_name'), $data, 1024)) {};
381  
382 For more information, please see the CGI / File Upload section, as file uploads
383 are implemented via the CGI.pm module.
384  
385 =cut
386  
387 16 ▶sub Form {
Subroutine Form: called CC=4 SCAR=14.5
Pod documented
388 16 my ( $self, $name ) = @_;
389  
390 16 ▶ if ( $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) { } 1 15 16
391 1 my $value = $self->_get_FormField( $name );
392 1 ▶ return wantarray && ref $value eq 'ARRAY' ? @$value : $value;
Branch: 1 branch
BranchTrueFalseTotal
wantarray && ref $value eq 'ARRAY' ? : 0 1 1
Condition33%1/3
wantarray and ref $value eq "ARRAY"
!ll&&!rl&&r
100
MC/DC0%0/2
wantarray and ref $value eq "ARRAY"
wantarray ref $value eq "ARRAY"
Truth table33%1/3
A and B
A= wantarrayB= ref $value eq "ARRAY"
ABresult
0X0
100
111
393 } else {
394 15 return $self->_get_Form;
395 }
396 }
397  
398 =item $Request->Params($name)
399  
400 API extension. If C<RequestParams> CONFIG is set, the C<< $Request->Params >>
401 object is created with combined contents of C<< $Request->QueryString >> and
402 C<< $Request->Form >>. This is for developer convenience simlar to CGI.pm's
403 C<param()> method. Just like for C<< $Response->Form >>, one could create a
404 nice alias like:
405  
406 # in global.asa
407 use vars qw( $Params );
408 sub Script_OnStart {
409 $Params = $Request->Params;
410 }
411  
412 =cut
413  
414 16 ▶sub Params {
Subroutine Params: called CC=4 SCAR=14.5
Pod documented
415 16 my ( $self, $name ) = @_;
416  
417 16 ▶ if ( $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) { } 2 14 16
418 2 my $param = $self->_get_Param( $name );
419 2 ▶ return wantarray && ref $param eq 'ARRAY' ? @$param : $param;
Branch: 1 branch
BranchTrueFalseTotal
wantarray && ref $param eq 'ARRAY' ? : 0 2 2
Condition33%1/3
wantarray and ref $param eq "ARRAY"
!ll&&!rl&&r
200
MC/DC0%0/2
wantarray and ref $param eq "ARRAY"
wantarray ref $param eq "ARRAY"
Truth table33%1/3
A and B
A= wantarrayB= ref $param eq "ARRAY"
ABresult
0X0
100
111
420 } else {
421 14 return $self->_get_Params;
422 }
423 }
424  
425 =item $Request->QueryString($name)
426  
427 Returns the value of the input of name C<$name> used in a form with GET method,
428 or passed by appending a query string to the end of a url as in
429 http://localhost/?data=value. If C<$name> is not specified, returns a ref to a
430 hash of all the query string data.
431  
432 =cut
433  
434 15 ▶sub QueryString {
Subroutine QueryString: called CC=4 SCAR=14.5
Pod documented
435 15 my ( $self, $name ) = @_;
436  
437 15 ▶ if ( $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) { } 1 14 15
438 1 my $qparam = $self->_get_Query( $name );
439 1 ▶ return wantarray && ref $qparam eq 'ARRAY' ? @$qparam : $qparam;
Branch: 1 branch
BranchTrueFalseTotal
wantarray && ref $qparam eq 'ARRAY' ? : 0 1 1
Condition33%1/3
wantarray and ref $qparam eq "ARRAY"
!ll&&!rl&&r
100
MC/DC0%0/2
wantarray and ref $qparam eq "ARRAY"
wantarray ref $qparam eq "ARRAY"
Truth table33%1/3
A and B
A= wantarrayB= ref $qparam eq "ARRAY"
ABresult
0X0
100
111
440 } else {
441 14 return $self->_get_QueryString;
442 }
443 }
444  
445 =item $Request->ServerVariables($name)
446  
447 Returns the value of the server variable / environment variable with name
448 C<$name>. If C<$name> is not specified, returns a ref to a hash of all the
449 server / environment variables data. The following would be a common use of
450 this method:
451  
452 $env = $Request->ServerVariables();
453 # %{$env} here would be equivalent to the cgi %ENV in perl.
454  
455 =cut
456  
457 15 ▶sub ServerVariables {
Subroutine ServerVariables: called CC=4 SCAR=14.5
Pod documented
458 15 my ( $self, $name ) = @_;
459  
460 15 ▶ if ( $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) { } 1 14 15
461 1 my $var = $self->_get_ServerVariable( $name );
462 1 ▶ return wantarray && ref $var eq 'ARRAY' ? @$var : $var;
Branch: 1 branch
BranchTrueFalseTotal
wantarray && ref $var eq 'ARRAY' ? : 0 1 1
Condition33%1/3
wantarray and ref $var eq "ARRAY"
!ll&&!rl&&r
100
MC/DC0%0/2
wantarray and ref $var eq "ARRAY"
wantarray ref $var eq "ARRAY"
Truth table33%1/3
A and B
A= wantarrayB= ref $var eq "ARRAY"
ABresult
0X0
100
111
463 } else {
464 14 return $self->_get_ServerVariables;
465 }
466 }
467  
468 9 __PACKAGE__->meta->make_immutable;
469  
470 =back
471  
472 =head1 SEE ALSO
473  
474 =over
475  
476 =item * L<CatalystX::ASP::Session>
477  
478 =item * L<CatalystX::ASP::Response>
479  
480 =item * L<CatalystX::ASP::Application>
481  
482 =item * L<CatalystX::ASP::Server>
483  
484 =back
« ASP/Response.pm ↑ Summary ASP/Session.pm »