Skip to content

Commit

Permalink
get Compression working (again) in a fashion closer to the existing code
Browse files Browse the repository at this point in the history
  • Loading branch information
perigrin committed Nov 5, 2012
1 parent 1ec7d64 commit b6d130c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
1 change: 1 addition & 0 deletions eg/track.pl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
password => $password,
method => $method || "sample",
%args,
use_compression => 1,
on_tweet => sub {
my $tweet = shift;
print "$tweet->{user}{screen_name}: $tweet->{text}\n";
Expand Down
37 changes: 29 additions & 8 deletions lib/AnyEvent/Twitter/Stream.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use MIME::Base64;
use URI;
use URI::Escape;
use Carp;
use Compress::Raw::Zlib;

our $STREAMING_SERVER = 'stream.twitter.com';
our $USERSTREAM_SERVER = 'userstream.twitter.com';
Expand Down Expand Up @@ -54,6 +55,16 @@ sub new {
$decode_json = 1;
}

my ($zlib, my $_zstatus);
if (delete $args{use_compression}){
($zlib, $_zstatus) = Compress::Raw::Zlib::Inflate->new(
-LimitOutput => 1,
-AppendOutput => 1,
-WindowBits => WANT_GZIP_OR_ZLIB,
);
die "Can't make inflator: $_zstatus" unless $zlib;
}

unless ($methods{$method}) {
$on_error->("Method $method not available.");
return;
Expand Down Expand Up @@ -145,6 +156,7 @@ sub new {
$self->{connection_guard} = http_request($request_method, $uri,
headers => {
Accept => '*/*',
( defined $zlib ? ('Accept-Encoding' => 'deflate') : ()),
Authorization => $auth,
($request_method eq 'POST'
? ('Content-Type' => 'application/x-www-form-urlencoded')
Expand All @@ -165,7 +177,7 @@ sub new {
my ($handle, $headers) = @_;

return unless $handle;

my $input;
my $chunk_reader = sub {
my ($handle, $line) = @_;

Expand All @@ -174,13 +186,22 @@ sub new {

$handle->push_read(chunk => $len, sub {
my ($handle, $chunk) = @_;

$handle->push_read(line => sub {
length $_[1] and die 'bad chunk (missing last empty line)';
});

$on_json_message->($chunk);
});
$handle->push_read(line => sub { length $_[1] and die 'bad chunk (missing last empty line)'; });

unless ($headers->{'content-encoding'}) {
$on_json_message->($chunk);
} elsif ($headers->{'content-encoding'} eq 'deflate') {
$input .= $chunk;
my ($message);
do {
$_zstatus = $zlib->inflate(\$input, \$message);
return unless $_zstatus == Z_OK || $_zstatus == Z_BUF_ERROR;
} while ( $_zstatus == Z_OK && length $input );
$on_json_message->($message);
} else {
die "Don't know how to decode $headers->{'content-encoding'}"
}
});
};
my $line_reader = sub {
my ($handle, $line) = @_;
Expand Down

0 comments on commit b6d130c

Please sign in to comment.