Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge fa2fe07 into caebdb4
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Jul 19, 2018
2 parents caebdb4 + fa2fe07 commit 51fe709
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
12 changes: 9 additions & 3 deletions src/functions/marshal_uri_from_sapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,15 @@ function marshalUriFromSapi(array $server, array $headers)

// URI scheme
$scheme = 'http';
$https = array_key_exists('HTTPS', $server) ? $server['HTTPS'] : false;
if (($https && 'off' !== $https)
|| $getHeaderFromArray('x-forwarded-proto', $headers, false) === 'https'
if (array_key_exists('HTTPS', $server)) {
$https = $server['HTTPS'];
} elseif (array_key_exists('https', $server)) {
$https = $server['https'];
} else {
$https = false;
}
if (($https && 'off' !== strtolower($https))
|| strtolower($getHeaderFromArray('x-forwarded-proto', $headers, false)) === 'https'
) {
$scheme = 'https';
}
Expand Down
51 changes: 45 additions & 6 deletions test/ServerRequestFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,14 +306,29 @@ public function testMarshalHostAndPortWillDetectPortInIpv6StyleHost()
$this->assertNull($uri->getPort());
}

public function testMarshalUriDetectsHttpsSchemeFromServerValue()
/**
* @return array
*/
public function httpsParamProvider()
{
return [
'lowercase' => ['https'],
'uppercase' => ['HTTPS'],
];
}

/**
* @dataProvider httpsParamProvider
* @param string $param
*/
public function testMarshalUriDetectsHttpsSchemeFromServerValue($param)
{
$request = new ServerRequest();
$request = $request->withUri(new Uri('http://example.com/'));
$request = $request->withHeader('Host', 'example.com');

$server = [
'HTTPS' => true,
$param => true,
];

$uri = marshalUriFromSapi($server, $request->getHeaders());
Expand All @@ -322,14 +337,34 @@ public function testMarshalUriDetectsHttpsSchemeFromServerValue()
$this->assertSame('https', $uri->getScheme());
}

public function testMarshalUriUsesHttpSchemeIfHttpsServerValueEqualsOff()
/**
* @return iterable
*/
public function httpsDisableParamProvider()
{
foreach ($this->httpsParamProvider() as $key => $data) {
$param = array_shift($data);
foreach (['lowercase-off', 'uppercase-off'] as $type) {
$key = sprintf('%s-%s', $key, $type);
$value = false !== strpos($type, 'lowercase') ? 'off' : 'OFF';
yield $key => [$param, $value];
}
}
}

/**
* @dataProvider httpsDisableParamProvider
* @param string $param
* @param string $value
*/
public function testMarshalUriUsesHttpSchemeIfHttpsServerValueEqualsOff($param, $value)
{
$request = new ServerRequest();
$request = $request->withUri(new Uri('http://example.com/'));
$request = $request->withHeader('Host', 'example.com');

$server = [
'HTTPS' => 'off',
$param => $value,
];

$uri = marshalUriFromSapi($server, $request->getHeaders());
Expand All @@ -338,12 +373,16 @@ public function testMarshalUriUsesHttpSchemeIfHttpsServerValueEqualsOff()
$this->assertSame('http', $uri->getScheme());
}

public function testMarshalUriDetectsHttpsSchemeFromXForwardedProtoValue()
/**
* @dataProvider httpsParamProvider
* @param string $xForwardedProto
*/
public function testMarshalUriDetectsHttpsSchemeFromXForwardedProtoValue($xForwardedProto)
{
$request = new ServerRequest();
$request = $request->withUri(new Uri('http://example.com/'));
$request = $request->withHeader('Host', 'example.com');
$request = $request->withHeader('X-Forwarded-Proto', 'https');
$request = $request->withHeader('X-Forwarded-Proto', $xForwardedProto);

$server = [];

Expand Down

0 comments on commit 51fe709

Please sign in to comment.