问题
I need to create an HTTP::Response with compressed data. How do I go about making the content gzipped? Do I just add the appropriate headers and compress it myself using Compress::Zlib? Or does any of the LWP modules provide a method for handling this?
回答1:
Is this what you need? You gzip the data, set the Content-encoding header, and send it off.
use strict;
use warnings;
use HTTP::Response;
use IO::Compress::Gzip qw(gzip);
my $data = q(My cat's name is Buster);
my $gzipped_data;
my $gzip = gzip \$data => \$gzipped_data;
print STDERR $gzipped_data;
my $response = HTTP::Response->new;
$response->code( 200 );
$response->header( 'Content-type' => 'text/plain' );
$response->header( 'Content-encoding' => 'gzip' );
$response->header( 'Content-length' => length $gzipped_data );
$response->content( $gzipped_data );
print $response->as_string;
来源:https://stackoverflow.com/questions/6866341/how-do-i-create-a-gzip-compressed-httpresponse