in reply to uncompress gzip data in a callback

I haven't had much of a call for LWP, so I've never had to decompress one chunk at a time. So what I would do is, first, check my system's gzip setup:
#!/usr/bin/perl use strict; use warnings; use HTTP::Message; my $can_accept = HTTP::Message::decodable(); print $can_accept, "\n";
It'll return a list of allowed encodings. My system has gzip, x-gzip, deflate, and x-bzip2. Then, in your script, you want to set it up something like this:
#!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; my $ua = LWP::UserAgent->new; my $can_accept = HTTP::Message::decodable; my $response = $ua->get('http://www.perlmonks.org', 'Accept-Encoding' => $can_accept, ); print $response->decoded_content(charset => 'none');
For compression, I'd stick with Compress::Zlib. The documentation has some examples that might be useful for you.

Replies are listed 'Best First'.
Re^2: uncompress gzip data in a callback
by Weevil (Novice) on May 03, 2010 at 22:27 UTC
    Unfortunately the callback is provided with the encoded data. When using a callback no data is stored, so decoded_content returns nothing.
      Try this
      use LWP::UserAgent; use Compress::Raw::Zlib; sub mkGunz { my $gun = new Compress::Raw::Zlib::Inflate( WindowBits => WANT_GZIP); return sub { my $out; my $status = $gun->inflate($_[0], $out); if ($status == Z_OK || $status == Z_STREAM_END) { print $out; } else { die $status; } } ; } my $ua = LWP::UserAgent->new; my $URL = 'http://whatever/'; my $res = $ua->request(HTTP::Request->new(GET => $URL), mkGunz());

        Update: Closed: Dumb user error! See below.

        This does not seem to work. And I wonder how could it? Given that you pass $_[0] to inflate() each time, and the callback gets called with lots of little bits, none of which is decodable stand alone?