MediaWiki master
HTTPFileStreamer.php
Go to the documentation of this file.
1<?php
23use Wikimedia\AtEase\AtEase;
24use Wikimedia\Timestamp\ConvertibleTimestamp;
25
33 protected $path;
35 protected $obResetFunc;
37 protected $streamMimeFunc;
39 protected $headerFunc;
40
41 // Do not send any HTTP headers (i.e. body only)
42 public const STREAM_HEADLESS = 1;
43 // Do not try to tear down any PHP output buffers
44 public const STREAM_ALLOW_OB = 2;
45
53 public static function preprocessHeaders( $headers ) {
54 $rawHeaders = [];
55 $optHeaders = [];
56 foreach ( $headers as $name => $header ) {
57 $nameLower = strtolower( $name );
58 if ( in_array( $nameLower, [ 'range', 'if-modified-since' ], true ) ) {
59 $optHeaders[$nameLower] = $header;
60 } else {
61 $rawHeaders[] = "$name: $header";
62 }
63 }
64 return [ $rawHeaders, $optHeaders ];
65 }
66
74 public function __construct( $path, array $params = [] ) {
75 $this->path = $path;
76
77 $this->obResetFunc = $params['obResetFunc'] ??
78 [ __CLASS__, 'resetOutputBuffers' ];
79
80 $this->streamMimeFunc = $params['streamMimeFunc'] ??
81 [ __CLASS__, 'contentTypeFromPath' ];
82
83 $this->headerFunc = $params['headerFunc'] ?? 'header';
84 }
85
97 public function stream(
98 $headers = [], $sendErrors = true, $optHeaders = [], $flags = 0
99 ) {
100 $headless = ( $flags & self::STREAM_HEADLESS );
101
102 // Don't stream it out as text/html if there was a PHP error
103 if ( $headers && headers_sent() ) {
104 echo "Headers already sent, terminating.\n";
105 return false;
106 }
107
108 $headerFunc = $headless
109 ? static function ( $header ) {
110 // no-op
111 }
112 : [ $this, 'header' ];
113
114 AtEase::suppressWarnings();
115 $info = stat( $this->path );
116 AtEase::restoreWarnings();
117
118 if ( !is_array( $info ) ) {
119 if ( $sendErrors ) {
120 self::send404Message( $this->path, $flags );
121 }
122 return false;
123 }
124
125 // Send Last-Modified HTTP header for client-side caching
126 $mtimeCT = new ConvertibleTimestamp( $info['mtime'] );
127 $headerFunc( 'Last-Modified: ' . $mtimeCT->getTimestamp( TS_RFC2822 ) );
128
129 if ( ( $flags & self::STREAM_ALLOW_OB ) == 0 ) {
130 call_user_func( $this->obResetFunc );
131 }
132
133 $type = call_user_func( $this->streamMimeFunc, $this->path );
134 if ( $type && $type != 'unknown/unknown' ) {
135 $headerFunc( "Content-type: $type" );
136 } else {
137 // Send a content type which is not known to Internet Explorer, to
138 // avoid triggering IE's content type detection. Sending a standard
139 // unknown content type here essentially gives IE license to apply
140 // whatever content type it likes.
141 $headerFunc( 'Content-type: application/x-wiki' );
142 }
143
144 // Don't send if client has up to date cache
145 if ( isset( $optHeaders['if-modified-since'] ) ) {
146 $modsince = preg_replace( '/;.*$/', '', $optHeaders['if-modified-since'] );
147 if ( $mtimeCT->getTimestamp( TS_UNIX ) <= strtotime( $modsince ) ) {
148 ini_set( 'zlib.output_compression', 0 );
149 $headerFunc( 304 );
150 return true; // ok
151 }
152 }
153
154 // Send additional headers
155 foreach ( $headers as $header ) {
156 $headerFunc( $header );
157 }
158
159 if ( isset( $optHeaders['range'] ) ) {
160 $range = self::parseRange( $optHeaders['range'], $info['size'] );
161 if ( is_array( $range ) ) {
162 $headerFunc( 206 );
163 $headerFunc( 'Content-Length: ' . $range[2] );
164 $headerFunc( "Content-Range: bytes {$range[0]}-{$range[1]}/{$info['size']}" );
165 } elseif ( $range === 'invalid' ) {
166 if ( $sendErrors ) {
167 $headerFunc( 416 );
168 $headerFunc( 'Cache-Control: no-cache' );
169 $headerFunc( 'Content-Type: text/html; charset=utf-8' );
170 $headerFunc( 'Content-Range: bytes */' . $info['size'] );
171 }
172 return false;
173 } else { // unsupported Range request (e.g. multiple ranges)
174 $range = null;
175 $headerFunc( 'Content-Length: ' . $info['size'] );
176 }
177 } else {
178 $range = null;
179 $headerFunc( 'Content-Length: ' . $info['size'] );
180 }
181
182 if ( is_array( $range ) ) {
183 $handle = fopen( $this->path, 'rb' );
184 if ( $handle ) {
185 $ok = true;
186 fseek( $handle, $range[0] );
187 $remaining = $range[2];
188 while ( $remaining > 0 && $ok ) {
189 $bytes = min( $remaining, 8 * 1024 );
190 $data = fread( $handle, $bytes );
191 $remaining -= $bytes;
192 $ok = ( $data !== false );
193 print $data;
194 }
195 } else {
196 return false;
197 }
198 } else {
199 return readfile( $this->path ) !== false; // faster
200 }
201
202 return true;
203 }
204
212 public static function send404Message( $fname, $flags = 0 ) {
213 if ( ( $flags & self::STREAM_HEADLESS ) == 0 ) {
214 HttpStatus::header( 404 );
215 header( 'Cache-Control: no-cache' );
216 header( 'Content-Type: text/html; charset=utf-8' );
217 }
218 $encFile = htmlspecialchars( $fname );
219 $encScript = htmlspecialchars( $_SERVER['SCRIPT_NAME'] );
220 echo "<!DOCTYPE html><html><body>
221 <h1>File not found</h1>
222 <p>Although this PHP script ($encScript) exists, the file requested for output
223 ($encFile) does not.</p>
224 </body></html>
225 ";
226 }
227
236 public static function parseRange( $range, $size ) {
237 $m = [];
238 if ( preg_match( '#^bytes=(\d*)-(\d*)$#', $range, $m ) ) {
239 [ , $start, $end ] = $m;
240 if ( $start === '' && $end === '' ) {
241 $absRange = [ 0, $size - 1 ];
242 } elseif ( $start === '' ) {
243 $absRange = [ $size - (int)$end, $size - 1 ];
244 } elseif ( $end === '' ) {
245 $absRange = [ (int)$start, $size - 1 ];
246 } else {
247 $absRange = [ (int)$start, (int)$end ];
248 }
249 if ( $absRange[0] >= 0 && $absRange[1] >= $absRange[0] ) {
250 if ( $absRange[0] < $size ) {
251 $absRange[1] = min( $absRange[1], $size - 1 ); // stop at EOF
252 $absRange[2] = $absRange[1] - $absRange[0] + 1;
253 return $absRange;
254 } elseif ( $absRange[0] == 0 && $size == 0 ) {
255 return 'unrecognized'; // the whole file should just be sent
256 }
257 }
258 return 'invalid';
259 }
260 return 'unrecognized';
261 }
262
263 protected static function resetOutputBuffers() {
264 while ( ob_get_status() ) {
265 if ( !ob_end_clean() ) {
266 // Could not remove output buffer handler; abort now
267 // to avoid getting in some kind of infinite loop.
268 break;
269 }
270 }
271 }
272
279 protected static function contentTypeFromPath( $filename ) {
280 $ext = strrchr( $filename, '.' );
281 $ext = $ext ? strtolower( substr( $ext, 1 ) ) : '';
282
283 switch ( $ext ) {
284 case 'gif':
285 return 'image/gif';
286 case 'png':
287 return 'image/png';
288 case 'jpg':
289 return 'image/jpeg';
290 case 'jpeg':
291 return 'image/jpeg';
292 }
293
294 return 'unknown/unknown';
295 }
296
297 private function header( $header ) {
298 if ( is_int( $header ) ) {
299 $header = HttpStatus::getHeader( $header );
300 }
301
302 ( $this->headerFunc )( $header );
303 }
304}
array $params
The job parameters.
Functions related to the output of file content.
static preprocessHeaders( $headers)
Takes HTTP headers in a name => value format and converts them to the weird format expected by stream...
static send404Message( $fname, $flags=0)
Send out a standard 404 message for a file.
static contentTypeFromPath( $filename)
Determine the file type of a file based on the path.
static parseRange( $range, $size)
Convert a Range header value to an absolute (start, end) range tuple.
__construct( $path, array $params=[])
stream( $headers=[], $sendErrors=true, $optHeaders=[], $flags=0)
Stream a file to the browser, adding all the headings and fun stuff.
$header