@@ -201,26 +201,25 @@ public function setContentDisposition($disposition, $filename = '', $filenameFal
201
201
*/
202
202
public function prepare (Request $ request )
203
203
{
204
- if (\in_array ($ this ->getStatusCode (), [self ::HTTP_NO_CONTENT , self ::HTTP_NOT_MODIFIED ])) {
204
+ parent ::prepare ($ request );
205
+
206
+ if ($ this ->isInformational () || $ this ->isEmpty ()) {
207
+ $ this ->maxlen = 0 ;
208
+
205
209
return $ this ;
206
210
}
207
211
208
212
if (!$ this ->headers ->has ('Content-Type ' )) {
209
213
$ this ->headers ->set ('Content-Type ' , $ this ->file ->getMimeType () ?: 'application/octet-stream ' );
210
214
}
211
215
212
- if ('HTTP/1.0 ' !== $ request ->server ->get ('SERVER_PROTOCOL ' )) {
213
- $ this ->setProtocolVersion ('1.1 ' );
214
- }
215
-
216
- $ this ->ensureIEOverSSLCompatibility ($ request );
217
-
218
216
$ this ->offset = 0 ;
219
217
$ this ->maxlen = -1 ;
220
218
221
219
if (false === $ fileSize = $ this ->file ->getSize ()) {
222
220
return $ this ;
223
221
}
222
+ $ this ->headers ->remove ('Transfer-Encoding ' );
224
223
$ this ->headers ->set ('Content-Length ' , $ fileSize );
225
224
226
225
if (!$ this ->headers ->has ('Accept-Ranges ' )) {
@@ -290,6 +289,10 @@ public function prepare(Request $request)
290
289
}
291
290
}
292
291
292
+ if ($ request ->isMethod ('HEAD ' )) {
293
+ $ this ->maxlen = 0 ;
294
+ }
295
+
293
296
return $ this ;
294
297
}
295
298
@@ -313,40 +316,42 @@ private function hasValidIfRangeHeader(?string $header): bool
313
316
*/
314
317
public function sendContent ()
315
318
{
316
- if (!$ this ->isSuccessful ()) {
317
- return parent ::sendContent ();
318
- }
319
+ try {
320
+ if (!$ this ->isSuccessful ()) {
321
+ return parent ::sendContent ();
322
+ }
319
323
320
- if (0 === $ this ->maxlen ) {
321
- return $ this ;
322
- }
324
+ if (0 === $ this ->maxlen ) {
325
+ return $ this ;
326
+ }
323
327
324
- $ out = fopen ('php://output ' , 'w ' );
325
- $ file = fopen ($ this ->file ->getPathname (), 'r ' );
328
+ $ out = fopen ('php://output ' , 'w ' );
329
+ $ file = fopen ($ this ->file ->getPathname (), 'r ' );
326
330
327
- ignore_user_abort (true );
331
+ ignore_user_abort (true );
328
332
329
- if (0 !== $ this ->offset ) {
330
- fseek ($ file , $ this ->offset );
331
- }
333
+ if (0 !== $ this ->offset ) {
334
+ fseek ($ file , $ this ->offset );
335
+ }
332
336
333
- $ length = $ this ->maxlen ;
334
- while ($ length && !feof ($ file )) {
335
- $ read = ($ length > $ this ->chunkSize ) ? $ this ->chunkSize : $ length ;
336
- $ length -= $ read ;
337
+ $ length = $ this ->maxlen ;
338
+ while ($ length && !feof ($ file )) {
339
+ $ read = ($ length > $ this ->chunkSize ) ? $ this ->chunkSize : $ length ;
340
+ $ length -= $ read ;
337
341
338
- stream_copy_to_stream ($ file , $ out , $ read );
342
+ stream_copy_to_stream ($ file , $ out , $ read );
339
343
340
- if (connection_aborted ()) {
341
- break ;
344
+ if (connection_aborted ()) {
345
+ break ;
346
+ }
342
347
}
343
- }
344
348
345
- fclose ($ out );
346
- fclose ($ file );
347
-
348
- if ($ this ->deleteFileAfterSend && file_exists ($ this ->file ->getPathname ())) {
349
- unlink ($ this ->file ->getPathname ());
349
+ fclose ($ out );
350
+ fclose ($ file );
351
+ } finally {
352
+ if ($ this ->deleteFileAfterSend && file_exists ($ this ->file ->getPathname ())) {
353
+ unlink ($ this ->file ->getPathname ());
354
+ }
350
355
}
351
356
352
357
return $ this ;
0 commit comments