如何创建 gzip 压缩的 HTTP::Response? [英] How do I create a gzip compressed HTTP::Response?

查看:69
本文介绍了如何创建 gzip 压缩的 HTTP::Response?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

这是您需要的吗?您对数据进行 gzip,设置 Content-encoding 标头,然后将其发送出去.

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;

这篇关于如何创建 gzip 压缩的 HTTP::Response?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆