if (my @ct = HTTP::Headers::Util::split_header_words($self->header("Content-Type"))) {
($ct, undef, %ct_param) = @{$ct[-1]};
die "Can't decode multipart content" if $ct =~ m,^multipart/,;
}
$content_ref = $self->content_ref;
die "Can't decode ref content" if ref($content_ref) ne "SCALAR";
if (my $h = $self->header("Content-Encoding")) {
$h =~ s/^\s+//;
$h =~ s/\s+$//;
for my $ce (reverse split(/\s*,\s*/, lc($h))) {
next unless $ce;
next if $ce eq "identity";
if ($ce eq "gzip" || $ce eq "x-gzip") {
require Compress::Zlib;
unless ($content_ref_iscopy) {
# memGunzip is documented to destroy its buffer argument
my $copy = $$content_ref;
$content_ref = \$copy;
$content_ref_iscopy++;
}
$content_ref = \Compress::Zlib::memGunzip($$content_ref);
die "Can't gunzip content" unless defined $$content_ref;
}
elsif ($ce eq "x-bzip2") {
require Compress::Bzip2;
unless ($content_ref_iscopy) {
# memBunzip is documented to destroy its buffer argument
my $copy = $$content_ref;
$content_ref = \$copy;
$content_ref_iscopy++;
}
$content_ref = \Compress::Bzip2::memBunzip($$content_ref);
die "Can't bunzip content" unless defined $$content_ref;
}
elsif ($ce eq "deflate") {
require Compress::Zlib;
my $out = Compress::Zlib::uncompress($$content_ref);
unless (defined $out) {
# "Content-Encoding: deflate" is supposed to mean the "zlib"
# format of RFC 1950, but Microsoft got that wrong, so some
# servers sends the raw compressed "deflate" data. This
# tries to inflate this format.
unless ($content_ref_iscopy) {
# the $i->inflate method is documented to destroy its
# buffer argument
my $copy = $$content_ref;
$content_ref = \$copy;
$content_ref_iscopy++;
}
my($i, $status) = Compress::Zlib::inflateInit(
WindowBits => -Compress::Zlib::MAX_WBITS(),
);
my $OK = Compress::Zlib::Z_OK();
die "Can't init inflate object" unless $i && $status == $OK;
($out, $status) = $i->inflate($content_ref);
if ($status != Compress::Zlib::Z_STREAM_END()) {
if ($status == $OK) {
$self->push_header("Client-Warning" =>
"Content might be truncated; incomplete deflate stream");
}
else {
# something went bad, can't trust $out any more
$out = undef;
}
}
}
die "Can't inflate content" unless defined $out;
$content_ref = \$out;
$content_ref_iscopy++;
}
elsif ($ce eq "compress" || $ce eq "x-compress") {
die "Can't uncompress content";
}
elsif ($ce eq "base64") { # not really C-T-E, but should be harmless
require MIME::Base64;
$content_ref = \MIME::Base64::decode($$content_ref);
$content_ref_iscopy++;
}
elsif ($ce eq "quoted-printable") { # not really C-T-E, but should be harmless
require MIME::QuotedPrint;
$content_ref = \MIME::QuotedPrint::decode($$content_ref);
$content_ref_iscopy++;
}
else {
die "Don't know how to decode Content-Encoding '$ce'";
}
}
}
if ($ct && $ct =~ m,^text/,,) {
my $charset = $opt{charset} || $ct_param{charset} || $opt{default_charset} || "ISO-8859-1";
$charset = lc($charset);
if ($charset ne "none") {
require Encode;
if (do{my $v = $Encode::VERSION; $v =~ s/_//g; $v} < 2.0901 &&
!$content_ref_iscopy)
{
=3= |