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