Merge "Added Range support to FileBackend::streamFile()"
authorBrion VIBBER <brion@wikimedia.org>
Tue, 19 Jul 2016 19:20:12 +0000 (19:20 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Tue, 19 Jul 2016 19:20:12 +0000 (19:20 +0000)
1  2 
includes/filerepo/FileRepo.php
includes/libs/MultiHttpClient.php

@@@ -533,10 -533,11 +533,10 @@@ class FileRepo 
        public function findFileFromKey( $sha1, $options = [] ) {
                $time = isset( $options['time'] ) ? $options['time'] : false;
                # First try to find a matching current version of a file...
 -              if ( $this->fileFactoryKey ) {
 -                      $img = call_user_func( $this->fileFactoryKey, $sha1, $this, $time );
 -              } else {
 +              if ( !$this->fileFactoryKey ) {
                        return false; // find-by-sha1 not supported
                }
 +              $img = call_user_func( $this->fileFactoryKey, $sha1, $this, $time );
                if ( $img && $img->exists() ) {
                        return $img;
                }
         *
         * @param string $virtualUrl
         * @param array $headers Additional HTTP headers to send on success
+        * @param array $optHeaders HTTP request headers (if-modified-since, range, ...)
         * @return Status
         * @since 1.27
         */
-       public function streamFileWithStatus( $virtualUrl, $headers = [] ) {
+       public function streamFileWithStatus( $virtualUrl, $headers = [], $optHeaders = [] ) {
                $path = $this->resolveToStoragePath( $virtualUrl );
-               $params = [ 'src' => $path, 'headers' => $headers ];
+               $params = [ 'src' => $path, 'headers' => $headers, 'options' => $optHeaders ];
  
                return $this->backend->streamFile( $params );
        }
@@@ -35,6 -35,8 +35,8 @@@
   *                use application/x-www-form-urlencoded (headers sent automatically)
   *   - stream   : resource to stream the HTTP response body to
   *   - proxy    : HTTP proxy to use
+  *   - flags    : map of boolean flags which supports:
+  *                  - relayResponseHeaders : write out header via header()
   * Request maps can use integer index 0 instead of 'method' and 1 instead of 'url'.
   *
   * @author Aaron Schulz
@@@ -104,7 -106,7 +106,7 @@@ class MultiHttpClient 
         *   - reqTimeout     : post-connection timeout per request (seconds)
         * @return array Response array for request
         */
 -      final public function run( array $req, array $opts = [] ) {
 +      public function run( array $req, array $opts = [] ) {
                return $this->runMulti( [ $req ], $opts )[0]['response'];
        }
  
                                $req['body'] = '';
                                $req['headers']['content-length'] = 0;
                        }
+                       $req['flags'] = isset( $req['flags'] ) ? $req['flags'] : [];
                        $handles[$index] = $this->getCurlHandle( $req, $opts );
                        if ( count( $reqs ) > 1 ) {
                                // https://github.com/guzzle/guzzle/issues/349
  
                curl_setopt( $ch, CURLOPT_HEADERFUNCTION,
                        function ( $ch, $header ) use ( &$req ) {
+                               if ( !empty( $req['flags']['relayResponseHeaders'] ) ) {
+                                       header( $header );
+                               }
                                $length = strlen( $header );
                                $matches = [];
                                if ( preg_match( "/^(HTTP\/1\.[01]) (\d{3}) (.*)/", $header, $matches ) ) {