use AnyEvent ; use AnyEvent::Strict ; use AnyEvent::Handle; sub http_get { my ($host, $uri, $cb) = @_; print "http_get: $host, $uri called\n"; ###RPT # store results here #my ($response, $header, $body); my $handle; $handle = new AnyEvent::Handle connect => [$host => 'http'], on_error => sub { print "on_error: condition\n"; ###RPT $cb->("HTTP/1.0 500 $!"); $handle->destroy; # explicitly destroy handle }, on_eof => sub { print "on_eof: condition\n"; ###RPT $cb->($response, $header, $body); $handle->destroy; # explicitly destroy handle }; print "moving to push_write Get\n"; ###RPT $handle->push_write ("GET $uri HTTP/1.0\015\012\015\012"); print "after push_write Get\n"; ###RPT # now fetch response status line $handle->push_read (line => sub { my ($handle, $line) = @_; print "push_read: statusLine <$line>\n"; ###RPT $response = $line; }); # then the headers $handle->push_read (line => "\015\012\015\012", sub { my ($handle, $line) = @_; print "push_read: header <$line>\n"; ###RPT $header = $line; }); # and finally handle any remaining data as body $handle->on_read (sub { $body .= $_[0]->rbuf; print "on_read: body <$line>\n"; ###RPT $_[0]->rbuf = ""; }); } http_get "www.google.com", "/", sub { # my ($response, $header, $body) = @_; print $response, "\n", $header, "\n", ###RPT $body; };