blib/lib/SignalWire/Server/AgentServer.pm

filter: Statementstmt 69.4% 91 / 131 Branchbran 38.0% 19 / 50 Conditioncond 15.1% 5 / 33 MC/DCmcdc 5.2% 1 / 19 Subroutinesub 91.3% 21 / 23 Podpod 0.0% 0 / 7 totaltotal 52.0% 137 / 263 CC 40 SCAR 43.1
CC
40
Cov
54%
__ANON__
171.2
serve_static_files
42.0
run
12.0
CRAP 74.6
« blib/lib/SignalWire/SWAIG/FunctionResult.pm ↑ Summary blib/lib/SignalWire/REST/HttpClient.pm »
1 package SignalWire::Server::AgentServer;
2 # Copyright (c) 2025 SignalWire
3 # Licensed under the MIT License.
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use JSON qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 16 ▶has host => (is => 'rw', default => sub { '0.0.0.0' });
Subroutine __ANON__: called CC=1 SCAR=0.0
13 16 ▶has port => (is => 'rw', default => sub { $ENV{PORT} || 3000 });
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"PORT"} or 3000 0 16 16
14 16 ▶has log_level => (is => 'rw', default => sub { 'info' });
Subroutine __ANON__: called CC=1 SCAR=0.0
15 16 ▶has agents => (is => 'rw', default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
16  
17 # SIP routing
18 16 ▶has _sip_routing_enabled => (is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
19 16 ▶has _sip_username_mapping => (is => 'rw', default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
20  
21 # Static file routes: { route => directory }
22 16 ▶has _static_routes => (is => 'rw', default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
23  
24 15 ▶sub register {
Subroutine register: called CC=5 SCAR=16.2
Pod undocumented
25 15 my ($self, $agent, $route) = @_;
26  
27 15 ▶ $route //= $agent->route;
Condition66%2/3
$route //= $agent->route
l!l&&r!l&&!r
2130
MC/DC0%0/2
$route //= $agent->route
$route $agent->route
Truth table66%2/3
A //= B
A= $routeB= $agent->route
ABresult
1X1
011
000
28 15 ▶ $route = "/$route" unless $route =~ m{^/};
Branch: 1 branch
BranchTrueFalseTotal
unless $route =~ m[^/] 1 14 15
29 15 ▶ $route =~ s{/+$}{} unless $route eq '/';
Branch: 1 branch
BranchTrueFalseTotal
unless $route eq "/" 15 0 15
30  
31 15 ▶ if (exists $self->agents->{$route}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->agents->{$route}) 1 14 15
32 1 croak("Route '$route' is already registered");
33 }
34  
35 14 $agent->route($route);
36 14 $self->agents->{$route} = $agent;
37 14 return $self;
38 }
39  
40 1 ▶sub unregister {
Subroutine unregister: called CC=3 SCAR=11.3
Pod undocumented
41 1 my ($self, $route) = @_;
42 1 ▶ $route = "/$route" unless $route =~ m{^/};
Branch: 1 branch
BranchTrueFalseTotal
unless $route =~ m[^/] 0 1 1
43 1 ▶ $route =~ s{/+$}{} unless $route eq '/';
Branch: 1 branch
BranchTrueFalseTotal
unless $route eq "/" 1 0 1
44 1 delete $self->agents->{$route};
45 1 return $self;
46 }
47  
48 1 ▶sub list_agents {
Subroutine list_agents: called CC=1 SCAR=0.0
Pod undocumented
49 1 my ($self) = @_;
50 1 return [ sort keys %{ $self->agents } ];
51 }
52  
53 2 ▶sub get_agent {
Subroutine get_agent: called CC=1 SCAR=0.0
Pod undocumented
54 2 my ($self, $route) = @_;
55 2 return $self->agents->{$route};
56 }
57  
58 0 ▶sub serve_static_files {
Subroutine serve_static_files: not called CC=6 SCAR=37.4
Pod undocumented
59 0 my ($self, $directory, $route) = @_;
60  
61 0 ▶ croak("serve_static_files requires a directory") unless defined $directory;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $directory 0 0 0
62 0 ▶ croak("serve_static_files requires a route") unless defined $route;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $route 0 0 0
63 0 ▶ croak("Static directory '$directory' does not exist") unless -d $directory;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $directory 0 0 0
64  
65 0 ▶ $route = "/$route" unless $route =~ m{^/};
Branch: 1 branch
BranchTrueFalseTotal
unless $route =~ m[^/] 0 0 0
66 0 ▶ $route =~ s{/+$}{} unless $route eq '/';
Branch: 1 branch
BranchTrueFalseTotal
unless $route eq "/" 0 0 0
67  
68 # Resolve the directory to an absolute path for security
69 0 $self->_static_routes->{$route} = File::Spec->rel2abs($directory);
70 0 return $self;
71 }
72  
73 7 ▶sub psgi_app {
Subroutine psgi_app: called CC=1 SCAR=0.0
Pod undocumented
74 7 my ($self) = @_;
75 7 return $self->_build_psgi_app;
76 }
77  
78 7 ▶sub _build_psgi_app {
Subroutine _build_psgi_app: called CC=1 SCAR=1.5
79 7 my ($self) = @_;
80 7 require Plack::Request;
81  
82 7 my $server = $self;
83  
84 # MIME type mapping for static files
85 7 my %mime_types = (
86 html => 'text/html',
87 htm => 'text/html',
88 css => 'text/css',
89 js => 'application/javascript',
90 json => 'application/json',
91 png => 'image/png',
92 jpg => 'image/jpeg',
93 jpeg => 'image/jpeg',
94 gif => 'image/gif',
95 svg => 'image/svg+xml',
96 ico => 'image/x-icon',
97 txt => 'text/plain',
98 pdf => 'application/pdf',
99 xml => 'application/xml',
100 woff => 'font/woff',
101 woff2 => 'font/woff2',
102 ttf => 'font/ttf',
103 eot => 'application/vnd.ms-fontobject',
104 );
105  
106 # Build a plain PSGI app with route dispatch
107 7 ▶ my $core_app = sub {
Subroutine __ANON__: called CC=25 SCAR=51.4
108 7 my $env = shift;
109 7 ▶ my $path = $env->{PATH_INFO} // '/';
Condition50%1/2
$env->{"PATH_INFO"} // "/"
l!l
70
MC/DC0%0/1
$env->{"PATH_INFO"} // "/"
$env->{"PATH_INFO"}
Truth table50%1/2
A
A= $env->{"PATH_INFO"}
Aresult
11
00
110 7 ▶ $path =~ s{/+$}{} unless $path eq '/';
Branch: 1 branch
BranchTrueFalseTotal
unless $path eq "/" 7 0 7
111  
112 # Health/ready (no auth)
113 7 ▶ if ($path eq '/health') {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "/health") 2 5 7
114 1 my @agent_names = map { $server->agents->{$_}->name }
115 2 sort keys %{ $server->agents };
116 2 return [200, ['Content-Type' => 'application/json'],
117 [encode_json({ status => 'healthy', agents => \@agent_names })]];
118 }
119 5 ▶ if ($path eq '/ready') {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "/ready") 1 4 5
120 1 return [200, ['Content-Type' => 'application/json'],
121 [encode_json({ status => 'ready' })]];
122 }
123  
124 # Check static file routes (longest prefix match)
125 0 for my $static_route (sort { length($b) <=> length($a) } keys %{ $server->_static_routes }) {
126 0 ▶ my $prefix = $static_route eq '/' ? '' : $static_route;
Branch: 1 branch
BranchTrueFalseTotal
$static_route eq '/' ? : 0 0 0
127 0 ▶ if ($path eq $static_route || index($path, "$prefix/") == 0 || ($static_route eq '/' && $path =~ m{^/})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq $static_route or index($path, "$prefix/") == 0 or $static_route eq "/" and $path =~ m[^/]) 0 0 0
Condition0%0/9
$path eq $static_route or index($path, "$prefix/") == 0 or $static_route eq "/" and $path =~ m[^/]
l!l&&r!l&&!r
000
$path eq $static_route or index($path, "$prefix/") == 0
l!l&&r!l&&!r
000
$static_route eq "/" and $path =~ m[^/]
!ll&&!rl&&r
000
MC/DC0%0/4
$path eq $static_route or index($path, "$prefix/") == 0 or $static_route eq "/" and $path =~ m[^/]
$path eq $static_route index($path, "$prefix/") == 0 $static_route eq "/" $path =~ m[^/]
Truth table0%0/5
A or B or C and D
A= $path eq $static_routeB= index($path, "$prefix/") == 0C= $static_route eq "/"D= $path =~ m[^/]
ABCDresult
1XXX1
01XX1
00111
000X0
00100
128 0 ▶ next if $static_route eq '/' && $path eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if $static_route eq "/" and $path eq "/" 0 0 0
Condition0%0/3
$static_route eq "/" and $path eq "/"
!ll&&!rl&&r
000
MC/DC0%0/2
$static_route eq "/" and $path eq "/"
$static_route eq "/" $path eq "/"
Truth table0%0/3
A and B
A= $static_route eq "/"B= $path eq "/"
ABresult
0X0
100
111
129 0 my $rel_path = substr($path, length($prefix));
130 0 $rel_path =~ s{^/}{};
131  
132 # Path traversal protection: reject ".." components
133 0 ▶ if ($rel_path =~ m{(?:^|/)\.\.(?:/|$)}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel_path =~ m[(?:^|/)\.\.(?:/|$)]) 0 0 0
134 0 return [403, [
135 'Content-Type' => 'text/plain',
136 'X-Content-Type-Options' => 'nosniff',
137 'X-Frame-Options' => 'DENY',
138 'Cache-Control' => 'no-store',
139 ], ['Forbidden']];
140 }
141  
142 0 my $base_dir = $server->_static_routes->{$static_route};
143 0 my $file_path = File::Spec->catfile($base_dir, split(m{/}, $rel_path));
144  
145 # Resolve to absolute and verify it's within the base directory
146 0 my $abs_path = File::Spec->rel2abs($file_path);
147 0 ▶ unless (index($abs_path, $base_dir) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless (index($abs_path, $base_dir) == 0) 0 0 0
148 0 return [403, [
149 'Content-Type' => 'text/plain',
150 'X-Content-Type-Options' => 'nosniff',
151 'X-Frame-Options' => 'DENY',
152 'Cache-Control' => 'no-store',
153 ], ['Forbidden']];
154 }
155  
156 0 ▶ if (-f $abs_path && -r $abs_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $abs_path and -r $abs_path) 0 0 0
Condition0%0/3
-f $abs_path and -r $abs_path
!ll&&!rl&&r
000
MC/DC0%0/2
-f $abs_path and -r $abs_path
-f $abs_path -r $abs_path
Truth table0%0/3
A and B
A= -f $abs_pathB= -r $abs_path
ABresult
0X0
100
111
157 # Determine MIME type from extension
158 0 my ($ext) = ($abs_path =~ /\.(\w+)$/);
159 0 ▶ $ext = lc($ext // '');
Condition0%0/2
$ext // ""
l!l
00
MC/DC0%0/1
$ext // ""
$ext
Truth table0%0/2
A
A= $ext
Aresult
11
00
160 0 ▶ my $content_type = $mime_types{$ext} // 'application/octet-stream';
Condition0%0/2
$mime_types{$ext} // "application/octet-stream"
l!l
00
MC/DC0%0/1
$mime_types{$ext} // "application/octet-stream"
$mime_types{$ext}
Truth table0%0/2
A
A= $mime_types{$ext}
Aresult
11
00
161  
162 0 ▶ open my $fh, '<:raw', $abs_path or
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $abs_path or return [500, ["Content-Type", "text/plain"], ["Internal Server Error"]] 0 0 0
163 return [500, ['Content-Type' => 'text/plain'], ['Internal Server Error']];
164 0 local $/;
165 0 my $content = <$fh>;
166 0 close $fh;
167  
168 0 return [200, [
169 'Content-Type' => $content_type,
170 'Content-Length' => length($content),
171 'X-Content-Type-Options' => 'nosniff',
172 'X-Frame-Options' => 'DENY',
173 'Cache-Control' => 'no-store',
174 ], [$content]];
175 }
176  
177 # Static route matched but file not found - fall through
178 }
179 }
180  
181 # Find matching agent by longest prefix
182 4 my $matched_route;
183 4 for my $route (sort { length($b) <=> length($a) } keys %{ $server->agents }) {
184 4 ▶ if ($route eq '/') {
Branch: 1 branch
BranchTrueFalseTotal
if ($route eq "/") 0 4 4
185 0 $matched_route = $route;
186 0 last;
187 }
188 4 ▶ if ($path eq $route || index($path, "$route/") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq $route or index($path, "$route/") == 0) 3 1 4
Condition66%2/3
$path eq $route or index($path, "$route/") == 0
l!l&&r!l&&!r
301
MC/DC50%1/2
$path eq $route or index($path, "$route/") == 0
$path eq $route index($path, "$route/") == 0
Truth table66%2/3
A or B
A= $path eq $routeB= index($path, "$route/") == 0
ABresult
1X1
011
000
189 3 $matched_route = $route;
190 3 last;
191 }
192 }
193  
194 4 ▶ if (defined $matched_route) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $matched_route) 3 1 4
195 3 my $agent = $server->agents->{$matched_route};
196 3 my $agent_app = $agent->psgi_app;
197 3 return $agent_app->($env);
198 }
199  
200 1 return [404, ['Content-Type' => 'application/json'],
201 [encode_json({ error => 'Not Found' })]];
202 7 };
203  
204 # Wrap with security headers
205 7 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
206 7 my $env = shift;
207 7 my $res = $core_app->($env);
208 7 ▶ if (ref $res eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $res eq "ARRAY") 7 0 7
209 7 push @{ $res->[1] },
210 'X-Content-Type-Options' => 'nosniff',
211 'X-Frame-Options' => 'DENY',
212 'Cache-Control' => 'no-store';
213 }
214 7 return $res;
215 7 };
216 }
217  
218 0 ▶sub run {
Subroutine run: not called CC=3 SCAR=24.8
Pod undocumented
219 0 my ($self, %opts) = @_;
220 0 my $app = $self->psgi_app;
221 0 ▶ my $host = $opts{host} // $self->host;
Condition0%0/3
$opts{"host"} // $self->host
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"host"} // $self->host
$opts{"host"} $self->host
Truth table0%0/3
A // B
A= $opts{"host"}B= $self->host
ABresult
1X1
011
000
222 0 ▶ my $port = $opts{port} // $self->port;
Condition0%0/3
$opts{"port"} // $self->port
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"port"} // $self->port
$opts{"port"} $self->port
Truth table0%0/3
A // B
A= $opts{"port"}B= $self->port
ABresult
1X1
011
000
223  
224 0 require Plack::Runner;
225 0 my $runner = Plack::Runner->new;
226 0 $runner->parse_options(
227 '--host' => $host,
228 '--port' => $port,
229 '--server' => 'HTTP::Server::PSGI',
230 );
231 0 $runner->run($app);
232 }
233  
234 1 1;
« blib/lib/SignalWire/SWAIG/FunctionResult.pm ↑ Summary blib/lib/SignalWire/REST/HttpClient.pm »