How do I create a gzip compressed HTTP::Response?

倾然丶 夕夏残阳落幕 提交于 2019-12-07 18:18:38

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!