blib/lib/ASP4/Request.pm

filter: Statementstmt 88.3% 53 / 60 Branchbran 57.1% 8 / 14 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 71.4% 10 / 14 Podpod 44.4% 4 / 9 totaltotal 76.0% 76 / 100 CC 9 SCAR 9.9
CC
9
Cov
82%
Cookies
6.0
Reroute
4.0
new
3.0
CRAP 2.7
« blib/lib/ASP4/ErrorHandler.pm ↑ Summary blib/lib/ASP4/ConfigParser.pm »
1  
2 package ASP4::Request;
3  
4 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use warnings 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 sub new
9 5145 ▶{
Subroutine new: called CC=3 SCAR=11.1
Pod undocumented
10 5145 my ($class, %args) = @_;
11
12 5145 my $cgi = $class->context->cgi;
13 my $s = bless {
14 %args,
15 form => {
16 (
17 map {
18 # CGI->Vars joins multi-value params with a null byte. Which sucks.
19 # To avoid that behavior, we do this instead:
20 12 my @val = map { $cgi->unescape( $_ ) } ( $cgi->param($_) );
21 12 ▶ $cgi->unescape($_) => scalar(@val) > 1 ? \@val : shift(@val)
Branch: 1 branch
BranchTrueFalseTotal
scalar @val > 1 ? : 0 12 12
22 } $cgi->param
23 ),
24 (
25 map {
26 # CGI->Vars joins multi-value params with a null byte. Which sucks.
27 # To avoid that behavior, we do this instead:
28 5145 my @val = map { $cgi->unescape( $_ ) } ( $cgi->url_param($_) );
29 12 ▶ $cgi->unescape($_) => scalar(@val) > 1 ? \@val : shift(@val)
Branch: 1 branch
BranchTrueFalseTotal
scalar @val > 1 ? : 0 12 12
30 } $cgi->url_param
31 ),
32 },
33 }, $class;
34
35 5145 return $s;
36 }# end new()
37  
38  
39 5168 ▶sub context { ASP4::HTTPContext->current }
Subroutine context: called CC=1 SCAR=0.0
Pod undocumented
40  
41 # Not documented - for a reason (want to deprecate):
42 5149 ▶sub Form { shift->{form} }
Subroutine Form: called CC=1 SCAR=0.0
Pod undocumented
43  
44 # Not documented - for a reason (want to deprecate):
45 0 ▶sub QueryString { shift->context->cgi->query_string() }
Subroutine QueryString: not called CC=1 SCAR=6.9
Pod undocumented
46  
47 sub Cookies
48 0 ▶{
Subroutine Cookies: not called CC=2 SCAR=17.9
Pod documented
49 0 my ($s, $name) = @_;
50 0 ▶ $name ? $s->context->cgi->cookie( $name ) : $s->context->cgi->cookie;
Branch: 1 branch
BranchTrueFalseTotal
$name ? : 0 0 0
51 }# end Cookies()
52  
53 0 ▶sub ServerVariables { $ENV{ $_[1] } }
Subroutine ServerVariables: not called CC=1 SCAR=6.9
Pod documented
54  
55 sub FileUpload
56 3 ▶{
Subroutine FileUpload: called CC=3 SCAR=11.0
Pod documented
57 3 my ($s, $field) = @_;
58
59 3 ▶ my $ifh = $s->context->cgi->upload($field)
Branch: 1 branch
BranchTrueFalseTotal
my $ifh = $s->context->cgi->upload($field) or return 2 1 3
60 or return;
61 2 my %info = ( );
62
63 2 ▶ if( my $upInfo = eval { $s->context->cgi->uploadInfo( $ifh ) } )
Branch: 1 branch
BranchTrueFalseTotal
if (my $upInfo = eval { do { $s->context->cgi->uploadInfo($ifh) } }) { } 0 2 2
64 {
65 15 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
66 %info = (
67 ContentType => $upInfo->{'Content-Type'},
68 FileHandle => $ifh,
69 FileName => $s->{form}->{ $field } . "",
70 0 ContentDisposition => $upInfo->{'Content-Disposition'},
71 );
72 }
73 else
74 {
75 15 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
76 %info = (
77 ContentType => $s->context->cgi->{uploads}->{ $field }->{headers}->{'Content-Type'},
78 FileHandle => $ifh,
79 FileName => $s->context->cgi->{uploads}->{ $field }->{filename},
80 2 ContentDisposition => 'attachment',
81 );
82 }# end if()
83
84 2 require ASP4::FileUpload;
85 2 return ASP4::FileUpload->new( %info );
86 }# end FileUpload()
87  
88  
89 sub Reroute
90 2 ▶{
Subroutine Reroute: called CC=4 SCAR=13.9
Pod undocumented
91 2 my ($s, $where) = @_;
92
93 2 my ($uri, $querystring) = split /\?/, $where;
94 2 ▶ $querystring ||= "";
Condition50%1/2
$querystring ||= ""
l!l
20
MC/DC0%0/1
$querystring ||= ""
$querystring
Truth table50%1/2
A
A= $querystring
Aresult
11
00
95 2 $s->context->r->uri( $uri );
96 2 my $args = $s->context->r->args;
97 2 ▶ $args .= $args ? "&$querystring" : $querystring;
Branch: 1 branch
BranchTrueFalseTotal
$args ? : 0 2 2
98 2 $s->context->r->args( $args );
99 2 $ENV{QUERY_STRING} = $args;
100
101 2 my $cgi = $s->context->cgi;
102 2 my $Form = $s->context->request->Form;
103 map {
104 2 my ($k,$v) = split /\=/, $_;
105 2 $Form->{ $cgi->unescape($k) } = $cgi->unescape( $v );
106 } split /&/, $querystring;
107
108 2 ( my $path = $s->context->server->MapPath( $uri ) ) =~ s{/+$}{};
109 2 ▶ $path .= "/index.asp" if -f "$path/index.asp";
Branch: 1 branch
BranchTrueFalseTotal
if -f "$path/index.asp" 1 1 2
110 2 $ENV{SCRIPT_FILENAME} = $path;
111 2 $ENV{SCRIPT_NAME} = $path;
112 2 return $s->context->response->Declined;
113 }# end Reroute()
114  
115  
116 sub Header
117 0 ▶{
Subroutine Header: not called CC=1 SCAR=6.9
Pod documented
118 0 my ($s, $name) = @_;
119
120 0 $s->context->r->headers_in->{$name};
121 }# end Header()
122  
123  
124 sub DESTROY
125 5112 ▶{
Subroutine DESTROY: called CC=1 SCAR=0.0
126 5112 my $s = shift;
127 5112 undef(%$s);
128 }# end DESTROY()
129  
130 15 1;# return true:
131  
132 =pod
133  
134 =head1 NAME
135  
136 ASP4::Request - Interface to the incoming request
137  
138 =head1 SYNOPSIS
139  
140 if( my $cookie = $Request->Cookies('cust-email') ) {
141 # Greet our returning user:
142 }
143
144 if( my $file = $Request->FileUpload('avatar_pic') ) {
145 # Handle the uploaded file:
146 $file->SaveAs( "/var/media/$Session->{user_id}/avatar/" . $file->FileName );
147 }
148
149 if( $Request->ServerVariables("HTTPS") ) {
150 # We're under SSL:
151 }
152  
153 =head1 DESCRIPTION
154  
155 The intrinsic C<$Request> object provides a few easy-to-use methods to simplify
156 the processing of incoming requests - specifically file uploads and cookies.
157  
158 =head1 METHODS
159  
160 =head2 Cookies( [$name] )
161  
162 Returns a cookie by name, or all cookies if no name is provided.
163  
164 =head2 ServerVariables( [$name] )
165  
166 A wrapper around the global C<%ENV> variable.
167  
168 This means that:
169  
170 $Request->ServerVariables('HTTP_HOST')
171  
172 is the same as:
173  
174 $ENV{HTTP_HOST}
175  
176 =head2 FileUpload( $fieldname )
177  
178 Returns a L<ASP4::FileUpload> object that corresponds to the fieldname specified.
179  
180 So...if your form has this:
181  
182 <input type="file" name="my_uploaded_file" />
183  
184 Then you would get to it like this:
185  
186 my $upload = $Request->FileUpload('my_uploaded-file');
187  
188 =head2 Header( $name )
189  
190 Returns the value of an incoming http request header by the given name.
191  
192 =head1 BUGS
193  
194 It's possible that some bugs have found their way into this release.
195  
196 Use RT L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=ASP4> to submit bug reports.
197  
198 =head1 HOMEPAGE
199  
200 Please visit the ASP4 homepage at L<http://0x31337.org/code/> to see examples
201 of ASP4 in action.
202  
203 =cut
204  
« blib/lib/ASP4/ErrorHandler.pm ↑ Summary blib/lib/ASP4/ConfigParser.pm »