WordPress.org

Make WordPress Core

Ticket #42560: ms-files.diff

File ms-files.diff, 1.9 KB (added by cg923, 2 years ago)

Diff file for ms-files.php

  • src/wp-includes/ms-files.php

    diff --git src/wp-includes/ms-files.php src/wp-includes/ms-files.php
    index e5c1db3f89..f9bb8004e0 100644
    $etag = '"' . md5( $last_modified ) . '"'; 
    5656header( "Last-Modified: $last_modified GMT" );
    5757header( 'ETag: ' . $etag );
    5858header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + 100000000 ) . ' GMT' );
     59// Note support for range requests.
     60header( 'Accept-Ranges: bytes' );
    5961
    6062// Support for Conditional GET - use stripslashes to avoid formatting.php dependency
    6163$client_etag = isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) ? stripslashes( $_SERVER['HTTP_IF_NONE_MATCH'] ) : false;
    if ( ( $client_last_modified && $client_etag ) 
    7880        exit;
    7981}
    8082
     83// Support for byte-range requests
     84// Safari requires this and will not play mp4 files (and possibly others)
     85// without it.
     86if ( ! empty( $_SERVER['HTTP_RANGE'] ) && preg_match('/^bytes=(\d+)-(\d+)$/i', $_SERVER['HTTP_RANGE'], $m ) ) {
     87        $byte_start = (int) $m[1];
     88        $byte_end = (int) $m[2];
     89        if ( filesize( $file ) - 1 < $byte_end || $byte_end < $byte_start ) {
     90                status_header( 416 );
     91                die( '416 &#8212; Request Range Not Satisfiable.' );
     92        }
     93        status_header( 206 );
     94        header( 'Content-Range: bytes ' . $byte_start . '-' . $byte_end . '/' . filesize( $file ) );
     95        header( 'Content-Length: ' . ($byte_end - $byte_start + 1) );
     96
     97        // Stream the file in 1kb chunks to avoid overloading PHP memory usage for large files.
     98        $handle = fopen( $file, "r" );
     99        fseek( $handle, $byte_start );
     100        $chunk_position = $byte_start;
     101        while ( $chunk_position <= $byte_end && !feof($handle) ) {
     102                $chunk_length = min( 1024, $byte_end - $chunk_position + 1 );
     103                print fread( $handle, $chunk_length );
     104                $chunk_position += 1024;
     105                flush();
     106        }
     107        fclose( $handle );
     108        flush();
     109        exit;
     110}
     111
    81112// If we made it this far, just serve the file
    82113readfile( $file );
    83114flush();