Skip to content

Commit

Permalink
feature #21469 [HttpFoundation] Find the original request protocol ve…
Browse files Browse the repository at this point in the history
…rsion (thewilkybarkid)

This PR was submitted for the master branch but it was merged into the 3.4 branch instead (closes #21469).

Discussion
----------

[HttpFoundation] Find the original request protocol version

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #21415
| License       | MIT
| Doc PR        |

Adds a new method to `Request` to find the original protocol version from the `Via` header, if the request is from a trusted proxy.

Commits
-------

5a37f84 [HttpFoundation] Find the original request protocol version
  • Loading branch information
fabpot committed Jul 6, 2017
2 parents 7685880 + 5a37f84 commit 4f187ce
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Symfony/Component/HttpFoundation/Request.php
Expand Up @@ -1581,6 +1581,30 @@ public function isMethodCacheable()
return in_array($this->getMethod(), array('GET', 'HEAD'));
}

/**
* Returns the protocol version.
*
* If the application is behind a proxy, the protocol version used in the
* requests between the client and the proxy and between the proxy and the
* server might be different. This returns the former (from the "Via" header)
* if the proxy is trusted (see "setTrustedProxies()"), otherwise it returns
* the latter (from the "SERVER_PROTOCOL" server parameter).
*
* @return string
*/
public function getProtocolVersion()
{
if ($this->isFromTrustedProxy()) {
preg_match('~^(HTTP/)?([1-9]\.[0-9]) ~', $this->headers->get('Via'), $matches);

if ($matches) {
return 'HTTP/'.$matches[2];
}
}

return $this->server->get('SERVER_PROTOCOL');
}

/**
* Returns the request body content.
*
Expand Down
30 changes: 30 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
Expand Up @@ -2188,6 +2188,36 @@ public function testGetTrustedHeaderName()
Request::setTrustedHeaderName(Request::HEADER_CLIENT_PORT, 'X_FORWARDED_PORT');
Request::setTrustedHeaderName(Request::HEADER_CLIENT_PROTO, 'X_FORWARDED_PROTO');
}

/**
* @dataProvider protocolVersionProvider
*/
public function testProtocolVersion($serverProtocol, $trustedProxy, $via, $expected)
{
if ($trustedProxy) {
Request::setTrustedProxies(array('1.1.1.1'));
}

$request = new Request();
$request->server->set('SERVER_PROTOCOL', $serverProtocol);
$request->server->set('REMOTE_ADDR', '1.1.1.1');
$request->headers->set('Via', $via);

$this->assertSame($expected, $request->getProtocolVersion());
}

public function protocolVersionProvider()
{
return array(
'untrusted without via' => array('HTTP/2.0', false, '', 'HTTP/2.0'),
'untrusted with via' => array('HTTP/2.0', false, '1.0 fred, 1.1 nowhere.com (Apache/1.1)', 'HTTP/2.0'),
'trusted without via' => array('HTTP/2.0', true, '', 'HTTP/2.0'),
'trusted with via' => array('HTTP/2.0', true, '1.0 fred, 1.1 nowhere.com (Apache/1.1)', 'HTTP/1.0'),
'trusted with via and protocol name' => array('HTTP/2.0', true, 'HTTP/1.0 fred, HTTP/1.1 nowhere.com (Apache/1.1)', 'HTTP/1.0'),
'trusted with broken via' => array('HTTP/2.0', true, 'HTTP/1^0 foo', 'HTTP/2.0'),
'trusted with partially-broken via' => array('HTTP/2.0', true, '1.0 fred, foo', 'HTTP/1.0'),
);
}
}

class RequestContentProxy extends Request
Expand Down

0 comments on commit 4f187ce

Please sign in to comment.