@@ -182,22 +182,25 @@ public function setContentDisposition(string $disposition, string $filename = ''
182182 */
183183 public function prepare (Request $ request ): static
184184 {
185- if (!$ this ->headers ->has ('Content-Type ' )) {
186- $ this ->headers ->set ('Content-Type ' , $ this ->file ->getMimeType () ?: 'application/octet-stream ' );
187- }
185+ parent ::prepare ($ request );
188186
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 ;
191191 }
192192
193- $ this ->ensureIEOverSSLCompatibility ($ request );
193+ if (!$ this ->headers ->has ('Content-Type ' )) {
194+ $ this ->headers ->set ('Content-Type ' , $ this ->file ->getMimeType () ?: 'application/octet-stream ' );
195+ }
194196
195197 $ this ->offset = 0 ;
196198 $ this ->maxlen = -1 ;
197199
198200 if (false === $ fileSize = $ this ->file ->getSize ()) {
199201 return $ this ;
200202 }
203+ $ this ->headers ->remove ('Transfer-Encoding ' );
201204 $ this ->headers ->set ('Content-Length ' , $ fileSize );
202205
203206 if (!$ this ->headers ->has ('Accept-Ranges ' )) {
@@ -267,6 +270,10 @@ public function prepare(Request $request): static
267270 }
268271 }
269272
273+ if ($ request ->isMethod ('HEAD ' )) {
274+ $ this ->maxlen = 0 ;
275+ }
276+
270277 return $ this ;
271278 }
272279
@@ -288,40 +295,42 @@ private function hasValidIfRangeHeader(?string $header): bool
288295 */
289296 public function sendContent (): static
290297 {
291- if (!$ this ->isSuccessful ()) {
292- return parent ::sendContent ();
293- }
298+ try {
299+ if (!$ this ->isSuccessful ()) {
300+ return parent ::sendContent ();
301+ }
294302
295- if (0 === $ this ->maxlen ) {
296- return $ this ;
297- }
303+ if (0 === $ this ->maxlen ) {
304+ return $ this ;
305+ }
298306
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 ' );
301309
302- ignore_user_abort (true );
310+ ignore_user_abort (true );
303311
304- if (0 !== $ this ->offset ) {
305- fseek ($ file , $ this ->offset );
306- }
312+ if (0 !== $ this ->offset ) {
313+ fseek ($ file , $ this ->offset );
314+ }
307315
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 ;
312320
313- stream_copy_to_stream ($ file , $ out , $ read );
321+ stream_copy_to_stream ($ file , $ out , $ read );
314322
315- if (connection_aborted ()) {
316- break ;
323+ if (connection_aborted ()) {
324+ break ;
325+ }
317326 }
318- }
319327
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+ }
325334 }
326335
327336 return $ this ;
0 commit comments