App-Phoebe
view release on metacpan or search on metacpan
script/titan view on Meta::CPAN
$token //= '';
die "â You must provide an URL\n" unless $url;
my($scheme, $authority, $path, $query, $fragment) =
$url =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?|;
die "â The URL '$url' must use the titan scheme\n" unless $scheme and $scheme eq 'titan';
die "â The URL '$url' must have an authority\n" unless $authority;
die "â The URL '$url' must have a path\n" unless $path;
die "â The URL '$url' must not have a query\n" if $query;
die "â The URL '$url' must not have a fragment\n" if $fragment;
if (@files > 1) {
die "â The URL '$url' must have a path that ends in a slash\n" if $path !~ /\/$/;
} elsif (not @files) {
die "â The URL '$url' must have a path that does not end in a slash\n" if $path =~ /\/$/;
}
for my $file (@files) {
die "â The file '$file' does not exist\n" unless -e $file;
die "â The file '$file' cannot be read\n" unless -r $file;
}
warn "Without a token chances are slim⦠ð
\n" unless $token;
say "Start typing and end your input with Ctrl-D⦠ð" if -t and not @files;
say "Reading from the pipe⦠ð" if not -t and not @files;
my ($host, $port) = split(/:/, $authority, 2);
$port //= 1965;
undef $/;
my $temp_fh;
unless (@files) {
my $data = <STDIN>;
my $file;
($temp_fh, $file) = tempfile();
print $temp_fh $data;
close($temp_fh);
push(@files, $file);
}
my %args = (PeerHost => $host,
PeerService => $port,
SSL_verify_mode => SSL_VERIFY_NONE);
# Default certs
$args{SSL_cert_file} = $cert;
$args{SSL_key_file} = $key;
$args{SSL_cert_file} //= 'client-cert.pem' if -f 'client-cert.pem';
$args{SSL_key_file} //= 'client-key.pem' if -f 'client-key.pem';
for my $file (@files) {
open(my $fh, '<', $file) or die "â The file '$file' cannot be read: $!\n";
my $data = <$fh>;
close($fh);
my $size = length($data);
my $type = $mime;
$type //= qx(/usr/bin/file --mime-type --brief "$file");
$type =~ s/\s+$//; # remove trailing whitespace
# If the URL ends in a slash, append the URI-escaped filename without suffix
my $furl = $url;
if ($path =~ /\/$/) {
my ($name) = fileparse($file, '.gmi');
$furl .= uri_escape($name);
}
# create client
my $socket = IO::Socket::SSL->new(%args)
or die "Cannot construct client socket: $@";
# send data in one go
print $socket "$furl;size=$size;mime=$type;token=$token\r\n$data";
# print response
my $response = <$socket>;
if ($response) {
$response =~ s/\r//g;
print $response;
} else {
warn "No response for $file: $!\n";
}
}
( run in 1.347 second using v1.01-cache-2.11-cpan-39bf76dae61 )