Skip to content

Commit 30f246f

Browse files
Find the original request protocol version
1 parent 2183f98 commit 30f246f

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,6 +1535,24 @@ public function isMethodCacheable()
15351535
return in_array($this->getMethod(), array('GET', 'HEAD'));
15361536
}
15371537

1538+
/**
1539+
* Returns the original protocol version.
1540+
*
1541+
* @return string
1542+
*/
1543+
public function getOriginalProtocolVersion()
1544+
{
1545+
if ($this->isFromTrustedProxy()) {
1546+
preg_match('~^(HTTP/)?([1-9].[0-9]) ~', $this->headers->get('Via'), $matches);
1547+
1548+
if ($matches) {
1549+
return 'HTTP/'.$matches[2];
1550+
}
1551+
}
1552+
1553+
return $this->server->get('SERVER_PROTOCOL');
1554+
}
1555+
15381556
/**
15391557
* Returns the request body content.
15401558
*

src/Symfony/Component/HttpFoundation/Tests/RequestTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,6 +2050,36 @@ public function methodCacheableProvider()
20502050
array('CONNECT', false),
20512051
);
20522052
}
2053+
2054+
/**
2055+
* @dataProvider originalProtocolVersionProvider
2056+
*/
2057+
public function testOriginalProtocolVersion($serverProtocol, $trustedProxy, $via, $expected)
2058+
{
2059+
if ($trustedProxy) {
2060+
Request::setTrustedProxies(array('1.1.1.1'));
2061+
}
2062+
2063+
$request = new Request();
2064+
$request->server->set('SERVER_PROTOCOL', $serverProtocol);
2065+
$request->server->set('REMOTE_ADDR', '1.1.1.1');
2066+
$request->headers->set('Via', $via);
2067+
2068+
$this->assertSame($expected, $request->getOriginalProtocolVersion());
2069+
}
2070+
2071+
public function originalProtocolVersionProvider()
2072+
{
2073+
return array(
2074+
'untrusted without via' => array('HTTP/2.0', false, '', 'HTTP/2.0'),
2075+
'untrusted with via' => array('HTTP/2.0', false, '1.0 fred, 1.1 nowhere.com (Apache/1.1)', 'HTTP/2.0'),
2076+
'trusted without via' => array('HTTP/2.0', true, '', 'HTTP/2.0'),
2077+
'trusted with via' => array('HTTP/2.0', true, '1.0 fred, 1.1 nowhere.com (Apache/1.1)', 'HTTP/1.0'),
2078+
'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'),
2079+
'trusted with broken via' => array('HTTP/2.0', true, 'foo', 'HTTP/2.0'),
2080+
'trusted with partially-broken via' => array('HTTP/2.0', true, '1.0 fred, foo', 'HTTP/1.0'),
2081+
);
2082+
}
20532083
}
20542084

20552085
class RequestContentProxy extends Request

0 commit comments

Comments
 (0)