Skip to content

Commit

Permalink
add www. prefix for already https as well when previous uri doesnot has
Browse files Browse the repository at this point in the history
www. prefix for configured add_www_prefix
  • Loading branch information
samsonasik committed Oct 11, 2017
1 parent 0dbdd29 commit ec92719
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
17 changes: 0 additions & 17 deletions src/HttpsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,6 @@ private function isSkippedHttpStrictTransportSecurity($uriScheme, $match, $respo
);
}

/**
* Validate Scheme and Forced Https Config
*
* @param string $uriScheme
* @param RouteMatch|RouteResult $match
*
* @return bool
*/
private function validateSchemeAndToBeForcedHttpsConfig($uriScheme, $match)
{
if ($this->isSchemeHttps($uriScheme) || ! $this->isGoingToBeForcedToHttps($match)) {
return false;
}

return true;
}

/**
* Add www. prefix when required in the config
*
Expand Down
10 changes: 9 additions & 1 deletion src/Listener/ForceHttps.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,18 @@ public function forceHttpsScheme(MvcEvent $e)

$routeMatch = $e->getRouteMatch();
$response = $this->setHttpStrictTransportSecurity($uriScheme, $routeMatch, $response);
if (! $this->validateSchemeAndToBeForcedHttpsConfig($uriScheme, $routeMatch)) {
if (! $this->isGoingToBeForcedToHttps($routeMatch)) {
return;
}

if ($this->isSchemeHttps($uriScheme)) {
$uriString = $uri->toString();
$httpsRequestUri = $this->withWwwPrefixWhenRequired($uriString);
if ($uriString === $httpsRequestUri) {
return;
}
}

$httpsRequestUri = $this->withWwwPrefixWhenRequired($uri->setScheme('https')->toString());

// 307 keeps headers, request method, and request body
Expand Down
10 changes: 9 additions & 1 deletion src/Middleware/ForceHttps.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,18 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
$uriScheme = $uri->getScheme();

$response = $this->setHttpStrictTransportSecurity($uriScheme, $match, $response);
if (! $this->validateSchemeAndToBeForcedHttpsConfig($uriScheme, $match)) {
if (! $this->isGoingToBeForcedToHttps($match)) {
return $next($request, $response);
}

if ($this->isSchemeHttps($uriScheme)) {
$uriString = $uri->__toString();
$httpsRequestUri = $this->withWwwPrefixWhenRequired($uriString);
if ($uriString === $httpsRequestUri) {
return $next($request, $response);
}
}

$newUri = $uri->withScheme('https');
$httpsRequestUri = $this->withWwwPrefixWhenRequired($newUri->__toString());

Expand Down

0 comments on commit ec92719

Please sign in to comment.