File Coverage

blib/lib/Prancer/Request.pm
Criterion Covered Total %
statement 21 106 19.8
branch 0 20 0.0
condition n/a
subroutine 7 30 23.3
pod 19 21 90.5
total 47 177 26.6


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