App-phoebe
view release on metacpan or search on metacpan
script/spartan view on Meta::CPAN
use Pod::Text;
use Getopt::Long;
use Encode::Locale;
use Encode qw(encode decode);
use URI::Escape;
my $help;
my $verbose;
GetOptions(
'help' => \$help,
'verbose' => \$verbose)
or die("Error in command line arguments\n");
# Help
if ($help) {
my $parser = Pod::Text->new();
$parser->parse_file($0);
exit;
}
# Regular arguments
my ($url) = @ARGV;
die "â You must provide an URL\n" unless $url;
my($scheme, $authority, $path, $query, $fragment) =
$url =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?|;
die "â The URL '$url' must use the spartan scheme\n" unless $scheme and $scheme eq 'spartan';
die "â The URL '$url' must have an authority\n" unless $authority;
my ($host, $port) = split(/:/, $authority);
$port ||= 300;
$path ||= "/";
warn "Contacting $host:$port\n" if $verbose;
my $data = "";
if (not -t *STDIN) {
local $/ = undef;
$data = <STDIN>;
}
# create client
Mojo::IOLoop->client({address => $host, port => $port} => sub {
my ($loop, $err, $stream) = @_;
die $err if $err;
# 1h timeout (for chat)
$stream->timeout(3600);
my ($header, $mimetype, $encoding);
$stream->on(read => sub {
my ($stream, $bytes) = @_;
if ($header and $encoding) {
print encode(locale => decode($encoding, $bytes));
} elsif ($header) {
print encode(locale => $bytes);
} else {
($header) = $bytes =~ /^(.*?)\r\n/;
warn "$header\n";
if ($header =~ /^2\d* (?:text\/\S+)?(?:; *charset=(\S+))?$/g) {
# empty, or text without charset defaults to UTF-8
$encoding = $1 || 'UTF-8';
}
$bytes =~ s/^(.*?)\r\n//; # remove header
if ($encoding) {
say encode(locale => decode($encoding, $bytes));
} else {
print encode(locale => $bytes);
}
}});
# Write request
my $size = length($data);
warn "Requesting $host $path $size\n" if $verbose;
$stream->write("$host $path $size\r\n$data")});
# Start event loop if necessary
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
( run in 1.666 second using v1.01-cache-2.11-cpan-39bf76dae61 )