Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Item13378: cannot encode output when it's zipped content coming from …
…the cache
  • Loading branch information
Comment committed May 20, 2015
1 parent 5b752fc commit 0b71690
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
15 changes: 12 additions & 3 deletions core/lib/Foswiki.pm
Expand Up @@ -841,7 +841,7 @@ sub generateHTTPHeaders {
->dispatch( 'modifyHeaderHandler', $hopts, $this->{request} );

# add http compression and conditional cache controls
if ( !$this->inContext('command_line') && $text ) {
if ( $Foswiki::engine->isa('Foswiki::Engine::CGI') && $text ) {

if ( $Foswiki::cfg{HttpCompress}
&& ( $ENV{'HTTP_ACCEPT_ENCODING'} || $ENV{'SPDY'} ) )
Expand All @@ -857,8 +857,15 @@ sub generateHTTPHeaders {
$text = $cachedPage->{data};
}
else {
# SMELL: this assumes we can just chuck the $text at the
# response and get it printed, but unicode support requires
# that the CGI response utf8-encodes what is written to it.
# We don't want to utf-8 encode the zipped content, and the
# CGI Engine won't as long as the Content-Encoding includes
# 'gzip'. If any other compression method is used, this has
# to be considered.
require Compress::Zlib;
$text = Compress::Zlib::memGzip($text);
$text = Compress::Zlib::memGzip( Encode::encode_utf8($text) );
}
}
elsif ($cachedPage
Expand All @@ -873,7 +880,9 @@ sub generateHTTPHeaders {
# only know situation this can happen is for older browsers like IE6
# which does not understand gzip'ed http encodings
require Compress::Zlib;
$text = Compress::Zlib::memGunzip( $cachedPage->{data} );
$text =
Encode::decode_utf8(
Compress::Zlib::memGunzip( $cachedPage->{data} ) );
}

# we need to force the browser into a check on every
Expand Down
11 changes: 9 additions & 2 deletions core/lib/Foswiki/Engine/CGI.pm
Expand Up @@ -278,12 +278,19 @@ sub finalizeHeaders {
$this->SUPER::finalizeHeaders( $res, $req );

my $hdr = $res->printHeaders;
$this->write($hdr);
if ( ( $res->headers->{'Content-Encoding'} || '' ) =~ /gzip/ ) {

# Content has come from cache; don't encode it
$this->write($hdr);
}
else {
$this->write( Encode::encode_utf8($hdr) );
}
}

sub write {
my $s = $_[1];
print Encode::encode_utf8($s);
print $s;
}

1;
Expand Down
11 changes: 7 additions & 4 deletions core/lib/Foswiki/PageCache.pm
Expand Up @@ -136,7 +136,10 @@ sub genVariationKey {
# add a flag to distinguish compressed from uncompressed cache entries
$variationKey .= '::'
. (
( $Foswiki::cfg{HttpCompress} && $session->inContext('command_line') )
(
$Foswiki::cfg{HttpCompress}
&& $Foswiki::engine->isa('Foswiki::Engine::CLI')
)
? 1
: 0
);
Expand Down Expand Up @@ -243,11 +246,11 @@ sub cachePage {
unless ($isDirty) {
$data =~ s/([\t ]?)[ \t]*<\/?(nop|noautolink)\/?>/$1/gis;

if ( $Foswiki::cfg{HttpCompress}
&& !$session->inContext('command_line') )
if ( $Foswiki::cfg{HttpCompress}
&& $Foswiki::engine->isa('Foswiki::Engine::CGI') )
{
require Compress::Zlib;
$data = Compress::Zlib::memGzip($data);
$data = Compress::Zlib::memGzip( Encode::encode_utf8($data) );
}
$etag = $time;
$lastModified = Foswiki::Time::formatTime( $time, '$http', 'gmtime' );
Expand Down

0 comments on commit 0b71690

Please sign in to comment.