2011-07-28 57 views
3

我需要用壓縮數據創建一個HTTP :: Response。我如何着手解壓內容?我只需添加適當的標題並使用Compress :: Zlib自己壓縮它?或者是否有任何LWP模塊提供了處理這個問題的方法?如何創建gzip壓縮的HTTP :: Response?

+0

[This perlmonks article](http://www.perlmonks.org/?node=858560)可能會有幫助。 – maerics

+0

看到這個問題:http://stackoverflow.com/questions/1285305/how-can-i-accept-gzip-compressed-content-using-lwpuseragent –

+0

這些是關於下載gzipped內容。我的問題是關於通過我的perl webserver服務gzip內容。我的內容如「abc 12345」;我需要將它的gzip版本作爲HTTP :: Response發送給客戶端/瀏覽器。 – mrburns

回答

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;