| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Leyland::Context; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: The working environment of an HTTP request and Leyland response |
|
4
|
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
711
|
use Moo; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
7
|
|
|
6
|
1
|
|
|
1
|
|
401
|
use namespace::clean; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
9
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
209
|
use Carp; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
81
|
|
|
9
|
1
|
|
|
1
|
|
186574
|
use Data::Dumper; |
|
|
1
|
|
|
|
|
9128
|
|
|
|
1
|
|
|
|
|
96
|
|
|
10
|
1
|
|
|
1
|
|
12
|
use JSON; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
10
|
|
|
11
|
1
|
|
|
1
|
|
800
|
use Leyland::Exception; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
49
|
|
|
12
|
1
|
|
|
1
|
|
687
|
use Leyland::Logger; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
34
|
|
|
13
|
1
|
|
|
1
|
|
8
|
use Module::Load; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
10
|
|
|
14
|
1
|
|
|
1
|
|
45
|
use Text::SpanningTable; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
23
|
|
|
15
|
1
|
|
|
1
|
|
10
|
use Try::Tiny; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
86
|
|
|
16
|
1
|
|
|
1
|
|
164978
|
use XML::TreePP; |
|
|
1
|
|
|
|
|
13445
|
|
|
|
1
|
|
|
|
|
4338
|
|
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
extends 'Plack::Request'; |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 NAME |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
Leyland::Context - The working environment of an HTTP request and Leyland response |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# every request automatically gets a Leyland::Context object, which |
|
27
|
|
|
|
|
|
|
# is available in your routes and your views: |
|
28
|
|
|
|
|
|
|
post '^/blog$' { |
|
29
|
|
|
|
|
|
|
my $post = Blog->new( |
|
30
|
|
|
|
|
|
|
subject => $c->params->{subject}, |
|
31
|
|
|
|
|
|
|
user => $c->user, |
|
32
|
|
|
|
|
|
|
date => DateTime->now, |
|
33
|
|
|
|
|
|
|
text => $c->params->{text} |
|
34
|
|
|
|
|
|
|
); |
|
35
|
|
|
|
|
|
|
return $c->template('blog.html', { post => $post }); |
|
36
|
|
|
|
|
|
|
} |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# by the way, since Leyland is RESTful, your application can accept |
|
39
|
|
|
|
|
|
|
# requests of any content type, making the above something like |
|
40
|
|
|
|
|
|
|
# this: |
|
41
|
|
|
|
|
|
|
post '^/blog$' accepts 'text/plain' { |
|
42
|
|
|
|
|
|
|
my $post = Blog->new( |
|
43
|
|
|
|
|
|
|
subject => $c->params->{subject}, |
|
44
|
|
|
|
|
|
|
user => $c->user, |
|
45
|
|
|
|
|
|
|
date => DateTime->now, |
|
46
|
|
|
|
|
|
|
text => $c->data |
|
47
|
|
|
|
|
|
|
); |
|
48
|
|
|
|
|
|
|
return $c->template('blog.html', { post => $post }); |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
The Leyland context object is the heart and soul of your application. Or |
|
54
|
|
|
|
|
|
|
something. Anyway, it's an object that escorts an HTTP request to somewhere |
|
55
|
|
|
|
|
|
|
in your application through its entire lifetime, up to the point where an |
|
56
|
|
|
|
|
|
|
HTTP response is sent back to the client. This is quite similar to the |
|
57
|
|
|
|
|
|
|
L context object. |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
The context object holds a lot of information about the request, such as |
|
60
|
|
|
|
|
|
|
its content type, its method, the parameters/content provided with it, |
|
61
|
|
|
|
|
|
|
the routes it matched in your application, etc. Your controllers and views |
|
62
|
|
|
|
|
|
|
will mostly interact with this object, which apart from the information |
|
63
|
|
|
|
|
|
|
mentioned just now, provides you with useful methods to generate the final |
|
64
|
|
|
|
|
|
|
response, and perform other necessary operations such as logging. |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
The Leyland context object inherits from L, so you can |
|
67
|
|
|
|
|
|
|
use all of its attributes and methods. But keep in mind that this class |
|
68
|
|
|
|
|
|
|
performs some modifications on these methods, all documented in the |
|
69
|
|
|
|
|
|
|
L"PLACK MODIFICATIONS"> section. |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
This document is for reference purposes only, please refer to L |
|
72
|
|
|
|
|
|
|
for more information on using the context object. |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head1 EXTENDS |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
L |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head2 app |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Holds the object of the application. |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head2 cwe |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
Holds the L environment in which the application is running. This |
|
87
|
|
|
|
|
|
|
is the C environment variable. See the C<-E> or C<--env> switch |
|
88
|
|
|
|
|
|
|
in L for more information. Defaults to 'development'. |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head2 res |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
The L object used to respond to the client. |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head2 routes |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
An array reference of all routes matched by the request (there can be |
|
97
|
|
|
|
|
|
|
more than one). |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head2 current_route |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
The index of the route to be invoked in the "routes" attribute above. By |
|
102
|
|
|
|
|
|
|
default this would be the first route (i.e. index 0), unless Ces are |
|
103
|
|
|
|
|
|
|
performed. |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head2 froutes |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
An array reference of all routes matched by an internal forward. |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head2 current_froute |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
The index of the route to be forwarded to in the "froutes" attribute above. |
|
112
|
|
|
|
|
|
|
By default this would be the first route (i.e. index 0), unless Ces |
|
113
|
|
|
|
|
|
|
are performed. |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head2 controller |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
The controller class of the current route. |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head2 wanted_mimes |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
An array reference of all media types the client accepts, ordered by |
|
122
|
|
|
|
|
|
|
the client's preference, as defined by the "Accept" HTTP header. |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head2 want |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
The media type the L has decided to return to the |
|
127
|
|
|
|
|
|
|
client. |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head2 lang |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
The language to use when localizing responses, probably according to the |
|
132
|
|
|
|
|
|
|
client's wishes. Defaults to 'en' for English. |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head2 stash |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
A hash-ref of data meant to be available for the views/templates when |
|
137
|
|
|
|
|
|
|
rendering resources. |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head2 user |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
Something (anything really) that describes the user that initiated the |
|
142
|
|
|
|
|
|
|
request. This attribute will only be defined by the application, you are |
|
143
|
|
|
|
|
|
|
free to choose whatever scheme of authentication as you wish, this attribute |
|
144
|
|
|
|
|
|
|
is provided for your convenience. You will use the C method |
|
145
|
|
|
|
|
|
|
to set the value of this attribute. |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head2 json |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
A L object for usage by routes as they see fit. |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head2 xml |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
An L object for usage by routes as they see fit. |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head2 _pass_next |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
Holds a boolean value indicating whether the route has decided to pass |
|
158
|
|
|
|
|
|
|
the request to the next matching route. Not to be used directly. |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head2 _data |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
Holds the content of the HTTP request for POST and PUT requests after |
|
163
|
|
|
|
|
|
|
parsing. Not to be used directly. |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=cut |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
has 'app' => ( |
|
168
|
|
|
|
|
|
|
is => 'ro', |
|
169
|
|
|
|
|
|
|
isa => sub { die "app must be a Leyland object" unless ref $_[0] && $_[0]->isa('Leyland') }, |
|
170
|
|
|
|
|
|
|
required => 1, |
|
171
|
|
|
|
|
|
|
handles => ['cwe', 'views', 'config'] |
|
172
|
|
|
|
|
|
|
); |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
has 'res' => ( |
|
175
|
|
|
|
|
|
|
is => 'lazy', |
|
176
|
|
|
|
|
|
|
isa => sub { die "res must be a Plack::Response object" unless ref $_[0] && $_[0]->isa('Plack::Response') } |
|
177
|
|
|
|
|
|
|
); |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
has 'routes' => ( |
|
180
|
|
|
|
|
|
|
is => 'ro', |
|
181
|
|
|
|
|
|
|
isa => sub { die "routes must be an array-ref" unless ref $_[0] && ref $_[0] eq 'ARRAY' }, |
|
182
|
|
|
|
|
|
|
predicate => 'has_routes', |
|
183
|
|
|
|
|
|
|
writer => '_set_routes' |
|
184
|
|
|
|
|
|
|
); |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
has 'current_route' => ( |
|
187
|
|
|
|
|
|
|
is => 'ro', |
|
188
|
|
|
|
|
|
|
isa => sub { die "current_route must be an integer" unless $_[0] =~ m/^\d+$/ }, |
|
189
|
|
|
|
|
|
|
default => sub { 0 }, |
|
190
|
|
|
|
|
|
|
writer => '_set_current_route' |
|
191
|
|
|
|
|
|
|
); |
|
192
|
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
has 'froutes' => ( |
|
194
|
|
|
|
|
|
|
is => 'ro', |
|
195
|
|
|
|
|
|
|
isa => sub { die "froutes must be an array-ref" unless ref $_[0] && ref $_[0] eq 'ARRAY' }, |
|
196
|
|
|
|
|
|
|
predicate => 'has_froutes', |
|
197
|
|
|
|
|
|
|
writer => '_set_froutes', |
|
198
|
|
|
|
|
|
|
clearer => '_clear_froutes' |
|
199
|
|
|
|
|
|
|
); |
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
has 'current_froute' => ( |
|
202
|
|
|
|
|
|
|
is => 'ro', |
|
203
|
|
|
|
|
|
|
isa => sub { die "current_froute must be an integer" unless $_[0] =~ m/^\d+$/ }, |
|
204
|
|
|
|
|
|
|
default => sub { 0 }, |
|
205
|
|
|
|
|
|
|
writer => '_set_current_froute' |
|
206
|
|
|
|
|
|
|
); |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
has 'controller' => ( |
|
209
|
|
|
|
|
|
|
is => 'ro', |
|
210
|
|
|
|
|
|
|
isa => sub { die "controller must be a scalar" if ref $_[0] }, |
|
211
|
|
|
|
|
|
|
writer => '_set_controller' |
|
212
|
|
|
|
|
|
|
); |
|
213
|
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
has 'wanted_mimes' => ( |
|
215
|
|
|
|
|
|
|
is => 'ro', |
|
216
|
|
|
|
|
|
|
isa => sub { die "wanted_mimes must be an array-ref" unless ref $_[0] && ref $_[0] eq 'ARRAY' }, |
|
217
|
|
|
|
|
|
|
builder => '_build_mimes' |
|
218
|
|
|
|
|
|
|
); |
|
219
|
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
has 'want' => ( |
|
221
|
|
|
|
|
|
|
is => 'ro', |
|
222
|
|
|
|
|
|
|
isa => sub { die "want must be a scalar" if ref $_[0] }, |
|
223
|
|
|
|
|
|
|
writer => '_set_want' |
|
224
|
|
|
|
|
|
|
); |
|
225
|
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
has 'lang' => ( |
|
227
|
|
|
|
|
|
|
is => 'ro', |
|
228
|
|
|
|
|
|
|
isa => sub { die "lang must be a scalar" if ref $_[0] }, |
|
229
|
|
|
|
|
|
|
writer => 'set_lang', |
|
230
|
|
|
|
|
|
|
default => sub { 'en' } |
|
231
|
|
|
|
|
|
|
); |
|
232
|
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
has 'stash' => ( |
|
234
|
|
|
|
|
|
|
is => 'ro', |
|
235
|
|
|
|
|
|
|
isa => sub { die "stash must be an hash-ref" unless ref $_[0] && ref $_[0] eq 'HASH' }, |
|
236
|
|
|
|
|
|
|
default => sub { {} } |
|
237
|
|
|
|
|
|
|
); |
|
238
|
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
has 'user' => ( |
|
240
|
|
|
|
|
|
|
is => 'ro', |
|
241
|
|
|
|
|
|
|
predicate => 'has_user', |
|
242
|
|
|
|
|
|
|
writer => 'set_user', |
|
243
|
|
|
|
|
|
|
clearer => 'clear_user' |
|
244
|
|
|
|
|
|
|
); |
|
245
|
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
has 'log' => ( |
|
247
|
|
|
|
|
|
|
is => 'lazy', |
|
248
|
|
|
|
|
|
|
isa => sub { die "log must be a Leyland::Logger object" unless ref $_[0] && ref $_[0] eq 'Leyland::Logger' } |
|
249
|
|
|
|
|
|
|
); |
|
250
|
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
has 'json' => ( |
|
252
|
|
|
|
|
|
|
is => 'ro', |
|
253
|
|
|
|
|
|
|
isa => sub { die "json must be a JSON object" unless ref $_[0] && ref $_[0] eq 'JSON' }, |
|
254
|
|
|
|
|
|
|
default => sub { JSON->new->utf8(0)->convert_blessed } |
|
255
|
|
|
|
|
|
|
); |
|
256
|
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
has 'xml' => ( |
|
258
|
|
|
|
|
|
|
is => 'ro', |
|
259
|
|
|
|
|
|
|
isa => sub { die "xml must be an XML::TreePP object" unless ref $_[0] && ref $_[0] eq 'XML::TreePP' }, |
|
260
|
|
|
|
|
|
|
default => sub { my $xml = XML::TreePP->new(); $xml->set(utf8_flag => 1); return $xml; } |
|
261
|
|
|
|
|
|
|
); |
|
262
|
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
has '_pass_next' => ( |
|
264
|
|
|
|
|
|
|
is => 'ro', |
|
265
|
|
|
|
|
|
|
default => sub { 0 }, |
|
266
|
|
|
|
|
|
|
writer => '_set_pass_next' |
|
267
|
|
|
|
|
|
|
); |
|
268
|
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
has '_data' => ( |
|
270
|
|
|
|
|
|
|
is => 'ro', |
|
271
|
|
|
|
|
|
|
predicate => '_has_data', |
|
272
|
|
|
|
|
|
|
writer => '_set_data' |
|
273
|
|
|
|
|
|
|
); |
|
274
|
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
=head1 OBJECT METHODS |
|
276
|
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
Since this class extends L, it inherits all its methods, |
|
278
|
|
|
|
|
|
|
so refer to Plack::Request for a full list. However, this module performs |
|
279
|
|
|
|
|
|
|
some modifications on certain Plack::Request methods, all of which are |
|
280
|
|
|
|
|
|
|
documented in the L"PLACK MODIFICATIONS"> section. |
|
281
|
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
=head2 leyland |
|
283
|
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
An alias for the "app" attribute. |
|
285
|
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
=cut |
|
287
|
|
|
|
|
|
|
|
|
288
|
0
|
|
|
0
|
1
|
|
sub leyland { shift->app } |
|
289
|
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
=head2 has_routes() |
|
291
|
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
Returns a true value if the request matched any routes. |
|
293
|
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
=head2 has_froutes() |
|
295
|
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
Returns a true value if the request has routes matched in an internal forward. |
|
297
|
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
=head2 set_lang( $lang ) |
|
299
|
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
Sets the language to be used for localization. |
|
301
|
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
=head2 has_user() |
|
303
|
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
Returns a true value if the "user" argument has a value. |
|
305
|
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
=head2 set_user( $user ) |
|
307
|
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
Sets the "user" argument with a new value. This value could be anything, |
|
309
|
|
|
|
|
|
|
a simple string/number, a hash-ref, an object, whatever your app uses. |
|
310
|
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
=head2 clear_user() |
|
312
|
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
Clears the value of the "user" argument, if any. Useful for logout actions. |
|
314
|
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
=head2 params() |
|
316
|
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
A shortcut for C<< $c->parameters->as_hashref_mixed >>. Note that this |
|
318
|
|
|
|
|
|
|
is read-only, so changes you make to the hash-ref returned by this method |
|
319
|
|
|
|
|
|
|
are not stored. For example, if you run C<< $c->params->{something} = 'whoa' >>, |
|
320
|
|
|
|
|
|
|
subsequent calls to C<< $c->params >> will not have the "something" |
|
321
|
|
|
|
|
|
|
key. |
|
322
|
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
=cut |
|
324
|
|
|
|
|
|
|
|
|
325
|
0
|
|
|
0
|
1
|
|
sub params { shift->parameters->as_hashref_mixed } |
|
326
|
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
=head2 data( [ $dont_parse ] ) |
|
328
|
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
Returns the data of the request for POST and PUT requests. If the data |
|
330
|
|
|
|
|
|
|
is JSON or XML, this module will attempt to automatically convert it to a Perl |
|
331
|
|
|
|
|
|
|
data structure, which will be returned by this method (if conversion will |
|
332
|
|
|
|
|
|
|
fail, this method will return an empty hash-ref). Otherwise, the data will be returned |
|
333
|
|
|
|
|
|
|
as is. You can force this method to return the data as is even if it's |
|
334
|
|
|
|
|
|
|
JSON or XML by passing a true value to this method. |
|
335
|
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
If the request had no content, an empty hash-ref will be returned. This is different |
|
337
|
|
|
|
|
|
|
than version 0.003 and down where it would have returned C. |
|
338
|
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
=cut |
|
340
|
|
|
|
|
|
|
|
|
341
|
|
|
|
|
|
|
sub data { |
|
342
|
0
|
|
|
0
|
1
|
|
my ($self, $dont_parse) = @_; |
|
343
|
|
|
|
|
|
|
|
|
344
|
0
|
0
|
0
|
|
|
|
return {} unless $self->content_type && $self->content; |
|
345
|
|
|
|
|
|
|
|
|
346
|
0
|
0
|
|
|
|
|
return $self->_data if $self->_has_data; |
|
347
|
|
|
|
|
|
|
|
|
348
|
0
|
0
|
0
|
|
|
|
if ($self->content_type =~ m!^application/json! && !$dont_parse) { |
|
|
|
0
|
0
|
|
|
|
|
|
349
|
0
|
|
|
0
|
|
|
my $data = try { $self->json->decode($self->content) } catch { {} }; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
350
|
0
|
0
|
|
|
|
|
return unless $data; |
|
351
|
0
|
|
|
|
|
|
$self->_set_data($data); |
|
352
|
0
|
|
|
|
|
|
return $self->_data; |
|
353
|
|
|
|
|
|
|
} elsif ($self->content_type =~ m!^application/(atom+)?xml! && !$dont_parse) { |
|
354
|
0
|
|
|
0
|
|
|
my $data = try { $self->xml->parse($self->content) } catch { {} }; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
355
|
0
|
0
|
|
|
|
|
return unless $data; |
|
356
|
0
|
|
|
|
|
|
$self->_set_data($data); |
|
357
|
0
|
|
|
|
|
|
return $self->_data; |
|
358
|
|
|
|
|
|
|
} else { |
|
359
|
0
|
|
|
|
|
|
my $data = $self->content; |
|
360
|
0
|
|
|
|
|
|
$self->_set_data($data); |
|
361
|
0
|
|
|
|
|
|
return $self->_data; |
|
362
|
|
|
|
|
|
|
} |
|
363
|
|
|
|
|
|
|
|
|
364
|
0
|
|
|
|
|
|
return; |
|
365
|
|
|
|
|
|
|
} |
|
366
|
|
|
|
|
|
|
|
|
367
|
|
|
|
|
|
|
=head2 pass() |
|
368
|
|
|
|
|
|
|
|
|
369
|
|
|
|
|
|
|
Causes Leyland to invoke the next matching route, if any, after this |
|
370
|
|
|
|
|
|
|
request has finished (meaning it does not pass immediately). Since you |
|
371
|
|
|
|
|
|
|
will most likely want to pass routes immediately, use C<< return $self->pass >> |
|
372
|
|
|
|
|
|
|
in your routes to do so. |
|
373
|
|
|
|
|
|
|
|
|
374
|
|
|
|
|
|
|
=cut |
|
375
|
|
|
|
|
|
|
|
|
376
|
|
|
|
|
|
|
sub pass { |
|
377
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
378
|
|
|
|
|
|
|
|
|
379
|
|
|
|
|
|
|
# are we passing inside an internal forward or externally? |
|
380
|
|
|
|
|
|
|
# in any case, do not allow passing if we don't have routes to pass to |
|
381
|
0
|
0
|
0
|
|
|
|
if ($self->has_froutes && $self->current_froute + 1 < scalar @{$self->froutes}) { |
|
|
0
|
0
|
0
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
382
|
0
|
|
|
|
|
|
$self->_set_current_froute($self->current_froute + 1); |
|
383
|
0
|
|
|
|
|
|
$self->_set_pass_next(1); |
|
384
|
0
|
|
|
|
|
|
return 1; |
|
385
|
|
|
|
|
|
|
} elsif (!$self->has_froutes && $self->current_route + 1 < scalar @{$self->routes}) { |
|
386
|
0
|
|
|
|
|
|
$self->_set_current_route($self->current_route + 1); |
|
387
|
0
|
|
|
|
|
|
$self->_set_pass_next(1); |
|
388
|
0
|
|
|
|
|
|
return 1; |
|
389
|
|
|
|
|
|
|
} |
|
390
|
|
|
|
|
|
|
|
|
391
|
0
|
|
|
|
|
|
return 0; |
|
392
|
|
|
|
|
|
|
} |
|
393
|
|
|
|
|
|
|
|
|
394
|
|
|
|
|
|
|
=head2 render( $tmpl_name, [ \%context, $use_layout ] ) |
|
395
|
|
|
|
|
|
|
|
|
396
|
|
|
|
|
|
|
=head2 template( $tmpl_name, [ \%context, $use_layout ] ) |
|
397
|
|
|
|
|
|
|
|
|
398
|
|
|
|
|
|
|
Renders the view/template named C<$tmpl_name> using the first view class |
|
399
|
|
|
|
|
|
|
defined by the application. Anything in the C<$context> hash-ref will be |
|
400
|
|
|
|
|
|
|
automatically available inside the template, which will be rendered inside |
|
401
|
|
|
|
|
|
|
whatever layout template is defined, unless C<$use_layout> is provided |
|
402
|
|
|
|
|
|
|
and holds a false value (well, 0 really). The context object (i.e. C<$c>) |
|
403
|
|
|
|
|
|
|
will automatically be embedded in the C<$context> hash-ref under the name |
|
404
|
|
|
|
|
|
|
"c", as well as the application object (i.e. C<< $c->app >>) under the |
|
405
|
|
|
|
|
|
|
name "l". Anything in the stash (i.e. C<< $c->stash >>) will also be |
|
406
|
|
|
|
|
|
|
embedded in the context hash-ref, but keys in C<$context> take precedence |
|
407
|
|
|
|
|
|
|
to the stash, so if the stash has the key 'name' and C<$context> also has |
|
408
|
|
|
|
|
|
|
the key 'name', then the one from C<$context> will be used. |
|
409
|
|
|
|
|
|
|
|
|
410
|
|
|
|
|
|
|
Returns the rendered output. You will mostly use this at the end of your |
|
411
|
|
|
|
|
|
|
routes as the return value. |
|
412
|
|
|
|
|
|
|
|
|
413
|
|
|
|
|
|
|
The two methods are the same, C is provided as an alias for |
|
414
|
|
|
|
|
|
|
C. |
|
415
|
|
|
|
|
|
|
|
|
416
|
|
|
|
|
|
|
=cut |
|
417
|
|
|
|
|
|
|
|
|
418
|
|
|
|
|
|
|
sub render { |
|
419
|
0
|
|
|
0
|
1
|
|
my ($self, $tmpl_name, $context, $use_layout) = @_; |
|
420
|
|
|
|
|
|
|
|
|
421
|
|
|
|
|
|
|
# first, run the pre_template sub |
|
422
|
0
|
|
|
|
|
|
$self->controller->pre_template($self, $tmpl_name, $context, $use_layout); |
|
423
|
|
|
|
|
|
|
|
|
424
|
|
|
|
|
|
|
# allow passing $use_layout but not passing $context |
|
425
|
0
|
0
|
0
|
|
|
|
if (defined $context && ref $context ne 'HASH') { |
|
426
|
0
|
|
|
|
|
|
$use_layout = $context; |
|
427
|
0
|
|
|
|
|
|
$context = {}; |
|
428
|
|
|
|
|
|
|
} |
|
429
|
|
|
|
|
|
|
|
|
430
|
|
|
|
|
|
|
# default $use_layout to 1 |
|
431
|
0
|
0
|
|
|
|
|
$use_layout = 1 unless defined $use_layout; |
|
432
|
|
|
|
|
|
|
|
|
433
|
0
|
|
|
|
|
|
$context->{c} = $self; |
|
434
|
0
|
|
|
|
|
|
$context->{l} = $self->leyland; |
|
435
|
0
|
|
|
|
|
|
foreach (keys %{$self->stash}) { |
|
|
0
|
|
|
|
|
|
|
|
436
|
0
|
0
|
|
|
|
|
$context->{$_} = $self->stash->{$_} unless exists $context->{$_}; |
|
437
|
|
|
|
|
|
|
} |
|
438
|
|
|
|
|
|
|
|
|
439
|
0
|
0
|
|
|
|
|
return unless scalar @{$self->views}; |
|
|
0
|
|
|
|
|
|
|
|
440
|
|
|
|
|
|
|
|
|
441
|
0
|
|
|
|
|
|
return $self->views->[0]->render($tmpl_name, $context, $use_layout); |
|
442
|
|
|
|
|
|
|
} |
|
443
|
|
|
|
|
|
|
|
|
444
|
0
|
|
|
0
|
1
|
|
sub template { shift->render(@_) } |
|
445
|
|
|
|
|
|
|
|
|
446
|
|
|
|
|
|
|
=head2 forward( $path, [ @args ] ) |
|
447
|
|
|
|
|
|
|
|
|
448
|
|
|
|
|
|
|
Immediately forwards the request (internally) to a different location defined |
|
449
|
|
|
|
|
|
|
by C<$path>. Leyland will attempt to find routes that match the provided |
|
450
|
|
|
|
|
|
|
path (without performing HTTP negotiations for the request's method, |
|
451
|
|
|
|
|
|
|
content type, accepted media types, etc. like L does). |
|
452
|
|
|
|
|
|
|
The first matching route will be invoked, with C<@args> passed to it (if |
|
453
|
|
|
|
|
|
|
provided). The returned output (before serializing, if would have been |
|
454
|
|
|
|
|
|
|
performed had the route been invoked directly) is returned and the route |
|
455
|
|
|
|
|
|
|
from which the C has been called continues. If you don't want |
|
456
|
|
|
|
|
|
|
it to continue, simple use C<< return $c->forward('/somewhere') >>. |
|
457
|
|
|
|
|
|
|
|
|
458
|
|
|
|
|
|
|
The path should include the HTTP method of the route to forward to, by |
|
459
|
|
|
|
|
|
|
prefixing C<$path> with the method name and a colon, like so: C<< $c->forward('POST:/somewhere') >>. |
|
460
|
|
|
|
|
|
|
If a method is not provided (i.e. C<< $c->forward('/somewhere') >>), C |
|
461
|
|
|
|
|
|
|
will assume a C method. Note that this differs from version C<0.003> and |
|
462
|
|
|
|
|
|
|
down, where it would forward to the first matching route, regardless of the method. |
|
463
|
|
|
|
|
|
|
This is a safety measure so you do not accidentally forward to C routes. |
|
464
|
|
|
|
|
|
|
|
|
465
|
|
|
|
|
|
|
Note that if no routes are found, a 500 Internal Server Error will be |
|
466
|
|
|
|
|
|
|
thrown, not a 404 Not Found error, as this really is an internal server |
|
467
|
|
|
|
|
|
|
error. |
|
468
|
|
|
|
|
|
|
|
|
469
|
|
|
|
|
|
|
=cut |
|
470
|
|
|
|
|
|
|
|
|
471
|
|
|
|
|
|
|
sub forward { |
|
472
|
0
|
|
|
0
|
1
|
|
my ($self, $path) = (shift, shift); |
|
473
|
|
|
|
|
|
|
|
|
474
|
0
|
0
|
|
|
|
|
$self->exception({ code => 500, error => "You must provide a path to forward to" }) unless $path; |
|
475
|
|
|
|
|
|
|
|
|
476
|
0
|
|
|
|
|
|
my $method; |
|
477
|
|
|
|
|
|
|
|
|
478
|
0
|
0
|
|
|
|
|
if ($path =~ m/^(GET|POST|PUT|DELETE|HEAD|OPTIONS):/) { |
|
479
|
0
|
|
|
|
|
|
$method = $1; |
|
480
|
0
|
|
|
|
|
|
$path = $'; |
|
481
|
|
|
|
|
|
|
} else { |
|
482
|
0
|
|
|
|
|
|
$method = 'GET'; |
|
483
|
|
|
|
|
|
|
} |
|
484
|
|
|
|
|
|
|
|
|
485
|
0
|
|
|
|
|
|
$self->log->debug("Attempting to forward request to $path with a $method method."); |
|
486
|
|
|
|
|
|
|
|
|
487
|
0
|
|
|
|
|
|
my $routes = Leyland::Negotiator->_negotiate_path($self, { |
|
488
|
|
|
|
|
|
|
app_routes => $self->app->routes, |
|
489
|
|
|
|
|
|
|
path => $path, |
|
490
|
|
|
|
|
|
|
method => $method, |
|
491
|
|
|
|
|
|
|
internal => 1 |
|
492
|
|
|
|
|
|
|
}); |
|
493
|
|
|
|
|
|
|
|
|
494
|
0
|
|
|
|
|
|
$self->_set_froutes($routes); |
|
495
|
|
|
|
|
|
|
|
|
496
|
0
|
0
|
|
|
|
|
$self->exception({ code => 500, error => "Can't forward as no matching routes were found" }) unless scalar @$routes; |
|
497
|
|
|
|
|
|
|
|
|
498
|
0
|
|
|
|
|
|
my @pass = ($routes->[0]->{class}, $self); |
|
499
|
0
|
0
|
|
|
|
|
push(@pass, @{$routes->[0]->{captures}}) if scalar @{$routes->[0]->{captures}}; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
500
|
0
|
0
|
|
|
|
|
push(@pass, @_) if scalar @_; |
|
501
|
|
|
|
|
|
|
|
|
502
|
|
|
|
|
|
|
# invoke the first matching route |
|
503
|
0
|
|
|
|
|
|
my $ret = $routes->[0]->{code}->(@pass); |
|
504
|
|
|
|
|
|
|
|
|
505
|
|
|
|
|
|
|
# are we passing to the next matching route? |
|
506
|
|
|
|
|
|
|
# to prevent infinite loops, limit passing to no more than 100 times |
|
507
|
0
|
|
0
|
|
|
|
while ($self->_pass_next && $self->current_froute < 100) { |
|
508
|
0
|
|
|
|
|
|
$self->log->debug("Passing request to the next matching route."); |
|
509
|
|
|
|
|
|
|
|
|
510
|
|
|
|
|
|
|
# we need to pass to the next matching route. |
|
511
|
|
|
|
|
|
|
# first, let's erase the pass flag from the context |
|
512
|
|
|
|
|
|
|
# so we don't try to do this infinitely |
|
513
|
0
|
|
|
|
|
|
$self->_set_pass_next(0); |
|
514
|
|
|
|
|
|
|
# no let's invoke the route |
|
515
|
0
|
|
|
|
|
|
$ret = $routes->[$self->current_froute]->{code}->(@pass); |
|
516
|
|
|
|
|
|
|
} |
|
517
|
|
|
|
|
|
|
|
|
518
|
0
|
|
|
|
|
|
$self->_clear_froutes; |
|
519
|
0
|
|
|
|
|
|
$self->_set_current_froute(0); |
|
520
|
|
|
|
|
|
|
|
|
521
|
0
|
|
|
|
|
|
return $ret; |
|
522
|
|
|
|
|
|
|
} |
|
523
|
|
|
|
|
|
|
|
|
524
|
|
|
|
|
|
|
=head2 loc( $msg, [ @args ] ) |
|
525
|
|
|
|
|
|
|
|
|
526
|
|
|
|
|
|
|
Uses L to localize the provided string to the language |
|
527
|
|
|
|
|
|
|
defined in the "lang" attribute, possibly performing some replacements |
|
528
|
|
|
|
|
|
|
with the values provided in C<@args>. See L |
|
529
|
|
|
|
|
|
|
for more info. |
|
530
|
|
|
|
|
|
|
|
|
531
|
|
|
|
|
|
|
=cut |
|
532
|
|
|
|
|
|
|
|
|
533
|
|
|
|
|
|
|
sub loc { |
|
534
|
0
|
|
|
0
|
1
|
|
my ($self, $msg, @args) = @_; |
|
535
|
|
|
|
|
|
|
|
|
536
|
0
|
|
|
|
|
|
return $self->app->localizer->loc($msg, $self->lang, @args); |
|
537
|
|
|
|
|
|
|
} |
|
538
|
|
|
|
|
|
|
|
|
539
|
|
|
|
|
|
|
=head2 exception( \%err ) |
|
540
|
|
|
|
|
|
|
|
|
541
|
|
|
|
|
|
|
Throws a L. C<$err> must have a "code" key with the |
|
542
|
|
|
|
|
|
|
error's HTTP status code, and most likely an "error" key with a description |
|
543
|
|
|
|
|
|
|
of the error. See L for more information. |
|
544
|
|
|
|
|
|
|
|
|
545
|
|
|
|
|
|
|
=cut |
|
546
|
|
|
|
|
|
|
|
|
547
|
|
|
|
|
|
|
sub exception { |
|
548
|
0
|
|
|
0
|
1
|
|
my ($self, $err) = @_; |
|
549
|
|
|
|
|
|
|
|
|
550
|
0
|
0
|
0
|
|
|
|
$err->{location} = $err->{location}->as_string |
|
551
|
|
|
|
|
|
|
if $err->{location} && ref $err->{location} =~ m/^URI/; |
|
552
|
|
|
|
|
|
|
|
|
553
|
0
|
|
|
|
|
|
Leyland::Exception->throw($err); |
|
554
|
|
|
|
|
|
|
} |
|
555
|
|
|
|
|
|
|
|
|
556
|
|
|
|
|
|
|
=head2 uri_for( $path, [ \%query ] ) |
|
557
|
|
|
|
|
|
|
|
|
558
|
|
|
|
|
|
|
Returns a L object with the full URI to the provided path. If a |
|
559
|
|
|
|
|
|
|
C<$query> hash-ref is provided, it will be converted to a query string |
|
560
|
|
|
|
|
|
|
and used in the URI object. |
|
561
|
|
|
|
|
|
|
|
|
562
|
|
|
|
|
|
|
=cut |
|
563
|
|
|
|
|
|
|
|
|
564
|
|
|
|
|
|
|
sub uri_for { |
|
565
|
0
|
|
|
0
|
1
|
|
my ($self, $path, $args) = @_; |
|
566
|
|
|
|
|
|
|
|
|
567
|
0
|
|
|
|
|
|
my $uri = $self->base; |
|
568
|
0
|
|
|
|
|
|
my $full_path = $uri->path . $path; |
|
569
|
0
|
|
|
|
|
|
$full_path =~ s!^/!!; # remove starting slash |
|
570
|
0
|
|
|
|
|
|
$uri->path($full_path); |
|
571
|
0
|
0
|
|
|
|
|
$uri->query_form($args) if $args; |
|
572
|
|
|
|
|
|
|
|
|
573
|
0
|
|
|
|
|
|
return $uri; |
|
574
|
|
|
|
|
|
|
} |
|
575
|
|
|
|
|
|
|
|
|
576
|
|
|
|
|
|
|
=head2 finalize( \$ret ) |
|
577
|
|
|
|
|
|
|
|
|
578
|
|
|
|
|
|
|
This method is meant to be overridden by classes that extend this class, |
|
579
|
|
|
|
|
|
|
if used in your application. It is automatically called after the route |
|
580
|
|
|
|
|
|
|
has been invoked and it gets a reference to the output returned from the |
|
581
|
|
|
|
|
|
|
route (after serialization, if performed), even if this output is a scalar |
|
582
|
|
|
|
|
|
|
(like HTML text), in which case C<$ret> will be a reference to a scalar. |
|
583
|
|
|
|
|
|
|
|
|
584
|
|
|
|
|
|
|
You can use it to modify and manipulate the returned output if you wish. |
|
585
|
|
|
|
|
|
|
|
|
586
|
|
|
|
|
|
|
The default C method provided by this class does not do anything. |
|
587
|
|
|
|
|
|
|
|
|
588
|
|
|
|
|
|
|
=cut |
|
589
|
|
|
|
|
|
|
|
|
590
|
0
|
|
|
0
|
1
|
|
sub finalize { 1 } # meant to be overridden |
|
591
|
|
|
|
|
|
|
|
|
592
|
|
|
|
|
|
|
=head2 accepts( $mime ) |
|
593
|
|
|
|
|
|
|
|
|
594
|
|
|
|
|
|
|
Returns a true value if the client accepts the provided MIME type. |
|
595
|
|
|
|
|
|
|
|
|
596
|
|
|
|
|
|
|
=cut |
|
597
|
|
|
|
|
|
|
|
|
598
|
|
|
|
|
|
|
sub accepts { |
|
599
|
0
|
|
|
0
|
1
|
|
my ($self, $mime) = @_; |
|
600
|
|
|
|
|
|
|
|
|
601
|
0
|
|
|
|
|
|
foreach (@{$self->wanted_mimes}) { |
|
|
0
|
|
|
|
|
|
|
|
602
|
0
|
0
|
|
|
|
|
return 1 if $_->{mime} eq $mime; |
|
603
|
|
|
|
|
|
|
} |
|
604
|
|
|
|
|
|
|
|
|
605
|
0
|
|
|
|
|
|
return; |
|
606
|
|
|
|
|
|
|
} |
|
607
|
|
|
|
|
|
|
|
|
608
|
|
|
|
|
|
|
=head1 INTERNAL METHODS |
|
609
|
|
|
|
|
|
|
|
|
610
|
|
|
|
|
|
|
The following methods are only to be used internally: |
|
611
|
|
|
|
|
|
|
|
|
612
|
|
|
|
|
|
|
=cut |
|
613
|
|
|
|
|
|
|
|
|
614
|
0
|
|
|
0
|
|
|
sub _build_res { shift->new_response(200) } |
|
615
|
|
|
|
|
|
|
|
|
616
|
|
|
|
|
|
|
sub _build_mimes { |
|
617
|
0
|
|
|
0
|
|
|
my $self = shift; |
|
618
|
|
|
|
|
|
|
|
|
619
|
0
|
|
|
|
|
|
my @wanted_mimes; |
|
620
|
|
|
|
|
|
|
|
|
621
|
0
|
|
|
|
|
|
my $accept = $self->header('Accept'); |
|
622
|
0
|
0
|
|
|
|
|
if ($accept) { |
|
623
|
0
|
|
|
|
|
|
my @mimes = split(/, ?/, $accept); |
|
624
|
0
|
|
|
|
|
|
foreach (@mimes) { |
|
625
|
0
|
|
|
|
|
|
my ($mime, $q) = split(/;q=/, $_); |
|
626
|
0
|
0
|
|
|
|
|
$q = 1 unless defined $q; |
|
627
|
0
|
|
|
|
|
|
push(@wanted_mimes, { mime => $mime, q => $q }); |
|
628
|
|
|
|
|
|
|
} |
|
629
|
|
|
|
|
|
|
@wanted_mimes = reverse sort { |
|
630
|
0
|
0
|
0
|
|
|
|
if ($a->{q} > $b->{q}) { |
|
|
0
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
631
|
0
|
|
|
|
|
|
return 1; |
|
632
|
|
|
|
|
|
|
} elsif ($b->{q} > $a->{q}) { |
|
633
|
0
|
|
|
|
|
|
return -1; |
|
634
|
|
|
|
|
|
|
} elsif ($a->{mime} eq 'text/html' && $b->{mime} ne 'text/html') { |
|
635
|
0
|
|
|
|
|
|
return 1; |
|
636
|
|
|
|
|
|
|
} elsif ($b->{mime} eq 'text/html' && $a->{mime} ne 'text/html') { |
|
637
|
0
|
|
|
|
|
|
return -1; |
|
638
|
|
|
|
|
|
|
} elsif ($a->{mime} eq 'application/xhtml+xml' && $b->{mime} ne 'application/xhtml+xml') { |
|
639
|
0
|
|
|
|
|
|
return 1; |
|
640
|
|
|
|
|
|
|
} elsif ($b->{mime} eq 'application/xhtml+xml' && $a->{mime} ne 'application/xhtml+xml') { |
|
641
|
0
|
|
|
|
|
|
return -1; |
|
642
|
|
|
|
|
|
|
} else { |
|
643
|
0
|
|
|
|
|
|
return 0; |
|
644
|
|
|
|
|
|
|
} |
|
645
|
|
|
|
|
|
|
} @wanted_mimes; |
|
646
|
0
|
|
|
|
|
|
return \@wanted_mimes; |
|
647
|
|
|
|
|
|
|
} else { |
|
648
|
0
|
|
|
|
|
|
return []; |
|
649
|
|
|
|
|
|
|
} |
|
650
|
|
|
|
|
|
|
} |
|
651
|
|
|
|
|
|
|
|
|
652
|
|
|
|
|
|
|
sub _respond { |
|
653
|
0
|
|
|
0
|
|
|
my ($self, $status, $headers, $content) = @_; |
|
654
|
|
|
|
|
|
|
|
|
655
|
0
|
0
|
0
|
|
|
|
$self->res->status($status) if $status && $status =~ m/^\d+$/; |
|
656
|
0
|
0
|
0
|
|
|
|
$self->res->headers($headers) if $headers && ref $headers eq 'HASH'; |
|
657
|
0
|
|
|
|
|
|
$self->res->header('X-Framework' => 'Leyland '.$Leyland::DISPLAY_VERSION); |
|
658
|
0
|
0
|
|
|
|
|
if ($content) { |
|
659
|
0
|
|
|
|
|
|
my $body = Encode::encode('UTF-8', $content); |
|
660
|
0
|
|
|
|
|
|
$self->res->body($body); |
|
661
|
0
|
|
|
|
|
|
$self->res->content_length(length($body)); |
|
662
|
|
|
|
|
|
|
} |
|
663
|
|
|
|
|
|
|
|
|
664
|
0
|
|
|
|
|
|
$self->_log_response; |
|
665
|
|
|
|
|
|
|
|
|
666
|
0
|
|
|
|
|
|
return $self->res->finalize; |
|
667
|
|
|
|
|
|
|
} |
|
668
|
|
|
|
|
|
|
|
|
669
|
|
|
|
|
|
|
sub _log_request { |
|
670
|
0
|
|
|
0
|
|
|
my $self = shift; |
|
671
|
|
|
|
|
|
|
|
|
672
|
0
|
|
|
|
|
|
print STDOUT "\n", '='x80, "\n", |
|
673
|
|
|
|
|
|
|
'| New request: ', $self->method, ' ', $self->path, ' from ', $self->address, "\n", |
|
674
|
|
|
|
|
|
|
'-'x80, "\n"; |
|
675
|
|
|
|
|
|
|
} |
|
676
|
|
|
|
|
|
|
|
|
677
|
|
|
|
|
|
|
sub _log_response { |
|
678
|
0
|
|
|
0
|
|
|
my $self = shift; |
|
679
|
|
|
|
|
|
|
|
|
680
|
0
|
|
|
|
|
|
print STDOUT '-'x80, "\n", |
|
681
|
|
|
|
|
|
|
'| Response code: ', $self->res->status, ' ', $Leyland::CODES->{$self->res->status}, "\n", |
|
682
|
|
|
|
|
|
|
'| Response type: ', $self->res->content_type, "\n", |
|
683
|
|
|
|
|
|
|
'='x80, "\n"; |
|
684
|
|
|
|
|
|
|
} |
|
685
|
|
|
|
|
|
|
|
|
686
|
|
|
|
|
|
|
sub _invoke_route { |
|
687
|
0
|
|
|
0
|
|
|
my $self = shift; |
|
688
|
|
|
|
|
|
|
|
|
689
|
0
|
|
|
|
|
|
my $i = $self->current_route; |
|
690
|
|
|
|
|
|
|
|
|
691
|
0
|
|
|
|
|
|
$self->_set_controller($self->routes->[$i]->{class}); |
|
692
|
|
|
|
|
|
|
|
|
693
|
|
|
|
|
|
|
# but first invoke all 'auto' subs up to the matching route's controller |
|
694
|
0
|
|
|
|
|
|
foreach ($self->_route_parents($self->routes->[$i])) { |
|
695
|
0
|
|
|
|
|
|
$_->auto($self, @{$self->routes->[$i]->{captures}}); |
|
|
0
|
|
|
|
|
|
|
|
696
|
|
|
|
|
|
|
} |
|
697
|
|
|
|
|
|
|
|
|
698
|
|
|
|
|
|
|
# then invoke the pre_route subroutine |
|
699
|
0
|
|
|
|
|
|
$self->controller->pre_route($self, @{$self->routes->[$i]->{captures}}); |
|
|
0
|
|
|
|
|
|
|
|
700
|
|
|
|
|
|
|
|
|
701
|
|
|
|
|
|
|
# invoke the route itself |
|
702
|
0
|
|
|
|
|
|
$self->_set_want($self->routes->[$i]->{media}); |
|
703
|
0
|
|
|
|
|
|
my $ret = $self->_serialize( |
|
704
|
0
|
|
|
|
|
|
$self->routes->[$i]->{code}->($self->controller, $self, @{$self->routes->[$i]->{captures}}), |
|
705
|
|
|
|
|
|
|
$self->routes->[$i]->{media} |
|
706
|
|
|
|
|
|
|
); |
|
707
|
|
|
|
|
|
|
|
|
708
|
|
|
|
|
|
|
# invoke the post_route subroutine |
|
709
|
0
|
|
|
|
|
|
$self->controller->post_route($self, \$ret); |
|
710
|
|
|
|
|
|
|
|
|
711
|
0
|
|
|
|
|
|
return $ret; |
|
712
|
|
|
|
|
|
|
} |
|
713
|
|
|
|
|
|
|
|
|
714
|
|
|
|
|
|
|
sub _serialize { |
|
715
|
0
|
|
|
0
|
|
|
my ($self, $obj, $want) = @_; |
|
716
|
|
|
|
|
|
|
|
|
717
|
0
|
|
|
|
|
|
my $ct = $self->res->content_type; |
|
718
|
0
|
0
|
|
|
|
|
unless ($ct) { |
|
719
|
0
|
0
|
0
|
|
|
|
$ct = $want.'; charset=UTF-8' if $want && $want =~ m/text|json|xml|html|atom/; |
|
720
|
0
|
|
0
|
|
|
|
$ct ||= 'text/plain; charset=UTF-8'; |
|
721
|
0
|
|
|
|
|
|
$self->log->debug($ct .' will be returned'); |
|
722
|
0
|
|
|
|
|
|
$self->res->content_type($ct); |
|
723
|
|
|
|
|
|
|
} |
|
724
|
|
|
|
|
|
|
|
|
725
|
0
|
0
|
0
|
|
|
|
if (ref $obj && ref $obj eq 'ARRAY' && (scalar @$obj == 2 || scalar @$obj == 3) && ref $obj->[0] eq 'HASH') { |
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
726
|
|
|
|
|
|
|
# render specified template |
|
727
|
0
|
0
|
0
|
|
|
|
if ((exists $obj->[0]->{$want} && $obj->[0]->{$want} eq '') || !exists $obj->[0]->{$want}) { |
|
|
|
|
0
|
|
|
|
|
|
728
|
|
|
|
|
|
|
# empty string for template name means deserialize |
|
729
|
|
|
|
|
|
|
# same goes if the route returns the wanted type |
|
730
|
|
|
|
|
|
|
# but has no template rule for it |
|
731
|
0
|
|
|
|
|
|
return $self->_structure($obj->[1], $want); |
|
732
|
|
|
|
|
|
|
} else { |
|
733
|
0
|
0
|
0
|
|
|
|
my $use_layout = scalar @$obj == 3 && defined $obj->[2] ? $obj->[2] : 1; |
|
734
|
0
|
|
|
|
|
|
return $self->template($obj->[0]->{$want}, $obj->[1], $use_layout); |
|
735
|
|
|
|
|
|
|
} |
|
736
|
|
|
|
|
|
|
} elsif (ref $obj && (ref $obj eq 'ARRAY' || ref $obj eq 'HASH')) { |
|
737
|
|
|
|
|
|
|
# serialize according to wanted type |
|
738
|
0
|
|
|
|
|
|
return $self->_structure($obj, $want); |
|
739
|
|
|
|
|
|
|
} elsif (ref $obj) { |
|
740
|
|
|
|
|
|
|
# $obj is some kind of reference, use Data::Dumper; |
|
741
|
0
|
|
|
|
|
|
Dumper($obj); |
|
742
|
|
|
|
|
|
|
} else { |
|
743
|
|
|
|
|
|
|
# $obj is a scalar, return as is |
|
744
|
0
|
|
|
|
|
|
return $obj; |
|
745
|
|
|
|
|
|
|
} |
|
746
|
|
|
|
|
|
|
} |
|
747
|
|
|
|
|
|
|
|
|
748
|
|
|
|
|
|
|
sub _route_parents { |
|
749
|
0
|
|
|
0
|
|
|
my ($self, $route) = @_; |
|
750
|
|
|
|
|
|
|
|
|
751
|
0
|
|
|
|
|
|
my @parents; |
|
752
|
|
|
|
|
|
|
|
|
753
|
0
|
|
|
|
|
|
my $class = $route->{class}; |
|
754
|
0
|
|
|
|
|
|
while ($class =~ m/Controller::(.+)$/) { |
|
755
|
|
|
|
|
|
|
# attempt to find a controller for this class |
|
756
|
0
|
|
|
|
|
|
foreach ($self->app->controllers) { |
|
757
|
0
|
0
|
|
|
|
|
if ($_ eq $class) { |
|
758
|
0
|
|
|
|
|
|
push(@parents, $_); |
|
759
|
0
|
|
|
|
|
|
last; |
|
760
|
|
|
|
|
|
|
} |
|
761
|
|
|
|
|
|
|
} |
|
762
|
|
|
|
|
|
|
# now strip the class once |
|
763
|
0
|
|
|
|
|
|
$class =~ s/::[^:]+$//; |
|
764
|
|
|
|
|
|
|
} |
|
765
|
0
|
|
|
|
|
|
$class .= '::Root'; |
|
766
|
0
|
|
|
|
|
|
push(@parents, $class); |
|
767
|
|
|
|
|
|
|
|
|
768
|
0
|
|
|
|
|
|
return @parents; |
|
769
|
|
|
|
|
|
|
} |
|
770
|
|
|
|
|
|
|
|
|
771
|
|
|
|
|
|
|
sub _structure { |
|
772
|
0
|
|
|
0
|
|
|
my ($self, $obj, $want) = @_; |
|
773
|
|
|
|
|
|
|
|
|
774
|
0
|
0
|
0
|
|
|
|
if ($want eq 'application/json') { |
|
|
|
0
|
|
|
|
|
|
|
775
|
0
|
|
|
|
|
|
return $self->json->encode($obj); |
|
776
|
|
|
|
|
|
|
} elsif ($want eq 'application/atom+xml' || $want eq 'application/xml') { |
|
777
|
0
|
|
|
|
|
|
return $self->xml->write($obj); |
|
778
|
|
|
|
|
|
|
} else { |
|
779
|
|
|
|
|
|
|
# return json anyway (temporary) |
|
780
|
0
|
|
|
|
|
|
return $self->json->encode($obj); |
|
781
|
|
|
|
|
|
|
} |
|
782
|
|
|
|
|
|
|
} |
|
783
|
|
|
|
|
|
|
|
|
784
|
|
|
|
|
|
|
sub _build_log { |
|
785
|
0
|
|
|
0
|
|
|
my $self = shift; |
|
786
|
|
|
|
|
|
|
|
|
787
|
0
|
|
|
|
|
|
my %opts; |
|
788
|
0
|
0
|
|
|
|
|
$opts{logger} = $self->env->{'psgix.logger'} |
|
789
|
|
|
|
|
|
|
if $self->env->{'psgix.logger'}; |
|
790
|
|
|
|
|
|
|
|
|
791
|
0
|
|
|
|
|
|
Leyland::Logger->new(%opts); |
|
792
|
|
|
|
|
|
|
} |
|
793
|
|
|
|
|
|
|
|
|
794
|
|
|
|
|
|
|
=head2 FOREIGNBUILDARGS( \%args ) |
|
795
|
|
|
|
|
|
|
|
|
796
|
|
|
|
|
|
|
=cut |
|
797
|
|
|
|
|
|
|
|
|
798
|
|
|
|
|
|
|
sub FOREIGNBUILDARGS { |
|
799
|
0
|
|
|
0
|
1
|
|
my ($class, %args) = @_; |
|
800
|
|
|
|
|
|
|
|
|
801
|
0
|
|
|
|
|
|
return ($args{env}); |
|
802
|
|
|
|
|
|
|
} |
|
803
|
|
|
|
|
|
|
|
|
804
|
|
|
|
|
|
|
=head2 BUILD() |
|
805
|
|
|
|
|
|
|
|
|
806
|
|
|
|
|
|
|
=cut |
|
807
|
|
|
|
|
|
|
|
|
808
|
0
|
|
|
0
|
1
|
|
sub BUILD { shift->_log_request } |
|
809
|
|
|
|
|
|
|
|
|
810
|
|
|
|
|
|
|
=head1 PLACK MODIFICATIONS |
|
811
|
|
|
|
|
|
|
|
|
812
|
|
|
|
|
|
|
The following modifications are performed on methods provided by L, |
|
813
|
|
|
|
|
|
|
from which this class inherits. |
|
814
|
|
|
|
|
|
|
|
|
815
|
|
|
|
|
|
|
=head2 content() |
|
816
|
|
|
|
|
|
|
|
|
817
|
|
|
|
|
|
|
Returns the request content after UTF-8 decoding it (Plack::Request doesn't |
|
818
|
|
|
|
|
|
|
decode the content, this class does since Leyland is purely UTF-8). |
|
819
|
|
|
|
|
|
|
|
|
820
|
|
|
|
|
|
|
=cut |
|
821
|
|
|
|
|
|
|
|
|
822
|
|
|
|
|
|
|
around content => sub { |
|
823
|
|
|
|
|
|
|
my ($orig, $self) = @_; |
|
824
|
|
|
|
|
|
|
|
|
825
|
|
|
|
|
|
|
return $self->env->{'leyland.request.content'} ||= Encode::decode('UTF-8', $self->$orig); |
|
826
|
|
|
|
|
|
|
}; |
|
827
|
|
|
|
|
|
|
|
|
828
|
|
|
|
|
|
|
=head2 session() |
|
829
|
|
|
|
|
|
|
|
|
830
|
|
|
|
|
|
|
Returns the C hash-ref, or, if it doesn't exist, an empty |
|
831
|
|
|
|
|
|
|
hash-ref. This is different from Plack::Request, which won't return anything |
|
832
|
|
|
|
|
|
|
if there is no session hash-ref. If C really doesn't exist, |
|
833
|
|
|
|
|
|
|
however, then the returned hash-ref won't be very useful and data entered |
|
834
|
|
|
|
|
|
|
to it will only be alive for the lifetime of the request. |
|
835
|
|
|
|
|
|
|
|
|
836
|
|
|
|
|
|
|
=cut |
|
837
|
|
|
|
|
|
|
|
|
838
|
|
|
|
|
|
|
around session => sub { |
|
839
|
|
|
|
|
|
|
my ($orig, $self) = @_; |
|
840
|
|
|
|
|
|
|
|
|
841
|
|
|
|
|
|
|
return $self->$orig || {}; |
|
842
|
|
|
|
|
|
|
}; |
|
843
|
|
|
|
|
|
|
|
|
844
|
|
|
|
|
|
|
=head2 query_parameters() |
|
845
|
|
|
|
|
|
|
|
|
846
|
|
|
|
|
|
|
Returns a L object of query string (GET) parameters, |
|
847
|
|
|
|
|
|
|
after UTF-8 decoding them (Plack::Request doesn't |
|
848
|
|
|
|
|
|
|
decode the query, this class does since Leyland is purely UTF-8). |
|
849
|
|
|
|
|
|
|
|
|
850
|
|
|
|
|
|
|
=cut |
|
851
|
|
|
|
|
|
|
|
|
852
|
|
|
|
|
|
|
around query_parameters => sub { |
|
853
|
|
|
|
|
|
|
my ($orig, $self) = @_; |
|
854
|
|
|
|
|
|
|
|
|
855
|
|
|
|
|
|
|
if ($self->env->{'leyland.request.query'}) { |
|
856
|
|
|
|
|
|
|
return $self->env->{'leyland.request.query'}; |
|
857
|
|
|
|
|
|
|
} else { |
|
858
|
|
|
|
|
|
|
my $params = $self->$orig->as_hashref_mixed; |
|
859
|
|
|
|
|
|
|
foreach (keys %$params) { |
|
860
|
|
|
|
|
|
|
if (ref $params->{$_}) { # implied: ref $params->{$_} eq 'ARRAY' |
|
861
|
|
|
|
|
|
|
my $arr = []; |
|
862
|
|
|
|
|
|
|
foreach my $val (@{$params->{$_}}) { |
|
863
|
|
|
|
|
|
|
push(@$arr, Encode::decode('UTF-8', $val)); |
|
864
|
|
|
|
|
|
|
} |
|
865
|
|
|
|
|
|
|
$params->{$_} = $arr; |
|
866
|
|
|
|
|
|
|
} else { |
|
867
|
|
|
|
|
|
|
$params->{$_} = Encode::decode('UTF-8', $params->{$_}); |
|
868
|
|
|
|
|
|
|
} |
|
869
|
|
|
|
|
|
|
} |
|
870
|
|
|
|
|
|
|
return $self->env->{'leyland.request.query'} = Hash::MultiValue->from_mixed($params); |
|
871
|
|
|
|
|
|
|
} |
|
872
|
|
|
|
|
|
|
}; |
|
873
|
|
|
|
|
|
|
|
|
874
|
|
|
|
|
|
|
=head2 body_parameters() |
|
875
|
|
|
|
|
|
|
|
|
876
|
|
|
|
|
|
|
Returns a L object of posted parameters in the request |
|
877
|
|
|
|
|
|
|
body (POST/PUT), after UTF-8 decoding them (Plack::Request doesn't |
|
878
|
|
|
|
|
|
|
decode the body, this class does since Leyland is purely UTF-8). |
|
879
|
|
|
|
|
|
|
|
|
880
|
|
|
|
|
|
|
=cut |
|
881
|
|
|
|
|
|
|
|
|
882
|
|
|
|
|
|
|
around body_parameters => sub { |
|
883
|
|
|
|
|
|
|
my ($orig, $self) = @_; |
|
884
|
|
|
|
|
|
|
|
|
885
|
|
|
|
|
|
|
$self->_parse_request_body |
|
886
|
|
|
|
|
|
|
unless $self->env->{'leyland.request.body'}; |
|
887
|
|
|
|
|
|
|
|
|
888
|
|
|
|
|
|
|
return $self->env->{'leyland.request.body'}; |
|
889
|
|
|
|
|
|
|
}; |
|
890
|
|
|
|
|
|
|
|
|
891
|
|
|
|
|
|
|
after _parse_request_body => sub { |
|
892
|
|
|
|
|
|
|
my $self = shift; |
|
893
|
|
|
|
|
|
|
|
|
894
|
|
|
|
|
|
|
# decode the body parameters |
|
895
|
|
|
|
|
|
|
if ($self->env->{'plack.request.body'} && !$self->env->{'leyland.request.body'}) { |
|
896
|
|
|
|
|
|
|
my $body = $self->env->{'plack.request.body'}->as_hashref_mixed; |
|
897
|
|
|
|
|
|
|
foreach (keys %$body) { |
|
898
|
|
|
|
|
|
|
if (ref $body->{$_}) { # implied: ref $body->{$_} eq 'ARRAY' |
|
899
|
|
|
|
|
|
|
my $arr = []; |
|
900
|
|
|
|
|
|
|
foreach my $val (@{$body->{$_}}) { |
|
901
|
|
|
|
|
|
|
push(@$arr, Encode::decode('UTF-8', $val)); |
|
902
|
|
|
|
|
|
|
} |
|
903
|
|
|
|
|
|
|
$body->{$_} = $arr; |
|
904
|
|
|
|
|
|
|
} else { |
|
905
|
|
|
|
|
|
|
$body->{$_} = Encode::decode('UTF-8', $body->{$_}); |
|
906
|
|
|
|
|
|
|
} |
|
907
|
|
|
|
|
|
|
} |
|
908
|
|
|
|
|
|
|
$self->env->{'leyland.request.body'} = Hash::MultiValue->from_mixed($body); |
|
909
|
|
|
|
|
|
|
} |
|
910
|
|
|
|
|
|
|
}; |
|
911
|
|
|
|
|
|
|
|
|
912
|
|
|
|
|
|
|
around _uri_base => sub { |
|
913
|
|
|
|
|
|
|
my ($orig, $self) = @_; |
|
914
|
|
|
|
|
|
|
|
|
915
|
|
|
|
|
|
|
my $base = $self->$orig; |
|
916
|
|
|
|
|
|
|
$base .= '/' unless $base =~ m!/$!; |
|
917
|
|
|
|
|
|
|
return $base; |
|
918
|
|
|
|
|
|
|
}; |
|
919
|
|
|
|
|
|
|
|
|
920
|
|
|
|
|
|
|
=head1 AUTHOR |
|
921
|
|
|
|
|
|
|
|
|
922
|
|
|
|
|
|
|
Ido Perlmuter, C<< >> |
|
923
|
|
|
|
|
|
|
|
|
924
|
|
|
|
|
|
|
=head1 BUGS |
|
925
|
|
|
|
|
|
|
|
|
926
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
|
927
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
|
928
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
|
929
|
|
|
|
|
|
|
|
|
930
|
|
|
|
|
|
|
=head1 SUPPORT |
|
931
|
|
|
|
|
|
|
|
|
932
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
|
933
|
|
|
|
|
|
|
|
|
934
|
|
|
|
|
|
|
perldoc Leyland::Context |
|
935
|
|
|
|
|
|
|
|
|
936
|
|
|
|
|
|
|
You can also look for information at: |
|
937
|
|
|
|
|
|
|
|
|
938
|
|
|
|
|
|
|
=over 4 |
|
939
|
|
|
|
|
|
|
|
|
940
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
|
941
|
|
|
|
|
|
|
|
|
942
|
|
|
|
|
|
|
L |
|
943
|
|
|
|
|
|
|
|
|
944
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
|
945
|
|
|
|
|
|
|
|
|
946
|
|
|
|
|
|
|
L |
|
947
|
|
|
|
|
|
|
|
|
948
|
|
|
|
|
|
|
=item * CPAN Ratings |
|
949
|
|
|
|
|
|
|
|
|
950
|
|
|
|
|
|
|
L |
|
951
|
|
|
|
|
|
|
|
|
952
|
|
|
|
|
|
|
=item * Search CPAN |
|
953
|
|
|
|
|
|
|
|
|
954
|
|
|
|
|
|
|
L |
|
955
|
|
|
|
|
|
|
|
|
956
|
|
|
|
|
|
|
=back |
|
957
|
|
|
|
|
|
|
|
|
958
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
|
959
|
|
|
|
|
|
|
|
|
960
|
|
|
|
|
|
|
Copyright 2010-2014 Ido Perlmuter. |
|
961
|
|
|
|
|
|
|
|
|
962
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
|
963
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
|
964
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
|
965
|
|
|
|
|
|
|
|
|
966
|
|
|
|
|
|
|
See http://dev.perl.org/licenses/ for more information. |
|
967
|
|
|
|
|
|
|
|
|
968
|
|
|
|
|
|
|
=cut |
|
969
|
|
|
|
|
|
|
|
|
970
|
|
|
|
|
|
|
1; |