| 1 | #!perl | ||||||||||||||||||||||||||||||
| 2 | # gmitool - a utility for gemini, part of Net::Gemini | ||||||||||||||||||||||||||||||
| 3 | 18 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 4 | 18 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | 18 | ▶ | use Cpanel::JSON::XS qw(decode_json encode_json); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 6 | 18 | ▶ | use Encode qw(decode); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 7 | 18 | ▶ | use File::Path qw(make_path); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 8 | 18 | ▶ | use File::Slurper qw(read_text write_text); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 9 | 18 | ▶ | use File::Spec::Functions qw(catdir catfile splitpath); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 10 | 18 | ▶ | use Getopt::Long qw(GetOptionsFromArray); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 11 | 18 | ▶ | use Net::Gemini 0.08; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 12 | 18 | ▶ | use Parse::MIME 'parse_mime_type'; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 13 | 18 | ▶ | use URI (); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||
| 15 | 18 | Getopt::Long::Configure('bundling'); | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||
| 17 | 18 | my $Allow_Verified; # allow verified certificates | |||||||||||||||||||||||||||||
| 18 | my $Be_Quiet; | ||||||||||||||||||||||||||||||
| 19 | 18 | my $Force_Update; # clobber known_hosts? | |||||||||||||||||||||||||||||
| 20 | 18 | my $Hosts_Dirty; # do we need to update known_hosts? | |||||||||||||||||||||||||||||
| 21 | 18 | my $Known_Hosts; # hashref, from/to JSON | |||||||||||||||||||||||||||||
| 22 | 18 | my $Show_Status; # log various things to stderr | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||
| 24 | 18 | ▶ | die "gmitool: command [args ..]\n" unless @ARGV; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||
| 26 | 17 | my %commands = ( get => \&get, link => \&link ); | |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||
| 28 | 17 | my $cmd = shift; | |||||||||||||||||||||||||||||
| 29 | 17 | ▶ | if ( $cmd =~ m{^gemini://} ) { | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 30 | 1 | unshift @ARGV, $cmd; | |||||||||||||||||||||||||||||
| 31 | 1 | $cmd = 'get'; | |||||||||||||||||||||||||||||
| 32 | } elsif ( !exists $commands{$cmd} ) { | ||||||||||||||||||||||||||||||
| 33 | 1 | die "gmitool: no such command '$cmd' (", | |||||||||||||||||||||||||||||
| 34 | join( ' ', sort keys %commands ), | ||||||||||||||||||||||||||||||
| 35 | ")\n"; | ||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||
| 37 | 16 | $commands{$cmd}->( \@ARGV ); | |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||
| 39 | # gets a gemini page | ||||||||||||||||||||||||||||||
| 40 | sub get { | ||||||||||||||||||||||||||||||
| 41 | 13 | ▶ | my ($args) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
get: called CC=41 SCAR=44.9
| |||||||||||||||||||||||||||||||
| 42 | 13 | ▶ | GetOptionsFromArray( | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 43 | $args, | ||||||||||||||||||||||||||||||
| 44 | 'A' => \$Allow_Verified, | ||||||||||||||||||||||||||||||
| 45 | 'C=s' => \my $client_cert, | ||||||||||||||||||||||||||||||
| 46 | 'E=s' => \my $output_encoding, | ||||||||||||||||||||||||||||||
| 47 | 'H=s' => \my $sni_host, | ||||||||||||||||||||||||||||||
| 48 | 'K=s' => \my $client_key, | ||||||||||||||||||||||||||||||
| 49 | 'S' => \$Show_Status, | ||||||||||||||||||||||||||||||
| 50 | 'V=s' => \my $verify, | ||||||||||||||||||||||||||||||
| 51 | 'f' => \$Force_Update, | ||||||||||||||||||||||||||||||
| 52 | 'links|l' => \my $Only_Links, | ||||||||||||||||||||||||||||||
| 53 | 'q' => \$Be_Quiet, | ||||||||||||||||||||||||||||||
| 54 | 't=i', => \my $timeout | ||||||||||||||||||||||||||||||
| 55 | ) or exit 64; | ||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||
| 57 | 12 | my $resource = $args->[0]; | |||||||||||||||||||||||||||||
| 58 | 12 | ▶ | die "Usage: gmitool get [options] url\n" | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $resource and length $resource
MC/DC100%2/2
defined $resource and length $resource
defined $resource
length $resource
Truth table100%3/3
A and B
A= defined $resourceB= length $resource
| |||||||||||||||||||||||||||||||
| 59 | unless defined $resource and length $resource; | ||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||
| 61 | # meta is UTF-8 and may appear in STDERR | ||||||||||||||||||||||||||||||
| 62 | # KLUGE this assumes that UTF-8 is correct; maybe this could also | ||||||||||||||||||||||||||||||
| 63 | # use $output_encoding if that is set? (or remove output encoding | ||||||||||||||||||||||||||||||
| 64 | # and insist on things being UTF-8) | ||||||||||||||||||||||||||||||
| 65 | 9 | binmode *STDERR, ':encoding(UTF-8)'; | |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||
| 67 | # default if -E not specified and content has a charset | ||||||||||||||||||||||||||||||
| 68 | 9 | ▶ | $output_encoding = ':encoding(UTF-8)' unless defined $output_encoding; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||
| 70 | 9 | my ( $known_hosts_file, %param, @unveils ); | |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||
| 72 | 9 | ▶ | if ( defined $client_cert ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 73 | 1 | $param{ssl}->{SSL_cert_file} = $client_cert; | |||||||||||||||||||||||||||||
| 74 | 1 | push @unveils, [ $client_cert, 'r' ]; | |||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||
| 76 | 9 | ▶ | if ( defined $client_key ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 77 | 1 | $param{ssl}->{SSL_key_file} = $client_key; | |||||||||||||||||||||||||||||
| 78 | 1 | push @unveils, [ $client_key, 'r' ]; | |||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||
| 80 | 9 | ▶ | if ( defined $sni_host ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 81 | 1 | $param{ssl}->{SSL_hostname} = $sni_host; | |||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||
| 83 | 9 | ▶ | $param{ssl}->{Timeout} = $timeout || 30; | ||||||||||||||||||||||||||||
|
Condition100%2/2
$timeout || 30
MC/DC100%1/1
$timeout || 30
$timeout
Truth table100%2/2
A
A= $timeout
| |||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||
| 85 | 9 | ▶ | if ( defined $verify ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 86 | 2 | ▶ | if ( $verify eq 'peer' ) { | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 87 | 1 | $param{ssl}->{SSL_verify_mode} = 1; # SSL_VERIFY_PEER | |||||||||||||||||||||||||||||
| 88 | 1 | $param{ssl}->{SSL_verify_callback} = undef; | |||||||||||||||||||||||||||||
| 89 | } elsif ( $verify eq 'none' ) { | ||||||||||||||||||||||||||||||
| 90 | 1 | ▶ | warn "NOTICE no certificate verification\n" if $Show_Status; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 91 | 1 | $param{ssl}->{SSL_verify_mode} = 0; # SSL_VERIFY_NONE | |||||||||||||||||||||||||||||
| 92 | 1 | $param{ssl}->{SSL_verify_callback} = undef; | |||||||||||||||||||||||||||||
| 93 | } else { | ||||||||||||||||||||||||||||||
| 94 | 0 | die "gmitool: unknown verify mode '$verify'\n"; | |||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||
| 96 | } else { | ||||||||||||||||||||||||||||||
| 97 | 7 | my $cache_dir; | |||||||||||||||||||||||||||||
| 98 | 7 | ▶ | if (length $ENV{GMITOOL_HOSTS} ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 99 | 7 | $cache_dir = (splitpath($ENV{GMITOOL_HOSTS}))[1]; | |||||||||||||||||||||||||||||
| 100 | 7 | $known_hosts_file = $ENV{GMITOOL_HOSTS}; | |||||||||||||||||||||||||||||
| 101 | } else { | ||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | die "gmitool: HOME is not set" unless defined $ENV{HOME}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 103 | 0 | $cache_dir = catdir( $ENV{HOME}, qw{.cache gmitool} ); | |||||||||||||||||||||||||||||
| 104 | 0 | $known_hosts_file = catfile( $cache_dir, 'known_hosts' ); | |||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||
| 106 | 7 | make_path($cache_dir); | |||||||||||||||||||||||||||||
| 107 | 7 | my $buf; | |||||||||||||||||||||||||||||
| 108 | 7 | eval { $buf = read_text($known_hosts_file) }; | |||||||||||||||||||||||||||||
| 109 | 7 | ▶ | $Known_Hosts = decode_json($buf) if defined $buf and length $buf; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $buf and length $buf
MC/DC50%1/2
defined $buf and length $buf
defined $buf
length $buf
Truth table66%2/3
A and B
A= defined $bufB= length $buf
| |||||||||||||||||||||||||||||||
| 110 | 7 | push @unveils, [ $cache_dir, 'cw' ]; | |||||||||||||||||||||||||||||
| 111 | 7 | @param{qw(tofu verify_ssl)} = ( 1, \&verify_ssl ); | |||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||
| 114 | 9 | my $pledge = eval { | |||||||||||||||||||||||||||||
| 115 | 9 | require OpenBSD::Pledge; | |||||||||||||||||||||||||||||
| 116 | 0 | require OpenBSD::Unveil; | |||||||||||||||||||||||||||||
| 117 | 0 | OpenBSD::Unveil->import; | |||||||||||||||||||||||||||||
| 118 | 0 | 1; | |||||||||||||||||||||||||||||
| 119 | }; | ||||||||||||||||||||||||||||||
| 120 | 9 | ▶ | if ($pledge) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 121 | 0 | OpenBSD::Pledge::pledge(qw{cpath dns inet rpath unveil wpath}); | |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | unveil( $ENV{SSL_CERT_DIR}, 'r' ) if exists $ENV{SSL_CERT_DIR}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | unveil( $ENV{SSL_CERT_FILE}, 'r' ) if exists $ENV{SSL_CERT_FILE}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 124 | 0 | unveil(qw{/etc/ssl r}); | |||||||||||||||||||||||||||||
| 125 | 0 | for my $dir (@INC) { unveil($dir, 'r') } | |||||||||||||||||||||||||||||
| 126 | 0 | for my $pathperm (@unveils) { unveil(@$pathperm) } | |||||||||||||||||||||||||||||
| 127 | 0 | unveil(); | |||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||
| 130 | 9 | my ( $gem, $code ); | |||||||||||||||||||||||||||||
| 131 | 9 | my $redirects = -1; | |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||
| 133 | 11 | REQUEST: | |||||||||||||||||||||||||||||
| 134 | ( $gem, $code ) = Net::Gemini->get( $resource, %param ); | ||||||||||||||||||||||||||||||
| 135 | 10 | ▶ | if ( $pledge and $code != 3 ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$pledge and $code != 3
MC/DC0%0/2
$pledge and $code != 3
$pledge
$code != 3
Truth table33%1/3
A and B
A= $pledgeB= $code != 3
| |||||||||||||||||||||||||||||||
| 136 | 0 | OpenBSD::Pledge::pledge(qw{cpath rpath wpath}); | |||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||
| 138 | 10 | ▶ | if ( $code == 2 ) { | ||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||
| 139 | 8 | ▶ | warn "META " . $gem->meta . "\n" if $Show_Status; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 140 | 8 | my ( $type, $sub, $pr ) = parse_mime_type( $gem->meta ); | |||||||||||||||||||||||||||||
| 141 | 8 | my ( $encoded, $charset ) = is_encoded($pr); | |||||||||||||||||||||||||||||
| 142 | 8 | ▶ | $encoded = 0 if defined $output_encoding and $output_encoding eq ''; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $output_encoding and $output_encoding eq ""
MC/DC0%0/2
defined $output_encoding and $output_encoding eq ""
defined $output_encoding
$output_encoding eq ""
Truth table33%1/3
A and B
A= defined $output_encodingB= $output_encoding eq ""
| |||||||||||||||||||||||||||||||
| 143 | 8 | ▶ | if ($Only_Links) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 144 | 1 | ▶ | if ( $type eq 'text' and $sub eq 'gemini' ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$type eq "text" and $sub eq "gemini"
MC/DC0%0/2
$type eq "text" and $sub eq "gemini"
$type eq "text"
$sub eq "gemini"
Truth table33%1/3
A and B
A= $type eq "text"B= $sub eq "gemini"
| |||||||||||||||||||||||||||||||
| 145 | 1 | show_links($gem); | |||||||||||||||||||||||||||||
| 146 | 1 | goto CLEANUP; | |||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||
| 149 | 7 | ▶ | if ($encoded) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 150 | 2 | my $body = ''; | |||||||||||||||||||||||||||||
| 151 | 2 | ▶ | $gem->getmore( sub { $body .= $_[0]; 1 } ); | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 152 | 2 | ▶ | binmode STDOUT, $output_encoding if defined $output_encoding; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 153 | 2 | print decode( $charset, $body, Encode::FB_CROAK ); | |||||||||||||||||||||||||||||
| 154 | } else { | ||||||||||||||||||||||||||||||
| 155 | 5 | binmode STDOUT, ':raw'; # garbage in, garbage out | |||||||||||||||||||||||||||||
| 156 | 5 | ▶ | $gem->getmore( sub { syswrite STDOUT, $_[0]; 1 } ); | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||
| 158 | } elsif ( $code == 0 ) { | ||||||||||||||||||||||||||||||
| 159 | 0 | my $e = $gem->error; | |||||||||||||||||||||||||||||
| 160 | 0 | chomp $e; | |||||||||||||||||||||||||||||
| 161 | 0 | die "gmitool: error: $e\n"; | |||||||||||||||||||||||||||||
| 162 | } elsif ( $code == 3 ) { | ||||||||||||||||||||||||||||||
| 163 | 2 | ▶ | die "gmitool: too many redirects ($redirects) " . $gem->meta . "\n" | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 164 | if ++$redirects >= 5; # amfora also uses 5 max redirects | ||||||||||||||||||||||||||||||
| 165 | 2 | my $new = $gem->meta; | |||||||||||||||||||||||||||||
| 166 | 2 | $resource = URI->new_abs( $new, $gem->{_uri} ); | |||||||||||||||||||||||||||||
| 167 | 2 | ▶ | warn "REDIRECT " . $resource . "\n" if $Show_Status; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 168 | 2 | sleep 1; # don't be too quick about a loop | |||||||||||||||||||||||||||||
| 169 | 2 | goto REQUEST; | |||||||||||||||||||||||||||||
| 170 | } elsif ( $code == 4 ) { | ||||||||||||||||||||||||||||||
| 171 | 0 | die 'gmitool: temporary-failure ' | |||||||||||||||||||||||||||||
| 172 | . $gem->status . ' ' | ||||||||||||||||||||||||||||||
| 173 | . $gem->meta . "\n"; | ||||||||||||||||||||||||||||||
| 174 | } elsif ( $code == 5 ) { | ||||||||||||||||||||||||||||||
| 175 | 0 | die 'gmitool: permanent-failure ' | |||||||||||||||||||||||||||||
| 176 | . $gem->status . ' ' | ||||||||||||||||||||||||||||||
| 177 | . $gem->meta . "\n"; | ||||||||||||||||||||||||||||||
| 178 | } elsif ( $code == 6 ) { | ||||||||||||||||||||||||||||||
| 179 | 0 | die 'gmitool: client-certificate ' | |||||||||||||||||||||||||||||
| 180 | . $gem->status . ' ' | ||||||||||||||||||||||||||||||
| 181 | . $gem->meta . "\n"; | ||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||
| 184 | CLEANUP: | ||||||||||||||||||||||||||||||
| 185 | 8 | ▶ | if ($Hosts_Dirty) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 186 | 2 | write_text( $known_hosts_file, encode_json($Known_Hosts) ); | |||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||
| 190 | # is it encoded in something besides UTF-8, or also besides US-ASCII of | ||||||||||||||||||||||||||||||
| 191 | # which UTF-8 is a superset? | ||||||||||||||||||||||||||||||
| 192 | sub is_encoded { | ||||||||||||||||||||||||||||||
| 193 | 8 | ▶ | my ($pr) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
is_encoded: called CC=8 SCAR=21.8
| |||||||||||||||||||||||||||||||
| 194 | 8 | ▶ | if ( exists $pr->{charset} ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 195 | # TODO can US-ASCII appear in any other forms (and how likely | ||||||||||||||||||||||||||||||
| 196 | # are we to see them, and would it even cause a problem? | ||||||||||||||||||||||||||||||
| 197 | return 1, $pr->{charset} | ||||||||||||||||||||||||||||||
| 198 | unless $pr->{charset} =~ m/(?i)utf-8/ | ||||||||||||||||||||||||||||||
| 199 | 7 | ▶ | or $pr->{charset} =~ m/(?i)ascii/; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$pr->{"charset"} =~ /(?i)utf-8/ or $pr->{"charset"} =~ /(?i)ascii/
MC/DC50%1/2
$pr->{"charset"} =~ /(?i)utf-8/ or $pr->{"charset"} =~ /(?i)ascii/
$pr->{"charset"} =~ /(?i)utf-8/
$pr->{"charset"} =~ /(?i)ascii/
Truth table66%2/3
A or B
A= $pr->{"charset"} =~ /(?i)utf-8/B= $pr->{"charset"} =~ /(?i)ascii/
| |||||||||||||||||||||||||||||||
| 200 | } else { | ||||||||||||||||||||||||||||||
| 201 | # a server might return 'CHARSET' or maybe 'Charset' or whatever | ||||||||||||||||||||||||||||||
| 202 | # according to the gemini torture tests (see t/torture.t), so | ||||||||||||||||||||||||||||||
| 203 | # look for look for those alternative forms | ||||||||||||||||||||||||||||||
| 204 | 1 | for my $key (%$pr) { | |||||||||||||||||||||||||||||
| 205 | 1 | ▶ | if ( $key =~ m/^(?i)charset$/ ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 206 | return 1, $pr->{$key} | ||||||||||||||||||||||||||||||
| 207 | unless $pr->{$key} =~ m/(?i)utf-8/ | ||||||||||||||||||||||||||||||
| 208 | 1 | ▶ | or $pr->{$key} =~ m/(?i)ascii/; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$pr->{$key} =~ /(?i)utf-8/ or $pr->{$key} =~ /(?i)ascii/
MC/DC0%0/2
$pr->{$key} =~ /(?i)utf-8/ or $pr->{$key} =~ /(?i)ascii/
$pr->{$key} =~ /(?i)utf-8/
$pr->{$key} =~ /(?i)ascii/
Truth table33%1/3
A or B
A= $pr->{$key} =~ /(?i)utf-8/B= $pr->{$key} =~ /(?i)ascii/
| |||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||
| 212 | 6 | return 0, 'UTF-8'; | |||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||
| 215 | # extracts links in text/gemini input | ||||||||||||||||||||||||||||||
| 216 | sub link { | ||||||||||||||||||||||||||||||
| 217 | 3 | ▶ | my ($args) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
link: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||
| 218 | 3 | ▶ | GetOptionsFromArray( $args, 'base|b=s' => \my $base ) or exit 64; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 219 | 2 | ▶ | $base = '.' unless defined $base; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 220 | 2 | ▶ | while ( my $line = readline ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 221 | 2 | ▶ | if ( $line =~ m/^=>\s*(\S+)/ ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 222 | 2 | my $u = URI->new_abs( $1, $base ); | |||||||||||||||||||||||||||||
| 223 | 2 | print $u->canonical, "\n"; | |||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||
| 228 | # parse and qualify links out of what is assumed to be text/gemini | ||||||||||||||||||||||||||||||
| 229 | # content. links can be followed by an optional description: | ||||||||||||||||||||||||||||||
| 230 | # =>/about/ | ||||||||||||||||||||||||||||||
| 231 | # => photos/ all the cats | ||||||||||||||||||||||||||||||
| 232 | sub show_links { | ||||||||||||||||||||||||||||||
| 233 | 1 | ▶ | my ($gem) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
show_links: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 234 | 1 | my $base = $gem->{_uri}; | |||||||||||||||||||||||||||||
| 235 | # KLUGE in theory may need support encoding, but if we assume that | ||||||||||||||||||||||||||||||
| 236 | # the links are ASCII with anything fancy encoded, and that the | ||||||||||||||||||||||||||||||
| 237 | # environment assumes UTF-8 or ASCII... | ||||||||||||||||||||||||||||||
| 238 | 1 | binmode *STDOUT, ':raw'; | |||||||||||||||||||||||||||||
| 239 | 1 | my $buf = ''; | |||||||||||||||||||||||||||||
| 240 | 1 | my $eom; | |||||||||||||||||||||||||||||
| 241 | $gem->getmore( | ||||||||||||||||||||||||||||||
| 242 | sub { | ||||||||||||||||||||||||||||||
| 243 | 1 | ▶ | $buf .= $_[0]; | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||
| 244 | 1 | $eom = 0; | |||||||||||||||||||||||||||||
| 245 | 1 | ▶ | while ( $buf =~ m{^=>[ \t]*(\S+)(?:[ \t]+[^\r\n]*)?[\r\n]}gm ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 246 | 2 | print URI->new_abs( $1, $base )->canonical, "\n"; | |||||||||||||||||||||||||||||
| 247 | 2 | $eom = $+[0]; | |||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||
| 249 | 1 | ▶ | substr $buf, 0, $eom, '' if $eom; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 250 | 1 | 1; | |||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||
| 252 | 1 | ); | |||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||
| 255 | # NOTE the host and possibly certificate maybe should be hashed for | ||||||||||||||||||||||||||||||
| 256 | # privacy, though the benefits of this seem dubious given how few gemini | ||||||||||||||||||||||||||||||
| 257 | # servers there are and other means of collecting connection information | ||||||||||||||||||||||||||||||
| 258 | sub verify_ssl { | ||||||||||||||||||||||||||||||
| 259 | 12 | ▶ | my ($param) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
verify_ssl: called CC=8 SCAR=21.0
| |||||||||||||||||||||||||||||||
| 260 | 12 | ▶ | return 1 if $Allow_Verified and $param->{okay}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$Allow_Verified and $param->{"okay"}
MC/DC50%1/2
$Allow_Verified and $param->{"okay"}
$Allow_Verified
$param->{"okay"}
Truth table66%2/3
A and B
A= $Allow_VerifiedB= $param->{"okay"}
| |||||||||||||||||||||||||||||||
| 261 | 11 | my $key = join '|', @{$param}{qw(host port)}; | |||||||||||||||||||||||||||||
| 262 | 11 | my $new = { map { $_ => $param->{$_} } | |||||||||||||||||||||||||||||
| 263 | qw(digest ip notAfter notBefore okay) }; | ||||||||||||||||||||||||||||||
| 264 | # one could save the whole certificate with something like | ||||||||||||||||||||||||||||||
| 265 | #$new->{cert} = Net::SSLeay::PEM_get_string_X509($param->{cert}); | ||||||||||||||||||||||||||||||
| 266 | 11 | my @fields = qw{notAfter notBefore ip okay}; | |||||||||||||||||||||||||||||
| 267 | 11 | ▶ | if ( !exists $Known_Hosts->{$key} ) { | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 268 | 1 | ( $Known_Hosts->{$key}, $Hosts_Dirty ) = ( $new, 1 ); | |||||||||||||||||||||||||||||
| 269 | } elsif ($Force_Update) { | ||||||||||||||||||||||||||||||
| 270 | 2 | ▶ | unless ( $Be_Quiet or $Known_Hosts->{$key}{digest} eq $param->{digest} ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$Be_Quiet or $Known_Hosts->{$key}{"digest"} eq $param->{"digest"}
MC/DC0%0/2
$Be_Quiet or $Known_Hosts->{$key}{"digest"} eq $param->{"digest"}
$Be_Quiet
$Known_Hosts->{$key}{"digest"} eq $param->{"digest"}
Truth table33%1/3
A or B
A= $Be_QuietB= $Known_Hosts->{$key}{"digest"} eq $param->{"digest"}
| |||||||||||||||||||||||||||||||
| 271 | 0 | verify_warn( $key, $Known_Hosts->{$key}, $new, \@fields ); | |||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||
| 273 | # merge new over old in the event there are unknown keys that | ||||||||||||||||||||||||||||||
| 274 | # something else added to the cache | ||||||||||||||||||||||||||||||
| 275 | 2 | my %update = ( %{ $Known_Hosts->{$key} }, %$new ); | |||||||||||||||||||||||||||||
| 276 | 2 | ( $Known_Hosts->{$key}, $Hosts_Dirty ) = ( \%update, 1 ); | |||||||||||||||||||||||||||||
| 277 | } else { | ||||||||||||||||||||||||||||||
| 278 | 8 | ▶ | if ( $Known_Hosts->{$key}{digest} ne $param->{digest} ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 279 | 1 | verify_warn( $key, $Known_Hosts->{$key}, $new, \@fields ); | |||||||||||||||||||||||||||||
| 280 | 1 | exit 1; | |||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||
| 283 | 10 | return 1; | |||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||
| 286 | sub verify_warn { | ||||||||||||||||||||||||||||||
| 287 | 1 | ▶ | my ( $key, $old, $new, $fields ) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
verify_warn: called CC=4 SCAR=16.1
| |||||||||||||||||||||||||||||||
| 288 | 1 | warn qq(gmitool: digest mismatch "$key"\n); | |||||||||||||||||||||||||||||
| 289 | 1 | for my $dtl (@$fields) { | |||||||||||||||||||||||||||||
| 290 | 4 | ▶ | if ( exists $old->{$dtl} and $old->{$dtl} ne $new->{$dtl} ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $old->{$dtl} and $old->{$dtl} ne $new->{$dtl}
MC/DC0%0/2
exists $old->{$dtl} and $old->{$dtl} ne $new->{$dtl}
exists $old->{$dtl}
$old->{$dtl} ne $new->{$dtl}
Truth table33%1/3
A and B
A= exists $old->{$dtl}B= $old->{$dtl} ne $new->{$dtl}
| |||||||||||||||||||||||||||||||
| 291 | 0 | warn " $dtl\t$old->{$dtl}\t$new->{$dtl}\n"; | |||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||
| 296 | __END__ | ||||||||||||||||||||||||||||||