lib/Langertha/Skeid/Protocol/MCP.pm

filter: Statementstmt 100% 84 / 84 Branchbran 88.2% 30 / 34 Conditioncond 62.6% 42 / 67 MC/DCmcdc 40.0% 14 / 35 Subroutinesub 100% 17 / 17 Podpod 100% 14 / 14 totaltotal 80.0% 201 / 251 CC 45 SCAR 19.8
CC
45
Cov
84%
upstream_url
13.7
url_problem
12.6
listing_entry
6.3
CRAP 7.3
« blib/lib/Langertha/Skeid/CapacityProbe/Prometheus.pm ↑ Summary lib/Langertha/Skeid/Protocol/Rerank.pm »
1 package Langertha::Skeid::Protocol::MCP;
2 113 our $VERSION = '0.004';
3 # ABSTRACT: The MCP gateway's wire -- relayed headers, upstream URLs and the server listing
4 113 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 113 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 113 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 # The transports a server entry can name. Only one of them is relayed; the other is a hint for
10 # a server on the customer's own machine.
11 113 my %TRANSPORT = (
12 'streamable-http' => { relayed => 1 },
13 'stdio' => { relayed => 0 },
14 );
15  
16 # The client's request headers that travel upstream, beside every Mcp-* one. Nothing else of
17 # the client's does -- its Authorization least of all.
18 113 my %REQUEST_HEADER = map { $_ => 1 } qw( content-type accept last-event-id );
19  
20 # Header names a server's static `headers:` may not set, and why. A credential in the config
21 # would be a secret on disk (ADR 0003); the rest belong to the relay or to the connection.
22 113 my $CREDENTIAL = 'a credential is named by api_key_ref or api_key_env, never written into the config';
23 113 my $RELAYED = 'the relay passes it on from the client';
24 113 my $FRAMING = 'it belongs to the connection';
25 my %CONFIG_HEADER_PROBLEM = (
26 452 (map { $_ => $CREDENTIAL } qw( authorization proxy-authorization x-api-key cookie )),
27 339 (map { $_ => $RELAYED } keys %REQUEST_HEADER),
28 113 (map { $_ => $FRAMING } qw( host content-length transfer-encoding content-encoding accept-encoding
29 connection keep-alive te trailer upgrade expect )),
30 );
31  
32 # The upstream's response headers that reach the client, beside every Mcp-* one. Whatever else
33 # the server sets -- cookies, Server, CORS, HSTS -- is about the server and its own site, not
34 # for a client of this gateway. WWW-Authenticate is not here: it would come only with an
35 # upstream 401, which is answered 502 and never relayed.
36 113 my %RESPONSE_HEADER = map { $_ => 1 } qw( content-type cache-control retry-after );
37  
38  
39 135 ▶sub response_header_allowed {
Subroutine response_header_allowed: called CC=4 SCAR=13.9
Pod documented
40 135 my ($class, $name) = @_;
41 135 ▶ my $lc = lc($name // '');
Condition50%1/2
$name // ""
l!l
1350
MC/DC0%0/1
$name // ""
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
42 135 ▶ return ($RESPONSE_HEADER{$lc} || $lc =~ /\Amcp-/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$RESPONSE_HEADER{$lc} || $lc =~ /\Amcp-/ ? : 45 90 135
Condition100%3/3
$RESPONSE_HEADER{$lc} or $lc =~ /\Amcp-/
l!l&&r!l&&!r
261990
MC/DC100%2/2
$RESPONSE_HEADER{$lc} or $lc =~ /\Amcp-/
$RESPONSE_HEADER{$lc} $lc =~ /\Amcp-/
Truth table100%3/3
A or B
A= $RESPONSE_HEADER{$lc}B= $lc =~ /\Amcp-/
ABresult
1X1
011
000
43 }
44  
45  
46 1 ▶sub transports { return sort keys %TRANSPORT }
Subroutine transports: called CC=1 SCAR=0.0
Pod documented
47  
48  
49 128 ▶sub default_transport { 'streamable-http' }
Subroutine default_transport: called CC=1 SCAR=0.0
Pod documented
50  
51  
52 370 ▶sub is_transport {
Subroutine is_transport: called CC=4 SCAR=14.9
Pod documented
53 370 my ($class, $transport) = @_;
54 370 ▶ return (defined($transport) && !ref($transport) && $TRANSPORT{$transport}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $transport && !ref($transport) && $TRANSPORT{$transport} ? : 369 1 370
Condition50%3/6
defined $transport and not ref $transport and $TRANSPORT{$transport}
!ll&&!rl&&r
01369
defined $transport and not ref $transport
!ll&&!rl&&r
00370
MC/DC33%1/3
defined $transport and not ref $transport and $TRANSPORT{$transport}
defined $transport not ref $transport $TRANSPORT{$transport}
Truth table50%2/4
A and not(B) and C
A= defined $transportB= not ref $transportC= $TRANSPORT{$transport}
ABCresult
0XX0
10X0
1100
1111
55 }
56  
57  
58 214 ▶sub is_relayed {
Subroutine is_relayed: called CC=3 SCAR=11.1
Pod documented
59 214 my ($class, $transport) = @_;
60 214 ▶ return ($class->is_transport($transport) && $TRANSPORT{$transport}{relayed}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$class->is_transport($transport) && $TRANSPORT{$transport}{'relayed'} ? : 179 35 214
Condition66%2/3
$class->is_transport($transport) and $TRANSPORT{$transport}{"relayed"}
!ll&&!rl&&r
035179
MC/DC50%1/2
$class->is_transport($transport) and $TRANSPORT{$transport}{"relayed"}
$class->is_transport($transport) $TRANSPORT{$transport}{"relayed"}
Truth table66%2/3
A and B
A= $class->is_transport($transport)B= $TRANSPORT{$transport}{"relayed"}
ABresult
0X0
100
111
61 }
62  
63  
64 65 ▶sub listing_path { '/v1/mcp/servers' }
Subroutine listing_path: called CC=1 SCAR=0.0
Pod documented
65  
66  
67 43 ▶sub endpoint {
Subroutine endpoint: called CC=1 SCAR=0.0
Pod documented
68 43 my ($class, $name) = @_;
69 43 return '/mcp/' . $name;
70 }
71  
72  
73 158 ▶sub name_problem {
Subroutine name_problem: called CC=4 SCAR=14.6
Pod documented
74 158 my ($class, $name) = @_;
75 158 ▶ return undef if defined($name) && !ref($name) && $name =~ /\A[A-Za-z0-9][A-Za-z0-9._-]{0,63}\z/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name and not ref $name and $name =~ /\A[A-Za-z0-9][A-Za-z0-9._-]{0,63}\z/ 157 1 158
Condition50%3/6
defined $name and not ref $name and $name =~ /\A[A-Za-z0-9][A-Za-z0-9._-]{0,63}\z/
!ll&&!rl&&r
01157
defined $name and not ref $name
!ll&&!rl&&r
00158
MC/DC33%1/3
defined $name and not ref $name and $name =~ /\A[A-Za-z0-9][A-Za-z0-9._-]{0,63}\z/
defined $name not ref $name $name =~ /\A[A-Za-z0-9][A-Za-z0-9._-]{0,63}\z/
Truth table50%2/4
A and not(B) and C
A= defined $nameB= not ref $nameC= $name =~ /\A[A-Za-z0-9][A-Za-z0-9._-]{0,63}\z/
ABCresult
0XX0
10X0
1100
1111
76 1 return 'a server name must start with a letter or digit and hold only letters, digits, ".", '
77 . '"_" and "-", at most 64 characters: it is the <name> of /mcp/<name>';
78 }
79  
80  
81 145 ▶sub url_problem {
Subroutine url_problem: called CC=10 SCAR=25.3
Pod documented
82 145 my ($class, $url) = @_;
83 145 ▶ my $parsed = (defined($url) && !ref($url) && length($url)) ? Mojo::URL->new("$url") : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $url && !ref($url) && length $url ? : 145 0 145
Condition33%2/6
defined $url and not ref $url and length $url
!ll&&!rl&&r
00145
defined $url and not ref $url
!ll&&!rl&&r
00145
MC/DC0%0/3
defined $url and not ref $url and length $url
defined $url not ref $url length $url
Truth table25%1/4
A and not(B) and C
A= defined $urlB= not ref $urlC= length $url
ABCresult
0XX0
10X0
1100
1111
84 145 ▶ return 'must be an absolute http or https URL'
Branch: 1 branch
BranchTrueFalseTotal
unless $parsed and ($parsed->scheme // "") =~ /\Ahttps?\z/i and length($parsed->host // "") 3 142 145
Condition70%7/10
$parsed and ($parsed->scheme // "") =~ /\Ahttps?\z/i and length($parsed->host // "")
!ll&&!rl&&r
30142
$parsed and ($parsed->scheme // "") =~ /\Ahttps?\z/i
!ll&&!rl&&r
03142
$parsed->scheme // ""
l!l
1423
$parsed->host // ""
l!l
1420
MC/DC33%1/3
$parsed and ($parsed->scheme // "") =~ /\Ahttps?\z/i and length($parsed->host // "")
$parsed $parsed->scheme $parsed->host
Truth table50%2/4
A and B and C
A= $parsedB= $parsed->schemeC= $parsed->host
ABCresult
0XX0
10X0
1100
1111
85 unless $parsed && ($parsed->scheme // '') =~ /\Ahttps?\z/i && length($parsed->host // '');
86 142 ▶ return 'must not carry credentials: an upstream key is named by api_key_ref or api_key_env'
Branch: 1 branch
BranchTrueFalseTotal
if defined $parsed->userinfo 1 141 142
87 if defined $parsed->userinfo;
88 141 return undef;
89 }
90  
91  
92 29 ▶sub config_header_problem {
Subroutine config_header_problem: called CC=3 SCAR=11.0
Pod documented
93 29 my ($class, $name) = @_;
94 29 ▶ my $lc = lc($name // '');
Condition50%1/2
$name // ""
l!l
290
MC/DC0%0/1
$name // ""
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
95 29 ▶ return $RELAYED if $lc =~ /\Amcp-/;
Branch: 1 branch
BranchTrueFalseTotal
if $lc =~ /\Amcp-/ 1 28 29
96 28 return $CONFIG_HEADER_PROBLEM{$lc};
97 }
98  
99  
100 29 ▶sub request_headers {
Subroutine request_headers: called CC=4 SCAR=13.9
Pod documented
101 29 my ($class, $headers) = @_;
102 29 my %out;
103 29 for my $name (@{$headers->names}) {
104 168 my $lc = lc($name);
105 168 ▶ next unless $REQUEST_HEADER{$lc} || $lc =~ /\Amcp-/;
Branch: 1 branch
BranchTrueFalseTotal
unless $REQUEST_HEADER{$lc} or $lc =~ /\Amcp-/ 149 19 168
Condition100%3/3
$REQUEST_HEADER{$lc} or $lc =~ /\Amcp-/
l!l&&r!l&&!r
145149
MC/DC100%2/2
$REQUEST_HEADER{$lc} or $lc =~ /\Amcp-/
$REQUEST_HEADER{$lc} $lc =~ /\Amcp-/
Truth table100%3/3
A or B
A= $REQUEST_HEADER{$lc}B= $lc =~ /\Amcp-/
ABresult
1X1
011
000
106 19 $out{$name} = $headers->header($name);
107 }
108 29 return %out;
109 }
110  
111  
112 37 ▶sub upstream_url {
Subroutine upstream_url: called CC=13 SCAR=26.2
Pod documented
113 37 my ($class, $base, $rest, $query) = @_;
114 37 my $url = Mojo::URL->new($base);
115  
116 37 ▶ if (defined($rest) && length($rest)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rest and length $rest) 7 30 37
Condition66%2/3
defined $rest and length $rest
!ll&&!rl&&r
3007
MC/DC50%1/2
defined $rest and length $rest
defined $rest length $rest
Truth table66%2/3
A and B
A= defined $restB= length $rest
ABresult
0X0
100
111
117 7 my @segments = split m{/}, $rest, -1;
118 7 ▶ my $trailing = (@segments && $segments[-1] eq '') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@segments && $segments[-1] eq '' ? : 0 7 7
Condition33%1/3
@segments and $segments[-1] eq ""
!ll&&!rl&&r
070
MC/DC0%0/2
@segments and $segments[-1] eq ""
@segments $segments[-1] eq ""
Truth table33%1/3
A and B
A= @segmentsB= $segments[-1] eq ""
ABresult
0X0
100
111
119 7 ▶ pop @segments if $trailing;
Branch: 1 branch
BranchTrueFalseTotal
if $trailing 0 7 7
120 7 for my $segment (@segments) {
121 10 ▶ return undef if $segment eq '.' || $segment eq '..' || $segment =~ /\\/;
Branch: 1 branch
BranchTrueFalseTotal
if $segment eq "." or $segment eq ".." or $segment =~ /\\/ 6 4 10
Condition100%6/6
$segment eq "." or $segment eq ".." or $segment =~ /\\/
l!l&&r!l&&!r
514
$segment eq "." or $segment eq ".."
l!l&&r!l&&!r
145
MC/DC100%3/3
$segment eq "." or $segment eq ".." or $segment =~ /\\/
$segment eq "." $segment eq ".." $segment =~ /\\/
Truth table100%4/4
A or B or C
A= $segment eq "."B= $segment eq ".."C= $segment =~ /\\/
ABCresult
1XX1
01X1
0011
0000
122 }
123 1 ▶ if (@segments) {
Branch: 1 branch
BranchTrueFalseTotal
if (@segments) 1 0 1
124 1 my $path = $url->path;
125 1 push @{$path->parts}, @segments;
126 1 $path->trailing_slash($trailing);
127 }
128 }
129  
130 31 ▶ $url->query->append(@{$query->pairs}) if $query && @{$query->pairs};
Branch: 1 branch
BranchTrueFalseTotal
if $query and @{$query->pairs;} 1 30 31
Condition66%2/3
$query and @{$query->pairs;}
!ll&&!rl&&r
0301
MC/DC50%1/2
$query and @{$query->pairs;}
$query @{$query->pairs;}
Truth table66%2/3
A and B
A= $queryB= @{$query->pairs;}
ABresult
0X0
100
111
131 31 return $url;
132 }
133  
134  
135 19 ▶sub listing_entry {
Subroutine listing_entry: called CC=6 SCAR=18.4
Pod documented
136 19 my ($class, $server, $base) = @_;
137 my %entry = (
138 name => $server->{name},
139 transport => $server->{transport},
140 19 ▶ description => ($server->{description} // ''),
Condition50%1/2
$server->{"description"} // ""
l!l
190
MC/DC0%0/1
$server->{"description"} // ""
$server->{"description"}
Truth table50%1/2
A
A= $server->{"description"}
Aresult
11
00
141 );
142 19 ▶ if ($class->is_relayed($server->{transport})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->is_relayed($server->{'transport'})) { } 14 5 19
143 14 ▶ $entry{url} = ($base // '') . $class->endpoint($server->{name});
Condition50%1/2
$base // ""
l!l
140
MC/DC0%0/1
$base // ""
$base
Truth table50%1/2
A
A= $base
Aresult
11
00
144 } else {
145 5 $entry{command} = $server->{command};
146 5 ▶ $entry{args} = [ @{$server->{args} || []} ];
Condition50%1/2
$server->{'args'} || []
l!l
50
MC/DC0%0/1
$server->{'args'} || []
$server->{'args'}
Truth table50%1/2
A
A= $server->{'args'}
Aresult
11
00
147 5 ▶ $entry{env} = { %{$server->{env} || {}} };
Condition50%1/2
$server->{'env'} || {}
l!l
50
MC/DC0%0/1
$server->{'env'} || {}
$server->{'env'}
Truth table50%1/2
A
A= $server->{'env'}
Aresult
11
00
148 }
149 19 return \%entry;
150 }
151  
152  
153 21 ▶sub is_event_stream {
Subroutine is_event_stream: called CC=3 SCAR=11.1
Pod documented
154 21 my ($class, $content_type) = @_;
155 21 ▶ return (defined($content_type) && $content_type =~ m{\A\s*text/event-stream\s*(?:;|\z)}i) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $content_type && $content_type =~ m[\A\s*text/event-stream\s*(?:;|\z)]i ? : 4 17 21
Condition66%2/3
defined $content_type and $content_type =~ m[\A\s*text/event-stream\s*(?:;|\z)]i
!ll&&!rl&&r
0174
MC/DC50%1/2
defined $content_type and $content_type =~ m[\A\s*text/event-stream\s*(?:;|\z)]i
defined $content_type $content_type =~ m[\A\s*text/event-stream\s*(?:;|\z)]i
Truth table66%2/3
A and B
A= defined $content_typeB= $content_type =~ m[\A\s*text/event-stream\s*(?:;|\z)]i
ABresult
0X0
100
111
156 }
157  
158  
159 113 1;
160  
161 __END__
« blib/lib/Langertha/Skeid/CapacityProbe/Prometheus.pm ↑ Summary lib/Langertha/Skeid/Protocol/Rerank.pm »