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