line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Terse; |
2
|
|
|
|
|
|
|
our $VERSION = '0.19'; |
3
|
13
|
|
|
13
|
|
3434621
|
use 5.006; |
|
13
|
|
|
|
|
95
|
|
4
|
13
|
|
|
13
|
|
71
|
use strict; |
|
13
|
|
|
|
|
26
|
|
|
13
|
|
|
|
|
297
|
|
5
|
13
|
|
|
13
|
|
64
|
use warnings; |
|
13
|
|
|
|
|
27
|
|
|
13
|
|
|
|
|
375
|
|
6
|
13
|
|
|
13
|
|
59
|
no warnings 'redefine'; |
|
13
|
|
|
|
|
33
|
|
|
13
|
|
|
|
|
441
|
|
7
|
13
|
|
|
13
|
|
7176
|
use Plack::Request; |
|
13
|
|
|
|
|
1130208
|
|
|
13
|
|
|
|
|
621
|
|
8
|
13
|
|
|
13
|
|
5889
|
use Plack::Response; |
|
13
|
|
|
|
|
24505
|
|
|
13
|
|
|
|
|
400
|
|
9
|
13
|
|
|
13
|
|
95
|
use Cpanel::JSON::XS; |
|
13
|
|
|
|
|
29
|
|
|
13
|
|
|
|
|
865
|
|
10
|
13
|
|
|
13
|
|
82
|
use Scalar::Util qw/reftype/; |
|
13
|
|
|
|
|
22
|
|
|
13
|
|
|
|
|
632
|
|
11
|
13
|
|
|
13
|
|
7470
|
use Time::HiRes qw(gettimeofday); |
|
13
|
|
|
|
|
18972
|
|
|
13
|
|
|
|
|
84
|
|
12
|
13
|
|
|
13
|
|
9119
|
use Terse::WebSocket; |
|
13
|
|
|
|
|
58
|
|
|
13
|
|
|
|
|
468
|
|
13
|
13
|
|
|
13
|
|
6903
|
use Want qw/want/; |
|
13
|
|
|
|
|
23717
|
|
|
13
|
|
|
|
|
1201
|
|
14
|
13
|
|
|
13
|
|
7367
|
use Digest::SHA; |
|
13
|
|
|
|
|
41408
|
|
|
13
|
|
|
|
|
665
|
|
15
|
13
|
|
|
13
|
|
88
|
use URI; |
|
13
|
|
|
|
|
29
|
|
|
13
|
|
|
|
|
618
|
|
16
|
13
|
|
|
13
|
|
6070
|
use Struct::WOP qw/all/ => { type => ['UTF-8'], destruct => 1 }; |
|
13
|
|
|
|
|
19808
|
|
|
13
|
|
|
|
|
137
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our ($JSON, %PRIVATE); |
19
|
|
|
|
|
|
|
BEGIN { |
20
|
13
|
|
|
13
|
|
1990
|
$JSON = Cpanel::JSON::XS->new->utf8->canonical(1)->allow_blessed->convert_blessed; |
21
|
|
|
|
|
|
|
%PRIVATE = ( |
22
|
13
|
|
|
|
|
43
|
map { $_ => 1 } |
|
221
|
|
|
|
|
46442
|
|
23
|
|
|
|
|
|
|
qw/new run logger logInfo logError websocket delayed_response build_terse content_type raiseError graft pretty serialize DESTROY TO_JSON AUTOLOAD to_app/ |
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub new { |
28
|
44
|
|
|
44
|
1
|
49615
|
my ($pkg, %args) = @_; |
29
|
|
|
|
|
|
|
|
30
|
44
|
50
|
|
|
|
134
|
$pkg = ref $pkg if ref $pkg; |
31
|
|
|
|
|
|
|
|
32
|
44
|
100
|
|
|
|
121
|
if (delete $args{private}) { |
33
|
6
|
|
|
|
|
30
|
for my $key (keys %args) { |
34
|
41
|
50
|
|
|
|
84
|
if ($key !~ m/^_/) { |
35
|
41
|
|
|
|
|
96
|
$args{"_$key"} = delete $args{$key}; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
44
|
|
|
|
|
240
|
return bless \%args, $pkg; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub run { |
44
|
3
|
|
|
3
|
1
|
6136
|
my ($pkg, %args) = @_; |
45
|
|
|
|
|
|
|
|
46
|
3
|
|
|
|
|
14
|
my $j = $pkg->new( |
47
|
|
|
|
|
|
|
private => 1, |
48
|
|
|
|
|
|
|
login => 'login', |
49
|
|
|
|
|
|
|
logout => 'logout', |
50
|
|
|
|
|
|
|
auth => 'auth', |
51
|
|
|
|
|
|
|
insecure_session => 0, |
52
|
|
|
|
|
|
|
content_type => 'application/json', |
53
|
|
|
|
|
|
|
request_class => 'Plack::Request', |
54
|
|
|
|
|
|
|
websocket_class => 'Terse::WebSocket', |
55
|
|
|
|
|
|
|
sock => 'psgix.io', |
56
|
|
|
|
|
|
|
stream_check => 'psgi.streaming', |
57
|
|
|
|
|
|
|
favicon => 'favicon.ico', |
58
|
|
|
|
|
|
|
%args |
59
|
|
|
|
|
|
|
); |
60
|
|
|
|
|
|
|
|
61
|
3
|
|
|
|
|
20
|
$j->headers = {}; |
62
|
|
|
|
|
|
|
|
63
|
3
|
|
|
|
|
10
|
$j->_build_terse(); |
64
|
|
|
|
|
|
|
|
65
|
3
|
|
|
|
|
16
|
$j->request = $j->{_request_class}->new($args{plack_env}); |
66
|
3
|
|
|
|
|
13
|
$j->response = $pkg->new( |
67
|
|
|
|
|
|
|
authenticated => \0, |
68
|
|
|
|
|
|
|
error => \0, |
69
|
|
|
|
|
|
|
errors => [], |
70
|
|
|
|
|
|
|
); |
71
|
|
|
|
|
|
|
|
72
|
3
|
50
|
|
|
|
14
|
if ($j->request->env->{PATH_INFO} =~ m/favicon.ico$/) { |
73
|
0
|
0
|
|
|
|
0
|
return [500, [], []] unless -f $j->_favicon; |
74
|
0
|
|
|
|
|
0
|
open my $fh, '<', $j->_favicon; |
75
|
0
|
|
|
|
|
0
|
my $favicon = do { local $/; <$fh> }; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
76
|
0
|
|
|
|
|
0
|
close $fh; |
77
|
0
|
|
|
|
|
0
|
return [200, ['Content-Type', 'image/vnd.microsoft.icon'], [$favicon] ]; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
3
|
|
|
|
|
27
|
my $content_type = $j->request->content_type; |
81
|
3
|
50
|
33
|
|
|
26
|
if ($content_type && $content_type =~ m/application\/json/) { |
82
|
0
|
|
0
|
|
|
0
|
$j->graft('params', $j->request->raw_body || "{}"); |
83
|
|
|
|
|
|
|
} else { |
84
|
3
|
50
|
|
|
|
4
|
$j->params = {%{$j->request->parameters || {}}}; |
|
3
|
|
|
|
|
12
|
|
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
3
|
50
|
50
|
|
|
15
|
unless ((reftype($j->params) || "") eq 'HASH') { |
88
|
0
|
|
|
|
|
0
|
$j->response->raiseError('Invalid parameters', 400); |
89
|
0
|
|
|
|
|
0
|
return $j->_response($j->response); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
3
|
|
|
|
|
13
|
$j->sid = $j->request->cookies->{sid}; |
93
|
|
|
|
|
|
|
|
94
|
3
|
50
|
|
|
|
12
|
unless ($j->sid) { |
95
|
0
|
|
|
|
|
0
|
my $h = Digest::SHA->new(256); |
96
|
0
|
|
|
|
|
0
|
my @us = gettimeofday; |
97
|
0
|
|
|
|
|
0
|
push @us, map { $j->request->env->{$_} } grep { |
98
|
0
|
|
|
|
|
0
|
$_ =~ /^HTTP(?:_|$)/; |
99
|
0
|
|
|
|
|
0
|
} keys %{ $j->request->env }; |
|
0
|
|
|
|
|
0
|
|
100
|
0
|
|
|
|
|
0
|
$h->add(@us); |
101
|
0
|
|
|
|
|
0
|
$j->sid = $h->hexdigest; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
$j->sid = { |
105
|
|
|
|
|
|
|
value => $j->is_logout ? "" : $j->sid, |
106
|
|
|
|
|
|
|
path => $j->{_sid_path} || $j->{_root_path} || "/", |
107
|
|
|
|
|
|
|
secure => !$j->{_insecure_session}, |
108
|
3
|
50
|
50
|
|
|
14
|
samesite => 'none' |
109
|
|
|
|
|
|
|
}; |
110
|
|
|
|
|
|
|
|
111
|
3
|
|
|
|
|
6
|
my $auth = $j->{_auth}; |
112
|
|
|
|
|
|
|
|
113
|
3
|
|
|
|
|
9
|
my ($session) = $j->_dispatch($auth, $pkg->new()); |
114
|
|
|
|
|
|
|
|
115
|
3
|
|
|
|
|
12
|
my $req = $j->params->req; |
116
|
3
|
50
|
50
|
|
|
20
|
$req =~ /^([a-z][0-9a-zA-Z_]{1,31})$/ && do { $req = $1 // '' } if $req; |
|
3
|
|
33
|
|
|
14
|
|
117
|
3
|
50
|
|
|
|
17
|
$req = $j->{_application}->preprocess_req($req, $j) if $j->{_application}->can('preprocess_req'); |
118
|
3
|
50
|
33
|
|
|
18
|
if (!$req || !$session || $PRIVATE{$req}) { |
|
|
|
33
|
|
|
|
|
119
|
0
|
|
|
|
|
0
|
$j->response->raiseError('Invalid request', 400); |
120
|
0
|
|
|
|
|
0
|
return $j->_response($j->response); |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
3
|
|
|
|
|
11
|
$j->req = $req; |
124
|
3
|
|
|
|
|
12
|
$j->response->authenticated = \1; |
125
|
3
|
|
|
|
|
14
|
$j->session = $session; |
126
|
|
|
|
|
|
|
|
127
|
3
|
50
|
33
|
|
|
14
|
$j->sid->expires = (ref $j->session && $j->session->expires) || (time + 24 * 60 * 60) |
128
|
|
|
|
|
|
|
if (!$j->sid->expires); |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
($j->is_login, $j->is_logout) = ( |
131
|
|
|
|
|
|
|
$j->{_login} eq $req, |
132
|
3
|
|
|
|
|
15
|
$j->{_logout} eq $req |
133
|
|
|
|
|
|
|
); |
134
|
|
|
|
|
|
|
|
135
|
3
|
|
|
|
|
13
|
my ($out) = $j->_dispatch($req); |
136
|
|
|
|
|
|
|
|
137
|
3
|
100
|
|
|
|
14
|
return $j->_response($j->response) if $j->response->error; |
138
|
|
|
|
|
|
|
|
139
|
2
|
50
|
33
|
|
|
9
|
$j->session = $out if ( $j->is_login || $j->is_logout ); |
140
|
|
|
|
|
|
|
|
141
|
2
|
100
|
|
|
|
19
|
($j->session) = $j->_dispatch($auth, $j->session) if $j->response->authenticated; |
142
|
|
|
|
|
|
|
|
143
|
2
|
50
|
66
|
|
|
12
|
if ((!$j->response->authenticated || !$j->session) && !($j->is_login || $j->is_logout)) { |
|
|
|
33
|
|
|
|
|
|
|
|
66
|
|
|
|
|
144
|
1
|
|
|
|
|
5
|
$j->response->raiseError('Unauthenticated during the request', 400); |
145
|
1
|
|
|
|
|
4
|
return $j->_response($j->response); |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
1
|
|
|
|
|
10
|
return $j->_response($j->response, $j->sid, $j->content_type); |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
sub to_app { |
152
|
0
|
|
|
0
|
0
|
0
|
my ($self, $new, $run) = @_; |
153
|
0
|
0
|
|
|
|
0
|
my $app = $self->new($new ? %{ $new } : ()); |
|
0
|
|
|
|
|
0
|
|
154
|
|
|
|
|
|
|
return sub { |
155
|
0
|
|
|
0
|
|
0
|
my ($env) = (shift); |
156
|
|
|
|
|
|
|
Terse->run( |
157
|
|
|
|
|
|
|
plack_env => $env, |
158
|
|
|
|
|
|
|
application => $app, |
159
|
0
|
0
|
|
|
|
0
|
($env->{'psgix.logger'} ? (logger => $env->{'psgix.logger'}) : ()), |
160
|
|
|
|
|
|
|
); |
161
|
0
|
|
|
|
|
0
|
}; |
162
|
|
|
|
|
|
|
}; |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub logger { |
165
|
4
|
|
|
4
|
1
|
5521
|
my ($self, $logger) = @_; |
166
|
4
|
100
|
|
|
|
12
|
$self->{_logger} = $logger if ($logger); |
167
|
4
|
|
|
|
|
14
|
return $self->{_logger}; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
sub logError { |
171
|
4
|
|
|
4
|
1
|
7523
|
my ($self, $message, $status, $no_response) = @_; |
172
|
|
|
|
|
|
|
$self->{_application} |
173
|
4
|
100
|
|
|
|
37
|
? $self->response->raiseError($message, $status) |
174
|
|
|
|
|
|
|
: $self->raiseError($message, $status); |
175
|
4
|
100
|
|
|
|
15
|
$message = { message => $message } if (!ref $message); |
176
|
|
|
|
|
|
|
$message = $self->{_application}->_logError($message, $status) |
177
|
4
|
100
|
100
|
|
|
55
|
if ($self->{_application} && $self->{_application}->can('_logError')); |
178
|
|
|
|
|
|
|
ref $self->{_logger} eq 'CODE' |
179
|
|
|
|
|
|
|
? $self->{_logger}->('error', $message) |
180
|
|
|
|
|
|
|
: $self->{_logger}->error($message) |
181
|
4
|
100
|
|
|
|
33
|
if $self->{_logger}; |
|
|
100
|
|
|
|
|
|
182
|
4
|
100
|
|
|
|
28
|
$self->response->no_response = 1 if $no_response; |
183
|
4
|
|
|
|
|
17
|
return $self; |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
sub logInfo { |
187
|
8
|
|
|
8
|
1
|
13691
|
my ($self, $message) = @_; |
188
|
8
|
50
|
|
|
|
36
|
$message = { message => $message } if (!ref $message); |
189
|
|
|
|
|
|
|
$message = $self->{_application}->_logInfo($message) |
190
|
8
|
100
|
66
|
|
|
119
|
if ($self->{_application} && $self->{_application}->can('_logInfo')); |
191
|
|
|
|
|
|
|
ref $self->{_logger} eq 'CODE' |
192
|
|
|
|
|
|
|
? $self->{_logger}->('info', $message) |
193
|
|
|
|
|
|
|
: $self->{_logger}->info($message) |
194
|
8
|
100
|
|
|
|
99
|
if $self->{_logger}; |
|
|
100
|
|
|
|
|
|
195
|
8
|
|
|
|
|
50
|
return $self; |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
sub raiseError { |
199
|
6
|
|
|
6
|
1
|
52
|
my ($self, $message, $code) = @_; |
200
|
6
|
50
|
|
|
|
20
|
return $self->response->raiseError($message, $code) if $self->{_application}; |
201
|
6
|
|
|
|
|
31
|
$self->{error} = \1; |
202
|
6
|
50
|
100
|
|
|
46
|
if ((reftype($message) || '') eq 'ARRAY') { |
203
|
0
|
|
|
|
|
0
|
push @{$self->{errors}}, @{$message}; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
204
|
|
|
|
|
|
|
} else { |
205
|
6
|
|
|
|
|
10
|
push @{$self->{errors}}, $message; |
|
6
|
|
|
|
|
20
|
|
206
|
|
|
|
|
|
|
} |
207
|
6
|
50
|
33
|
|
|
28
|
$self->{status_code} = $code if ($code && !$self->{status_code}); |
208
|
6
|
|
|
|
|
10
|
return $self; |
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
sub graft { |
212
|
5
|
|
|
5
|
1
|
488
|
my ($self, $name, $json) = @_; |
213
|
|
|
|
|
|
|
|
214
|
5
|
100
|
|
|
|
29
|
unless ($json =~ m/[\{\[]/) { |
215
|
1
|
|
|
|
|
3
|
$self->{$name} = $json; |
216
|
1
|
|
|
|
|
4
|
return $self->{$name}; |
217
|
|
|
|
|
|
|
} |
218
|
|
|
|
|
|
|
|
219
|
4
|
|
|
|
|
12
|
$self->{$name} = eval { |
220
|
4
|
|
|
|
|
37
|
$JSON->decode($json); |
221
|
|
|
|
|
|
|
}; |
222
|
|
|
|
|
|
|
|
223
|
4
|
100
|
|
|
|
17
|
return 0 if $@; |
224
|
|
|
|
|
|
|
|
225
|
3
|
|
|
|
|
13
|
return $self->_bless_tree($self->{$name}); |
226
|
|
|
|
|
|
|
} |
227
|
|
|
|
|
|
|
|
228
|
1
|
|
|
1
|
1
|
102
|
sub pretty { $_[0]->{_pretty} = 1; $_[0]; } |
|
1
|
|
|
|
|
4
|
|
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
sub serialize { |
231
|
6
|
|
|
6
|
1
|
2997
|
my ($self, $die) = @_; |
232
|
6
|
|
66
|
|
|
40
|
my $pretty = !!(reftype $self eq 'HASH' && $self->{_pretty}); |
233
|
6
|
|
|
|
|
10
|
my $out = eval { |
234
|
6
|
|
|
|
|
33
|
$JSON->pretty($pretty)->encode(maybe_decode($self)); |
235
|
|
|
|
|
|
|
}; |
236
|
6
|
50
|
66
|
|
|
2045
|
die $@ if ($@ && $die); |
237
|
6
|
|
66
|
|
|
32
|
return $out || $@; |
238
|
|
|
|
|
|
|
} |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
sub _build_terse { |
241
|
3
|
|
|
3
|
|
6
|
my ($t) = @_; |
242
|
|
|
|
|
|
|
|
243
|
3
|
50
|
|
|
|
8
|
if (! $t->{_application}) { |
244
|
0
|
|
|
|
|
0
|
$t->response->raiseError('No application passed to run', 500); |
245
|
0
|
|
|
|
|
0
|
return $t->_response($t->response); |
246
|
|
|
|
|
|
|
} |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
$t->{redirect} = sub { |
249
|
0
|
|
|
0
|
|
0
|
my ($self, $url, $response) = @_; |
250
|
0
|
|
|
|
|
0
|
$url = URI->new($url); |
251
|
0
|
0
|
|
|
|
0
|
$url->query_form( $url->query_form, %{$response || {}}); |
|
0
|
|
|
|
|
0
|
|
252
|
0
|
|
|
|
|
0
|
$self->response->status_code = 302; |
253
|
0
|
|
|
|
|
0
|
$self->response->message = 'Found'; |
254
|
0
|
|
|
|
|
0
|
$self->headers->Location = $url->as_string; |
255
|
0
|
|
|
|
|
0
|
return $self; |
256
|
3
|
|
|
|
|
16
|
}; |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
$t->{websocket} = sub { |
259
|
0
|
|
|
0
|
|
0
|
my ($self, %args) = @_; |
260
|
0
|
|
|
|
|
0
|
my $websocket = $t->{_websocket_class}->new($self); |
261
|
0
|
0
|
|
|
|
0
|
if (!ref $websocket) { |
262
|
0
|
|
|
|
|
0
|
$args{error}->($t, $websocket); |
263
|
0
|
|
|
|
|
0
|
return; |
264
|
|
|
|
|
|
|
} |
265
|
|
|
|
|
|
|
$t->{_delayed_response} = sub { |
266
|
0
|
|
|
|
|
0
|
my $responder = shift; |
267
|
0
|
|
|
|
|
0
|
$websocket->start($t, \%args, $responder); |
268
|
0
|
|
|
|
|
0
|
}; |
269
|
0
|
|
|
|
|
0
|
return $websocket; |
270
|
3
|
50
|
33
|
|
|
19
|
} unless $t->{websocket} || !$t->{_websocket_class}; |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
$t->{delayed_response} = sub { |
273
|
0
|
|
|
0
|
|
0
|
my ($self, $response, $sid, $ct, $status) = @_; |
274
|
0
|
|
0
|
|
|
0
|
$sid ||= $self->sid; |
275
|
0
|
|
0
|
|
|
0
|
$status ||= 200; |
276
|
0
|
|
0
|
|
|
0
|
$ct ||= 'application/json'; |
277
|
|
|
|
|
|
|
return $self->{_application}->delayed_response_handle( |
278
|
|
|
|
|
|
|
$self, $response, $sid, $ct, $status |
279
|
0
|
0
|
|
|
|
0
|
) if $self->{_application_has_delayed_response_handler}; |
280
|
|
|
|
|
|
|
$self->{_delayed_response} = sub { |
281
|
0
|
|
|
|
|
0
|
my $responder = shift; |
282
|
0
|
|
|
|
|
0
|
my $res = $self->_build_response($sid, $ct, $status); |
283
|
0
|
|
|
|
|
0
|
$res = [splice @{$res->finalize}, 0, 2]; |
|
0
|
|
|
|
|
0
|
|
284
|
0
|
|
|
|
|
0
|
my $writer = $responder->($res); |
285
|
0
|
|
|
|
|
0
|
$response = eval { $response->($writer); }; |
|
0
|
|
|
|
|
0
|
|
286
|
0
|
0
|
0
|
|
|
0
|
if ($@ || $self->response->error) { |
|
|
0
|
|
|
|
|
|
287
|
0
|
|
0
|
|
|
0
|
$res->[0] = $self->response->status_code || 500; |
288
|
0
|
0
|
|
|
|
0
|
$self->raiseError($@) if $@; |
289
|
0
|
|
|
|
|
0
|
push @{$res}, [$self->response->serialize]; |
|
0
|
|
|
|
|
0
|
|
290
|
0
|
|
|
|
|
0
|
return $responder->($res); |
291
|
|
|
|
|
|
|
} |
292
|
|
|
|
|
|
|
elsif ($response) { |
293
|
0
|
|
|
|
|
0
|
$writer->write($response->serialize); |
294
|
|
|
|
|
|
|
} |
295
|
0
|
|
|
|
|
0
|
$writer->close; |
296
|
0
|
|
|
|
|
0
|
}; |
297
|
0
|
|
|
|
|
0
|
$self; |
298
|
3
|
50
|
|
|
|
17
|
} unless $t->{delayed_response}; |
299
|
|
|
|
|
|
|
|
300
|
3
|
50
|
|
|
|
24
|
$t->{_application}->build_terse($t) if $t->{_application}->can('build_terse'); |
301
|
3
|
|
|
|
|
14
|
$t->{_application_has_dispatcher} = !! $t->{_application}->can('dispatch'); |
302
|
3
|
|
|
|
|
12
|
$t->{_application_has_response_handler} = !! $t->{_application}->can('response_handle'); |
303
|
3
|
|
|
|
|
12
|
$t->{_application_has_delayed_response_handler} = !! $t->{_application}->can('delayed_response_handle'); |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
$t->{_build_response} = sub { |
306
|
3
|
|
|
3
|
|
13
|
my ($self, $sid, $content, $status) = @_; |
307
|
3
|
|
66
|
|
|
12
|
my $res = $self->request->new_response($self->response->{status_code} ||= $status); |
308
|
3
|
50
|
|
|
|
96
|
$res->cookies($self->cookies) if $self->cookies; |
309
|
3
|
50
|
|
|
|
12
|
$res->headers({%{$self->headers}}) if $self->headers; |
|
3
|
|
|
|
|
11
|
|
310
|
3
|
100
|
|
|
|
69
|
$res->cookies->{sid} = {%{$sid}} if $sid; |
|
1
|
|
|
|
|
9
|
|
311
|
3
|
|
|
|
|
18
|
$res->content_type($content); |
312
|
3
|
|
|
|
|
60
|
return $res; |
313
|
3
|
50
|
|
|
|
28
|
} unless $t->{_build_response}; |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
$t->{content_type} = sub { |
316
|
1
|
50
|
|
1
|
|
6
|
$_[0]->{_content_type} = $_[1] if $_[1]; |
317
|
1
|
|
|
|
|
7
|
return $_[0]->{_content_type}; |
318
|
3
|
50
|
|
|
|
12
|
} unless $t->{content_type}; |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
$t->{_response} = sub { |
321
|
3
|
|
|
3
|
|
27
|
my ($self, $response_body, $sid, $ct, $status) = @_; |
322
|
3
|
50
|
|
|
|
15
|
return $self->{_application}->response_handle(@_) if $self->{_application_has_response_handler}; |
323
|
3
|
|
100
|
|
|
17
|
$ct ||= 'application/json'; |
324
|
3
|
|
|
|
|
6
|
my $res = $self->{_delayed_response}; |
325
|
3
|
50
|
|
|
|
6
|
return $res if ($res); |
326
|
3
|
|
50
|
|
|
22
|
$res = $self->_build_response($sid, $ct, $status || 200); |
327
|
3
|
|
|
|
|
11
|
$res->body($response_body->serialize()); |
328
|
3
|
|
|
|
|
26
|
return $res->finalize; |
329
|
3
|
50
|
|
|
|
13
|
} unless $t->{_response}; |
330
|
|
|
|
|
|
|
|
331
|
|
|
|
|
|
|
$t->{_dispatch} = sub { |
332
|
7
|
|
|
7
|
|
17
|
my ($self, $method, @params) = @_; |
333
|
|
|
|
|
|
|
my @out = $self->{_application_has_dispatcher} ? eval { |
334
|
0
|
|
|
|
|
0
|
$self->{_application}->dispatch($method, $self, @params) |
335
|
7
|
50
|
|
|
|
16
|
} : eval { |
336
|
7
|
100
|
|
|
|
41
|
unless ($self->{_application}->can($method)) { |
337
|
1
|
|
|
|
|
11
|
$self->response->raiseError('Invalid request - ' . $method, 400); |
338
|
1
|
|
|
|
|
3
|
return; |
339
|
|
|
|
|
|
|
} |
340
|
6
|
|
|
|
|
34
|
$self->{_application}->$method($self, @params); |
341
|
|
|
|
|
|
|
}; |
342
|
7
|
50
|
|
|
|
24
|
if ($@) { |
343
|
0
|
|
|
|
|
0
|
$self->response->raiseError(['Error while dispatching the request', $@], 400); |
344
|
0
|
|
|
|
|
0
|
return; |
345
|
|
|
|
|
|
|
} |
346
|
7
|
|
|
|
|
23
|
return @out; |
347
|
3
|
50
|
|
|
|
13
|
} unless $t->{_dispatch}; |
348
|
|
|
|
|
|
|
|
349
|
3
|
|
|
|
|
5
|
return $t; |
350
|
|
|
|
|
|
|
} |
351
|
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
sub _bless_tree { |
353
|
42
|
|
|
42
|
|
117
|
my ($self, $node) = @_; |
354
|
42
|
|
|
|
|
69
|
my $refnode = ref $node; |
355
|
42
|
100
|
100
|
|
|
149
|
return unless $refnode eq 'HASH' || $refnode eq 'ARRAY'; |
356
|
20
|
100
|
|
|
|
52
|
if ($refnode eq 'HASH'){ |
357
|
18
|
50
|
|
|
|
54
|
bless $node, $node->{_inherit} ? ref $self : __PACKAGE__; |
358
|
18
|
|
|
|
|
89
|
$self->_bless_tree($node->{$_}) for keys %$node; |
359
|
|
|
|
|
|
|
} |
360
|
20
|
100
|
|
|
|
52
|
if ($refnode eq 'ARRAY'){ |
361
|
2
|
|
|
|
|
6
|
bless $node, ref $self; |
362
|
2
|
|
|
|
|
10
|
$self->_bless_tree($_) for @$node; |
363
|
|
|
|
|
|
|
} |
364
|
20
|
|
|
|
|
38
|
$node; |
365
|
|
|
|
|
|
|
} |
366
|
|
|
|
|
|
|
|
367
|
|
|
|
|
|
|
sub TO_JSON { |
368
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
369
|
0
|
|
|
|
|
0
|
my $ref = reftype $self; |
370
|
0
|
0
|
0
|
|
|
0
|
return $self unless $ref && $ref =~ m/ARRAY|HASH/; |
371
|
0
|
0
|
|
|
|
0
|
return [@$self] if $ref eq 'ARRAY'; |
372
|
0
|
0
|
|
|
|
0
|
return 'cannot stringify application object' if $self->{_application}; |
373
|
0
|
|
|
|
|
0
|
my $output = {}; |
374
|
0
|
|
|
|
|
0
|
my $nodebug = ! $self->{_debug}; |
375
|
0
|
|
|
|
|
0
|
for(keys %$self){ |
376
|
0
|
|
|
|
|
0
|
my $skip; |
377
|
0
|
0
|
0
|
|
|
0
|
$skip++ if $_ =~ /^_/ && $nodebug; |
378
|
0
|
0
|
|
|
|
0
|
next if $skip; |
379
|
0
|
|
|
|
|
0
|
$output->{$_} = $self->{$_}; |
380
|
|
|
|
|
|
|
} |
381
|
0
|
|
|
|
|
0
|
return $output; |
382
|
|
|
|
|
|
|
} |
383
|
|
|
|
|
|
|
|
384
|
|
|
|
0
|
|
|
sub DESTROY {} |
385
|
|
|
|
|
|
|
|
386
|
|
|
|
|
|
|
sub AUTOLOAD : lvalue { |
387
|
178
|
|
|
178
|
|
10864
|
my $classname = ref $_[0]; |
388
|
178
|
|
|
|
|
239
|
my $validname = '[_a-zA-Z][\:a-zA-Z0-9_]*'; |
389
|
178
|
|
|
|
|
1229
|
our $AUTOLOAD =~ /^${classname}::($validname)$/; |
390
|
178
|
|
|
|
|
440
|
my $key = $1; |
391
|
178
|
50
|
|
|
|
361
|
die "illegal key name, must be of $validname form\n$AUTOLOAD" unless $key; |
392
|
178
|
100
|
|
|
|
341
|
my $miss = Want::want('REF OBJECT') ? {} : ''; |
393
|
178
|
|
|
|
|
10174
|
my $retval = $_[0]->{$key}; |
394
|
178
|
100
|
|
|
|
425
|
return $retval->(@_) if (ref $retval eq 'CODE'); |
395
|
161
|
50
|
|
|
|
312
|
die "illegal use of AUTOLOAD $classname -> $key - too many arguments" if (scalar @_ > 2); |
396
|
161
|
|
100
|
|
|
304
|
my $isBool = Want::want('SCALAR BOOL') && ((reftype($retval) // '') eq 'SCALAR'); |
397
|
161
|
100
|
|
|
|
12416
|
return $$retval if $isBool; |
398
|
154
|
|
66
|
|
|
658
|
$_[0]->{$key} = $_[1] // $retval // $miss; |
|
|
|
100
|
|
|
|
|
399
|
154
|
100
|
66
|
|
|
540
|
$_[0]->_bless_tree($_[0]->{$key}) if ref $_[0]->{$key} eq 'HASH' || ref $_[0]->{$key} eq 'ARRAY'; |
400
|
154
|
|
|
|
|
761
|
$_[0]->{$key}; |
401
|
|
|
|
|
|
|
} |
402
|
|
|
|
|
|
|
|
403
|
|
|
|
|
|
|
1; |
404
|
|
|
|
|
|
|
|
405
|
|
|
|
|
|
|
__END__ |