@@ -179,22 +179,25 @@ public function setContentDisposition(string $disposition, string $filename = ''
179179
180180 public function prepare (Request $ request ): static
181181 {
182- if (!$ this ->headers ->has ('Content-Type ' )) {
183- $ this ->headers ->set ('Content-Type ' , $ this ->file ->getMimeType () ?: 'application/octet-stream ' );
184- }
182+ parent ::prepare ($ request );
185183
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 ;
188188 }
189189
190- $ this ->ensureIEOverSSLCompatibility ($ request );
190+ if (!$ this ->headers ->has ('Content-Type ' )) {
191+ $ this ->headers ->set ('Content-Type ' , $ this ->file ->getMimeType () ?: 'application/octet-stream ' );
192+ }
191193
192194 $ this ->offset = 0 ;
193195 $ this ->maxlen = -1 ;
194196
195197 if (false === $ fileSize = $ this ->file ->getSize ()) {
196198 return $ this ;
197199 }
200+ $ this ->headers ->remove ('Transfer-Encoding ' );
198201 $ this ->headers ->set ('Content-Length ' , $ fileSize );
199202
200203 if (!$ this ->headers ->has ('Accept-Ranges ' )) {
@@ -264,6 +267,10 @@ public function prepare(Request $request): static
264267 }
265268 }
266269
270+ if ($ request ->isMethod ('HEAD ' )) {
271+ $ this ->maxlen = 0 ;
272+ }
273+
267274 return $ this ;
268275 }
269276
@@ -282,40 +289,42 @@ private function hasValidIfRangeHeader(?string $header): bool
282289
283290 public function sendContent (): static
284291 {
285- if (!$ this ->isSuccessful ()) {
286- return parent ::sendContent ();
287- }
292+ try {
293+ if (!$ this ->isSuccessful ()) {
294+ return parent ::sendContent ();
295+ }
288296
289- if (0 === $ this ->maxlen ) {
290- return $ this ;
291- }
297+ if (0 === $ this ->maxlen ) {
298+ return $ this ;
299+ }
292300
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 ' );
295303
296- ignore_user_abort (true );
304+ ignore_user_abort (true );
297305
298- if (0 !== $ this ->offset ) {
299- fseek ($ file , $ this ->offset );
300- }
306+ if (0 !== $ this ->offset ) {
307+ fseek ($ file , $ this ->offset );
308+ }
301309
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 ;
306314
307- stream_copy_to_stream ($ file , $ out , $ read );
315+ stream_copy_to_stream ($ file , $ out , $ read );
308316
309- if (connection_aborted ()) {
310- break ;
317+ if (connection_aborted ()) {
318+ break ;
319+ }
311320 }
312- }
313321
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+ }
319328 }
320329
321330 return $ this ;
0 commit comments