@@ -206,22 +206,25 @@ public function setContentDisposition(string $disposition, string $filename = ''
206206 */
207207 public function prepare (Request $ request )
208208 {
209- if (!$ this ->headers ->has ('Content-Type ' )) {
210- $ this ->headers ->set ('Content-Type ' , $ this ->file ->getMimeType () ?: 'application/octet-stream ' );
211- }
209+ parent ::prepare ($ request );
212210
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 ;
215215 }
216216
217- $ this ->ensureIEOverSSLCompatibility ($ request );
217+ if (!$ this ->headers ->has ('Content-Type ' )) {
218+ $ this ->headers ->set ('Content-Type ' , $ this ->file ->getMimeType () ?: 'application/octet-stream ' );
219+ }
218220
219221 $ this ->offset = 0 ;
220222 $ this ->maxlen = -1 ;
221223
222224 if (false === $ fileSize = $ this ->file ->getSize ()) {
223225 return $ this ;
224226 }
227+ $ this ->headers ->remove ('Transfer-Encoding ' );
225228 $ this ->headers ->set ('Content-Length ' , $ fileSize );
226229
227230 if (!$ this ->headers ->has ('Accept-Ranges ' )) {
@@ -291,6 +294,10 @@ public function prepare(Request $request)
291294 }
292295 }
293296
297+ if ($ request ->isMethod ('HEAD ' )) {
298+ $ this ->maxlen = 0 ;
299+ }
300+
294301 return $ this ;
295302 }
296303
@@ -312,40 +319,42 @@ private function hasValidIfRangeHeader(?string $header): bool
312319 */
313320 public function sendContent ()
314321 {
315- if (!$ this ->isSuccessful ()) {
316- return parent ::sendContent ();
317- }
322+ try {
323+ if (!$ this ->isSuccessful ()) {
324+ return parent ::sendContent ();
325+ }
318326
319- if (0 === $ this ->maxlen ) {
320- return $ this ;
321- }
327+ if (0 === $ this ->maxlen ) {
328+ return $ this ;
329+ }
322330
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 ' );
325333
326- ignore_user_abort (true );
334+ ignore_user_abort (true );
327335
328- if (0 !== $ this ->offset ) {
329- fseek ($ file , $ this ->offset );
330- }
336+ if (0 !== $ this ->offset ) {
337+ fseek ($ file , $ this ->offset );
338+ }
331339
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 ;
336344
337- stream_copy_to_stream ($ file , $ out , $ read );
345+ stream_copy_to_stream ($ file , $ out , $ read );
338346
339- if (connection_aborted ()) {
340- break ;
347+ if (connection_aborted ()) {
348+ break ;
349+ }
341350 }
342- }
343351
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+ }
349358 }
350359
351360 return $ this ;
0 commit comments