AE::HTTP http_get with want_body_handle doesn't work unless either an on_eof or on_error callback is defined

Peter Juhasz pjuhasz at uhusystems.com
Mon Jan 9 18:36:05 CET 2017


Hello,

consider the following example program that uses AE::HTTP's
want_body_handle functionality to download streaming data from a long
poll type HTTP service:

##########################################
#!/usr/bin/perl

use strict;
use warnings;
use AnyEvent;
use AnyEvent::HTTP;

$| = 1;

my $cv = AnyEvent->condvar;

http_get "http://google.com",
	want_body_handle => 1,
	sub {
		my $handle = shift;
		$handle->on_read(sub {
			my ($handle, $headers) = @_;
			print $handle->{rbuf};
			$handle->{rbuf} = '';
		});
		$handle->on_error(sub {
			undef $handle;
			die 'Server error: '.$_[2];
		});
		$handle->on_eof(sub {
			undef $handle;
		});
	};

$cv->recv;
##########################################

Obviously google.com is not streaming service, but still, this program
should retrieve the entirety of their site. It does, so far, so good.

But comment both the on_error and on_eof sections of the code, and it
seems to stop after retrieving a short section of the expected data (it
seems the size of the retrieved data is consistently 1416 bytes, which
is suspiciously close to the size of one TCP packet.)

Even more interestingly, comment just the contents of the two callbacks
(so that on_error and on_eof are just empty subs) and the behavior is
the same.

Comment out just one and it works as expected.

Question: Is this a bug in AE::HTTP (or AE::Handle), or is something
wrong with the way I'm using it?

Thanks,
Peter Juhasz




More information about the anyevent mailing list