@@ -182,22 +182,25 @@ public function setContentDisposition(string $disposition, string $filename = ''
182
182
*/
183
183
public function prepare (Request $ request ): static
184
184
{
185
- if (!$ this ->headers ->has ('Content-Type ' )) {
186
- $ this ->headers ->set ('Content-Type ' , $ this ->file ->getMimeType () ?: 'application/octet-stream ' );
187
- }
185
+ parent ::prepare ($ request );
188
186
189
- if ('HTTP/1.0 ' !== $ request ->server ->get ('SERVER_PROTOCOL ' )) {
190
- $ this ->setProtocolVersion ('1.1 ' );
187
+ if ($ this ->isInformational () || $ this ->isEmpty ()) {
188
+ $ this ->maxlen = 0 ;
189
+
190
+ return $ this ;
191
191
}
192
192
193
- $ this ->ensureIEOverSSLCompatibility ($ request );
193
+ if (!$ this ->headers ->has ('Content-Type ' )) {
194
+ $ this ->headers ->set ('Content-Type ' , $ this ->file ->getMimeType () ?: 'application/octet-stream ' );
195
+ }
194
196
195
197
$ this ->offset = 0 ;
196
198
$ this ->maxlen = -1 ;
197
199
198
200
if (false === $ fileSize = $ this ->file ->getSize ()) {
199
201
return $ this ;
200
202
}
203
+ $ this ->headers ->remove ('Transfer-Encoding ' );
201
204
$ this ->headers ->set ('Content-Length ' , $ fileSize );
202
205
203
206
if (!$ this ->headers ->has ('Accept-Ranges ' )) {
@@ -267,6 +270,10 @@ public function prepare(Request $request): static
267
270
}
268
271
}
269
272
273
+ if ($ request ->isMethod ('HEAD ' )) {
274
+ $ this ->maxlen = 0 ;
275
+ }
276
+
270
277
return $ this ;
271
278
}
272
279
@@ -288,40 +295,42 @@ private function hasValidIfRangeHeader(?string $header): bool
288
295
*/
289
296
public function sendContent (): static
290
297
{
291
- if (!$ this ->isSuccessful ()) {
292
- return parent ::sendContent ();
293
- }
298
+ try {
299
+ if (!$ this ->isSuccessful ()) {
300
+ return parent ::sendContent ();
301
+ }
294
302
295
- if (0 === $ this ->maxlen ) {
296
- return $ this ;
297
- }
303
+ if (0 === $ this ->maxlen ) {
304
+ return $ this ;
305
+ }
298
306
299
- $ out = fopen ('php://output ' , 'w ' );
300
- $ file = fopen ($ this ->file ->getPathname (), 'r ' );
307
+ $ out = fopen ('php://output ' , 'w ' );
308
+ $ file = fopen ($ this ->file ->getPathname (), 'r ' );
301
309
302
- ignore_user_abort (true );
310
+ ignore_user_abort (true );
303
311
304
- if (0 !== $ this ->offset ) {
305
- fseek ($ file , $ this ->offset );
306
- }
312
+ if (0 !== $ this ->offset ) {
313
+ fseek ($ file , $ this ->offset );
314
+ }
307
315
308
- $ length = $ this ->maxlen ;
309
- while ($ length && !feof ($ file )) {
310
- $ read = ($ length > $ this ->chunkSize ) ? $ this ->chunkSize : $ length ;
311
- $ length -= $ read ;
316
+ $ length = $ this ->maxlen ;
317
+ while ($ length && !feof ($ file )) {
318
+ $ read = ($ length > $ this ->chunkSize ) ? $ this ->chunkSize : $ length ;
319
+ $ length -= $ read ;
312
320
313
- stream_copy_to_stream ($ file , $ out , $ read );
321
+ stream_copy_to_stream ($ file , $ out , $ read );
314
322
315
- if (connection_aborted ()) {
316
- break ;
323
+ if (connection_aborted ()) {
324
+ break ;
325
+ }
317
326
}
318
- }
319
327
320
- fclose ($ out );
321
- fclose ($ file );
322
-
323
- if ($ this ->deleteFileAfterSend && is_file ($ this ->file ->getPathname ())) {
324
- unlink ($ this ->file ->getPathname ());
328
+ fclose ($ out );
329
+ fclose ($ file );
330
+ } finally {
331
+ if ($ this ->deleteFileAfterSend && is_file ($ this ->file ->getPathname ())) {
332
+ unlink ($ this ->file ->getPathname ());
333
+ }
325
334
}
326
335
327
336
return $ this ;
0 commit comments