Skip to content

Commit

Permalink
add Request::isHttps()
Browse files Browse the repository at this point in the history
  • Loading branch information
sndsgd committed Oct 23, 2016
1 parent 06eaac1 commit 3c6ed42
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 15 deletions.
21 changes: 16 additions & 5 deletions src/http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,21 +144,32 @@ public function getScheme(): string
{
# commonly provided by load balancers
if (isset($this->environment["HTTP_X_FORWARDED_PROTO"])) {
return $this->environment["HTTP_X_FORWARDED_PROTO"];
$scheme = strtolower($this->environment["HTTP_X_FORWARDED_PROTO"]);
if (in_array($scheme, [Scheme::HTTP, Scheme::HTTPS])) {
return $scheme;
}
}

# allow for setting `fastcgi_param HTTPS on;` in nginx config
if (isset($this->environment["HTTPS"])) {
return "https";
return Scheme::HTTPS;
}

# fallback to using the port
$port = $this->environment["SERVER_PORT"] ?? 80;
if ($port == 443) {
return "https";
return Scheme::HTTPS;
}

return "http";
return Scheme::HTTP;
}

/**
* {@inheritdoc}
*/
public function isHttps(): bool
{
return ($this->getScheme() === Scheme::HTTPS);
}

/**
Expand Down Expand Up @@ -299,7 +310,7 @@ public function getBodyParameters(): array
}
}
return $this->bodyParameters;
}
}

/**
* Stubbable method for creating a body decoder
Expand Down
7 changes: 7 additions & 0 deletions src/http/RequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ public function getProtocol(): string;
*/
public function getScheme(): string;

/**
* Determine whether the request was made over an https connection
*
* @return bool
*/
public function isHttps(): bool;

/**
* Retrieve a header
*
Expand Down
9 changes: 9 additions & 0 deletions src/http/Scheme.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace sndsgd\http;

class Scheme
{
const HTTP = "http";
const HTTPS = "https";
}
21 changes: 11 additions & 10 deletions tests/unit/http/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,17 @@ public function testGetScheme($server, $expect)
public function providerGetScheme()
{
return [
[["HTTP_X_FORWARDED_PROTO" => "https"], "https"],
[["HTTP_X_FORWARDED_PROTO" => "http"], "http"],
[["HTTP_X_FORWARDED_PROTO" => "asd"], "asd"],
[["HTTPS" => "on"], "https"],
[["HTTPS" => "whatever"], "https"],
[["SERVER_PORT" => 443], "https"],
[["SERVER_PORT" => "443"], "https"],
[["SERVER_PORT" => 80], "http"],
[["SERVER_PORT" => 42], "http"],
[[], "http"],
[["HTTP_X_FORWARDED_PROTO" => "https"], Scheme::HTTPS],
[["HTTP_X_FORWARDED_PROTO" => "http"], Scheme::HTTP],
[["HTTP_X_FORWARDED_PROTO" => "invalid"], Scheme::HTTP],
[["HTTP_X_FORWARDED_PROTO" => "invalid", "HTTPS" => "on"], Scheme::HTTPS],
[["HTTPS" => "on"], Scheme::HTTPS],
[["HTTPS" => "whatever"], Scheme::HTTPS],
[["SERVER_PORT" => 443], Scheme::HTTPS],
[["SERVER_PORT" => "443"], Scheme::HTTPS],
[["SERVER_PORT" => 80], Scheme::HTTP],
[["SERVER_PORT" => 42], Scheme::HTTP],
[[], Scheme::HTTP],
];
}

Expand Down
15 changes: 15 additions & 0 deletions tests/unit/http/SchemeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace sndsgd\http;

/**
* @coversDefaultClass \sndsgd\http\Scheme
*/
class SchemeTest extends \PHPUnit_Framework_TestCase
{
public function testConstants()
{
$this->assertSame(Scheme::HTTP, "http");
$this->assertSame(Scheme::HTTPS, "https");
}
}

0 comments on commit 3c6ed42

Please sign in to comment.