Skip to content

Commit

Permalink
Prevent caching on 302, add wildcard check
Browse files Browse the repository at this point in the history
  • Loading branch information
midwestE authored and midwestE committed Jun 23, 2020
1 parent 208c8d1 commit 4cc16ae
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"php": "^7.2.0",
"psr/http-message": "^1.0",
"slim/psr7": "^1.1",
"slim/http-cache": "^1.0",
"laminas/laminas-httphandlerrunner": "^1.2"
},
"require-dev": {
Expand Down
5 changes: 5 additions & 0 deletions src/RedirectRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ public function setSource(string $source): self
return $this;
}

public function isWildcard(): bool
{
return (strpos($this->source, '*') !== false) ? true : false;
}

public function toArray(): array
{
return \get_object_vars($this);
Expand Down
7 changes: 7 additions & 0 deletions src/RedirectUri.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;
use Slim\HttpCache\Cache;
use Slim\Psr7\Uri;

class RedirectUri extends Uri
Expand Down Expand Up @@ -55,6 +56,12 @@ public function toRedirectResponse(ResponseInterface $response = null): Response
{
$factory = new RedirectResponseFactory();
$response = $factory->createRedirectResponse($this, $response);
if ($response->getStatusCode() == 302) {
$cacheProvider = new \Slim\HttpCache\CacheProvider();
$response = $cacheProvider->denyCache($response);
$response = $response->withHeader('Pragma', 'no-cache');
$response = $cacheProvider->withExpires($response, strtotime('Yesterday'));
}
return $response;
}
}
21 changes: 21 additions & 0 deletions tests/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,27 @@ public function testRedirectSimpleWildcardPath()
$this->assertEquals('https://localhost/wildcard/test?query=string', $result->location);
}

public function testNoCacheOn302()
{
// 'Cache-Control', 'no-store,no-cache'
// 'Pragma', 'no-cache'
// 'Expires', strtotime('Yesterday')
$rule = [
"id" => "1",
"source" => "/",
"type" => "path",
"destination" => "/root",
"httpStatus" => 302,
"active" => 1
];
$result = $this->slimRedirect('https://localhost/?query=string', [$rule]);
$this->assertEquals($rule['httpStatus'], $result->responseStatus);
$this->assertEquals($result->location, 'https://localhost/root?query=string');
$this->assertEquals('no-store,no-cache', $result->response->getHeaderLine('Cache-Control'));
$this->assertEquals('no-cache', $result->response->getHeaderLine('Pragma'));
$this->assertGreaterThanOrEqual(strtotime('Yesterday'), strtotime($result->response->getHeaderLine('Expires')));
}

public function testParseDestination()
{
$rule = [
Expand Down
14 changes: 14 additions & 0 deletions tests/RedirectRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ public function testCreationFactory()
$this->assertInstanceOf(RedirectRule::class, $rule);
}

public function testIsWildcard()
{
$rule = [
"id" => "1",
"source" => "/wild/*/card",
"type" => "path",
"destination" => "/wildcard/*",
"httpStatus" => 301,
"active" => 1
];
$redirectRule = RedirectRule::factory($rule);
$this->assertTrue($redirectRule->isWildcard());
}

public function testToArray()
{
$rule = [
Expand Down

0 comments on commit 4cc16ae

Please sign in to comment.