lib/Yote/YapiServer/Handler.pm

filter: Statementstmt 81.7% 229 / 280 Branchbran 71.4% 100 / 140 Conditioncond 52.4% 32 / 61 MC/DCmcdc 32.4% 12 / 37 Subroutinesub 80.0% 16 / 20 Podpod 0.0% 0 / 11 totaltotal 70.8% 389 / 549 CC 101 SCAR 32.0
CC
101
Cov
75%
process_file_arg
110.0
handle
30.5
handle_call
20.4
CRAP 24.5
« blib/lib/Yote/YapiServer/Site.pm ↑ Summary blib/lib/Yote/YapiServer.pm »
1 package Yote::YapiServer::Handler;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Yote::YapiServer::Site;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Yote::YapiServer::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Yote::YapiServer::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Config vars — set by YapiServer during startup
14 1 our $max_file_size = 5_000_000; # 5MB default
15 1 our $webroot_dir = 'www/webroot';
16 1 our $debug_mode = 1;
17  
18 # Rate limit tracking (in-memory, resets on server restart)
19 1 our %rate_limits; # { ip => { endpoint => { count => N, window_start => T } } }
20  
21 #----------------------------------------------------------------------
22 # Main request handler
23 #----------------------------------------------------------------------
24  
25 sub handle {
26 19 ▶ my ($class, %args) = @_;
Subroutine handle: called CC=27 SCAR=34.2
Pod undocumented
27  
28 19 my $store = $args{store};
29 19 my $json_body = $args{body};
30 19 ▶ my $ip_address = $args{ip_address} // '127.0.0.1';
Condition50%1/2
$args{"ip_address"} // "127.0.0.1"
l!l
019
MC/DC0%0/1
$args{"ip_address"} // "127.0.0.1"
$args{"ip_address"}
Truth table50%1/2
A
A= $args{"ip_address"}
Aresult
11
00
31  
32 # Parse JSON request
33 19 my $request;
34 19 eval {
35 19 $request = decode_json($json_body);
36 };
37 19 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 18 19
38 1 return $class->error_response("invalid JSON: $@");
39 }
40  
41 # Extract request fields
42 18 my $action = $request->{action}; # connect, call, login, createUser, logout
43 18 my $token = $request->{token};
44 18 my $app = $request->{app};
45 18 my $target = $request->{target}; # app name or _obj_ID
46 18 my $method = $request->{method};
47 18 ▶ my $args_ = $request->{args} // {};
Condition100%2/2
$request->{"args"} // {}
l!l
612
MC/DC100%1/1
$request->{"args"} // {}
$request->{"args"}
Truth table100%2/2
A
A= $request->{"args"}
Aresult
11
00
48  
49 # Build context
50 18 my $context = {
51 ip_address => $ip_address,
52 request => $request,
53 };
54  
55 # Wrap entire request handling so uncaught errors return JSON, not HTTP 500
56 18 my ($succ, $res, $extra);
57 18 my ($root, $session, $user);
58 18 eval {
59 # Validate token if provided
60 18 ▶ $root = $store->fetch_root // $store->new_obj('Yote::YapiServer::Site');
Condition33%1/3
$store->fetch_root // $store->new_obj("Yote::YapiServer::Site")
l!l&&r!l&&!r
1800
MC/DC0%0/2
$store->fetch_root // $store->new_obj("Yote::YapiServer::Site")
$store->fetch_root $store->new_obj("Yote::YapiServer::Site")
Truth table33%1/3
A // B
A= $store->fetch_rootB= $store->new_obj("Yote::YapiServer::Site")
ABresult
1X1
011
000
61 18 $root->init;
62 18 ▶ if ($token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token) 4 14 18
63 4 $session = $root->validateToken($token);
64 4 ▶ if ($session) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session) 4 0 4
65 4 $user = $session->get_user;
66 4 $context->{session} = $session;
67 4 $context->{user} = $user;
68 }
69 }
70  
71 # Create anonymous session if none exists so exposed objects can be tracked
72 18 ▶ if (!$session) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session) 14 4 18
73 14 my $new_token = Yote::YapiServer::Session->generate_token();
74 14 my $expires = Yote::YapiServer::Session->calculate_expiry();
75 14 $session = $store->new_obj('Yote::YapiServer::Session',
76 token => $new_token,
77 expires => $expires,
78 ip_address => $ip_address,
79 );
80 14 $session->touch;
81 14 $store->set_path('sessions', $new_token, $session);
82 14 $context->{session} = $session;
83 }
84  
85 # Handle actions
86 18 ▶ if ($action eq 'connect') {
Branch: 5 branches
BranchTrueFalseTotal
if ($action eq 'connect') { } 3 15 18
elsif ($action eq 'login') { } 2 13 15
elsif ($action eq 'createUser') { } 1 12 13
elsif ($action eq 'logout') { } 1 11 12
elsif ($action eq 'call') { } 10 1 11
87 3 ($succ, $res, $extra) = $class->handle_connect($root, $app, $session, $user);
88 }
89 elsif ($action eq 'login') {
90 2 ▶ ($succ, $res, $extra) = $class->check_rate_limit('login', $ip_address, undef)
Condition33%1/3
$class->check_rate_limit("login", $ip_address, undef) // $root->login($args_, $context)
l!l&&r!l&&!r
020
MC/DC0%0/2
$class->check_rate_limit("login", $ip_address, undef) // $root->login($args_, $context)
$class->check_rate_limit("login", $ip_address, undef) $root->login($args_, $context)
Truth table33%1/3
A // B
A= $class->check_rate_limit("login", $ip_address, undef)B= $root->login($args_, $context)
ABresult
1X1
011
000
91 // $root->login($args_, $context);
92 }
93 elsif ($action eq 'createUser') {
94 1 ▶ ($succ, $res, $extra) = $class->check_rate_limit('createUser', $ip_address, undef)
Condition33%1/3
$class->check_rate_limit("createUser", $ip_address, undef) // $root->createUser($args_, $context)
l!l&&r!l&&!r
010
MC/DC0%0/2
$class->check_rate_limit("createUser", $ip_address, undef) // $root->createUser($args_, $context)
$class->check_rate_limit("createUser", $ip_address, undef) $root->createUser($args_, $context)
Truth table33%1/3
A // B
A= $class->check_rate_limit("createUser", $ip_address, undef)B= $root->createUser($args_, $context)
ABresult
1X1
011
000
95 // $root->createUser($args_, $context);
96 }
97 elsif ($action eq 'logout') {
98 1 ($succ, $res, $extra) = $root->logout($args_, $context);
99 }
100 elsif ($action eq 'call') {
101 10 ▶ ($succ, $res, $extra) = $class->check_rate_limit('default', $ip_address, $session)
Condition33%1/3
$class->check_rate_limit("default", $ip_address, $session) // $class->handle_call($root, $target, $method, $args_, $session, $user, $store)
l!l&&r!l&&!r
0100
MC/DC0%0/2
$class->check_rate_limit("default", $ip_address, $session) // $class->handle_call($root, $target, $method, $args_, $session, $user, $store)
$class->check_rate_limit("default", $ip_address, $session) $class->handle_call($root, $target, $method, $args_, $session, $user, $store)
Truth table33%1/3
A // B
A= $class->check_rate_limit("default", $ip_address, $session)B= $class->handle_call($root, $target, $method, $args_, $session, $user, $store)
ABresult
1X1
011
000
102 // $class->handle_call($root, $target, $method, $args_, $session, $user, $store);
103 }
104 else {
105 1 ($succ, $res) = (0, "unknown action: $action");
106 }
107  
108 18 $store->save;
109 };
110  
111 18 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 18 18
112 0 chomp(my $error = $@);
113 0 $class->log_error($context, $error);
114 0 ▶ my $msg = $debug_mode ? $error : "server error";
Branch: 1 branch
BranchTrueFalseTotal
$Yote::YapiServer::Handler::debug_mode ? : 0 0 0
115 0 return $class->error_response($msg);
116 }
117  
118 18 ▶ if (!$succ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($succ) 8 10 18
119 8 return $class->error_response($res);
120 }
121  
122 # For login/createUser/logout, serialize the result through serialize_value
123 # (handle_connect and handle_call already do their own serialization)
124 10 ▶ if ($action =~ /^(login|createUser|logout)$/ && defined $res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action =~ /^(login|createUser|logout)$/ and defined $res) 2 8 10
Condition100%3/3
$action =~ /^(login|createUser|logout)$/ and defined $res
!ll&&!rl&&r
712
MC/DC100%2/2
$action =~ /^(login|createUser|logout)$/ and defined $res
$action =~ /^(login|createUser|logout)$/ defined $res
Truth table100%3/3
A and B
A= $action =~ /^(login|createUser|logout)$/B= defined $res
ABresult
0X0
100
111
125 2 my $ctx = { objects => {}, classes => {} };
126 2 $res = $class->serialize_value($res, $ctx, $session, $user);
127 2 ▶ $extra //= {};
Condition50%1/2
$extra //= {}
l!l
20
MC/DC0%0/1
$extra //= {}
$extra
Truth table50%1/2
A
A= $extra
Aresult
11
00
128 2 ▶ $extra->{objects} = $ctx->{objects} if %{$ctx->{objects}};
Branch: 1 branch
BranchTrueFalseTotal
if %{$ctx->{"objects"};} 2 0 2
129 2 ▶ $extra->{classes} = $ctx->{classes} if %{$ctx->{classes}};
Branch: 1 branch
BranchTrueFalseTotal
if %{$ctx->{"classes"};} 2 0 2
130 }
131  
132 # Use session from extra token if login/createUser created a new one
133 10 ▶ if ($extra && $extra->{token}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extra and $extra->{"token"}) 2 8 10
Condition100%3/3
$extra and $extra->{"token"}
!ll&&!rl&&r
172
MC/DC100%2/2
$extra and $extra->{"token"}
$extra $extra->{"token"}
Truth table100%3/3
A and B
A= $extraB= $extra->{"token"}
ABresult
0X0
100
111
134 2 my $new_session = $root->validateToken($extra->{token});
135 2 ▶ $session = $new_session if $new_session;
Branch: 1 branch
BranchTrueFalseTotal
if $new_session 1 1 2
136 }
137  
138 10 return $class->success_response($res, $extra, $session);
139 }
140  
141 #----------------------------------------------------------------------
142 # Action handlers
143 #----------------------------------------------------------------------
144  
145 sub handle_connect {
146 3 ▶ my ($class, $root, $app_name, $session, $user) = @_;
Subroutine handle_connect: called CC=5 SCAR=16.1
Pod undocumented
147  
148 3 ▶ return 0, "app name required" unless $app_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $app_name 1 2 3
149  
150 2 my $app = $root->get_app($app_name);
151 2 ▶ return 0, "unknown app: $app_name" unless $app;
Branch: 1 branch
BranchTrueFalseTotal
unless $app 1 1 2
152  
153 # Expose app to session
154 1 $session->expose_object($app);
155  
156 1 my $app_id = "_app_$app_name";
157 1 my $methods = $app->connect_info_methods($session, $user);
158 1 my $public_vars = $app->public_vars;
159  
160 1 my $ctx = { objects => {}, classes => {} };
161  
162 # Build app object data from publicVars (serialize values for v/r prefixing)
163 1 my %serialized_vars;
164 1 for my $key (keys %$public_vars) {
165 1 $serialized_vars{$key} = $class->serialize_value($public_vars->{$key}, $ctx, $session, $user);
166 }
167 1 $ctx->{objects}{$app_id} = {
168 _class => $app->_client_class_name,
169 data => \%serialized_vars,
170 };
171  
172 # Add app methods to apps section
173 1 my %apps = ( $app_id => $methods );
174  
175 # Serialize user into objects/classes if present
176 1 ▶ if ($user) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user) 0 1 1
177 0 $class->serialize_value($user, $ctx, $session, $user);
178 }
179  
180 1 return 1, "r$app_id", { objects => $ctx->{objects}, classes => $ctx->{classes}, apps => \%apps };
181 }
182  
183 sub handle_call {
184 10 ▶ my ($class, $root, $target, $method, $args, $session, $user, $store) = @_;
Subroutine handle_call: called CC=19 SCAR=30.2
Pod undocumented
185  
186 10 ▶ return 0, "target required" unless $target;
Branch: 1 branch
BranchTrueFalseTotal
unless $target 1 9 10
187 9 ▶ return 0, "method required" unless $method;
Branch: 1 branch
BranchTrueFalseTotal
unless $method 1 8 9
188  
189 8 my $target_obj;
190  
191 # Determine target object
192 8 ▶ if ($target =~ /^_obj_(\d+)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($target =~ /^_obj_(\d+)$/) { } 2 6 8
elsif ($target =~ /^_app_(.+)$/) { } 1 5 6
193 # Object reference
194 2 my $obj_id = $1;
195  
196 # Security: verify object is in session's exposed set
197 2 ▶ unless ($session && $session->can_access($obj_id)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session and $session->can_access($obj_id)) 1 1 2
Condition66%2/3
$session and $session->can_access($obj_id)
!ll&&!rl&&r
011
MC/DC50%1/2
$session and $session->can_access($obj_id)
$session $session->can_access($obj_id)
Truth table66%2/3
A and B
A= $sessionB= $session->can_access($obj_id)
ABresult
0X0
100
111
198 1 return 0, "access denied";
199 }
200  
201 1 $target_obj = $store->fetch($obj_id);
202 1 ▶ return 0, "object not found" unless $target_obj;
Branch: 1 branch
BranchTrueFalseTotal
unless $target_obj 0 1 1
203 }
204 elsif ($target =~ /^_app_(.+)$/) {
205 # App reference via _app_ ID
206 1 my $app_name = $1;
207 1 $target_obj = $root->get_app($app_name);
208 1 ▶ return 0, "unknown app: $app_name" unless $target_obj;
Branch: 1 branch
BranchTrueFalseTotal
unless $target_obj 0 1 1
209 }
210 else {
211 # App name (legacy)
212 5 $target_obj = $root->get_app($target);
213 5 ▶ return 0, "unknown app: $target" unless $target_obj;
Branch: 1 branch
BranchTrueFalseTotal
unless $target_obj 0 5 5
214 }
215  
216 # Look up method definition for files flag
217 7 my $method_def = {};
218 7 ▶ if ($target_obj->can('method_defs')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target_obj->can("method_defs")) 7 0 7
219 7 my $methods = $target_obj->method_defs;
220 7 ▶ $method_def = $methods->{$method} // {};
Condition50%1/2
$methods->{$method} // {}
l!l
70
MC/DC0%0/1
$methods->{$method} // {}
$methods->{$method}
Truth table50%1/2
A
A= $methods->{$method}
Aresult
11
00
221 }
222  
223 # Validate arguments with v/r/f prefix dispatch
224 my $opts = {
225 session => $session,
226 store => $store,
227 allow_files => $method_def->{files},
228 7 max_file_size => $max_file_size,
229 webroot_dir => $webroot_dir,
230 user => $user,
231 };
232 7 my ($args_ok, $validated_args) = $class->validate_args($args, $opts);
233 7 ▶ return 0, $validated_args unless $args_ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $args_ok 0 7 7
234  
235 # Authorize method call
236 7 my ($auth_ok, $auth_error);
237 7 ▶ if ($target =~ /^_obj_/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target =~ /^_obj_/) { } 1 6 7
238 # Object method - use object-level authorization
239 1 ($auth_ok, $auth_error) = Yote::YapiServer::App::Base->authorize_object_method(
240 $target_obj, $method, $session, $user
241 );
242 } else {
243 # App method
244 6 ($auth_ok, $auth_error) = $target_obj->authorize_method($method, $session, $user);
245 }
246  
247 7 ▶ return 0, $auth_error unless $auth_ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $auth_ok 1 6 7
248  
249 # Call the method — methods return (1, $result) or (0, $error)
250 6 ▶ unless ($target_obj->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($target_obj->can($method)) 0 6 6
251 0 return 0, "method not implemented: $method";
252 }
253  
254 6 my ($ok, $result) = $target_obj->$method($validated_args, $session);
255 6 ▶ return 0, $result unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 6 6
256  
257 # Serialize result with context accumulation
258 6 my $ctx = { objects => {}, classes => {} };
259 6 my $resp = $class->serialize_value($result, $ctx, $session, $user);
260  
261 6 my %extra;
262 6 ▶ $extra{objects} = $ctx->{objects} if %{$ctx->{objects}};
Branch: 1 branch
BranchTrueFalseTotal
if %{$ctx->{"objects"};} 0 6 6
263 6 ▶ $extra{classes} = $ctx->{classes} if %{$ctx->{classes}};
Branch: 1 branch
BranchTrueFalseTotal
if %{$ctx->{"classes"};} 0 6 6
264  
265 6 return 1, $resp, \%extra;
266 }
267  
268 #----------------------------------------------------------------------
269 # Argument validation
270 #----------------------------------------------------------------------
271  
272 sub validate_args {
273 27 ▶ my ($class, $args, $opts) = @_;
Subroutine validate_args: called CC=15 SCAR=27.3
Pod undocumented
274  
275 # Array — recurse into each element
276 27 ▶ if (ref $args eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args eq "ARRAY") 1 26 27
277 1 my @validated;
278 1 for my $item (@$args) {
279 2 my ($ok, $result) = $class->validate_args($item, $opts);
280 2 ▶ return (0, $result) unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 2 2
281 2 push @validated, $result;
282 }
283 1 return (1, \@validated);
284 }
285  
286 # Hash — recurse into values
287 26 ▶ if (ref $args eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args eq "HASH") 14 12 26
288 14 my %validated;
289 14 for my $key (keys %$args) {
290 12 my ($ok, $result) = $class->validate_args($args->{$key}, $opts);
291 12 ▶ return (0, $result) unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 3 9 12
292 9 $validated{$key} = $result;
293 }
294 11 return (1, \%validated);
295 }
296  
297 # Leaf value — dispatch on v/r/f prefix
298 12 ▶ return (1, undef) unless defined $args;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args 0 12 12
299  
300 # v prefix — scalar value
301 12 ▶ if ($args =~ /^v(.*)$/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args =~ /^v(.*)$/s) 8 4 12
302 8 return (1, $1);
303 }
304  
305 # r prefix — object reference
306 4 ▶ if ($args =~ /^r_obj_(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args =~ /^r_obj_(\d+)$/) 2 2 4
307 2 my $obj_id = $1;
308 2 my $session = $opts->{session};
309 2 my $store = $opts->{store};
310  
311 2 ▶ unless ($session && $session->can_access($obj_id)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session and $session->can_access($obj_id)) 1 1 2
Condition66%2/3
$session and $session->can_access($obj_id)
!ll&&!rl&&r
101
MC/DC50%1/2
$session and $session->can_access($obj_id)
$session $session->can_access($obj_id)
Truth table66%2/3
A and B
A= $sessionB= $session->can_access($obj_id)
ABresult
0X0
100
111
312 1 return (0, "access denied to object: _obj_$obj_id");
313 }
314  
315 1 my $obj = $store->fetch($obj_id);
316 1 ▶ return (0, "object not found: _obj_$obj_id") unless $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless $obj 0 1 1
317  
318 1 return (1, $obj);
319 }
320  
321 # f prefix — file upload
322 2 ▶ if ($args =~ /^f/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args =~ /^f/) 1 1 2
323 1 ▶ unless ($opts->{allow_files}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts->{"allow_files"}) 1 0 1
324 1 return (0, "file uploads not allowed for this method");
325 }
326 0 return $class->process_file_arg($args, $opts);
327 }
328  
329 # No valid prefix
330 1 return (0, "invalid argument encoding: missing v/r/f prefix");
331 }
332  
333 #----------------------------------------------------------------------
334 # File upload processing
335 #----------------------------------------------------------------------
336  
337 sub process_file_arg {
338 0 ▶ my ($class, $value, $opts) = @_;
Subroutine process_file_arg: not called CC=10 SCAR=47.0
Pod undocumented
339  
340 # Parse: f<original_name>|data:<mime_type>;base64,<encoded_data>
341 0 ▶ unless ($value =~ /^f([^|]*)\|data:([^;]+);base64,(.+)$/s) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /^f([^|]*)\|data:([^;]+);base64,(.+)$/s) 0 0 0
342 0 return (0, "invalid file format");
343 }
344  
345 0 my $original_name = $1;
346 0 my $mime_type = $2;
347 0 my $encoded_data = $3;
348  
349 # Validate MIME type
350 0 my $ext = _mime_to_ext($mime_type);
351 0 ▶ return (0, "unsupported file type: $mime_type") unless $ext;
Branch: 1 branch
BranchTrueFalseTotal
unless $ext 0 0 0
352  
353 # Decode base64
354 0 require MIME::Base64;
355 0 my $data = MIME::Base64::decode_base64($encoded_data);
356  
357 # Check file size
358 0 ▶ my $max_size = $opts->{max_file_size} // 5_000_000;
Condition0%0/2
$opts->{"max_file_size"} // 5000000
l!l
00
MC/DC0%0/1
$opts->{"max_file_size"} // 5000000
$opts->{"max_file_size"}
Truth table0%0/2
A
A= $opts->{"max_file_size"}
Aresult
11
00
359 0 ▶ if (length($data) > $max_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data > $max_size) 0 0 0
360 0 return (0, "file too large (max " . int($max_size / 1_000_000) . "MB)");
361 }
362  
363 # Hash content for filename
364 0 require Digest::SHA;
365 0 my $hash = Digest::SHA::sha256_hex($data);
366 0 my $prefix = substr($hash, 0, 2);
367 0 my $filename = "$hash.$ext";
368  
369 # Save to webroot/img/<prefix>/
370 0 ▶ my $webroot = $opts->{webroot_dir} // 'www/webroot';
Condition0%0/2
$opts->{"webroot_dir"} // "www/webroot"
l!l
00
MC/DC0%0/1
$opts->{"webroot_dir"} // "www/webroot"
$opts->{"webroot_dir"}
Truth table0%0/2
A
A= $opts->{"webroot_dir"}
Aresult
11
00
371 0 my $dir = "$webroot/img/$prefix";
372  
373 0 require File::Path;
374 0 ▶ File::Path::make_path($dir) unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
375  
376 0 my $file_path = "$dir/$filename";
377  
378 # Content-addressed dedup — only write if not already present
379 0 ▶ unless (-f $file_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file_path) 0 0 0
380 0 ▶ open my $fh, '>', $file_path or return (0, "failed to save file");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file_path or return 0, "failed to save file" 0 0 0
381 0 binmode $fh;
382 0 print $fh $data;
383 0 close $fh;
384 }
385  
386 # Create File object in store
387 0 my $store = $opts->{store};
388 0 my $url = "/img/$prefix/$filename";
389  
390 0 my $file_obj = $store->new_obj('Yote::YapiServer::File',
391 url => $url,
392 type => $mime_type,
393 size => length($data),
394 original_name => $original_name,
395 file_path => $file_path,
396 );
397 0 ▶ $file_obj->set_owner($opts->{user}) if $opts->{user};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"user"} 0 0 0
398  
399 0 return (1, $file_obj);
400 }
401  
402 sub _mime_to_ext {
403 0 ▶ my ($mime) = @_;
Subroutine _mime_to_ext: not called CC=1 SCAR=6.9
404 0 my %map = (
405 'image/jpeg' => 'jpg',
406 'image/png' => 'png',
407 'image/gif' => 'gif',
408 'image/webp' => 'webp',
409 'image/svg+xml' => 'svg',
410 'application/pdf' => 'pdf',
411 'text/plain' => 'txt',
412 'text/csv' => 'csv',
413 'application/json' => 'json',
414 );
415 0 return $map{$mime};
416 }
417  
418 #----------------------------------------------------------------------
419 # Serialization
420 #----------------------------------------------------------------------
421  
422 sub serialize_value {
423 23 ▶ my ($class, $value, $ctx, $session, $viewer) = @_;
Subroutine serialize_value: called CC=13 SCAR=26.1
Pod undocumented
424  
425 23 ▶ return undef unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 23 23
426  
427 # Array
428 23 ▶ if (ref($value) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 1 22 23
429 1 return [ map { $class->serialize_value($_, $ctx, $session, $viewer) } @$value ];
430 }
431  
432 # Plain hash (unblessed)
433 22 ▶ if (ref($value) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH") 1 21 22
434 1 my %result;
435 1 for my $key (keys %$value) {
436 2 $result{$key} = $class->serialize_value($value->{$key}, $ctx, $session, $viewer);
437 }
438 1 return \%result;
439 }
440  
441 # Object with to_client_hash (blessed ref)
442 21 ▶ if (ref($value) && $value->can('to_client_hash')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value and $value->can("to_client_hash")) 7 14 21
Condition66%2/3
ref $value and $value->can("to_client_hash")
!ll&&!rl&&r
1407
MC/DC50%1/2
ref $value and $value->can("to_client_hash")
ref $value $value->can("to_client_hash")
Truth table66%2/3
A and B
A= ref $valueB= $value->can("to_client_hash")
ABresult
0X0
100
111
443 7 my $obj_id = "_obj_" . $value->id;
444  
445 # Dedup: skip objects already in ctx (or being serialized)
446 7 ▶ unless ($ctx->{objects}{$obj_id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ctx->{"objects"}{$obj_id}) 6 1 7
447 # Reserve slot to break circular references (e.g. User->File->User)
448 6 $ctx->{objects}{$obj_id} = 1;
449  
450 # Get the class name
451 6 my $client_class;
452 6 ▶ if ($value->can('_client_class_name')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->can('_client_class_name')) { } 6 0 6
453 6 $client_class = $value->_client_class_name;
454 } else {
455 0 $client_class = ref($value);
456 0 $client_class =~ s/^Yote::YapiServer::App:://;
457 0 $client_class =~ s/^Yote::YapiServer:://;
458 }
459  
460 # Get data fields from to_client_hash
461 6 my $data = $value->to_client_hash($session, $viewer);
462  
463 # Recursively serialize values within data
464 6 my %serialized_data;
465 6 for my $key (keys %$data) {
466 6 $serialized_data{$key} = $class->serialize_value($data->{$key}, $ctx, $session, $viewer);
467 }
468  
469 6 $ctx->{objects}{$obj_id} = {
470 _class => $client_class,
471 data => \%serialized_data,
472 };
473  
474 # Add methods to classes (once per class)
475 6 ▶ if (!$ctx->{classes}{$client_class} && $value->can('method_defs')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ctx->{"classes"}{$client_class} and $value->can("method_defs")) 5 1 6
Condition66%2/3
not $ctx->{"classes"}{$client_class} and $value->can("method_defs")
!ll&&!rl&&r
105
MC/DC50%1/2
not $ctx->{"classes"}{$client_class} and $value->can("method_defs")
not $ctx->{"classes"}{$client_class} $value->can("method_defs")
Truth table66%2/3
not(A) and B
A= not $ctx->{"classes"}{$client_class}B= $value->can("method_defs")
ABresult
0X0
100
111
476 5 my $methods = $value->method_defs;
477 5 $ctx->{classes}{$client_class} = [ sort keys %$methods ];
478 }
479  
480 # Expose object to session
481 6 ▶ $session->expose_object($value) if $session;
Branch: 1 branch
BranchTrueFalseTotal
if $session 6 0 6
482 }
483  
484 7 return "r$obj_id";
485 }
486  
487 # Scalar — prefix with 'v' to distinguish from references
488 14 return "v$value";
489 }
490  
491 #----------------------------------------------------------------------
492 # Rate limiting
493 #----------------------------------------------------------------------
494  
495 sub check_rate_limit {
496 26 ▶ my ($class, $endpoint, $ip, $session) = @_;
Subroutine check_rate_limit: called CC=10 SCAR=23.6
Pod undocumented
497  
498 my $config = $Yote::YapiServer::Site::RATE_LIMITS{$endpoint}
499 26 ▶ // $Yote::YapiServer::Site::RATE_LIMITS{default};
Condition33%1/3
$Yote::YapiServer::Site::RATE_LIMITS{$endpoint} // $Yote::YapiServer::Site::RATE_LIMITS{"default"}
l!l&&r!l&&!r
2600
MC/DC0%0/2
$Yote::YapiServer::Site::RATE_LIMITS{$endpoint} // $Yote::YapiServer::Site::RATE_LIMITS{"default"}
$Yote::YapiServer::Site::RATE_LIMITS{$endpoint} $Yote::YapiServer::Site::RATE_LIMITS{"default"}
Truth table33%1/3
A // B
A= $Yote::YapiServer::Site::RATE_LIMITS{$endpoint}B= $Yote::YapiServer::Site::RATE_LIMITS{"default"}
ABresult
1X1
011
000
500  
501 26 ▶ return undef unless $config;
Branch: 1 branch
BranchTrueFalseTotal
unless $config 0 26 26
502  
503 26 my $now = time();
504 26 my $key;
505 my $limit;
506  
507 26 ▶ if ($config->{per_ip}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($config->{'per_ip'}) { } 15 11 26
elsif ($config->{'per_session'} and $session) { } 10 1 11
508 15 $key = "ip:$ip:$endpoint";
509 15 $limit = $config->{per_ip};
510 }
511 ▶ elsif ($config->{per_session} && $session) {
Condition66%2/3
$config->{"per_session"} and $session
!ll&&!rl&&r
0110
MC/DC50%1/2
$config->{"per_session"} and $session
$config->{"per_session"} $session
Truth table66%2/3
A and B
A= $config->{"per_session"}B= $session
ABresult
0X0
100
111
512 10 $key = "session:" . $session->get_token . ":$endpoint";
513 10 $limit = $config->{per_session};
514 }
515 else {
516 1 return undef; # No applicable limit
517 }
518  
519 25 ▶ my $window = $config->{window} // 60;
Condition50%1/2
$config->{"window"} // 60
l!l
250
MC/DC0%0/1
$config->{"window"} // 60
$config->{"window"}
Truth table50%1/2
A
A= $config->{"window"}
Aresult
11
00
520  
521 # Initialize or reset window
522 25 ▶ $rate_limits{$key} //= { count => 0, window_start => $now };
Condition100%2/2
$rate_limits{$key} //= {"count", 0, "window_start", $now}
l!l
1015
MC/DC100%1/1
$rate_limits{$key} //= {"count", 0, "window_start", $now}
$rate_limits{$key}
Truth table100%2/2
A
A= $rate_limits{$key}
Aresult
11
00
523  
524 25 ▶ if ($now - $rate_limits{$key}{window_start} > $window) {
Branch: 1 branch
BranchTrueFalseTotal
if ($now - $rate_limits{$key}{"window_start"} > $window) 0 25 25
525 0 $rate_limits{$key} = { count => 0, window_start => $now };
526 }
527  
528 # Check limit
529 25 ▶ if ($rate_limits{$key}{count} >= $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rate_limits{$key}{"count"} >= $limit) 1 24 25
530 1 return 0, "rate limit exceeded, try again later";
531 }
532  
533 # Increment
534 24 $rate_limits{$key}{count}++;
535  
536 24 return undef; # OK to proceed
537 }
538  
539 #----------------------------------------------------------------------
540 # Response formatting
541 #----------------------------------------------------------------------
542  
543 sub success_response {
544 13 ▶ my ($class, $resp, $extra, $session) = @_;
Subroutine success_response: called CC=7 SCAR=19.7
Pod undocumented
545 13 ▶ $extra //= {};
Condition100%2/2
$extra //= {}
l!l
121
MC/DC100%1/1
$extra //= {}
$extra
Truth table100%2/2
A
A= $extra
Aresult
11
00
546  
547 13 my %envelope = ( ok => 1, resp => $resp );
548  
549 # Always include token when there's a session
550 13 ▶ if ($session && $session->can('get_token')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session and $session->can("get_token")) 13 0 13
Condition33%1/3
$session and $session->can("get_token")
!ll&&!rl&&r
0013
MC/DC0%0/2
$session and $session->can("get_token")
$session $session->can("get_token")
Truth table33%1/3
A and B
A= $sessionB= $session->can("get_token")
ABresult
0X0
100
111
551 13 $envelope{token} = $session->get_token;
552 }
553  
554 # Token from extra (login/createUser) overrides session token
555 13 ▶ if ($extra->{token}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extra->{"token"}) 3 10 13
556 3 $envelope{token} = $extra->{token};
557 }
558  
559 # Merge extra fields (objects, classes, apps)
560 13 for my $key (qw(objects classes apps)) {
561 39 ▶ $envelope{$key} = $extra->{$key} if $extra->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $extra->{$key} 10 29 39
562 }
563  
564 13 return encode_json(\%envelope);
565 }
566  
567 sub error_response {
568 10 ▶ my ($class, $error) = @_;
Subroutine error_response: called CC=1 SCAR=0.0
Pod undocumented
569 10 return encode_json({ ok => 0, error => $error });
570 }
571  
572 #----------------------------------------------------------------------
573 # Logging
574 #----------------------------------------------------------------------
575  
576 sub log_error {
577 0 ▶ my ($class, $context, $error) = @_;
Subroutine log_error: not called CC=3 SCAR=24.8
Pod undocumented
578 0 ▶ my $ip = $context->{ip_address} // 'unknown';
Condition0%0/2
$context->{"ip_address"} // "unknown"
l!l
00
MC/DC0%0/1
$context->{"ip_address"} // "unknown"
$context->{"ip_address"}
Truth table0%0/2
A
A= $context->{"ip_address"}
Aresult
11
00
579 0 ▶ my $action = $context->{request}{action} // 'unknown';
Condition0%0/2
$context->{"request"}{"action"} // "unknown"
l!l
00
MC/DC0%0/1
$context->{"request"}{"action"} // "unknown"
$context->{"request"}{"action"}
Truth table0%0/2
A
A= $context->{"request"}{"action"}
Aresult
11
00
580 0 warn "[Yote::YapiServer::Site] ERROR ip=$ip action=$action error=$error\n";
581 }
582  
583 sub log_call {
584 0 ▶ my ($class, $context, $target, $method, $success) = @_;
Subroutine log_call: not called CC=1 SCAR=6.9
Pod undocumented
585 # Implement audit logging here if needed
586 }
587  
588 1 1;
589  
590 __END__
« blib/lib/Yote/YapiServer/Site.pm ↑ Summary blib/lib/Yote/YapiServer.pm »