Add helper for HTTPFileStreamer header syntax
authorGergő Tisza <tgr.huwiki@gmail.com>
Sat, 20 Apr 2019 00:12:59 +0000 (17:12 -0700)
committerJforrester <jforrester@wikimedia.org>
Wed, 21 Aug 2019 16:21:23 +0000 (16:21 +0000)
Adds a helper function for transforming an intuitive header array
to the peculiar syntax expected by HTTPFileStreamer and the related
FileRepo/FileBackend streaming methods.

Change-Id: Idac9281b0f1b3c93f4ec1d1c3f336db110e5d260
(cherry picked from commit 65648f5523c9d1b772106e16e2adf57870892bc7)

includes/libs/filebackend/HTTPFileStreamer.php
tests/phpunit/includes/filebackend/HTTPFileStreamerTest.php [new file with mode: 0644]

index 9f8959c..e77d4d4 100644 (file)
@@ -39,6 +39,27 @@ class HTTPFileStreamer {
        // Do not try to tear down any PHP output buffers
        const STREAM_ALLOW_OB = 2;
 
+       /**
+        * Takes HTTP headers in a name => value format and converts them to the weird format
+        * expected by stream().
+        * @param string[] $headers
+        * @return array[] [ $headers, $optHeaders ]
+        * @since 1.34
+        */
+       public static function preprocessHeaders( $headers ) {
+               $rawHeaders = [];
+               $optHeaders = [];
+               foreach ( $headers as $name => $header ) {
+                       $nameLower = strtolower( $name );
+                       if ( in_array( $nameLower, [ 'range', 'if-modified-since' ], true ) ) {
+                               $optHeaders[$nameLower] = $header;
+                       } else {
+                               $rawHeaders[] = "$name: $header";
+                       }
+               }
+               return [ $rawHeaders, $optHeaders ];
+       }
+
        /**
         * @param string $path Local filesystem path to a file
         * @param array $params Options map, which includes:
diff --git a/tests/phpunit/includes/filebackend/HTTPFileStreamerTest.php b/tests/phpunit/includes/filebackend/HTTPFileStreamerTest.php
new file mode 100644 (file)
index 0000000..bb025b6
--- /dev/null
@@ -0,0 +1,36 @@
+<?php
+
+use PHPUnit\Framework\TestCase;
+
+class HTTPFileStreamerTest extends TestCase {
+
+       /**
+        * @covers HTTPFileStreamer::preprocessHeaders
+        * @dataProvider providePreprocessHeaders
+        */
+       public function testPreprocessHeaders( array $input, array $expectedRaw, array $expectedOpt ) {
+               list( $actualRaw, $actualOpt ) = HTTPFileStreamer::preprocessHeaders( $input );
+               $this->assertSame( $expectedRaw, $actualRaw );
+               $this->assertSame( $expectedOpt, $actualOpt );
+       }
+
+       public function providePreprocessHeaders() {
+               return [
+                       [
+                               [ 'Vary' => 'cookie', 'Cache-Control' => 'private' ],
+                               [ 'Vary: cookie', 'Cache-Control: private' ],
+                               [],
+                       ],
+                       [
+                               [
+                                       'Range' => 'bytes=(123-456)',
+                                       'Content-Type' => 'video/mp4',
+                                       'If-Modified-Since' => 'Wed, 21 Oct 2015 07:28:00 GMT',
+                               ],
+                               [ 'Content-Type: video/mp4' ],
+                               [ 'range' => 'bytes=(123-456)', 'if-modified-since' => 'Wed, 21 Oct 2015 07:28:00 GMT' ],
+                       ],
+               ];
+       }
+
+}