Skip to content

Commit 400533c

Browse files
cache: handle private separately (#58)
Don't send just a `Cache-Control: public` header if caching isn't configured.
1 parent 8f8a352 commit 400533c

5 files changed

Lines changed: 24 additions & 6 deletions

File tree

src/Cache/CachePublic.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
{
1010
public function tokenize(Tokenizer $tokenizer): Tokenizer
1111
{
12-
if (!$tokenizer->public) {
12+
if ($tokenizer->private) {
1313
return $tokenizer;
1414
}
1515

src/Cache/Control/Tokenizer.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ public function __construct(
4040
/**
4141
* @var bool indicates that any caches of any kind (public or shared) should not store this response.
4242
*/
43-
public bool $public = true,
43+
public bool $public = false,
44+
45+
/**
46+
* @var bool indicates that a public cache should not cache this
47+
*/
48+
public bool $private = false,
4449

4550
/**
4651
* @var bool indicates that the response will not be updated while it's fresh.
@@ -68,6 +73,7 @@ public function with(
6873
bool|null $proxyRevalidate = null,
6974
bool|null $noStore = null,
7075
bool|null $public = null,
76+
bool|null $private = null,
7177
bool|null $immutable = null,
7278
int|null|false $staleWhileRevalidating = false,
7379
int|null|false $staleIfError = false,
@@ -80,6 +86,7 @@ public function with(
8086
proxyRevalidate: $this->withBool($this->proxyRevalidate, $proxyRevalidate),
8187
noStore: $this->withBool($this->noStore, $noStore),
8288
public: $this->withBool($this->public, $public),
89+
private: $this->withBool($this->private, $private),
8390
immutable: $this->withBool($this->immutable, $immutable),
8491
staleWhileRevalidating: $this->withInt($this->staleWhileRevalidating, $staleWhileRevalidating),
8592
staleIfError: $this->withInt($this->staleIfError, $staleIfError),
@@ -99,7 +106,8 @@ private function withBool(bool $original, bool|null $var): bool
99106
public function render(): string
100107
{
101108
$header = [
102-
$this->public ? 'public' : 'private',
109+
...$this->header($this->private, 'private'),
110+
...$this->header($this->public, 'public'),
103111
...$this->header($this->maxAge, "max-age=$this->maxAge"),
104112
...$this->header($this->sMaxAge, "s-maxage=$this->sMaxAge"),
105113
...$this->header($this->noCache, "no-cache"),

src/Cache/NeverCache.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ public function tokenize(Tokenizer $tokenizer): Tokenizer
1818
proxyRevalidate: false,
1919
noStore: true,
2020
public: false,
21+
private: true,
2122
immutable: false,
2223
staleWhileRevalidating: null,
23-
staleIfError: null
24+
staleIfError: null,
2425
);
2526
}
2627
}

src/Cache/UserSpecific.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
{
1313
public function tokenize(Tokenizer $tokenizer): Tokenizer
1414
{
15-
return $tokenizer->with(public: false);
15+
return $tokenizer->with(public: false, private: true);
1616
}
1717
}

tests/CacheTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ function render(Tokenizer $tokenizer, AbstractCache ...$directives): string
2121

2222
it('can describe a simple public cache', function () {
2323
$tokenizer = new Tokenizer();
24-
expect(render($tokenizer))->toBe('public');
24+
$directives[] = new CachePublic();
25+
expect(render($tokenizer, ...$directives))->toBe('public');
2526
});
2627

2728
it('can handle max age rules', function () {
2829
$tokenizer = new Tokenizer();
2930

3031
$directives[] = new NeverChanges();
32+
$directives[] = new CachePublic();
3133
expect(render($tokenizer, ...$directives))->toBe("public max-age=604800 immutable");
3234

3335
$directives[] = new MaxAge(3000);
@@ -54,6 +56,7 @@ function render(Tokenizer $tokenizer, AbstractCache ...$directives): string
5456
$tokenizer = new Tokenizer();
5557

5658
$directives[] = new NeverChanges();
59+
$directives[] = new CachePublic();
5760
expect(render($tokenizer, ...$directives))->toBe("public max-age=604800 immutable");
5861
$directives[] = new Revalidate(RevalidationEnum::AfterError, 300);
5962
expect(render($tokenizer, ...$directives))->toBe("public stale-if-error=300");
@@ -96,3 +99,9 @@ function render(Tokenizer $tokenizer, AbstractCache ...$directives): string
9699
$directives[] = new CachePublic();
97100
expect(render($tokenizer, ...$directives))->toBe("private");
98101
});
102+
103+
it('renders nothing when empty', function() {
104+
$tokenizer = new Tokenizer();
105+
106+
expect(render($tokenizer))->toBe('');
107+
});

0 commit comments

Comments
 (0)