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