| 1 | #!/opt/bin/perl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | mcp-http - HTTP requests with URL and method permissions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | mcp-http --stdio | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | mcp-http --connect http://host:port/TOKEN/http | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | One MCP C<request> tool backed by L<AnyEvent::HTTP>. HTTP methods are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | permission actions. URL scopes: exact URL, path with any query, origin, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | hostname, hostname and subdomains, or any HTTP(S) URL. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | Supply a C<cookie_jar> from an earlier response to continue a session; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | the server keeps no cookies. This uses AnyEvent::HTTP's simplified jar, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | not a full browser cookie implementation. Treat the jar as confidential. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | Responses include status, headers, UTF-8 body or Base64 bytes. Optional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | C<grep> keeps lines containing a literal string; C<regex> keeps lines | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | matching a Perl regex; C<substitute> replaces regex matches with literal | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | replacement text. C<max_bytes> bounds downloads and C<truncated> reports | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | partial results. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | Redirects and retries are disabled. HTTPS verifies certificates by default. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | Set C<verify_tls> to false to skip certificate and hostname verification | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | for an individual request. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | Network access is NOT sandboxed; permitted names can reach local or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | private hosts. HTTP failures do not prove that a write did not happen. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | When spawned as a multibroker stdio child, the server exits after five | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | minutes without MCP requests and is restarted on demand. Directly connected | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | clients and HTTP listeners are unaffected. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | =head1 FIRST USE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | Configure your MCP client to launch: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | mcp-http --stdio --name web | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | Or combine it with other servers: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | mcp-multibroker --stdio web --- mcp-http --name web --stdio --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | Ask the assistant: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | Fetch https://example.org/ and report the status and page title. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | Do not submit forms or send any modifying requests. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | The C<request> tool receives arguments such as: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | {"url": "https://example.org/", "method": "GET", "max_bytes": 8192} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | The first request asks for permission to GET that URL. An approval for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | GET does not authorize POST, and an approval for an exact URL need not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | authorize its whole hostname. Review broader scope choices carefully, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | especially on hosts serving private or local resources. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | To carry a session between requests, supply the returned C<cookie_jar> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | to the next call. To inspect a large text response without returning it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | all, use C<grep> or C<regex> and a suitable C<max_bytes>. Redirect | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | responses are returned to the caller; they are not followed. Repeating | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | a failed modifying request can repeat its side effects. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | =head1 TOOL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | =over | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | =item request | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | Required C<url>. Optional C<method> (GET), C<headers>, UTF-8 C<body> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | or C<body_base64>, C<cookie_jar>, C<timeout> (30s inactivity), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | C<verify_tls> (true by default), C<max_bytes> (65536 default, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 2097152 maximum), C<grep>, C<regex>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | and C<substitute> with C<pattern> and C<replacement>. Responses contain | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | C<captured_bytes> and C<truncated>. Replacement text is literal. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | =head1 PERMISSIONS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | Profile C<http-NAME>. Rules match HTTP method as C<action> and URL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | as C<match.url>. Hostname and subdomain scopes are textual DNS suffixes, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | not organizational or public-suffix boundaries. No sandbox grants. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | C<verify_tls> does not affect permission matching. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | =head1 OPTIONS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | Shared switches are documented in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | L<AnyEvent::MCP::Server::Simple/COMMON COMMAND-LINE OPTIONS>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | The following shared switch has HTTP-permission-specific behavior. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | =over | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | =item --ask MODE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | Select permission prompt handling: C<helper> (default) uses the local | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | permission helper; C<tty> or C<yad> selects only that interface; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | C<mcp> forwards prompts to the MCP client; C<auto> uses client forms | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | when advertised, otherwise the local helper; C<none> denies unmatched | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | permission checks without prompting. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 2 | ▶ | use common::sense; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 2 | ▶ | use MIME::Base64 (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 2 | ▶ | use Encode (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 2 | ▶ | use JSON::XS (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 2 | ▶ | use AnyEvent::HTTP (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 2 | ▶ | use AnyEvent::MCP (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 2 | ▶ | use AnyEvent::MCP::Server::Permissions (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 2 | ▶ | use AnyEvent::MCP::Server::Simple; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 2 | my $options = mcp_getoptions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | usage => "Usage: mcp-http [OPTION]...\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | description => "HTTP requests with URL/method permissions and caller-managed cookies.", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | examples => " mcp-http --stdio\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 2 | ▶ | @ARGV and die "Unexpected arguments\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 2 | mcp_permissions +AnyEvent::MCP::Server::Permissions->new ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | name => "http-$options->{name}", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | mcp_config | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | idle_exit => 300, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | server_name => "mcp-http", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | server_version => $AnyEvent::MCP::VERSION, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | short_instructions => "HTTP requests with URL/method permissions; pass cookie_jar between calls. No redirects.", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | instructions => "request makes authorized HTTP(S) requests. Method is the permission action. " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | . "URL scopes range from exact URL to any HTTP(S) URL. No automatic redirects or retries. " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | . "Pass a returned cookie_jar to subsequent calls. " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | . "grep/regex select lines; substitute applies literal replacements. " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | . "Responses may be truncated and failed writes can still have taken effect.", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 20 | ▶ | on_request => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 20 | my ($server, $req) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | return mcp_log_request $server, $req | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 20 | ▶ | unless $req->{method} eq "tools/call" && $req->{tool} eq "request"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$req->{"method"} eq "tools/call" and $req->{"tool"} eq "request"
MC/DC50%1/2
$req->{"method"} eq "tools/call" and $req->{"tool"} eq "request"
$req->{"method"} eq "tools/call"
$req->{"tool"} eq "request"
Truth table66%2/3
A and B
A= $req->{"method"} eq "tools/call"B= $req->{"tool"} eq "request"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 17 | my $args = $req->{args}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 17 | ▶ | my $method = $args->{method} // "GET"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args->{"method"} // "GET"
MC/DC100%1/1
$args->{"method"} // "GET"
$args->{"method"}
Truth table100%2/2
A
A= $args->{"method"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 17 | ▶ | my $url = $args->{url} // ""; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args->{"url"} // ""
MC/DC0%0/1
$args->{"url"} // ""
$args->{"url"}
Truth table50%1/2
A
A= $args->{"url"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 17 | ▶ | $method = "<invalid>" if ref $method; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 17 | ▶ | $url = "<invalid>" if ref $url; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 17 | $url =~ s/[?#].*//s; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 17 | $url =~ s{(://)[^/@]*@}{$1}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 17 | $url =~ s/[\x00-\x1f\x7f]/ /g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 17 | mcp_log_request $server, $req, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | method => (substr $method, 0, 20), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | url => (substr $url, 0, 160); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 2 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 15 | ▶ | sub url_scopes { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
url_scopes: called CC=19 SCAR=32.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 15 | my ($url) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 15 | ▶ | defined $url && !ref $url && $url !~ /[\x00-\x20\x7f]/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $url and not ref $url and not $url =~ /[\x00-\x20\x7f]/u
defined $url and not ref $url
MC/DC0%0/3
defined $url and not ref $url and not $url =~ /[\x00-\x20\x7f]/u
defined $url
not ref $url
not $url =~ /[\x00-\x20\x7f]/u
Truth table25%1/4
A and not(B) and not(C)
A= defined $urlB= not ref $urlC= not $url =~ /[\x00-\x20\x7f]/u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | or die "Invalid URL\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 15 | ▶ | $url =~ m{\A(https?)://(\[[0-9a-f:.]+\]|[a-z0-9.-]+)(?::([0-9]+))?([^#]*)(?:#.*)?\z}i | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | or die "Expected HTTP(S) URL without userinfo\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 15 | my ($scheme, $host, $port, $rest) = (lc $1, lc $2, $3, $4); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 15 | ▶ | !defined $port || $port >= 1 && $port <= 65535 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
not defined $port or $port >= 1 and $port <= 65535
$port >= 1 and $port <= 65535
MC/DC0%0/3
not defined $port or $port >= 1 and $port <= 65535
not defined $port
$port >= 1
$port <= 65535
Truth table50%2/4
not(A) or B and C
A= not defined $portB= $port >= 1C= $port <= 65535
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | or die "Invalid URL port\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 15 | ▶ | $port = undef if defined $port && $port == ($scheme eq "https" ? 443 : 80); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
defined $port and $port == ($scheme eq "https" ? 443 : 80)
MC/DC100%2/2
defined $port and $port == ($scheme eq "https" ? 443 : 80)
defined $port
$port == ($scheme eq "https" ? 443 : 80)
Truth table100%3/3
A and B
A= defined $portB= $port == ($scheme eq "https" ? 443 : 80)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 15 | ▶ | $rest =~ m{\A(?:/|\?|\z)} or die "Invalid URL path\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 14 | ▶ | $rest = "/$rest" unless $rest =~ m{\A/}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 14 | ▶ | my $origin = "$scheme://$host" . (defined $port ? ":$port" : ""); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 14 | my $path = $rest; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 14 | $path =~ s/\?.*//s; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 14 | my $full = "$origin$rest"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 14 | ▶ | my $short_url = length $full > 100 ? substr ($full, 0, 97) . "..." : $full; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 14 | my $short_path = "$origin$path"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 14 | ▶ | $short_path = substr ($short_path, 0, 97) . "..." if length $short_path > 100; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 14 | my @choices = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | { label => "Exact URL: $short_url", match => $full }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | { label => "This path: $short_path (any query)", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | match => "^" . (quotemeta "$origin$path") . "(?:\\?.*)?\\z" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | { label => "This origin: $origin", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | match => "^" . (quotemeta $origin) . "/" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | { label => "This hostname: $host (any scheme or port)", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | match => "^https?://" . (quotemeta $host) . "(?::[0-9]+)?/" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 14 | ▶ | if ($host !~ /^\[/ && $host =~ /\./ && $host !~ /^[0-9.]+$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
not $host =~ /^\[/u and $host =~ /\./u and not $host =~ /^[0-9.]+$/u
not $host =~ /^\[/u and $host =~ /\./u
MC/DC33%1/3
not $host =~ /^\[/u and $host =~ /\./u and not $host =~ /^[0-9.]+$/u
not $host =~ /^\[/u
$host =~ /\./u
not $host =~ /^[0-9.]+$/u
Truth table50%2/4
not(A) and B and not(C)
A= not $host =~ /^\[/uB= $host =~ /\./uC= not $host =~ /^[0-9.]+$/u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 1 | push @choices, { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | label => "This hostname and subdomains: $host (any scheme or port)", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | match => "^https?://(?:[a-z0-9-]+\\.)*" . (quotemeta $host) . "(?::[0-9]+)?/", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 14 | push @choices, { label => "Any HTTP(S) URL", match => "^https?://" }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 14 | ($full, \@choices) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 5 | ▶ | sub text_regex { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
text_regex: called CC=5 SCAR=17.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 5 | my ($pattern) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 5 | ▶ | defined $pattern && !ref $pattern && length $pattern <= 512 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $pattern and not ref $pattern and length $pattern <= 512
defined $pattern and not ref $pattern
MC/DC0%0/3
defined $pattern and not ref $pattern and length $pattern <= 512
defined $pattern
not ref $pattern
length $pattern <= 512
Truth table25%1/4
A and not(B) and C
A= defined $patternB= not ref $patternC= length $pattern <= 512
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | or die "Invalid regex pattern\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 5 | my $regex = eval { qr/$pattern/ }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 5 | ▶ | $@ and die "Invalid regex: $@"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 4 | $regex | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 9 | ▶ | sub filter_text { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
filter_text: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 9 | my ($text, $args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 9 | ▶ | if (defined $args->{grep} || defined $args->{regex}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $args->{"grep"} or defined $args->{"regex"}
MC/DC100%2/2
defined $args->{"grep"} or defined $args->{"regex"}
defined $args->{"grep"}
defined $args->{"regex"}
Truth table100%3/3
A or B
A= defined $args->{"grep"}B= defined $args->{"regex"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 2 | my $grep = $args->{grep}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 2 | ▶ | my $regex = defined $args->{regex} ? text_regex ($args->{regex}) : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | $text = join "", grep { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 2 | ▶ | (!defined $grep || index ($_, $grep) >= 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%9/9
(!defined($grep) || index($_, $grep) != -1) && (!defined($regex) || /$regex/u)
not defined $grep or index($_, $grep) != -1
not defined $regex or /$regex/u
MC/DC75%3/4
(!defined($grep) || index($_, $grep) != -1) && (!defined($regex) || /$regex/u)
not defined $grep
index($_, $grep) != -1
not defined $regex
/$regex/u
Truth table57%4/7
not(A) or B && not(C) or D
A= not defined $grepB= index($_, $grep) != -1C= not defined $regexD= /$regex/u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | && (!defined $regex || /$regex/) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | } split /(?<=\n)/, $text; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 9 | ▶ | if (my $subst = $args->{substitute}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 1 | my $regex = text_regex ($subst->{pattern}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 1 | my $replacement = $subst->{replacement}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 1 | $text =~ s/$regex/$replacement/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | $text | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 9 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | mcp_tool request => "HTTP request. Method and URL permissions. No redirects; verify_tls=false skips HTTPS certificate checks. Caller-managed cookie_jar returned. " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | . "UTF-8 body or Base64, bounded response, optional grep/regex/substitute.", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | type => "object", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | required => ["url"], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | additionalProperties => JSON::XS::false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | properties => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | url => { type => "string", minLength => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | method => { type => "string", default => "GET" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | headers => { type => "object", additionalProperties => { type => "string" } }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | body => { type => "string", description => "UTF-8 request body" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | body_base64 => { type => "string", description => "Raw request body, Base64" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | cookie_jar => { type => "object", description => "AnyEvent::HTTP cookie jar returned by an earlier call" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | timeout => { type => "number", minimum => 1, maximum => 300, default => 30 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | verify_tls => { type => "boolean", default => JSON::XS::true }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | max_bytes => { type => "integer", minimum => 1, maximum => 2097152, default => 65536 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | grep => { type => "string", description => "Keep lines containing this literal substring" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | regex => { type => "string", description => "Keep lines matching this Perl regex" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | substitute => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | type => "object", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | required => ["pattern", "replacement"], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | additionalProperties => JSON::XS::false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | properties => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | pattern => { type => "string", maxLength => 512 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | replacement => { type => "string", maxLength => 512 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 17 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=37 SCAR=40.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 17 | my ($req, $args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 17 | ▶ | my $method = $args->{method} // "GET"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args->{"method"} // "GET"
MC/DC100%1/1
$args->{"method"} // "GET"
$args->{"method"}
Truth table100%2/2
A
A= $args->{"method"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 17 | ▶ | $method =~ /\A[A-Za-z0-9!#\$%&'*+.^_\x60|~-]+\z/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | or die "Invalid HTTP method\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | exists $args->{body} && exists $args->{body_base64} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 17 | ▶ | and die "Specify body or body_base64, not both\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $args->{"body"} and exists $args->{"body_base64"}
MC/DC50%1/2
exists $args->{"body"} and exists $args->{"body_base64"}
exists $args->{"body"}
exists $args->{"body_base64"}
Truth table66%2/3
A and B
A= exists $args->{"body"}B= exists $args->{"body_base64"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | !exists $args->{verify_tls} || JSON::XS::is_bool ($args->{verify_tls}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 16 | ▶ | or die "Invalid verify_tls\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not exists $args->{"verify_tls"} or &JSON::XS::is_bool($args->{"verify_tls"})
MC/DC100%2/2
not exists $args->{"verify_tls"} or &JSON::XS::is_bool($args->{"verify_tls"})
not exists $args->{"verify_tls"}
&JSON::XS::is_bool($args->{"verify_tls"})
Truth table100%3/3
not(A) or B
A= not exists $args->{"verify_tls"}B= &JSON::XS::is_bool($args->{"verify_tls"})
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 15 | my ($url, $scopes) = url_scopes ($args->{url}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 14 | ▶ | my $headers = $args->{headers} || {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args->{'headers'} || {}
MC/DC100%1/1
$args->{'headers'} || {}
$args->{'headers'}
Truth table100%2/2
A
A= $args->{'headers'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 14 | for my $key (keys %$headers) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | $key =~ /\A[A-Za-z0-9!#\$%&'*+.^_\x60|~-]+\z/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | && defined $headers->{$key} && !ref $headers->{$key} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 3 | ▶ | && $headers->{$key} !~ /[\r\n\x00]/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition44%4/9
$key =~ /\A[A-Za-z0-9!#\$%&'*+.^_\x60|~-]+\z/u and defined $headers->{$key} and not ref $headers->{$key} and not $headers->{$key} =~ /[\r\n\x00]/u
$key =~ /\A[A-Za-z0-9!#\$%&'*+.^_\x60|~-]+\z/u and defined $headers->{$key} and not ref $headers->{$key}
$key =~ /\A[A-Za-z0-9!#\$%&'*+.^_\x60|~-]+\z/u and defined $headers->{$key}
MC/DC25%1/4
$key =~ /\A[A-Za-z0-9!#\$%&'*+.^_\x60|~-]+\z/u and defined $headers->{$key} and not ref $headers->{$key} and not $headers->{$key} =~ /[\r\n\x00]/u
$key =~ /\A[A-Za-z0-9!#\$%&'*+.^_\x60|~-]+\z/u
defined $headers->{$key}
not ref $headers->{$key}
not $headers->{$key} =~ /[\r\n\x00]/u
Truth table40%2/5
A and B and not(C) and not(D)
A= $key =~ /\A[A-Za-z0-9!#\$%&'*+.^_\x60|~-]+\z/uB= defined $headers->{$key}C= not ref $headers->{$key}D= not $headers->{$key} =~ /[\r\n\x00]/u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | or die "Invalid HTTP header\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 13 | $headers = { %$headers }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 13 | ▶ | $headers->{"User-Agent"} = "mcp-http/$AnyEvent::MCP::VERSION" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | unless grep lc $_ eq "user-agent", keys %$headers; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 13 | ▶ | my $data = exists $args->{body} ? (AnyEvent::MCP::utf8enc $args->{body}) : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 13 | ▶ | if (defined $args->{body_base64}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 1 | ▶ | $args->{body_base64} =~ /\A(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?\z/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | or die "Invalid Base64 body\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 1 | $data = MIME::Base64::decode_base64 ($args->{body_base64}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 13 | ▶ | !defined $data || length $data <= 1048576 or die "Request body exceeds 1 MiB\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $data or length $data <= 1048576
MC/DC0%0/2
not defined $data or length $data <= 1048576
not defined $data
length $data <= 1048576
Truth table66%2/3
not(A) or B
A= not defined $dataB= length $data <= 1048576
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 13 | ▶ | my $limit = $args->{max_bytes} // 65536; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args->{"max_bytes"} // 65536
MC/DC100%1/1
$args->{"max_bytes"} // 65536
$args->{"max_bytes"}
Truth table100%2/2
A
A= $args->{"max_bytes"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 13 | ▶ | $limit >= 1 && $limit <= 2097152 or die "Invalid max_bytes\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$limit >= 1 and $limit <= 2097152
MC/DC0%0/2
$limit >= 1 and $limit <= 2097152
$limit >= 1
$limit <= 2097152
Truth table33%1/3
A and B
A= $limit >= 1B= $limit <= 2097152
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 13 | ▶ | !defined $args->{grep} || !ref $args->{grep} or die "Invalid grep\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $args->{"grep"} and ref $args->{"grep"}
MC/DC0%0/2
defined $args->{"grep"} and ref $args->{"grep"}
defined $args->{"grep"}
ref $args->{"grep"}
Truth table66%2/3
A and B
A= defined $args->{"grep"}B= ref $args->{"grep"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 13 | ▶ | text_regex ($args->{regex}) if defined $args->{regex}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 12 | ▶ | if (defined $args->{substitute}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | ref $args->{substitute} eq "HASH" && defined $args->{substitute}{replacement} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 1 | ▶ | && !ref $args->{substitute}{replacement} && length $args->{substitute}{replacement} <= 512 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%3/9
ref $args->{"substitute"} eq "HASH" and defined $args->{"substitute"}{"replacement"} and not ref $args->{"substitute"}{"replacement"} and length $args->{"substitute"}{"replacement"} <= 512
ref $args->{"substitute"} eq "HASH" and defined $args->{"substitute"}{"replacement"} and not ref $args->{"substitute"}{"replacement"}
ref $args->{"substitute"} eq "HASH" and defined $args->{"substitute"}{"replacement"}
MC/DC0%0/4
ref $args->{"substitute"} eq "HASH" and defined $args->{"substitute"}{"replacement"} and not ref $args->{"substitute"}{"replacement"} and length $args->{"substitute"}{"replacement"} <= 512
ref $args->{"substitute"} eq "HASH"
defined $args->{"substitute"}{"replacement"}
not ref $args->{"substitute"}{"replacement"}
length $args->{"substitute"}{"replacement"} <= 512
Truth table20%1/5
A and B and not(C) and D
A= ref $args->{"substitute"} eq "HASH"B= defined $args->{"substitute"}{"replacement"}C= not ref $args->{"substitute"}{"replacement"}D= length $args->{"substitute"}{"replacement"} <= 512
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | or die "Invalid substitute\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 1 | text_regex ($args->{substitute}{pattern}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 12 | my $jar = $args->{cookie_jar}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 12 | ▶ | !defined $jar || ref $jar eq "HASH" or die "Invalid cookie jar\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $jar or ref $jar eq "HASH"
MC/DC0%0/2
not defined $jar or ref $jar eq "HASH"
not defined $jar
ref $jar eq "HASH"
Truth table66%2/3
not(A) or B
A= not defined $jarB= ref $jar eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | $req->require_permission ({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | tool => $req->{tool}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 12 | action => $method, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | dimensions => { url => { label => "URL", value => $url, choices => $scopes } }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | sandbox_grants => [], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | reason => "$method $url", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 10 | my $body = ""; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 10 | my $truncated = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 10 | my $response_headers; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | my $guard; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | $guard = AnyEvent::HTTP::http_request | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | $method => $url, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | recurse => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | persistent => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | proxy => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | tls_ctx => ($args->{verify_tls} // 1) ? "high" : "low", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | timeout => $args->{timeout} // 30, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | headers => $headers, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | (defined $data ? (body => $data) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | (defined $jar ? (cookie_jar => $jar) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | 10 | ▶ | on_header => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 10 | $response_headers = { %{$_[0]} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 10 | 1 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 9 | ▶ | on_body => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 9 | my ($part) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 9 | ▶ | if (length ($body) + length ($part) > $limit) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 1 | $body .= substr $part, 0, $limit - length $body; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | 1 | $truncated = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 1 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 8 | $body .= $part; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | 8 | 1 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 10 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=13 SCAR=28.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | 10 | my ($unused, $hdr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | 10 | undef $guard; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | 10 | ▶ | return if $req->{finished}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | 10 | ▶ | $hdr = $response_headers if $truncated && $response_headers; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$truncated and $response_headers
MC/DC50%1/2
$truncated and $response_headers
$truncated
$response_headers
Truth table66%2/3
A and B
A= $truncatedB= $response_headers
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | my %result = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | status => 0 + $hdr->{Status}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | reason => $hdr->{Reason}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | 10 | ▶ | headers => { map +($_ => $hdr->{$_}), grep /^[a-z]/, keys %$hdr }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | captured_bytes => length $body, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | truncated => $truncated ? JSON::XS::true : JSON::XS::false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | (defined $jar ? (cookie_jar => $jar) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 10 | ▶ | if (defined $unused || $truncated) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $unused or $truncated
MC/DC0%0/2
defined $unused or $truncated
defined $unused
$truncated
Truth table66%2/3
A or B
A= defined $unusedB= $truncated
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 10 | my $bytes = $body; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | 10 | my $text = eval { Encode::decode ("UTF-8", $bytes, Encode::FB_CROAK) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 10 | ▶ | if (defined $text) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | 9 | $text = filter_text ($text, $args); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 9 | ▶ | if (length $text > $limit) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 0 | $text = substr $text, 0, $limit; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 0 | $result{truncated} = JSON::XS::true; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 9 | $result{body} = $text; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | ▶ | } elsif (defined $args->{grep} || defined $args->{regex} || defined $args->{substitute}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%2/6
defined $args->{"grep"} or defined $args->{"regex"} or defined $args->{"substitute"}
defined $args->{"grep"} or defined $args->{"regex"}
MC/DC0%0/3
defined $args->{"grep"} or defined $args->{"regex"} or defined $args->{"substitute"}
defined $args->{"grep"}
defined $args->{"regex"}
defined $args->{"substitute"}
Truth table25%1/4
A or B or C
A= defined $args->{"grep"}B= defined $args->{"regex"}C= defined $args->{"substitute"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | 0 | $req->error ("Cannot filter a non-UTF-8 response"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 1 | $result{body_base64} = MIME::Base64::encode_base64 ($body, ""); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | 10 | $req->result (\%result); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | 10 | ▶ | ; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition75%3/4
$args->{"verify_tls"} // 1
$args->{"timeout"} // 30
MC/DC100%1/1
$args->{"verify_tls"} // 1
$args->{"verify_tls"}
MC/DC0%0/1
$args->{"timeout"} // 30
$args->{"timeout"}
Truth table100%2/2
A
A= $args->{"verify_tls"}
Truth table50%1/2
A
A= $args->{"timeout"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | 0 | ▶ | $req->{on_cancel} = sub { undef $guard }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | 2 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | 2 | mcp_start %$options; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||