Skip to content

Instantly share code, notes, and snippets.

@augensalat
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save augensalat/caad97c62fb375dd4f4c to your computer and use it in GitHub Desktop.
Save augensalat/caad97c62fb375dd4f4c to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use Mojo::IOLoop;
use Mojo::UserAgent;
# Build a normal transaction
my $ua = Mojo::UserAgent->new;
my $bcr = my $bcw = 0;
$ua->on(start => sub {
my ($ua, $tx) = @_;
$tx->on(connection => sub {
my ($tx, $connection) = @_;
my $s = Mojo::IOLoop->stream($connection);
$s->on(write => sub {
my $bc = length pop;
$bcw += $bc;
warn "++ Stream write event - $bc bytes written.\n";
});
$s->on(read => sub {
my $bc = length pop;
$bcr += $bc;
warn "++ Stream read event - $bc bytes read.\n";
});
$s->on(close => sub { warn "++ Stream close event\n" });
});
});
my $tx = $ua->build_tx(GET => 'http://example.com/', {Connection => 'close'});
# Process transaction
$ua->start($tx => sub {
my ($ua, $tx) = @_;
my $res = $tx->res;
my $size = $res->start_line_size + $res->header_size + $res->body_size;
warn <<EOT;
++ DONE: $bcr bytes read, $bcw bytes written.
Response content size is $size bytes.
EOT
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment