Browse Source

Merge pull request #9444 from cakephp/psr7-protocolversion

Implement PSR7 protocolVersion methods.
José Lorenzo Rodríguez 9 years ago
parent
commit
fd101cd471
2 changed files with 93 additions and 0 deletions
  1. 49 0
      src/Network/Request.php
  2. 44 0
      tests/TestCase/Network/RequestTest.php

+ 49 - 0
src/Network/Request.php

@@ -187,6 +187,13 @@ class Request implements ArrayAccess
     protected $uploadedFiles = [];
 
     /**
+     * The HTTP protocol version used.
+     *
+     * @var string|null
+     */
+    protected $protocol;
+
+    /**
      * Wrapper method to create a new request from PHP superglobals.
      *
      * Uses the $_GET, $_POST, $_FILES, $_COOKIE, $_SERVER, $_ENV and php://input data to construct
@@ -1499,6 +1506,48 @@ class Request implements ArrayAccess
     }
 
     /**
+     * Retrieves the HTTP protocol version as a string.
+     *
+     * @return string HTTP protocol version.
+     */
+    public function getProtocolVersion()
+    {
+        if ($this->protocol) {
+            return $this->protocol;
+        }
+
+        // Lazily populate this data as it is generally not used.
+        preg_match('/^HTTP\/([\d.]+)$/', $this->env('SERVER_PROTOCOL'), $match);
+        $protocol = '1.1';
+        if (isset($match[1])) {
+            $protocol = $match[1];
+        }
+        $this->protocol = $protocol;
+
+        return $this->protocol;
+    }
+
+    /**
+     * Return an instance with the specified HTTP protocol version.
+     *
+     * The version string MUST contain only the HTTP version number (e.g.,
+     * "1.1", "1.0").
+     *
+     * @param string $version HTTP protocol version
+     * @return static
+     */
+    public function withProtocolVersion($version)
+    {
+        if (!preg_match('/^(1\.[01]|2)$/', $version)) {
+            throw new InvalidArgumentException("Unsupported protocol version '{$version}' provided");
+        }
+        $new = clone $this;
+        $new->protocol = $version;
+
+        return $new;
+    }
+
+    /**
      * Get/Set value from the request's environment data.
      * Fallback to using env() if key not set in $environment property.
      *

+ 44 - 0
tests/TestCase/Network/RequestTest.php

@@ -906,6 +906,50 @@ class RequestTest extends TestCase
     }
 
     /**
+     * Test getProtocolVersion()
+     *
+     * @return void
+     */
+    public function testGetProtocolVersion()
+    {
+        $request = new Request();
+        $this->assertEquals('1.1', $request->getProtocolVersion());
+
+        // SERVER var.
+        $request = new Request([
+            'environment' => ['SERVER_PROTOCOL' => 'HTTP/1.0']
+        ]);
+        $this->assertEquals('1.0', $request->getProtocolVersion());
+    }
+
+    /**
+     * Test withProtocolVersion()
+     *
+     * @return void
+     */
+    public function testWithProtocolVersion()
+    {
+        $request = new Request();
+        $new = $request->withProtocolVersion('1.0');
+        $this->assertNotSame($new, $request);
+        $this->assertEquals('1.1', $request->getProtocolVersion());
+        $this->assertEquals('1.0', $new->getProtocolVersion());
+    }
+
+    /**
+     * Test withProtocolVersion() and invalid data
+     *
+     * @expectedException InvalidArgumentException
+     * @expectedExceptionMessage Unsupported protocol version 'no good' provided
+     * @return void
+     */
+    public function testWithProtocolVersionInvalid()
+    {
+        $request = new Request();
+        $request->withProtocolVersion('no good');
+    }
+
+    /**
      * Test host retrieval.
      *
      * @return void