line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Prancer::Context; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
9
|
use strict; |
|
4
|
|
|
|
|
2
|
|
|
4
|
|
|
|
|
105
|
|
4
|
4
|
|
|
4
|
|
9
|
use warnings FATAL => 'all'; |
|
4
|
|
|
|
|
2
|
|
|
4
|
|
|
|
|
81
|
|
5
|
|
|
|
|
|
|
|
6
|
4
|
|
|
4
|
|
8
|
use Prancer; |
|
4
|
|
|
|
|
3
|
|
|
4
|
|
|
|
|
111
|
|
7
|
4
|
|
|
4
|
|
19
|
use Hash::Merge::Simple; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
1935
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub new { |
10
|
0
|
|
|
0
|
0
|
|
my $class = shift; |
11
|
0
|
|
|
|
|
|
my %args = @_; |
12
|
0
|
|
|
|
|
|
return bless({ |
13
|
|
|
|
|
|
|
'_env' => $args{'env'}, |
14
|
|
|
|
|
|
|
'_request' => $args{'request'}, |
15
|
|
|
|
|
|
|
'_response' => $args{'response'}, |
16
|
|
|
|
|
|
|
'_session' => $args{'session'}, |
17
|
|
|
|
|
|
|
}, $class); |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub env { |
21
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
22
|
0
|
|
|
|
|
|
return $self->{'_env'}; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub template { |
26
|
0
|
|
|
0
|
1
|
|
my ($self, $template, $vars) = @_; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
30
|
0
|
|
|
|
|
|
$vars = Hash::Merge::Simple->merge({'config' => Prancer::config()->as_hashref()}, $vars); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
|
33
|
0
|
|
|
|
|
|
$vars = Hash::Merge::Simple->merge({'session' => $self->session->as_hashref()}, $vars); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
|
$vars = Hash::Merge::Simple->merge({'params' => $self->params->as_hashref_mixed()}, $vars); |
37
|
|
|
|
|
|
|
|
38
|
0
|
|
|
|
|
|
return Prancer::template($template, $vars); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub session { |
42
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
43
|
0
|
|
|
|
|
|
return $self->{'_session'}; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub header { |
47
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
48
|
0
|
|
|
|
|
|
my %args = ( |
49
|
|
|
|
|
|
|
'get' => undef, |
50
|
|
|
|
|
|
|
'set' => undef, |
51
|
|
|
|
|
|
|
'value' => undef, |
52
|
|
|
|
|
|
|
@_ |
53
|
|
|
|
|
|
|
); |
54
|
|
|
|
|
|
|
|
55
|
0
|
0
|
|
|
|
|
if (defined($args{'get'})) { |
56
|
0
|
|
|
|
|
|
my $x = $self->{'_request'}; |
57
|
0
|
|
|
|
|
|
return $x->header($args{'get'}); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
0
|
0
|
|
|
|
|
if (defined($args{'set'})) { |
61
|
0
|
|
|
|
|
|
my $x = $self->{'_response'}; |
62
|
0
|
|
|
|
|
|
return $x->header($args{'set'} => $args{'value'}); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
|
return; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub cookie { |
69
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
70
|
0
|
|
|
|
|
|
my %args = ( |
71
|
|
|
|
|
|
|
'get' => undef, |
72
|
|
|
|
|
|
|
'set' => undef, |
73
|
|
|
|
|
|
|
'value' => undef, |
74
|
|
|
|
|
|
|
@_ |
75
|
|
|
|
|
|
|
); |
76
|
|
|
|
|
|
|
|
77
|
0
|
0
|
|
|
|
|
if (defined($args{'get'})) { |
78
|
0
|
|
|
|
|
|
my $x = $self->{'_request'}; |
79
|
0
|
|
|
|
|
|
return $x->cookie($args{'get'}); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
0
|
0
|
|
|
|
|
if (defined($args{'set'})) { |
83
|
0
|
|
|
|
|
|
my $x = $self->{'_response'}; |
84
|
0
|
|
|
|
|
|
return $x->cookie($args{'set'} => $args{'value'}); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
|
return; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub request { |
91
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
92
|
0
|
|
|
|
|
|
return $self->{'_request'}; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub param { |
96
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
97
|
0
|
|
|
|
|
|
return $self->{'_request'}->param(@_); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub params { |
101
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
102
|
0
|
|
|
|
|
|
return $self->{'_request'}->params(@_); |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub upload { |
106
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
107
|
0
|
|
|
|
|
|
return $self->{'_request'}->upload(@_); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub uploads { |
111
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
112
|
0
|
|
|
|
|
|
return $self->{'_request'}->uploads(@_); |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub response { |
116
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
117
|
0
|
|
|
|
|
|
return $self->{'_response'}; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub body { |
121
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
122
|
0
|
|
|
|
|
|
return $self->{'_response'}->body(@_); |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub finalize { |
126
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
127
|
0
|
|
|
|
|
|
return $self->{'_response'}->finalize(@_); |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
1; |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head1 NAME |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Prancer::Context |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 SYNOPSIS |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
The context gives you access to all pieces of a request from request parameters |
139
|
|
|
|
|
|
|
to cookies, sessions and headers. It can be used by calling C<context> from |
140
|
|
|
|
|
|
|
anywhere in a package that extends L<Prancer::Application>. Otherwise you must |
141
|
|
|
|
|
|
|
pass it to other packages to make it available. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
use Prancer::Application qw(:all); |
144
|
|
|
|
|
|
|
use parent qw(Prancer::Application); |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub handle { |
147
|
|
|
|
|
|
|
my ($self, $env) = @_; |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
mount('GET', '/', sub { |
150
|
|
|
|
|
|
|
context->header(set => 'Content-Type', value => 'text/plain'); |
151
|
|
|
|
|
|
|
context->body("hello world"); |
152
|
|
|
|
|
|
|
context->finalize(200); |
153
|
|
|
|
|
|
|
}); |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
return dispatch; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head1 METHODS |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=over 4 |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=item env |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Returns the PSGI environment for the request. |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=item template |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
This is a magical wrapper to C<Prancer::template()> in that it works by calling |
169
|
|
|
|
|
|
|
C<Prancer::template> but it will merge C<params>, C<config>, and C<session> |
170
|
|
|
|
|
|
|
into the list of template variables using their respective names as the first |
171
|
|
|
|
|
|
|
key. This allows you to write this in your template: |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
<% params.bar %> |
174
|
|
|
|
|
|
|
<% config.foo.bar %> |
175
|
|
|
|
|
|
|
<% session.asdf.fdsa %> |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=item session |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
This gives access to the session in various ways. For example: |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
my $does_foo_exist = context->session->has('foo'); |
182
|
|
|
|
|
|
|
my $foo = context->session->get('foo'); |
183
|
|
|
|
|
|
|
context->session->set('foo', 'bar'); |
184
|
|
|
|
|
|
|
context->session->remove('foo'); |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
Changes made to the session are persisted immediately to whatever medium |
187
|
|
|
|
|
|
|
backs your sessions. |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=item request |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
Returns the L<Prancer::Request> object for the request. |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=item response |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
Returns the L<Prancer::Response> object that will be used to generate the |
196
|
|
|
|
|
|
|
response. |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=item header |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
This gives access request and response headers. For example: |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
# get a request header |
203
|
|
|
|
|
|
|
my $useragent = context->header(get => 'user-agent'); |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
# set a response header |
206
|
|
|
|
|
|
|
context->header(set => 'Content-Type', value => 'text/plain'); |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=item cookie |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
This gives access to request and response cookies. For example: |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
# get a request cookie |
213
|
|
|
|
|
|
|
my $foo = context->cookie(get => 'foo'); |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
# set a response cookie |
216
|
|
|
|
|
|
|
context->cookie(set => 'foo', value => { |
217
|
|
|
|
|
|
|
'value' => 'bar', |
218
|
|
|
|
|
|
|
'domain' => '.example.com', |
219
|
|
|
|
|
|
|
}); |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=item param |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
This is a wrapper around the C<param> method to L<Prancer::Request>. |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=item params |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
This is a wrapper around the C<params> method to L<Prancer::Request>. |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=item upload |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
This is a wrapper around the C<upload> method to L<Prancer::Request>. |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=item uploads |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
This is a wrapper around the C<uploads> method to L<Prancer::Request>. |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=item body |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
This is a wrapper around the C<body> method to L<Prancer::Response>. |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=item finalize |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
This is a wrapper around the C<finalize> method to L<Prancer::Response>. |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=back |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=cut |
248
|
|
|
|
|
|
|
|