All pastes #922911 Raw Edit

Anonymous

public text v1 · immutable
#922911 ·published 2008-02-29 02:42 UTC
rendered paste body
    # Note that Compress::Zlib::memGunzip() destroys its input, thus $body2 .
    # There seems to be a bug (?) in Compress::Zlib such that compressing and
    #   then uncompressing some strings (ones with non-ASCII characters?) does
    #   not result in the original string.  One example is watan.com, which
    #   uses the "windows-1256" encoding.  Thus, only send the gzip'd $body if
    #   decompression exactly undoes compression.  Inefficient.  :(
    # jsm-- is there a better way, e.g. simply not compressing windows-1256
    #   text, or converting all text to utf-8?
    if ($ENV{HTTP_ACCEPT_ENCODING}=~ /\bgzip\b/i) {
	eval { require Compress::Zlib } ;
	if (!$@) {
	    my $body_pre= $body ;
	    my $body2= $body= Compress::Zlib::memGzip($body_pre) ;
	    my $body_post= Compress::Zlib::memGunzip($body2) ;
	    if ($body_pre eq $body_post) {
		$headers= "Content-Encoding: gzip\015\012" . $headers ;
	    } else {
		# Set $body back to what it was before the compression.
		$body= $body_pre ;
	    }
	}
    }