line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Terse; |
2
|
|
|
|
|
|
|
our $VERSION = '0.18'; |
3
|
13
|
|
|
13
|
|
3448651
|
use 5.006; |
|
13
|
|
|
|
|
99
|
|
4
|
13
|
|
|
13
|
|
74
|
use strict; |
|
13
|
|
|
|
|
25
|
|
|
13
|
|
|
|
|
318
|
|
5
|
13
|
|
|
13
|
|
67
|
use warnings; |
|
13
|
|
|
|
|
30
|
|
|
13
|
|
|
|
|
382
|
|
6
|
13
|
|
|
13
|
|
86
|
no warnings 'redefine'; |
|
13
|
|
|
|
|
24
|
|
|
13
|
|
|
|
|
510
|
|
7
|
13
|
|
|
13
|
|
7167
|
use Plack::Request; |
|
13
|
|
|
|
|
1132084
|
|
|
13
|
|
|
|
|
514
|
|
8
|
13
|
|
|
13
|
|
6056
|
use Plack::Response; |
|
13
|
|
|
|
|
24395
|
|
|
13
|
|
|
|
|
398
|
|
9
|
13
|
|
|
13
|
|
88
|
use Cpanel::JSON::XS; |
|
13
|
|
|
|
|
28
|
|
|
13
|
|
|
|
|
834
|
|
10
|
13
|
|
|
13
|
|
83
|
use Scalar::Util qw/reftype/; |
|
13
|
|
|
|
|
22
|
|
|
13
|
|
|
|
|
621
|
|
11
|
13
|
|
|
13
|
|
7438
|
use Time::HiRes qw(gettimeofday); |
|
13
|
|
|
|
|
19306
|
|
|
13
|
|
|
|
|
71
|
|
12
|
13
|
|
|
13
|
|
9032
|
use Terse::WebSocket; |
|
13
|
|
|
|
|
36
|
|
|
13
|
|
|
|
|
515
|
|
13
|
13
|
|
|
13
|
|
6674
|
use Want qw/want/; |
|
13
|
|
|
|
|
23647
|
|
|
13
|
|
|
|
|
832
|
|
14
|
13
|
|
|
13
|
|
7071
|
use Digest::SHA; |
|
13
|
|
|
|
|
41391
|
|
|
13
|
|
|
|
|
674
|
|
15
|
13
|
|
|
13
|
|
92
|
use URI; |
|
13
|
|
|
|
|
34
|
|
|
13
|
|
|
|
|
606
|
|
16
|
13
|
|
|
13
|
|
6093
|
use Struct::WOP qw/all/ => { type => ['UTF-8'], destruct => 1 }; |
|
13
|
|
|
|
|
19472
|
|
|
13
|
|
|
|
|
133
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our ($JSON, %PRIVATE); |
19
|
|
|
|
|
|
|
BEGIN { |
20
|
13
|
|
|
13
|
|
1893
|
$JSON = Cpanel::JSON::XS->new->utf8->canonical(1)->allow_blessed->convert_blessed; |
21
|
|
|
|
|
|
|
%PRIVATE = ( |
22
|
13
|
|
|
|
|
43
|
map { $_ => 1 } |
|
221
|
|
|
|
|
46267
|
|
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
|
47075
|
my ($pkg, %args) = @_; |
29
|
|
|
|
|
|
|
|
30
|
44
|
50
|
|
|
|
137
|
$pkg = ref $pkg if ref $pkg; |
31
|
|
|
|
|
|
|
|
32
|
44
|
100
|
|
|
|
127
|
if (delete $args{private}) { |
33
|
6
|
|
|
|
|
31
|
for my $key (keys %args) { |
34
|
41
|
50
|
|
|
|
80
|
if ($key !~ m/^_/) { |
35
|
41
|
|
|
|
|
94
|
$args{"_$key"} = delete $args{$key}; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
44
|
|
|
|
|
271
|
return bless \%args, $pkg; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub run { |
44
|
3
|
|
|
3
|
1
|
6280
|
my ($pkg, %args) = @_; |
45
|
|
|
|
|
|
|
|
46
|
3
|
|
|
|
|
12
|
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
|
|
|
|
|
11
|
$j->_build_terse(); |
64
|
|
|
|
|
|
|
|
65
|
3
|
|
|
|
|
14
|
$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
|
|
|
|
|
24
|
my $content_type = $j->request->content_type; |
81
|
3
|
50
|
33
|
|
|
24
|
if ($content_type && $content_type =~ m/application\/json/) { |
82
|
0
|
|
0
|
|
|
0
|
$j->graft('params', $j->request->raw_body || "{}"); |
83
|
|
|
|
|
|
|
} else { |
84
|
3
|
50
|
|
|
|
5
|
$j->params = {%{$j->request->parameters || {}}}; |
|
3
|
|
|
|
|
11
|
|
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
3
|
50
|
50
|
|
|
17
|
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
|
|
|
|
13
|
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
|
|
|
|
|
15
|
my $req = $j->params->req; |
116
|
3
|
50
|
50
|
|
|
18
|
$req =~ /^([a-z][0-9a-zA-Z_]{1,31})$/ && do { $req = $1 // '' } if $req; |
|
3
|
|
33
|
|
|
12
|
|
117
|
3
|
50
|
|
|
|
22
|
$req = $j->{_application}->preprocess_req($req, $j) if $j->{_application}->can('preprocess_req'); |
118
|
3
|
50
|
33
|
|
|
19
|
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
|
|
|
|
|
13
|
$j->req = $req; |
124
|
3
|
|
|
|
|
12
|
$j->response->authenticated = \1; |
125
|
3
|
|
|
|
|
29
|
$j->session = $session; |
126
|
|
|
|
|
|
|
|
127
|
3
|
50
|
33
|
|
|
25
|
$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
|
|
|
|
|
32
|
$j->{_logout} eq $req |
133
|
|
|
|
|
|
|
); |
134
|
|
|
|
|
|
|
|
135
|
3
|
|
|
|
|
16
|
my ($out) = $j->_dispatch($req); |
136
|
|
|
|
|
|
|
|
137
|
3
|
100
|
|
|
|
23
|
return $j->_response($j->response) if $j->response->error; |
138
|
|
|
|
|
|
|
|
139
|
2
|
50
|
33
|
|
|
7
|
$j->session = $out if ( $j->is_login || $j->is_logout ); |
140
|
|
|
|
|
|
|
|
141
|
2
|
100
|
|
|
|
8
|
($j->session) = $j->_dispatch($auth, $j->session) if $j->response->authenticated; |
142
|
|
|
|
|
|
|
|
143
|
2
|
50
|
66
|
|
|
11
|
if ((!$j->response->authenticated || !$j->session) && !($j->is_login || $j->is_logout)) { |
|
|
|
33
|
|
|
|
|
|
|
|
66
|
|
|
|
|
144
|
1
|
|
|
|
|
12
|
$j->response->raiseError('Unauthenticated during the request', 400); |
145
|
1
|
|
|
|
|
6
|
return $j->_response($j->response); |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
1
|
|
|
|
|
8
|
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
|
4920
|
my ($self, $logger) = @_; |
166
|
4
|
100
|
|
|
|
15
|
$self->{_logger} = $logger if ($logger); |
167
|
4
|
|
|
|
|
14
|
return $self->{_logger}; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
sub logError { |
171
|
4
|
|
|
4
|
1
|
6495
|
my ($self, $message, $status, $no_response) = @_; |
172
|
|
|
|
|
|
|
$self->{_application} |
173
|
4
|
100
|
|
|
|
36
|
? $self->response->raiseError($message, $status) |
174
|
|
|
|
|
|
|
: $self->raiseError($message, $status); |
175
|
4
|
100
|
|
|
|
12
|
$message = { message => $message } if (!ref $message); |
176
|
|
|
|
|
|
|
$message = $self->{_application}->_logError($message, $status) |
177
|
4
|
100
|
100
|
|
|
61
|
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
|
|
|
|
38
|
if $self->{_logger}; |
|
|
100
|
|
|
|
|
|
182
|
4
|
100
|
|
|
|
30
|
$self->response->no_response = 1 if $no_response; |
183
|
4
|
|
|
|
|
16
|
return $self; |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
sub logInfo { |
187
|
8
|
|
|
8
|
1
|
12383
|
my ($self, $message) = @_; |
188
|
8
|
50
|
|
|
|
36
|
$message = { message => $message } if (!ref $message); |
189
|
|
|
|
|
|
|
$message = $self->{_application}->_logInfo($message) |
190
|
8
|
100
|
66
|
|
|
85
|
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
|
|
|
|
158
|
if $self->{_logger}; |
|
|
100
|
|
|
|
|
|
195
|
8
|
|
|
|
|
52
|
return $self; |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
sub raiseError { |
199
|
6
|
|
|
6
|
1
|
52
|
my ($self, $message, $code) = @_; |
200
|
6
|
50
|
|
|
|
19
|
return $self->response->raiseError($message, $code) if $self->{_application}; |
201
|
6
|
|
|
|
|
30
|
$self->{error} = \1; |
202
|
6
|
50
|
100
|
|
|
39
|
if ((reftype($message) || '') eq 'ARRAY') { |
203
|
0
|
|
|
|
|
0
|
push @{$self->{errors}}, @{$message}; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
204
|
|
|
|
|
|
|
} else { |
205
|
6
|
|
|
|
|
9
|
push @{$self->{errors}}, $message; |
|
6
|
|
|
|
|
16
|
|
206
|
|
|
|
|
|
|
} |
207
|
6
|
50
|
33
|
|
|
30
|
$self->{status_code} = $code if ($code && !$self->{status_code}); |
208
|
6
|
|
|
|
|
11
|
return $self; |
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
sub graft { |
212
|
5
|
|
|
5
|
1
|
504
|
my ($self, $name, $json) = @_; |
213
|
|
|
|
|
|
|
|
214
|
5
|
100
|
|
|
|
31
|
unless ($json =~ m/[\{\[]/) { |
215
|
1
|
|
|
|
|
6
|
$self->{$name} = $json; |
216
|
1
|
|
|
|
|
4
|
return $self->{$name}; |
217
|
|
|
|
|
|
|
} |
218
|
|
|
|
|
|
|
|
219
|
4
|
|
|
|
|
8
|
$self->{$name} = eval { |
220
|
4
|
|
|
|
|
41
|
$JSON->decode($json); |
221
|
|
|
|
|
|
|
}; |
222
|
|
|
|
|
|
|
|
223
|
4
|
100
|
|
|
|
17
|
return 0 if $@; |
224
|
|
|
|
|
|
|
|
225
|
3
|
|
|
|
|
11
|
return $self->_bless_tree($self->{$name}); |
226
|
|
|
|
|
|
|
} |
227
|
|
|
|
|
|
|
|
228
|
1
|
|
|
1
|
1
|
101
|
sub pretty { $_[0]->{_pretty} = 1; $_[0]; } |
|
1
|
|
|
|
|
4
|
|
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
sub serialize { |
231
|
6
|
|
|
6
|
1
|
3411
|
my ($self, $die) = @_; |
232
|
6
|
|
66
|
|
|
42
|
my $pretty = !!(reftype $self eq 'HASH' && $self->{_pretty}); |
233
|
6
|
|
|
|
|
13
|
my $out = eval { |
234
|
6
|
|
|
|
|
46
|
$JSON->pretty($pretty)->encode(maybe_decode($self)); |
235
|
|
|
|
|
|
|
}; |
236
|
6
|
50
|
66
|
|
|
2177
|
die $@ if ($@ && $die); |
237
|
6
|
|
66
|
|
|
34
|
return $out || $@; |
238
|
|
|
|
|
|
|
} |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
sub _build_terse { |
241
|
3
|
|
|
3
|
|
5
|
my ($t) = @_; |
242
|
|
|
|
|
|
|
|
243
|
3
|
50
|
|
|
|
7
|
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
|
|
|
22
|
} 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
|
|
|
|
18
|
} 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
|
|
|
|
|
13
|
$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
|
|
11
|
my ($self, $sid, $content, $status) = @_; |
307
|
3
|
|
66
|
|
|
14
|
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
|
|
|
|
14
|
$res->headers({%{$self->headers}}) if $self->headers; |
|
3
|
|
|
|
|
18
|
|
310
|
3
|
100
|
|
|
|
77
|
$res->cookies->{sid} = {%{$sid}} if $sid; |
|
1
|
|
|
|
|
7
|
|
311
|
3
|
|
|
|
|
32
|
$res->content_type($content); |
312
|
3
|
|
|
|
|
59
|
return $res; |
313
|
3
|
50
|
|
|
|
14
|
} unless $t->{_build_response}; |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
$t->{content_type} = sub { |
316
|
1
|
50
|
|
1
|
|
5
|
$_[0]->{_content_type} = $_[1] if $_[1]; |
317
|
1
|
|
|
|
|
15
|
return $_[0]->{_content_type}; |
318
|
3
|
50
|
|
|
|
12
|
} unless $t->{content_type}; |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
$t->{_response} = sub { |
321
|
3
|
|
|
3
|
|
21
|
my ($self, $response_body, $sid, $ct, $status) = @_; |
322
|
3
|
50
|
|
|
|
11
|
return $self->{_application}->response_handle(@_) if $self->{_application_has_response_handler}; |
323
|
3
|
|
100
|
|
|
17
|
$ct ||= 'application/json'; |
324
|
3
|
|
|
|
|
10
|
my $res = $self->{_delayed_response}; |
325
|
3
|
50
|
|
|
|
6
|
return $res if ($res); |
326
|
3
|
|
50
|
|
|
21
|
$res = $self->_build_response($sid, $ct, $status || 200); |
327
|
3
|
|
|
|
|
9
|
$res->body($response_body->serialize()); |
328
|
3
|
|
|
|
|
21
|
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
|
|
|
|
17
|
} : eval { |
336
|
7
|
100
|
|
|
|
54
|
unless ($self->{_application}->can($method)) { |
337
|
1
|
|
|
|
|
14
|
$self->response->raiseError('Invalid request - ' . $method, 400); |
338
|
1
|
|
|
|
|
3
|
return; |
339
|
|
|
|
|
|
|
} |
340
|
6
|
|
|
|
|
22
|
$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
|
|
|
|
11
|
} unless $t->{_dispatch}; |
348
|
|
|
|
|
|
|
|
349
|
3
|
|
|
|
|
6
|
return $t; |
350
|
|
|
|
|
|
|
} |
351
|
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
sub _bless_tree { |
353
|
42
|
|
|
42
|
|
81
|
my ($self, $node) = @_; |
354
|
42
|
|
|
|
|
61
|
my $refnode = ref $node; |
355
|
42
|
100
|
100
|
|
|
155
|
return unless $refnode eq 'HASH' || $refnode eq 'ARRAY'; |
356
|
20
|
100
|
|
|
|
53
|
if ($refnode eq 'HASH'){ |
357
|
18
|
50
|
|
|
|
48
|
bless $node, $node->{_inherit} ? ref $self : __PACKAGE__; |
358
|
18
|
|
|
|
|
85
|
$self->_bless_tree($node->{$_}) for keys %$node; |
359
|
|
|
|
|
|
|
} |
360
|
20
|
100
|
|
|
|
53
|
if ($refnode eq 'ARRAY'){ |
361
|
2
|
|
|
|
|
5
|
bless $node, ref $self; |
362
|
2
|
|
|
|
|
10
|
$self->_bless_tree($_) for @$node; |
363
|
|
|
|
|
|
|
} |
364
|
20
|
|
|
|
|
40
|
$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
|
|
11533
|
my $classname = ref $_[0]; |
388
|
178
|
|
|
|
|
271
|
my $validname = '[_a-zA-Z][\:a-zA-Z0-9_]*'; |
389
|
178
|
|
|
|
|
1250
|
our $AUTOLOAD =~ /^${classname}::($validname)$/; |
390
|
178
|
|
|
|
|
431
|
my $key = $1; |
391
|
178
|
50
|
|
|
|
356
|
die "illegal key name, must be of $validname form\n$AUTOLOAD" unless $key; |
392
|
178
|
100
|
|
|
|
381
|
my $miss = Want::want('REF OBJECT') ? {} : ''; |
393
|
178
|
|
|
|
|
10402
|
my $retval = $_[0]->{$key}; |
394
|
178
|
100
|
|
|
|
445
|
return $retval->(@_) if (ref $retval eq 'CODE'); |
395
|
161
|
50
|
|
|
|
294
|
die "illegal use of AUTOLOAD $classname -> $key - too many arguments" if (scalar @_ > 2); |
396
|
161
|
|
100
|
|
|
291
|
my $isBool = Want::want('SCALAR BOOL') && ((reftype($retval) // '') eq 'SCALAR'); |
397
|
161
|
100
|
|
|
|
12308
|
return $$retval if $isBool; |
398
|
154
|
|
66
|
|
|
712
|
$_[0]->{$key} = $_[1] // $retval // $miss; |
|
|
|
100
|
|
|
|
|
399
|
154
|
100
|
66
|
|
|
552
|
$_[0]->_bless_tree($_[0]->{$key}) if ref $_[0]->{$key} eq 'HASH' || ref $_[0]->{$key} eq 'ARRAY'; |
400
|
154
|
|
|
|
|
713
|
$_[0]->{$key}; |
401
|
|
|
|
|
|
|
} |
402
|
|
|
|
|
|
|
|
403
|
|
|
|
|
|
|
1; |
404
|
|
|
|
|
|
|
|
405
|
|
|
|
|
|
|
__END__ |