MediaWiki master
OutputHandler.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Output;
24
28
43 public static function handle( $s, $phase ) {
44 $config = MediaWikiServices::getInstance()->getMainConfig();
45 $disableOutputCompression = $config->get( MainConfigNames::DisableOutputCompression );
46 // Don't send headers if output is being discarded (T278579)
47 if ( ( $phase & PHP_OUTPUT_HANDLER_CLEAN ) === PHP_OUTPUT_HANDLER_CLEAN ) {
48 $logger = LoggerFactory::getInstance( 'output' );
49 $logger->debug( __METHOD__ . " entrypoint={entry}; size={size}; phase=$phase", [
50 'entry' => MW_ENTRY_POINT,
51 'size' => strlen( $s ),
52 ] );
53
54 return $s;
55 }
56
57 // Check if a compression output buffer is already enabled via php.ini. Such
58 // buffers exists at the start of the request and are reflected by ob_get_level().
59 $phpHandlesCompression = (
60 ini_get( 'output_handler' ) === 'ob_gzhandler' ||
61 ini_get( 'zlib.output_handler' ) === 'ob_gzhandler' ||
62 !in_array(
63 strtolower( ini_get( 'zlib.output_compression' ) ),
64 [ '', 'off', '0' ]
65 )
66 );
67
68 if (
69 // Compression is not already handled by an internal PHP buffer
70 !$phpHandlesCompression &&
71 // Compression is not disabled by the application entry point
72 !defined( 'MW_NO_OUTPUT_COMPRESSION' ) &&
73 // Compression is not disabled by site configuration
74 !$disableOutputCompression
75 ) {
76 $s = self::handleGzip( $s );
77 }
78
79 if (
80 // Response body length does not depend on internal PHP compression buffer
81 !$phpHandlesCompression &&
82 // Response body length does not depend on mangling by a custom buffer
83 !ini_get( 'output_handler' ) &&
84 !ini_get( 'zlib.output_handler' )
85 ) {
86 self::emitContentLength( strlen( $s ) );
87 }
88
89 return $s;
90 }
91
102 private static function findUriExtension() {
103 // @todo FIXME: this sort of dupes some code in WebRequest::getRequestUrl()
104 if ( isset( $_SERVER['REQUEST_URI'] ) ) {
105 // Strip the query string...
106 $path = explode( '?', $_SERVER['REQUEST_URI'], 2 )[0];
107 } elseif ( isset( $_SERVER['SCRIPT_NAME'] ) ) {
108 // Probably IIS. QUERY_STRING appears separately.
109 $path = $_SERVER['SCRIPT_NAME'];
110 } else {
111 // Can't get the path from the server? :(
112 return '';
113 }
114
115 $period = strrpos( $path, '.' );
116 if ( $period !== false ) {
117 return strtolower( substr( $path, $period ) );
118 }
119 return '';
120 }
121
132 private static function handleGzip( $s ) {
133 if ( !function_exists( 'gzencode' ) ) {
134 wfDebug( __METHOD__ . "() skipping compression (gzencode unavailable)" );
135 return $s;
136 }
137 if ( headers_sent() ) {
138 wfDebug( __METHOD__ . "() skipping compression (headers already sent)" );
139 return $s;
140 }
141
142 $ext = self::findUriExtension();
143 if ( $ext == '.gz' || $ext == '.tgz' ) {
144 // Don't do gzip compression if the URL path ends in .gz or .tgz
145 // This confuses Safari and triggers a download of the page,
146 // even though it's pretty clearly labeled as viewable HTML.
147 // Bad Safari! Bad!
148 return $s;
149 }
150
151 if ( $s === '' ) {
152 // Do not gzip empty HTTP responses since that would not only bloat the body
153 // length, but it would result in invalid HTTP responses when the HTTP status code
154 // is one that must not be accompanied by a body (e.g. "204 No Content").
155 return $s;
156 }
157
158 if ( wfClientAcceptsGzip() ) {
159 wfDebug( __METHOD__ . "() is compressing output" );
160 header( 'Content-Encoding: gzip' );
161 $s = gzencode( $s, 6 );
162 }
163
164 // Set vary header if it hasn't been set already
165 $headers = headers_list();
166 $foundVary = false;
167 foreach ( $headers as $header ) {
168 $headerName = strtolower( substr( $header, 0, 5 ) );
169 if ( $headerName == 'vary:' ) {
170 $foundVary = true;
171 break;
172 }
173 }
174 if ( !$foundVary ) {
175 header( 'Vary: Accept-Encoding' );
176 }
177 return $s;
178 }
179
195 private static function emitContentLength( $length ) {
196 if ( headers_sent() ) {
197 wfDebug( __METHOD__ . "() headers already sent" );
198 return;
199 }
200
201 if (
202 in_array( http_response_code(), [ 200, 404 ], true ) ||
203 ( $_SERVER['SERVER_PROTOCOL'] ?? null ) === 'HTTP/1.0'
204 ) {
205 header( "Content-Length: $length" );
206 }
207 }
208}
209
211class_alias( OutputHandler::class, 'MediaWiki\\OutputHandler' );
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfClientAcceptsGzip( $force=false)
Whether the client accept gzip encoding.
const MW_ENTRY_POINT
Definition api.php:35
Create PSR-3 logger objects.
A class containing constants representing the names of configuration variables.
const DisableOutputCompression
Name constant for the DisableOutputCompression setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
static handle( $s, $phase)
Standard output handler for use with ob_start.
$header