r24275 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r24274‎ | r24275 | r24276 >
Date:19:06, 19 July 2007
Author:brion
Status:old
Tags:
Comment:
Don't do gzip compression if the URL path ends in .gz or .tgz
This confuses Safari and triggers a download of the page,
even though it's pretty clearly labeled as viewable HTML.
Bad Safari! Bad!

Bug is still present in Safari 2.0.4/Mac and 3.0.2/Win.

We have had a live hack for this on Wikimedia sites for .gz files;
hadn't noticed the .tgz problem before though.

While I was in there, also made a tweak so wfGzipHandler() makes
sure the Vary: header is there whether the current agent sent us
an Accept-Encoding header or not.
Modified paths:
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/includes/OutputHandler.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/OutputHandler.php
@@ -18,30 +18,67 @@
1919 }
2020
2121 /**
 22+ * Get the "file extension" that some client apps will estimate from
 23+ * the currently-requested URL.
 24+ * This isn't on WebRequest because we need it when things aren't initialized
 25+ * @private
 26+ */
 27+function wfRequestExtension() {
 28+ /// @fixme -- this sort of dupes some code in WebRequest::getRequestUrl()
 29+ if( isset( $_SERVER['REQUEST_URI'] ) ) {
 30+ // Strip the query string...
 31+ list( $path ) = explode( '?', $_SERVER['REQUEST_URI'], 2 );
 32+ } elseif( isset( $_SERVER['SCRIPT_NAME'] ) ) {
 33+ // Probably IIS. QUERY_STRING appears separately.
 34+ $path = $_SERVER['SCRIPT_NAME'];
 35+ } else {
 36+ // Can't get the path from the server? :(
 37+ return '';
 38+ }
 39+
 40+ $period = strrpos( $path, '.' );
 41+ if( $period !== false ) {
 42+ return strtolower( substr( $path, $period ) );
 43+ }
 44+ return '';
 45+}
 46+
 47+/**
2248 * Handler that compresses data with gzip if allowed by the Accept header.
2349 * Unlike ob_gzhandler, it works for HEAD requests too.
2450 */
2551 function wfGzipHandler( $s ) {
26 - if ( function_exists( 'gzencode' ) && !headers_sent() ) {
27 - $tokens = preg_split( '/[,; ]/', $_SERVER['HTTP_ACCEPT_ENCODING'] );
28 - if ( in_array( 'gzip', $tokens ) ) {
29 - header( 'Content-Encoding: gzip' );
30 - $s = gzencode( $s, 3 );
31 -
32 - # Set vary header if it hasn't been set already
33 - $headers = headers_list();
34 - $foundVary = false;
35 - foreach ( $headers as $header ) {
36 - if ( substr( $header, 0, 5 ) == 'Vary:' ) {
37 - $foundVary = true;
38 - break;
39 - }
40 - }
41 - if ( !$foundVary ) {
42 - header( 'Vary: Accept-Encoding' );
43 - }
 52+ if( !function_exists( 'gzencode' ) || headers_sent() ) {
 53+ return $s;
 54+ }
 55+
 56+ $ext = wfRequestExtension();
 57+ if( $ext == '.gz' || $ext == '.tgz' ) {
 58+ // Don't do gzip compression if the URL path ends in .gz or .tgz
 59+ // This confuses Safari and triggers a download of the page,
 60+ // even though it's pretty clearly labeled as viewable HTML.
 61+ // Bad Safari! Bad!
 62+ return $s;
 63+ }
 64+
 65+ $tokens = preg_split( '/[,; ]/', $_SERVER['HTTP_ACCEPT_ENCODING'] );
 66+ if ( in_array( 'gzip', $tokens ) ) {
 67+ header( 'Content-Encoding: gzip' );
 68+ $s = gzencode( $s, 3 );
 69+ }
 70+
 71+ // Set vary header if it hasn't been set already
 72+ $headers = headers_list();
 73+ $foundVary = false;
 74+ foreach ( $headers as $header ) {
 75+ if ( substr( $header, 0, 5 ) == 'Vary:' ) {
 76+ $foundVary = true;
 77+ break;
4478 }
4579 }
 80+ if ( !$foundVary ) {
 81+ header( 'Vary: Accept-Encoding' );
 82+ }
4683 return $s;
4784 }
4885
Index: trunk/phase3/RELEASE-NOTES
@@ -315,6 +315,7 @@
316316 * Fix several JavaScript bugs under MSIE 5/Macintosh
317317 * (bug 10591) Use Arabic numerals (0,1,2...) for the Malayam language
318318 * (bug 10642) Fix shift-click checkbox behavior for Opera 9.0+ and 6.0
 319+* Work around Safari bug with pages ending in ".gz" or ".tgz"
319320
320321
321322 == API changes since 1.10 ==

Follow-up revisions

RevisionCommit summaryAuthorDate
r24276Merged revisions 24213-24275 via svnmerge from...david20:20, 19 July 2007

Status & tagging log