Skip to content
This repository has been archived by the owner on Dec 20, 2021. It is now read-only.

Commit

Permalink
refactor: add Util\HTTP::getProtocol
Browse files Browse the repository at this point in the history
  • Loading branch information
RETFU committed Nov 23, 2017
1 parent 888d5db commit 775c725
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 28 deletions.
26 changes: 2 additions & 24 deletions src/RREST.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use RREST\Validator\ContentTypeValidator;
use RREST\Validator\JsonValidator;
use RREST\Validator\ProtocolValidator;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use RREST\Util\HTTP;

/**
* ApiSpec + Router = RREST.
Expand Down Expand Up @@ -127,7 +127,7 @@ public function addRoute()

//protocol
$protocolValidator = new ProtocolValidator(
$this->getProtocol(),
HTTP::getProtocol(),
$this->apiSpec->getProtocols()
);
if($protocolValidator->fails()) {
Expand Down Expand Up @@ -371,28 +371,6 @@ protected function hintHTTPPayloadBody($hintedPayloadBody)
$this->router->setPayloadBodyValue($hintedPayloadBody);
}

/**
* Return the protocol (http or https) used.
*
* @return string
*/
public function getProtocol()
{
$isSecure = false;
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
$isSecure = true;
} elseif (
!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
$_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' ||
!empty($_SERVER['HTTP_X_FORWARDED_SSL']) &&
$_SERVER['HTTP_X_FORWARDED_SSL'] == 'on'
) {
$isSecure = true;
}

return $isSecure ? 'HTTPS' : 'HTTP';
}

/**
* @param string $mimeType
* @param string[] $availableMimeTypes
Expand Down
29 changes: 29 additions & 0 deletions src/Util/HTTP.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php


namespace RREST\Util;

class HTTP
{
/**
* Return the protocol (http or https) used.
*
* @return string
*/
public static function getProtocol()
{
$isSecure = false;
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
$isSecure = true;
} elseif (
!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
$_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' ||
!empty($_SERVER['HTTP_X_FORWARDED_SSL']) &&
$_SERVER['HTTP_X_FORWARDED_SSL'] == 'on'
) {
$isSecure = true;
}

return $isSecure ? 'HTTPS' : 'HTTP';
}
}
9 changes: 5 additions & 4 deletions tests/units/RREST.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Silex\Application;
use RREST\Router\Silex;
use RREST\APISpec\RAML;
use RREST\Util\HTTP;

class RREST extends atoum
{
Expand Down Expand Up @@ -283,21 +284,21 @@ public function testGetProtocol()

$this
->given($this->newTestedInstance($apiSpec, $router))
->string($this->testedInstance->getProtocol())
->string(hTTP::getProtocol())
->isEqualTo('HTTP')
;

$_SERVER['HTTPS'] = true;
$this
->given($this->newTestedInstance($apiSpec, $router))
->string($this->testedInstance->getProtocol())
->string(hTTP::getProtocol())
->isEqualTo('HTTPS')
;

$_SERVER['HTTPS'] = 'on';
$this
->given($this->newTestedInstance($apiSpec, $router))
->string($this->testedInstance->getProtocol())
->string(hTTP::getProtocol())
->isEqualTo('HTTPS')
;

Expand All @@ -306,7 +307,7 @@ public function testGetProtocol()
$_SERVER['HTTP_X_FORWARDED_SSL'] = 'on';
$this
->given($this->newTestedInstance($apiSpec, $router))
->string($this->testedInstance->getProtocol())
->string(hTTP::getProtocol())
->isEqualTo('HTTPS')
;
}
Expand Down

0 comments on commit 775c725

Please sign in to comment.