【问题标题】:How do I create a gzip compressed HTTP::Response?如何创建 gzip 压缩的 HTTP::Response?
【发布时间】:2011-07-28 22:04:07
【问题描述】:

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

【问题讨论】:

  • This perlmonks article 可能会有所帮助。
  • 这些是关于下载 gzip 压缩内容的。我的问题是关于通过我的 perl 网络服务器提供压缩内容。我的内容是“abc 12345”;我需要将它的压缩版本作为 HTTP::Response 发送到客户端/浏览器。

标签: perl lwp


【解决方案1】:

这是你需要的吗?您 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;

【讨论】:

  • 感谢您的回答,但由于某种原因,浏览器无法解压缩 gzip 内容。在 Fiddler 中调试时,我收到错误消息“无法解压缩内容。GZip 标头中的幻数不正确。请确保您正在传递 GZip 流”。关于为什么会这样的任何想法?
  • @HBCondo - 您必须提出另一个问题并显示您的代码。
猜你喜欢
  • 2012-02-12
  • 1970-01-01
  • 1970-01-01
  • 2016-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-14
  • 2011-06-11
相关资源
最近更新 更多