Skip to content

Commit

Permalink
Issue #29: Update URI Scheme detection (#32)
Browse files Browse the repository at this point in the history
* Update SCHEME detection.

* Update tests accordingly.

* Add more tests for the new HTTPS related keys.

* add support for `X-Forwarded-Proto` header

* Revert "Add more tests for the new HTTPS related keys."

This reverts commit 9e37276.

* revert test change

* Added change log
  • Loading branch information
drupol authored and Nyholm committed Nov 5, 2019
1 parent f1fa9e8 commit e6a526e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 0.4.1

### Fixed

- Support for ´HTTP_X_FORWARDED_PROTO´

## 0.4.0

### Fixed
Expand Down
18 changes: 11 additions & 7 deletions src/ServerRequestCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,18 @@ private function createUriFromArray(array $server): UriInterface
{
$uri = $this->uriFactory->createUri('');

if (isset($server['REQUEST_SCHEME'])) {
$uri = $uri->withScheme($server['REQUEST_SCHEME']);
} elseif (isset($server['HTTPS'])) {
$uri = $uri->withScheme('on' === $server['HTTPS'] ? 'https' : 'http');
}
if (isset($server['HTTP_X_FORWARDED_PROTO'])) {
$uri = $uri->withScheme($server['HTTP_X_FORWARDED_PROTO']);
} else {
if (isset($server['REQUEST_SCHEME'])) {
$uri = $uri->withScheme($server['REQUEST_SCHEME']);
} elseif (isset($server['HTTPS'])) {
$uri = $uri->withScheme('on' === $server['HTTPS'] ? 'https' : 'http');
}

if (isset($server['SERVER_PORT'])) {
$uri = $uri->withPort($server['SERVER_PORT']);
if (isset($server['SERVER_PORT'])) {
$uri = $uri->withPort($server['SERVER_PORT']);
}
}

if (isset($server['HTTP_HOST'])) {
Expand Down
4 changes: 4 additions & 0 deletions tests/ServerRequestCreatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,10 @@ public function dataGetUriFromGlobals()
'https://www.blakesimpson.co.uk/blog/article.php?id=10&user=foo',
array_merge($server, ['HTTPS' => 'on', 'SERVER_PORT' => '443']),
],
'Secure request via proxy' => [
'https://www.blakesimpson.co.uk/blog/article.php?id=10&user=foo',
array_merge($server, ['HTTP_X_FORWARDED_PROTO' => 'https', 'SERVER_PORT' => '80']),
],
'HTTP_HOST missing' => [
'http://www.blakesimpson.co.uk/blog/article.php?id=10&user=foo',
array_merge($server, ['HTTP_HOST' => null]),
Expand Down

0 comments on commit e6a526e

Please # to comment.