Skip to content

Commit 9ba8659

Browse files
committed
Merge branch 'allow-add-www-prefix'
2 parents b7e6aae + 21876df commit 9ba8659

File tree

8 files changed

+92
-2
lines changed

8 files changed

+92
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Features
2020
- [x] Force Https to specific routes only.
2121
- [x] Keep headers, request method, and request body.
2222
- [x] Enable/disable HTTP Strict Transport Security Header and set its value.
23+
- [x] Allow add "www." prefix during redirection.
2324

2425
Installation
2526
------------
@@ -75,6 +76,7 @@ return [
7576
'enable' => true, // set to false to disable it
7677
'value' => 'max-age=31536000',
7778
],
79+
'add_www_prefix' => false, // set to true to add "www." prefix during redirection
7880
],
7981
// ...
8082
];

config/expressive-force-https-module.local.php.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ return [
1515
'enable' => true, // set to false to disable it
1616
'value' => 'max-age=31536000',
1717
],
18+
'add_www_prefix' => false,
1819
],
1920

2021
'dependencies' => [

config/force-https-module.local.php.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ return [
1313
'enable' => true, // set to false to disable it
1414
'value' => 'max-age=31536000',
1515
],
16+
'add_www_prefix' => false,
1617
],
1718
];

spec/Listener/ForceHttpsSpec.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,37 @@
211211

212212
});
213213

214+
it('redirect with www prefix with configurable "add_www_prefix" on force_all_routes', function () {
215+
216+
$listener = new ForceHttps([
217+
'enable' => true,
218+
'force_all_routes' => true,
219+
'force_specific_routes' => [],
220+
'strict_transport_security' => [
221+
'enable' => true,
222+
'value' => 'max-age=31536000',
223+
],
224+
'add_www_prefix' => true,
225+
]);
226+
227+
allow($this->mvcEvent)->toReceive('getRequest')->andReturn($this->request);
228+
allow($this->request)->toReceive('getUri')->andReturn($this->uri);
229+
allow($this->uri)->toReceive('getScheme')->andReturn('http');
230+
allow($this->mvcEvent)->toReceive('getRouteMatch', 'getMatchedRouteName')->andReturn('about');
231+
allow($this->uri)->toReceive('setScheme')->with('https')->andReturn($this->uri);
232+
allow($this->uri)->toReceive('toString')->andReturn('https://example.com/about');
233+
allow($this->mvcEvent)->toReceive('getResponse')->andReturn($this->response);
234+
allow($this->response)->toReceive('setStatusCode')->with(308)->andReturn($this->response);
235+
allow($this->response)->toReceive('getHeaders', 'addHeaderLine')->with('Location', 'https://example.com/about');
236+
allow($this->response)->toReceive('send');
237+
238+
$listener->forceHttpsScheme($this->mvcEvent);
239+
240+
expect($this->mvcEvent)->toReceive('getResponse');
241+
expect($this->response)->toReceive('getHeaders', 'addHeaderLine')->with('Location', 'https://www.example.com/about');
242+
243+
});
244+
214245
it('not redirect with set strict_transport_security exists and uri already has https scheme', function () {
215246

216247
$listener = new ForceHttps([

spec/Middleware/ForceHttpsSpec.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@
169169

170170
allow($this->router)->toReceive('match')->andReturn($match);
171171
allow($this->request)->toReceive('getUri', 'getScheme')->andReturn('http');
172+
allow($this->request)->toReceive('getUri', 'withScheme', '__toString')->andReturn('https://example.com/about');
172173

173174
allow($this->response)->toReceive('withStatus')->andReturn($this->response);
174175

@@ -187,6 +188,38 @@
187188
$listener->__invoke($this->request, $this->response, function () {});
188189

189190
expect($this->response)->toReceive('withStatus')->with(308);
191+
expect($this->response)->toReceive('withHeader')->with('Location', 'https://example.com/about');
192+
193+
});
194+
195+
it('return Response with 308 status with include www prefix on http and match with configurable "add_www_prefix"', function () {
196+
197+
Console::overrideIsConsole(false);
198+
$match = RouteResult::fromRoute(new Route('/about', 'About'));
199+
200+
allow($this->router)->toReceive('match')->andReturn($match);
201+
allow($this->request)->toReceive('getUri', 'getScheme')->andReturn('http');
202+
allow($this->request)->toReceive('getUri', 'withScheme', '__toString')->andReturn('https://example.com/about');
203+
204+
allow($this->response)->toReceive('withStatus')->andReturn($this->response);
205+
206+
$listener = new ForceHttps(
207+
[
208+
'enable' => true,
209+
'force_all_routes' => true,
210+
'strict_transport_security' => [
211+
'enable' => true,
212+
'value' => 'max-age=31536000',
213+
],
214+
'add_www_prefix' => true,
215+
],
216+
$this->router
217+
);
218+
219+
$listener->__invoke($this->request, $this->response, function () {});
220+
221+
expect($this->response)->toReceive('withStatus')->with(308);
222+
expect($this->response)->toReceive('withHeader')->with('Location', 'https://www.example.com/about');
190223

191224
});
192225

src/HttpsTrait.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,26 @@ private function validateSchemeAndToBeForcedHttpsConfig($uriScheme, $match)
5656

5757
return true;
5858
}
59+
60+
/**
61+
* Add www. prefix when required in the config
62+
*
63+
* @param string $httpsRequestUri
64+
* @return string
65+
*/
66+
private function withWwwPrefixWhenRequired($httpsRequestUri)
67+
{
68+
if (
69+
! isset($this->config['add_www_prefix']) ||
70+
! $this->config['add_www_prefix'] ||
71+
(
72+
$this->config['add_www_prefix'] === true &&
73+
substr($httpsRequestUri, 8, 4) === 'www.'
74+
)
75+
) {
76+
return $httpsRequestUri;
77+
}
78+
79+
return str_replace('https://', 'https://www.', $httpsRequestUri);
80+
}
5981
}

src/Listener/ForceHttps.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function forceHttpsScheme(MvcEvent $e)
9797

9898
/** @var $response \Zend\Http\PhpEnvironment\Response */
9999
$response = $e->getResponse();
100-
$httpsRequestUri = $uri->setScheme('https')->toString();
100+
$httpsRequestUri = $this->withWwwPrefixWhenRequired($uri->setScheme('https')->toString());
101101

102102
// 307 keeps headers, request method, and request body
103103
// \Zend\Http\PhpEnvironment\Response doesn't support 308 yet

src/Middleware/ForceHttps.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
7474
}
7575

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

7979
// 308 keeps headers, request method, and request body
8080
// \Zend\Diactoros\Response already support 308

0 commit comments

Comments
 (0)