Last active
August 29, 2015 14:23
-
-
Save augensalat/caad97c62fb375dd4f4c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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