Prancer/Request.pm

filter: Statementstmt 96.4% 109 / 113 Branchbran 95.0% 19 / 20 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 93.7% 30 / 32 Podpod 81.8% 18 / 22 totaltotal 93.2% 179 / 192 CC 14 SCAR 6.6
CC
14
Cov
96%
uri_for
4.0
_parse_cookies
3.0
param
3.0
CRAP 1.9
« Prancer/Plugin.pm ↑ Summary Prancer/Request/Upload.pm »
1 package Prancer::Request;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 our $VERSION = '1.05';
8  
9 3 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Hash::MultiValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use URI::Escape ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use Prancer::Request::Upload;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # even though this *should* work automatically, it was not
17 3 our @CARP_NOT = qw(Prancer Try::Tiny);
18  
19 8 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
20 8 my ($class, $env) = @_;
21 8 my $self = bless({
22 '_env' => $env,
23 '_request' => Plack::Request->new($env),
24 }, $class);
25  
26 # make instances of these and return those. these calls create new URI objects
27 # with every invocation so this should avoid creating unnecessary objects later
28 8 $self->{'_uri'} = $self->{'_request'}->uri();
29 8 $self->{'_base'} = $self->{'_request'}->base();
30  
31 # other manipulation routines
32 8 $self->{'_uploads'} = $self->_parse_uploads();
33 8 $self->{'_cookies'} = $self->_parse_cookies();
34  
35 8 return $self;
36 }
37  
38 8 ▶sub _parse_uploads {
Subroutine _parse_uploads: called CC=2 SCAR=6.9
39 8 my $self = shift;
40  
41 # turn all uploads into Prancer::Upload objects
42 8 my $result = Hash::MultiValue->new();
43 8 my $uploads = $self->{'_request'}->uploads();
44 8 for my $key (keys %{$uploads}) {
45 2 $result->add($key, map { Prancer::Request::Upload->new($_) } $uploads->get_all($key));
46 }
47  
48 8 return $result;
49 }
50  
51 8 ▶sub _parse_cookies {
Subroutine _parse_cookies: called CC=3 SCAR=11.0
52 8 my $self = shift;
53  
54 8 my $result = Hash::MultiValue->new();
55 8 ▶ return $result unless defined($self->{'_env'}->{'HTTP_COOKIE'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_env"}{"HTTP_COOKIE"} 7 1 8
56  
57 # translate all cookies
58 1 my @pairs = grep { m/=/x } split(/[;,]\s?/x, $self->{'_env'}->{'HTTP_COOKIE'});
59 1 for my $pair (@pairs) {
60 # trim leading and trailing whitespace
61 1 $pair =~ s/^\s+|\s+$//xg;
62  
63 1 my ($key, $value) = map { URI::Escape::uri_unescape($_) } split(/=/x, $pair, 2);
64 1 $result->add($key, $value);
65 }
66  
67 1 return $result;
68 }
69  
70 0 ▶sub env {
Subroutine env: not called CC=1 SCAR=6.9
Pod undocumented
71 0 my $self = shift;
72 0 return $self->{'_env'};
73 }
74  
75 1 ▶sub uri {
Subroutine uri: called CC=1 SCAR=0.0
Pod documented
76 1 my $self = shift;
77 1 return $self->{'_uri'};
78 }
79  
80 4 ▶sub base {
Subroutine base: called CC=1 SCAR=0.0
Pod documented
81 4 my $self = shift;
82 4 return $self->{'_base'};
83 }
84  
85 1 ▶sub method {
Subroutine method: called CC=1 SCAR=0.0
Pod documented
86 1 my $self = shift;
87 1 return $self->{'_request'}->method();
88 }
89  
90 1 ▶sub protocol {
Subroutine protocol: called CC=1 SCAR=0.0
Pod documented
91 1 my $self = shift;
92 1 return $self->{'_request'}->protocol();
93 }
94  
95 1 ▶sub scheme {
Subroutine scheme: called CC=1 SCAR=0.0
Pod documented
96 1 my $self = shift;
97 1 return $self->{'_request'}->scheme();
98 }
99  
100 1 ▶sub port {
Subroutine port: called CC=1 SCAR=0.0
Pod undocumented
101 1 my $self = shift;
102 1 return $self->{'_request'}->port();
103 }
104  
105 1 ▶sub secure {
Subroutine secure: called CC=2 SCAR=7.2
Pod documented
106 1 my $self = shift;
107 1 ▶ return ($self->{'_request'}->secure() ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_request'}->secure ? : 0 1 1
108 }
109  
110 1 ▶sub path {
Subroutine path: called CC=1 SCAR=0.0
Pod documented
111 1 my $self = shift;
112 1 return $self->{'_request'}->path();
113 }
114  
115 1 ▶sub body {
Subroutine body: called CC=1 SCAR=0.0
Pod documented
116 1 my $self = shift;
117 1 return $self->{'_request'}->body();
118 }
119  
120 1 ▶sub content {
Subroutine content: called CC=1 SCAR=0.0
Pod undocumented
121 1 my $self = shift;
122 1 return $self->{'_request'}->raw_body();
123 }
124  
125 1 ▶sub address {
Subroutine address: called CC=1 SCAR=0.0
Pod documented
126 1 my $self = shift;
127 1 return $self->{'_request'}->address();
128 }
129  
130 1 ▶sub user {
Subroutine user: called CC=1 SCAR=0.0
Pod documented
131 1 my $self = shift;
132 1 return $self->{'_request'}->user();
133 }
134  
135 0 ▶sub headers {
Subroutine headers: not called CC=1 SCAR=6.9
Pod documented
136 0 my $self = shift;
137 0 return $self->{'_request'}->headers();
138 }
139  
140 7 ▶sub param {
Subroutine param: called CC=3 SCAR=11.0
Pod documented
141 7 my $self = shift;
142  
143 # return the keys if nothing is asked for
144 7 ▶ return keys %{$self->params()} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 2 5 7
145  
146 5 my $key = shift;
147 5 ▶ return $self->params->get($key) unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 4 1 5
148 1 return $self->params->get_all($key);
149 }
150  
151 7 ▶sub params {
Subroutine params: called CC=1 SCAR=0.0
Pod documented
152 7 my $self = shift;
153 7 return $self->{'_request'}->parameters();
154 }
155  
156 5 ▶sub cookie {
Subroutine cookie: called CC=3 SCAR=11.0
Pod documented
157 5 my $self = shift;
158  
159 # return the keys if nothing is asked for
160 5 ▶ return keys %{$self->cookies()} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 2 3 5
161  
162 3 my $key = shift;
163 3 ▶ return $self->cookies->get($key) unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 2 1 3
164 1 return $self->cookies->get_all($key);
165 }
166  
167 5 ▶sub cookies {
Subroutine cookies: called CC=1 SCAR=0.0
Pod documented
168 5 my $self = shift;
169 5 return $self->{'_cookies'};
170 }
171  
172 4 ▶sub upload {
Subroutine upload: called CC=3 SCAR=11.0
Pod documented
173 4 my $self = shift;
174  
175 # return the keys if nothing is asked for
176 4 ▶ return keys %{$self->uploads()} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 2 2 4
177  
178 2 my $key = shift;
179 2 ▶ return $self->uploads->get($key) unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 1 1 2
180 1 return $self->uploads->get_all($key);
181 }
182  
183 4 ▶sub uploads {
Subroutine uploads: called CC=1 SCAR=0.0
Pod documented
184 4 my $self = shift;
185 4 return $self->{'_uploads'};
186 }
187  
188 3 ▶sub uri_for {
Subroutine uri_for: called CC=4 SCAR=13.9
Pod documented
189 3 my ($self, $path, $args) = @_;
190 3 my $uri = URI->new($self->base());
191  
192 # don't want multiple slashes clouding things up
193 3 ▶ if ($uri->path() =~ /\/$/x && $path =~ /^\//x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->path =~ m[/$]x and $path =~ m[^/]x) 2 1 3
Condition66%2/3
$uri->path =~ m[/$]x and $path =~ m[^/]x
!ll&&!rl&&r
012
MC/DC50%1/2
$uri->path =~ m[/$]x and $path =~ m[^/]x
$uri->path =~ m[/$]x $path =~ m[^/]x
Truth table66%2/3
A and B
A= $uri->path =~ m[/$]xB= $path =~ m[^/]x
ABresult
0X0
100
111
194 2 $path = substr($path, 1);
195 }
196  
197 3 $uri->path($uri->path() . $path);
198 3 ▶ $uri->query_form(@{$args}) if $args;
Branch: 1 branch
BranchTrueFalseTotal
if $args 1 2 3
199 3 return $uri;
200 }
201  
202 3 1;
203  
204 =head1 NAME
205  
206 Prancer::Request
207  
208 =head1 SYNOPSIS
209  
210 sub handler {
211 my ($self, $env, $request, $response, $session) = @_;
212  
213 sub (GET) {
214 my $path = $request->path();
215 my $cookie = $request->cookie("foo");
216 my $param = $request->param("bar");
217 my $cookie_names = $request->cookie();
218 my $user_agent = $request->headers->header("user-agent");
219  
220 ...
221  
222 return $response->finalize(200);
223 }
224 }
225  
226 =head1 METHODS
227  
228 =over
229  
230 =item uri
231  
232 Returns an URI object for the current request. The URI is constructed using
233 various environment values such as C<SCRIPT_NAME>, C<PATH_INFO>,
234 C<QUERY_STRING>, C<HTTP_HOST>, C<SERVER_NAME> and C<SERVER_PORT>.
235  
236 =item base
237  
238 Returns a URI object for the base path of current request. This is like C<uri>
239 but only contains up to C<SCRIPT_NAME> where your application is hosted at.
240  
241 =item method
242  
243 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
244  
245 =item protocol
246  
247 Returns the protocol (C<HTTP/1.0> or C<HTTP/1.1>) used for the current request.
248  
249 =item scheme
250  
251 Returns the scheme (C<http> or C<https>) of the request.
252  
253 =item secure
254  
255 Returns true or false, indicating whether the connection is secure (C<https>).
256  
257 =item path
258  
259 Returns B<PATH_INFO> in the environment but returns / in case it is empty.
260  
261 =item body
262  
263 Returns a handle to the input stream.
264  
265 =item address
266  
267 Returns the IP address of the client (C<REMOTE_ADDR>).
268  
269 =item user
270  
271 Returns C<REMOTE_USER> if it's set.
272  
273 =item headers
274  
275 Returns an L<HTTP::Headers::Fast> object containing the headers for the current
276 request.
277  
278 =item param
279  
280 When called with no arguments this will return a list of all parameter names.
281 When called in scalar context this will return the last value for the given
282 key. When called in list context this will return all values for the given key
283 in a list.
284  
285 =item params
286  
287 Returns a L<Hash::MultiValue> hash reference containing the merged GET and POST
288 parameters.
289  
290 =item cookie
291  
292 When called with no arguments this will return a list of all cookie names.
293 When called in scalar context this will return the last cookie for the given
294 key. When called in list context this will return all cookies for the given
295 key in a list.
296  
297 =item cookies
298  
299 Returns an L<Hash::MultiValue> containing all cookies.
300  
301 =item upload
302  
303 When called with no arguments this will return a list of all upload names.
304 When called in scalar context this will return the last
305 L<Prancer::Request::Upload> object for the given key. When called in list
306 context this will return all L<Prancer::Request::Upload> objects for the given
307 key.
308  
309 =item uploads
310  
311 Returns an L<Hash::MultiValue> containing all uploads.
312  
313 =item uri_for
314  
315 Generates a URL to a new location in an easy to use manner. For example:
316  
317 my $link = $request->uri_for("/logout", [ signoff => 1 ]);
318  
319 =back
320  
321 =cut
« Prancer/Plugin.pm ↑ Summary Prancer/Request/Upload.pm »