Add an X-Vary-Options header, as per my proposed squid patch
[lhc/web/wiklou.git] / includes / OutputHandler.php
1 <?php
2
3 /**
4 * Standard output handler for use with ob_start
5 */
6 function wfOutputHandler( $s ) {
7 global $wgDisableOutputCompression;
8 $s = wfMangleFlashPolicy( $s );
9 if ( !$wgDisableOutputCompression && !ini_get( 'zlib.output_compression' ) ) {
10 if ( !defined( 'MW_NO_OUTPUT_COMPRESSION' ) ) {
11 $s = wfGzipHandler( $s );
12 }
13 if ( !ini_get( 'output_handler' ) ) {
14 wfDoContentLength( strlen( $s ) );
15 }
16 }
17 return $s;
18 }
19
20 /**
21 * Get the "file extension" that some client apps will estimate from
22 * the currently-requested URL.
23 * This isn't on WebRequest because we need it when things aren't initialized
24 * @private
25 */
26 function wfRequestExtension() {
27 /// @fixme -- this sort of dupes some code in WebRequest::getRequestUrl()
28 if( isset( $_SERVER['REQUEST_URI'] ) ) {
29 // Strip the query string...
30 list( $path ) = explode( '?', $_SERVER['REQUEST_URI'], 2 );
31 } elseif( isset( $_SERVER['SCRIPT_NAME'] ) ) {
32 // Probably IIS. QUERY_STRING appears separately.
33 $path = $_SERVER['SCRIPT_NAME'];
34 } else {
35 // Can't get the path from the server? :(
36 return '';
37 }
38
39 $period = strrpos( $path, '.' );
40 if( $period !== false ) {
41 return strtolower( substr( $path, $period ) );
42 }
43 return '';
44 }
45
46 /**
47 * Handler that compresses data with gzip if allowed by the Accept header.
48 * Unlike ob_gzhandler, it works for HEAD requests too.
49 */
50 function wfGzipHandler( $s ) {
51 if( !function_exists( 'gzencode' ) || headers_sent() ) {
52 return $s;
53 }
54
55 $ext = wfRequestExtension();
56 if( $ext == '.gz' || $ext == '.tgz' ) {
57 // Don't do gzip compression if the URL path ends in .gz or .tgz
58 // This confuses Safari and triggers a download of the page,
59 // even though it's pretty clearly labeled as viewable HTML.
60 // Bad Safari! Bad!
61 return $s;
62 }
63
64 if( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) {
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
72 // Set vary header if it hasn't been set already
73 $headers = headers_list();
74 $foundVary = false;
75 foreach ( $headers as $header ) {
76 if ( substr( $header, 0, 5 ) == 'Vary:' ) {
77 $foundVary = true;
78 break;
79 }
80 }
81 if ( !$foundVary ) {
82 header( 'Vary: Accept-Encoding' );
83 header( 'X-Vary-Options: Accept-Encoding;list-contains=gzip' );
84 }
85 return $s;
86 }
87
88 /**
89 * Mangle flash policy tags which open up the site to XSS attacks.
90 */
91 function wfMangleFlashPolicy( $s ) {
92 return preg_replace( '/\<\s*cross-domain-policy\s*\>/i', '<NOT-cross-domain-policy>', $s );
93 }
94
95 /**
96 * Add a Content-Length header if possible. This makes it cooperate with squid better.
97 */
98 function wfDoContentLength( $length ) {
99 if ( !headers_sent() && $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0' ) {
100 header( "Content-Length: $length" );
101 }
102 }
103
104