如何创建gzip压缩的HTTP :: Response?

时间:2011-07-28 22:04:08

标签: perl lwp

我需要使用压缩数据创建HTTP :: Response。如何制作内容gzip?我是否只需添加适当的标头并使用Compress :: Zlib自行压缩它?或者是否有任何LWP模块提供了处理此问题的方法?

1 个答案:

答案 0 :(得分:2)

这是你需要的吗?您可以gzip数据,设置Content-encoding标头,然后将其发送出去。

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;