| 1 | # -*- Perl -*- | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # a Gemini protocol client | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # "The conjunction of Jupiter with one of the stars of Gemini, which | ||||||||||||||||||||||||||||||||||||||||||
| 6 | # 'we ourselves have seen' (1.6.343b30) has been dated in recent years | ||||||||||||||||||||||||||||||||||||||||||
| 7 | # to December 337 BC." | ||||||||||||||||||||||||||||||||||||||||||
| 8 | # -- Malcolm Wilson. Structure and Method in Aristotle's Meteorologica. | ||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | # NOTE this silently accepts URI with userinfo; those probably | ||||||||||||||||||||||||||||||||||||||||||
| 11 | # should be failed? | ||||||||||||||||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||||||||||||||||
| 13 | # KLUGE this may break if the URI module ever gets URI/gemini.pm | ||||||||||||||||||||||||||||||||||||||||||
| 14 | 39 | package URI::gemini { | |||||||||||||||||||||||||||||||||||||||||
| 15 | 39 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 39 | ▶ | use parent 'URI::_server'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 118 | ▶ | sub default_port { 1965 } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_port: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 19 | ▶ | sub userinfo { return undef } # gemini has no userinfo | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
userinfo: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 19 | ▶ | sub secure { 1 } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
secure: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | sub canonical { | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 122 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
canonical: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 122 | my $other = $self->SUPER::canonical; | |||||||||||||||||||||||||||||||||||||||||
| 24 | 122 | $self->SUPER::userinfo(undef); # gemini has no userinfo | |||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 122 | ▶ | my $slash_path = | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%4/6
defined $other->authority && !length($other->path) && !defined($other->query)
defined $other->authority and not length $other->path
MC/DC33%1/3
defined $other->authority && !length($other->path) && !defined($other->query)
defined $other->authority
not length $other->path
!defined($other->query)
Truth table50%2/4
A and not(B) && not(C)
A= defined $other->authorityB= not length $other->pathC= !defined($other->query)
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | defined( $other->authority ) | ||||||||||||||||||||||||||||||||||||||||||
| 28 | && !length( $other->path ) | ||||||||||||||||||||||||||||||||||||||||||
| 29 | && !defined( $other->query ); | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 122 | ▶ | if ($slash_path) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 1 | ▶ | $other = $other->clone if $other == $self; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 1 | $other->path("/"); | |||||||||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 122 | $other; | |||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | package Net::Gemini; | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 39 | our $VERSION = '0.08'; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 39 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 39 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 39 | ▶ | use Digest::SHA 'sha256_hex'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 39 | ▶ | use Encode (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 39 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 39 | ▶ | use IO::Socket::IP; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 39 | ▶ | use IO::Socket::SSL; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 39 | ▶ | use Net::SSLeay; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 39 | ▶ | use Parse::MIME 'parse_mime_type'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 39 | our @EXPORT_OK = qw(gemini_request); | |||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | sub _DEFAULT_BUFSIZE () { 4096 } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | sub _DEFAULT_MAX_CONTENT () { 2097152 } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | sub _DEFAULT_REDIRECTS () { 5 } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | sub _DEFAULT_REDIRECT_SLEEP () { 1 } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 19 | ▶ | sub code { $_[0]{_code} } # 0..6 response code | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
code: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | sub content { | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 3 | ▶ | $_[0]{_content}; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
content: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | } # NOTE only after certain calls and codes | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 18 | ▶ | sub error { $_[0]{_error} } # error message for 0 code | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
error: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 17 | ▶ | sub host { $_[0]{_host} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
host: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 17 | ▶ | sub ip { $_[0]{_ip} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ip: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 33 | ▶ | sub meta { $_[0]{_meta} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
meta: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 3 | ▶ | sub mime { $_[0]{_mime} } # NOTE only after certain calls and codes | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mime: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 17 | ▶ | sub port { $_[0]{_port} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
port: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 17 | ▶ | sub socket { $_[0]{_socket} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
socket: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | sub status { | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 18 | ▶ | $_[0]{_status}; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
status: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | } # two digit '1x', '2x', ... response code | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 19 | ▶ | sub uri { $_[0]{_uri} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
uri: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | # see VERIFICATION below; the caller should supply a custom callback. | ||||||||||||||||||||||||||||||||||||||||||
| 77 | # the default is thus "Trust On Almost Any Use" (TOAAU) or similar to | ||||||||||||||||||||||||||||||||||||||||||
| 78 | # what gg(1) of gmid does | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 84 | ▶ | sub _verify_ssl { 1 } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_verify_ssl: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | # minimal method to get a resource (see also ->request) | ||||||||||||||||||||||||||||||||||||||||||
| 82 | sub get { | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 156 | ▶ | my ( $class, $source, %param ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=28 SCAR=34.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 156 | my %obj; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 156 | ▶ | unless ( defined $source ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 19 | @obj{qw(_code _error)} = ( 0, "source is not defined" ); | |||||||||||||||||||||||||||||||||||||||||
| 87 | 19 | goto BLESSING; | |||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 137 | $obj{_uri} = URI->new($source); | |||||||||||||||||||||||||||||||||||||||||
| 91 | 137 | ▶ | unless ( $obj{_uri}->scheme eq 'gemini' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 19 | @obj{qw(_code _error)} = ( 0, "could not parse '$source'" ); | |||||||||||||||||||||||||||||||||||||||||
| 93 | 19 | goto BLESSING; | |||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 118 | @obj{qw/_host _port/} = ( $obj{_uri}->host, $obj{_uri}->port ); | |||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 118 | my $yuri = $obj{_uri}->canonical; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 118 | ▶ | if ( length $yuri > 1024 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 19 | @obj{qw(_code _error)} = ( 0, "URI is too long" ); | |||||||||||||||||||||||||||||||||||||||||
| 100 | 19 | goto BLESSING; | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | # VERIFICATION is based on the following though much remains up to | ||||||||||||||||||||||||||||||||||||||||||
| 104 | # the caller to manage | ||||||||||||||||||||||||||||||||||||||||||
| 105 | # gemini://makeworld.space/gemlog/2020-07-03-tofu-rec.gmi | ||||||||||||||||||||||||||||||||||||||||||
| 106 | # gemini://alexschroeder.ch/page/2020-07-20%20Does%20a%20Gemini%20certificate%20need%20a%20Common%20Name%20matching%20the%20domain%3F | ||||||||||||||||||||||||||||||||||||||||||
| 107 | eval { | ||||||||||||||||||||||||||||||||||||||||||
| 108 | $obj{_socket} = IO::Socket::IP->new( | ||||||||||||||||||||||||||||||||||||||||||
| 109 | PeerAddr => $obj{_host}, | ||||||||||||||||||||||||||||||||||||||||||
| 110 | PeerPort => $obj{_port}, | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 99 | ▶ | Proto => 'tcp' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | ) or die $!; | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 80 | $obj{_ip} = $obj{_socket}->peerhost; | |||||||||||||||||||||||||||||||||||||||||
| 114 | IO::Socket::SSL->start_SSL( | ||||||||||||||||||||||||||||||||||||||||||
| 115 | $obj{_socket}, | ||||||||||||||||||||||||||||||||||||||||||
| 116 | SSL_hostname => $obj{_host}, # SNI | ||||||||||||||||||||||||||||||||||||||||||
| 117 | ( $param{tofu} ? ( SSL_verifycn_scheme => 'none' ) : () ), | ||||||||||||||||||||||||||||||||||||||||||
| 118 | SSL_verify_callback => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 96 | ▶ | my ( $ok, $ctx_store, $certname, $error, $cert, $depth ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=17.1
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 96 | ▶ | if ( $depth != 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | return 1 if $param{tofu}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | return $ok; | |||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | my $digest = ( $param{verify_ssl} || \&_verify_ssl )->( | ||||||||||||||||||||||||||||||||||||||||||
| 125 | { host => $obj{_host}, | ||||||||||||||||||||||||||||||||||||||||||
| 126 | port => $obj{_port}, | ||||||||||||||||||||||||||||||||||||||||||
| 127 | cert => $cert, # warning, memory address! | ||||||||||||||||||||||||||||||||||||||||||
| 128 | # compatible with certID function of amfora | ||||||||||||||||||||||||||||||||||||||||||
| 129 | digest => | ||||||||||||||||||||||||||||||||||||||||||
| 130 | uc( sha256_hex( Net::SSLeay::X509_get_X509_PUBKEY($cert) ) ), | ||||||||||||||||||||||||||||||||||||||||||
| 131 | ip => $obj{_ip}, | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 96 | ▶ | notBefore => Net::SSLeay::P_ASN1_TIME_get_isotime( | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$param{'verify_ssl'} || \&Net::Gemini::_verify_ssl
MC/DC100%1/1
$param{'verify_ssl'} || \&Net::Gemini::_verify_ssl
$param{'verify_ssl'}
Truth table100%2/2
A
A= $param{'verify_ssl'}
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | Net::SSLeay::X509_get_notBefore($cert) | ||||||||||||||||||||||||||||||||||||||||||
| 134 | ), | ||||||||||||||||||||||||||||||||||||||||||
| 135 | notAfter => Net::SSLeay::P_ASN1_TIME_get_isotime( | ||||||||||||||||||||||||||||||||||||||||||
| 136 | Net::SSLeay::X509_get_notAfter($cert) | ||||||||||||||||||||||||||||||||||||||||||
| 137 | ), | ||||||||||||||||||||||||||||||||||||||||||
| 138 | okay => $ok, | ||||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 141 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 80 | ▶ | ( exists $param{ssl} ? %{ $param{ssl} } : () ), | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | ) or die $!; | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 79 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 145 | 99 | ▶ | } or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 19 | @obj{qw(_code _error)} = ( 0, "IO::Socket::SSL failed: $@" ); | |||||||||||||||||||||||||||||||||||||||||
| 147 | 19 | goto BLESSING; | |||||||||||||||||||||||||||||||||||||||||
| 148 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | 79 | binmode $obj{_socket}, ':raw'; | |||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 79 | my $n = syswrite $obj{_socket}, "$yuri\r\n"; | |||||||||||||||||||||||||||||||||||||||||
| 153 | 79 | ▶ | unless ( defined $n ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | @obj{qw(_code _error)} = ( 0, "send URI failed: $!" ); | |||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | goto BLESSING; | |||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | # get the STATUS SPACE header response (and, probably, more) | ||||||||||||||||||||||||||||||||||||||||||
| 159 | 79 | $obj{_buf} = ''; | |||||||||||||||||||||||||||||||||||||||||
| 160 | 79 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 161 | my $n = sysread $obj{_socket}, my $buf, | ||||||||||||||||||||||||||||||||||||||||||
| 162 | 155 | ▶ | $param{bufsize} || _DEFAULT_BUFSIZE; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$param{'bufsize'} || 4096
MC/DC100%1/1
$param{'bufsize'} || 4096
$param{'bufsize'}
Truth table100%2/2
A
A= $param{'bufsize'}
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | 155 | ▶ | unless ( defined $n ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | @obj{qw(_code _error)} = ( 0, "recv response failed: $!" ); | |||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | goto BLESSING; | |||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||
| 167 | 155 | ▶ | if ( $n == 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | @obj{qw(_code _error)} = ( 0, "recv EOF" ); | |||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | goto BLESSING; | |||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | 155 | $obj{_buf} .= $buf; | |||||||||||||||||||||||||||||||||||||||||
| 172 | 155 | ▶ | last if length $obj{_buf} >= 3; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||
| 174 | # NOTE this is sloppy; there are fewer "full two digit status codes" | ||||||||||||||||||||||||||||||||||||||||||
| 175 | # defined in the appendix, e.g. only 10, 11, 20, 30, 31, 40, ... | ||||||||||||||||||||||||||||||||||||||||||
| 176 | # on the other hand, this supports any new extensions to the | ||||||||||||||||||||||||||||||||||||||||||
| 177 | # existing numbers | ||||||||||||||||||||||||||||||||||||||||||
| 178 | 79 | ▶ | if ( $obj{_buf} =~ m/^(([1-6])[0-9])[ ]/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 61 | @obj{qw(_status _code)} = ( $1, $2 ); | |||||||||||||||||||||||||||||||||||||||||
| 180 | 61 | substr $obj{_buf}, 0, 3, ''; | |||||||||||||||||||||||||||||||||||||||||
| 181 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 182 | @obj{qw(_code _error)} = ( | ||||||||||||||||||||||||||||||||||||||||||
| 183 | 0, | ||||||||||||||||||||||||||||||||||||||||||
| 184 | "invalid response " . sprintf "%vx", | ||||||||||||||||||||||||||||||||||||||||||
| 185 | substr $obj{_buf}, | ||||||||||||||||||||||||||||||||||||||||||
| 186 | 18 | 0, 3 | |||||||||||||||||||||||||||||||||||||||||
| 187 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 188 | 18 | goto BLESSING; | |||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | # META -- at most 1024 characters, followed by \r\n. the loop is in | ||||||||||||||||||||||||||||||||||||||||||
| 192 | # the event the server is being naughty and trickling bytes in one | ||||||||||||||||||||||||||||||||||||||||||
| 193 | # by one (probably you will want a timeout somewhere, or an async | ||||||||||||||||||||||||||||||||||||||||||
| 194 | # version of this code) | ||||||||||||||||||||||||||||||||||||||||||
| 195 | 61 | ▶ | my $bufsize = $param{bufsize} || _DEFAULT_BUFSIZE; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$param{'bufsize'} || 4096
MC/DC100%1/1
$param{'bufsize'} || 4096
$param{'bufsize'}
Truth table100%2/2
A
A= $param{'bufsize'}
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | 61 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 197 | 18569 | ▶ | if ( $obj{_buf} =~ m/^(.{0,1024}?)\r\n/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | 43 | $obj{_meta} = $1; | |||||||||||||||||||||||||||||||||||||||||
| 199 | 43 | my $len = length $obj{_meta}; | |||||||||||||||||||||||||||||||||||||||||
| 200 | 43 | ▶ | if ( $len == 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | # special case mentioned in the specification | ||||||||||||||||||||||||||||||||||||||||||
| 202 | 7 | ▶ | $obj{_meta} = 'text/gemini;charset=utf-8' if $obj{_code} == 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 203 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 204 | eval { | ||||||||||||||||||||||||||||||||||||||||||
| 205 | $obj{_meta} = | ||||||||||||||||||||||||||||||||||||||||||
| 206 | 36 | Encode::decode( 'UTF-8', $obj{_meta}, Encode::FB_CROAK ); | |||||||||||||||||||||||||||||||||||||||||
| 207 | 36 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 208 | 36 | ▶ | } or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | @obj{qw(_code _error)} = ( 0, "failed to decode meta: $@" ); | |||||||||||||||||||||||||||||||||||||||||
| 210 | 0 | goto BLESSING; | |||||||||||||||||||||||||||||||||||||||||
| 211 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 212 | # another special case (RFC 2045 says that these things | ||||||||||||||||||||||||||||||||||||||||||
| 213 | # are not case sensitive, hence the (?i) despite the | ||||||||||||||||||||||||||||||||||||||||||
| 214 | # gemini specification saying "text/") | ||||||||||||||||||||||||||||||||||||||||||
| 215 | 36 | ▶ | if ( $obj{_code} == 2 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$obj{"_code"} == 2 and $obj{"_meta"} =~ m[^(?i)text/] and not $obj{"_meta"} =~ /(?i)charset=/
$obj{"_code"} == 2 and $obj{"_meta"} =~ m[^(?i)text/]
MC/DC66%2/3
$obj{"_code"} == 2 and $obj{"_meta"} =~ m[^(?i)text/] and not $obj{"_meta"} =~ /(?i)charset=/
$obj{"_code"} == 2
$obj{"_meta"} =~ m[^(?i)text/]
not $obj{"_meta"} =~ /(?i)charset=/
Truth table75%3/4
A and B and not(C)
A= $obj{"_code"} == 2B= $obj{"_meta"} =~ m[^(?i)text/]C= not $obj{"_meta"} =~ /(?i)charset=/
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | and $obj{_meta} =~ m{^(?i)text/} | ||||||||||||||||||||||||||||||||||||||||||
| 217 | and $obj{_meta} !~ m/(?i)charset=/ ) { | ||||||||||||||||||||||||||||||||||||||||||
| 218 | 19 | $obj{_meta} .= ';charset=utf-8'; | |||||||||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||
| 221 | 43 | substr $obj{_buf}, 0, $len + 2, ''; # +2 for the \r\n | |||||||||||||||||||||||||||||||||||||||||
| 222 | 43 | last; | |||||||||||||||||||||||||||||||||||||||||
| 223 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 224 | 18526 | my $len = length $obj{_buf}; | |||||||||||||||||||||||||||||||||||||||||
| 225 | 18526 | ▶ | if ( $len > 1024 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | 18 | @obj{qw(_code _error)} = ( 0, "meta is too long" ); | |||||||||||||||||||||||||||||||||||||||||
| 227 | 18 | goto BLESSING; | |||||||||||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||||||||||
| 229 | 18508 | my $buf; | |||||||||||||||||||||||||||||||||||||||||
| 230 | 18508 | my $n = sysread $obj{_socket}, $buf, $bufsize; | |||||||||||||||||||||||||||||||||||||||||
| 231 | 18508 | ▶ | unless ( defined $n ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 232 | 0 | @obj{qw(_code _error)} = ( 0, "recv response failed: $!" ); | |||||||||||||||||||||||||||||||||||||||||
| 233 | 0 | goto BLESSING; | |||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||
| 235 | 18508 | ▶ | if ( $n == 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | @obj{qw(_code _error)} = ( 0, "recv EOF" ); | |||||||||||||||||||||||||||||||||||||||||
| 237 | 0 | goto BLESSING; | |||||||||||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||||||||||
| 239 | 18508 | $obj{_buf} .= $buf; | |||||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | BLESSING: | ||||||||||||||||||||||||||||||||||||||||||
| 244 | 155 | ▶ | close $obj{_socket} if defined $obj{_socket} and $obj{_code} != 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $obj{"_socket"} and $obj{"_code"} != 2
MC/DC100%2/2
defined $obj{"_socket"} and $obj{"_code"} != 2
defined $obj{"_socket"}
$obj{"_code"} != 2
Truth table100%3/3
A and B
A= defined $obj{"_socket"}B= $obj{"_code"} != 2
| |||||||||||||||||||||||||||||||||||||||||||
| 245 | 155 | bless( \%obj, $class ), $obj{_code}; | |||||||||||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | # utility function that handles redirects and various means of content | ||||||||||||||||||||||||||||||||||||||||||
| 249 | # collection | ||||||||||||||||||||||||||||||||||||||||||
| 250 | sub gemini_request { | ||||||||||||||||||||||||||||||||||||||||||
| 251 | 7 | ▶ | my ( $source, %options ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
gemini_request: called CC=20 SCAR=32.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 252 | $options{max_redirects} = _DEFAULT_REDIRECTS | ||||||||||||||||||||||||||||||||||||||||||
| 253 | 7 | ▶ | unless exists $options{max_redirects}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 254 | $options{redirect_delay} = _DEFAULT_REDIRECT_SLEEP | ||||||||||||||||||||||||||||||||||||||||||
| 255 | 7 | ▶ | unless exists $options{redirect_delay}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | $options{max_size} = _DEFAULT_MAX_CONTENT | ||||||||||||||||||||||||||||||||||||||||||
| 257 | 7 | ▶ | unless exists $options{max_size}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | 7 | my ( $gem, $code ); | |||||||||||||||||||||||||||||||||||||||||
| 260 | 7 | my $redirects = 0; | |||||||||||||||||||||||||||||||||||||||||
| 261 | REQUEST: | ||||||||||||||||||||||||||||||||||||||||||
| 262 | ( $gem, $code ) = Net::Gemini->get( $source, | ||||||||||||||||||||||||||||||||||||||||||
| 263 | 15 | ▶ | ( exists $options{param} ? %{ $options{param} } : () ) ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | 15 | ▶ | if ( $code == 2 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 265 | 5 | my $len = length $gem->{_buf}; | |||||||||||||||||||||||||||||||||||||||||
| 266 | 5 | ▶ | my $bufsize = $options{bufsize} || _DEFAULT_BUFSIZE; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options{'bufsize'} || 4096
MC/DC100%1/1
$options{'bufsize'} || 4096
$options{'bufsize'}
Truth table100%2/2
A
A= $options{'bufsize'}
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | # this can make uninit noise for a meta of ";" which might be | ||||||||||||||||||||||||||||||||||||||||||
| 268 | # worth an upstream patch? | ||||||||||||||||||||||||||||||||||||||||||
| 269 | 5 | $gem->{_mime} = [ parse_mime_type( $gem->meta ) ]; | |||||||||||||||||||||||||||||||||||||||||
| 270 | 5 | ▶ | if ( exists $options{content_callback} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 271 | 1 | ▶ | if ($len) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | 1 | ▶ | $options{content_callback}->( $gem->{_buf}, $len, $gem ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 273 | or goto CLEANUP; | ||||||||||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | my $buf; | |||||||||||||||||||||||||||||||||||||||||
| 277 | 0 | $len = sysread $gem->{_socket}, $buf, $bufsize; | |||||||||||||||||||||||||||||||||||||||||
| 278 | 0 | ▶ | if ( !defined $len ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | die "sysread failed: $!\n"; | |||||||||||||||||||||||||||||||||||||||||
| 280 | } elsif ( $len == 0 ) { | ||||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | ▶ | $options{content_callback}->( $buf, $len, $gem ) or goto CLEANUP; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||||||||||||
| 285 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 286 | 4 | ▶ | if ($len) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 287 | 3 | ▶ | if ( $len > $options{max_size} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 288 | 0 | $gem->{_content} = substr $gem->{_buf}, 0, $options{max_size}; | |||||||||||||||||||||||||||||||||||||||||
| 289 | 0 | @{$gem}{qw(_code _error)} = ( 0, 'max_size' ); | |||||||||||||||||||||||||||||||||||||||||
| 290 | 0 | goto CLEANUP; | |||||||||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||||||||
| 292 | 3 | $gem->{_content} = $gem->{_buf}; | |||||||||||||||||||||||||||||||||||||||||
| 293 | 3 | $options{max_size} -= $len; | |||||||||||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||||||||||
| 295 | 4 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 296 | 7 | my $buf; | |||||||||||||||||||||||||||||||||||||||||
| 297 | 7 | $len = sysread $gem->{_socket}, $buf, $bufsize; | |||||||||||||||||||||||||||||||||||||||||
| 298 | 7 | ▶ | if ( !defined $len ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | die "sysread failed: $!\n"; | |||||||||||||||||||||||||||||||||||||||||
| 300 | } elsif ( $len == 0 ) { | ||||||||||||||||||||||||||||||||||||||||||
| 301 | 3 | last; | |||||||||||||||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||||||||||||||||
| 303 | 4 | ▶ | if ( $len > $options{max_size} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 304 | 1 | $gem->{_content} .= substr $buf, 0, $options{max_size}; | |||||||||||||||||||||||||||||||||||||||||
| 305 | 1 | @{$gem}{qw(_code _error)} = ( 0, 'max_size' ); | |||||||||||||||||||||||||||||||||||||||||
| 306 | 1 | goto CLEANUP; | |||||||||||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||||||||||
| 308 | 3 | $gem->{_content} .= $buf; | |||||||||||||||||||||||||||||||||||||||||
| 309 | 3 | $options{max_size} -= $len; | |||||||||||||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||||||||||||
| 312 | ▶ | } elsif ( $code == 3 and ++$redirects <= $options{max_redirects} ) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$code == 3 and ++$redirects <= $options{"max_redirects"}
MC/DC50%1/2
$code == 3 and ++$redirects <= $options{"max_redirects"}
$code == 3
++$redirects <= $options{"max_redirects"}
Truth table66%2/3
A and B
A= $code == 3B= ++$redirects <= $options{"max_redirects"}
| |||||||||||||||||||||||||||||||||||||||||||
| 313 | # a '31' permanent redirect should result in us not requesting | ||||||||||||||||||||||||||||||||||||||||||
| 314 | # the old URL again, but that would require more code here for | ||||||||||||||||||||||||||||||||||||||||||
| 315 | # something that is probably rare | ||||||||||||||||||||||||||||||||||||||||||
| 316 | 8 | my $new = $gem->{_meta}; | |||||||||||||||||||||||||||||||||||||||||
| 317 | 8 | $source = URI->new_abs( $new, $gem->{_uri} ); | |||||||||||||||||||||||||||||||||||||||||
| 318 | 8 | select( undef, undef, undef, $options{redirect_delay} ); | |||||||||||||||||||||||||||||||||||||||||
| 319 | 8 | goto REQUEST; | |||||||||||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||||||||||||
| 321 | CLEANUP: | ||||||||||||||||||||||||||||||||||||||||||
| 322 | 7 | undef $gem->{_buf}; | |||||||||||||||||||||||||||||||||||||||||
| 323 | 7 | close $gem->{_socket}; | |||||||||||||||||||||||||||||||||||||||||
| 324 | 7 | return $gem, $code; | |||||||||||||||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | # drain what remains (if anything) via a callback interface. assumes | ||||||||||||||||||||||||||||||||||||||||||
| 328 | # that a ->get call has been made | ||||||||||||||||||||||||||||||||||||||||||
| 329 | sub getmore { | ||||||||||||||||||||||||||||||||||||||||||
| 330 | 26 | ▶ | my ( $self, $callback, %param ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
getmore: called CC=7 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||
| 332 | 26 | my $len = length $self->{_buf}; | |||||||||||||||||||||||||||||||||||||||||
| 333 | 26 | ▶ | if ($len) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 334 | 25 | ▶ | $callback->( $self->{_buf}, $len ) or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 335 | 25 | undef $self->{_buf}; | |||||||||||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||
| 338 | 26 | ▶ | my $bufsize = $param{bufsize} || _DEFAULT_BUFSIZE; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$param{'bufsize'} || 4096
MC/DC100%1/1
$param{'bufsize'} || 4096
$param{'bufsize'}
Truth table100%2/2
A
A= $param{'bufsize'}
| |||||||||||||||||||||||||||||||||||||||||||
| 339 | 26 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 340 | 26 | my $buf; | |||||||||||||||||||||||||||||||||||||||||
| 341 | 26 | $len = sysread $self->{_socket}, $buf, $bufsize; | |||||||||||||||||||||||||||||||||||||||||
| 342 | 26 | ▶ | if ( !defined $len ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 343 | 0 | die "sysread failed: $!\n"; | |||||||||||||||||||||||||||||||||||||||||
| 344 | } elsif ( $len == 0 ) { | ||||||||||||||||||||||||||||||||||||||||||
| 345 | 25 | last; | |||||||||||||||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||||||||||||||||
| 347 | 1 | ▶ | $callback->( $buf, $len ) or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||||||||||||
| 349 | 25 | close $self->{_socket}; | |||||||||||||||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||
| 352 | 39 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 353 | __END__ | ||||||||||||||||||||||||||||||||||||||||||