Skip to content

Commit

Permalink
Session: emulates session.cache_limiter & session.cache_expire
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 31, 2019
1 parent b2380e4 commit 8b44821
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/Http/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public function start(): void
}

$this->sendCookie();
$this->sendCachingCookie();

$this->initialize();
}
Expand Down Expand Up @@ -509,4 +510,33 @@ private function sendCookie(): void
$tmp[0], $cookie['domain'], $cookie['secure'], $cookie['httponly'], $cookie['samesite'] ?? $tmp[1] ?? null
);
}


/**
* Sends the cache control HTTP headers.
*/
private function sendCachingCookie(): void
{
$expire = 60 * ini_get('session.cache_expire');
switch (ini_get('session.cache_limiter')) {
case 'public':
$this->response->setHeader('Expires', Helpers::formatDate(time() + $expire));
$this->response->setHeader('Cache-Control', "public, max-age=$expire");
$this->response->setHeader('Last-Modified', Helpers::formatDate(getlastmod()));
return;
case 'private_no_expire':
$this->response->setHeader('Cache-Control', "private, max-age=$expire");
$this->response->setHeader('Last-Modified', Helpers::formatDate(getlastmod()));
return;
case 'private':
$this->response->setHeader('Expires', 'Mon, 23 Jan 1978 10:00:00 GMT');
$this->response->setHeader('Cache-Control', "private, max-age=$expire");
$this->response->setHeader('Last-Modified', Helpers::formatDate(getlastmod()));
return;
case 'nocache':
$this->response->setHeader('Expires', 'Mon, 23 Jan 1978 10:00:00 GMT');
$this->response->setHeader('Cache-Control', 'no-store, no-cache, must-revalidate'); // For HTTP/1.1 conforming clients
$this->response->setHeader('Pragma', 'no-cache'); // For HTTP/1.0 conforming clients
}
}
}
106 changes: 106 additions & 0 deletions tests/Http/Session.cache.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

declare(strict_types=1);

use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


test(function () {
$factory = new Nette\Http\RequestFactory;
$response = new Nette\Http\Response;
$session = new Nette\Http\Session($factory->fromGlobals(), $response);

$session->setOptions([
'cache_limiter' => 'public',
'cache_expire' => '180',
]);
$session->start();

Assert::same(
[
'Set-Cookie' => ['PHPSESSID=' . $session->getId() . '; path=/; HttpOnly'],
'Expires' => [Nette\Http\Helpers::formatDate(time() + 180 * 60)],
'Cache-Control' => ['public, max-age=10800'],
'Last-Modified' => [Nette\Http\Helpers::formatDate(getlastmod())],
],
$response->getHeaders()
);

$session->close();
});


test(function () {
$factory = new Nette\Http\RequestFactory;
$response = new Nette\Http\Response;
$session = new Nette\Http\Session($factory->fromGlobals(), $response);

$session->setOptions([
'cache_limiter' => 'private_no_expire',
'cache_expire' => '180',
]);
$session->start();

Assert::same(
[
'Set-Cookie' => ['PHPSESSID=' . $session->getId() . '; path=/; HttpOnly'],
'Cache-Control' => ['private, max-age=10800'],
'Last-Modified' => [Nette\Http\Helpers::formatDate(getlastmod())],
],
$response->getHeaders()
);

$session->close();
});


test(function () {
$factory = new Nette\Http\RequestFactory;
$response = new Nette\Http\Response;
$session = new Nette\Http\Session($factory->fromGlobals(), $response);

$session->setOptions([
'cache_limiter' => 'private',
'cache_expire' => '180',
]);
$session->start();

Assert::same(
[
'Set-Cookie' => ['PHPSESSID=' . $session->getId() . '; path=/; HttpOnly'],
'Expires' => ['Mon, 23 Jan 1978 10:00:00 GMT'],
'Cache-Control' => ['private, max-age=10800'],
'Last-Modified' => [Nette\Http\Helpers::formatDate(getlastmod())],
],
$response->getHeaders()
);

$session->close();
});


test(function () {
$factory = new Nette\Http\RequestFactory;
$response = new Nette\Http\Response;
$session = new Nette\Http\Session($factory->fromGlobals(), $response);

$session->setOptions([
'cache_limiter' => 'nocache',
]);
$session->start();

Assert::same(
[
'Set-Cookie' => ['PHPSESSID=' . $session->getId() . '; path=/; HttpOnly'],
'Expires' => ['Mon, 23 Jan 1978 10:00:00 GMT'],
'Cache-Control' => ['no-store, no-cache, must-revalidate'],
'Pragma' => ['no-cache'],
],
$response->getHeaders()
);

$session->close();
});

0 comments on commit 8b44821

Please sign in to comment.