File Coverage

blib/lib/ASP4/Request.pm
Criterion Covered Total %
statement 26 31 83.8
branch 3 6 50.0
condition n/a
subroutine 9 12 75.0
pod 3 7 42.8
total 41 56 73.2


line stmt bran cond sub pod time code
1              
2             package ASP4::Request;
3              
4 9     9   715 use strict;
  9         14  
  9         246  
5 9     9   27 use warnings 'all';
  9         12  
  9         2002  
6              
7              
8             sub new
9             {
10 5122     5122 0 5912 my ($class, %args) = @_;
11            
12 5122         9296 return bless {
13             %args,
14             form => scalar( $class->context->cgi->Vars ),
15             }, $class;
16             }# end new()
17              
18              
19 5131     5131 0 9064 sub context { ASP4::HTTPContext->current }
20              
21             # Not documented - for a reason (want to deprecate):
22 15382     15382 0 20865 sub Form { shift->{form} }
23              
24             # Not documented - for a reason (want to deprecate):
25 0     0 0 0 sub QueryString { shift->context->cgi->query_string() }
26              
27             sub Cookies {
28 0     0 1 0 my ($s, $name) = @_;
29 0 0       0 $name ? $s->context->cgi->cookie( $name ) : $s->context->cgi->cookie;
30             }
31              
32 0     0 1 0 sub ServerVariables { $ENV{ $_[1] } }
33              
34             sub FileUpload
35             {
36 3     3 1 7 my ($s, $field) = @_;
37            
38 3 100       14 my $ifh = $s->context->cgi->upload($field)
39             or return;
40 2         5 my %info = ( );
41            
42 2 50       5 if( my $upInfo = eval { $s->context->cgi->uploadInfo( $ifh ) } )
  2         7  
43             {
44 9     9   35 no warnings 'uninitialized';
  9         13  
  9         593  
45             %info = (
46             ContentType => $upInfo->{'Content-Type'},
47             FileHandle => $ifh,
48             FileName => $s->{form}->{ $field } . "",
49 0         0 ContentDisposition => $upInfo->{'Content-Disposition'},
50             );
51             }
52             else
53             {
54 9     9   34 no warnings 'uninitialized';
  9         11  
  9         1309  
55             %info = (
56             ContentType => $s->context->cgi->{uploads}->{ $field }->{headers}->{'Content-Type'},
57             FileHandle => $ifh,
58             FileName => $s->context->cgi->{uploads}->{ $field }->{filename},
59 2         9 ContentDisposition => 'attachment',
60             );
61             }# end if()
62            
63 2         873 require ASP4::FileUpload;
64 2         19 return ASP4::FileUpload->new( %info );
65             }# end FileUpload()
66              
67              
68             sub DESTROY
69             {
70 5104     5104   4190 my $s = shift;
71 5104         10582 undef(%$s);
72             }# end DESTROY()
73              
74             1;# return true:
75              
76             =pod
77              
78             =head1 NAME
79              
80             ASP4::Request - Interface to the incoming request
81              
82             =head1 SYNOPSIS
83              
84             if( my $cookie = $Request->Cookies('cust-email') ) {
85             # Greet our returning user:
86             }
87            
88             if( my $file = $Request->FileUpload('avatar_pic') ) {
89             # Handle the uploaded file:
90             $file->SaveAs( "/var/media/$Session->{user_id}/avatar/" . $file->FileName );
91             }
92            
93             if( $Request->ServerVariables("HTTPS") ) {
94             # We're under SSL:
95             }
96              
97             =head1 DESCRIPTION
98              
99             The intrinsic C<$Request> object provides a few easy-to-use methods to simplify
100             the processing of incoming requests - specifically file uploads and cookies.
101              
102             =head1 METHODS
103              
104             =head2 Cookies( [$name] )
105              
106             Returns a cookie by name, or all cookies if no name is provided.
107              
108             =head2 ServerVariables( [$name] )
109              
110             A wrapper around the global C<%ENV> variable.
111              
112             This means that:
113              
114             $Request->ServerVariables('HTTP_HOST')
115              
116             is the same as:
117              
118             $ENV{HTTP_HOST}
119              
120             =head2 FileUpload( $fieldname )
121              
122             Returns a L object that corresponds to the fieldname specified.
123              
124             So...if your form has this:
125              
126            
127              
128             Then you would get to it like this:
129              
130             my $upload = $Request->FileUpload('my_uploaded-file');
131              
132             =cut
133