Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4f187ce

Browse filesBrowse files
committed
feature #21469 [HttpFoundation] Find the original request protocol version (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
2 parents 7685880 + 5a37f84 commit 4f187ce
Copy full SHA for 4f187ce

File tree

2 files changed

+54
-0
lines changed
Filter options

2 files changed

+54
-0
lines changed

‎src/Symfony/Component/HttpFoundation/Request.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Request.php
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,30 @@ public function isMethodCacheable()
15811581
return in_array($this->getMethod(), array('GET', 'HEAD'));
15821582
}
15831583

1584+
/**
1585+
* Returns the protocol version.
1586+
*
1587+
* If the application is behind a proxy, the protocol version used in the
1588+
* requests between the client and the proxy and between the proxy and the
1589+
* server might be different. This returns the former (from the "Via" header)
1590+
* if the proxy is trusted (see "setTrustedProxies()"), otherwise it returns
1591+
* the latter (from the "SERVER_PROTOCOL" server parameter).
1592+
*
1593+
* @return string
1594+
*/
1595+
public function getProtocolVersion()
1596+
{
1597+
if ($this->isFromTrustedProxy()) {
1598+
preg_match('~^(HTTP/)?([1-9]\.[0-9]) ~', $this->headers->get('Via'), $matches);
1599+
1600+
if ($matches) {
1601+
return 'HTTP/'.$matches[2];
1602+
}
1603+
}
1604+
1605+
return $this->server->get('SERVER_PROTOCOL');
1606+
}
1607+
15841608
/**
15851609
* Returns the request body content.
15861610
*

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
+30Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2188,6 +2188,36 @@ public function testGetTrustedHeaderName()
21882188
Request::setTrustedHeaderName(Request::HEADER_CLIENT_PORT, 'X_FORWARDED_PORT');
21892189
Request::setTrustedHeaderName(Request::HEADER_CLIENT_PROTO, 'X_FORWARDED_PROTO');
21902190
}
2191+
2192+
/**
2193+
* @dataProvider protocolVersionProvider
2194+
*/
2195+
public function testProtocolVersion($serverProtocol, $trustedProxy, $via, $expected)
2196+
{
2197+
if ($trustedProxy) {
2198+
Request::setTrustedProxies(array('1.1.1.1'));
2199+
}
2200+
2201+
$request = new Request();
2202+
$request->server->set('SERVER_PROTOCOL', $serverProtocol);
2203+
$request->server->set('REMOTE_ADDR', '1.1.1.1');
2204+
$request->headers->set('Via', $via);
2205+
2206+
$this->assertSame($expected, $request->getProtocolVersion());
2207+
}
2208+
2209+
public function protocolVersionProvider()
2210+
{
2211+
return array(
2212+
'untrusted without via' => array('HTTP/2.0', false, '', 'HTTP/2.0'),
2213+
'untrusted with via' => array('HTTP/2.0', false, '1.0 fred, 1.1 nowhere.com (Apache/1.1)', 'HTTP/2.0'),
2214+
'trusted without via' => array('HTTP/2.0', true, '', 'HTTP/2.0'),
2215+
'trusted with via' => array('HTTP/2.0', true, '1.0 fred, 1.1 nowhere.com (Apache/1.1)', 'HTTP/1.0'),
2216+
'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'),
2217+
'trusted with broken via' => array('HTTP/2.0', true, 'HTTP/1^0 foo', 'HTTP/2.0'),
2218+
'trusted with partially-broken via' => array('HTTP/2.0', true, '1.0 fred, foo', 'HTTP/1.0'),
2219+
);
2220+
}
21912221
}
21922222

21932223
class RequestContentProxy extends Request

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.