Skip to content

Commit ce4c5bc

Browse files
Merge branch '7.4' into 8.0
* 7.4: [Workflow] Add missing `@param` on Transition [HttpFoundation] Fix issue where ServerEvent with "0" data is not sent [HttpClient] Don't store response with authentication headers in shared mode
2 parents 377edfe + cad6482 commit ce4c5bc

File tree

2 files changed

+70
-9
lines changed

2 files changed

+70
-9
lines changed

CachingHttpClient.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -614,16 +614,21 @@ private function isServerResponseCacheable(int $statusCode, array $requestHeader
614614
return false;
615615
}
616616

617-
if (
618-
$this->sharedCache
619-
&& !isset($cacheControl['public']) && !isset($cacheControl['s-maxage']) && !isset($cacheControl['must-revalidate'])
620-
&& isset($requestHeaders['authorization'])
621-
) {
622-
return false;
623-
}
617+
if ($this->sharedCache) {
618+
if (
619+
!isset($cacheControl['public']) && !isset($cacheControl['s-maxage']) && !isset($cacheControl['must-revalidate'])
620+
&& isset($requestHeaders['authorization'])
621+
) {
622+
return false;
623+
}
624624

625-
if ($this->sharedCache && isset($cacheControl['private'])) {
626-
return false;
625+
if (isset($cacheControl['private'])) {
626+
return false;
627+
}
628+
629+
if (isset($responseHeaders['authentication-info']) || isset($responseHeaders['set-cookie']) || isset($responseHeaders['www-authenticate'])) {
630+
return false;
631+
}
627632
}
628633

629634
// Conditionals require an explicit expiration

Tests/CachingHttpClientTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,62 @@ public function testAPrivateCacheStoresAResponseWithPrivateDirective()
473473
self::assertSame('foo', $response->getContent());
474474
}
475475

476+
public function testASharedCacheDoesntStoreAResponseWithAuthenticationHeader()
477+
{
478+
$mockClient = new MockHttpClient([
479+
new MockResponse('foo', [
480+
'http_code' => 200,
481+
'response_headers' => [
482+
'Cache-Control' => 'max-age=300',
483+
'Set-Cookie' => 'foo=bar',
484+
],
485+
]),
486+
new MockResponse('bar'),
487+
]);
488+
489+
$client = new CachingHttpClient(
490+
$mockClient,
491+
$this->cacheAdapter,
492+
sharedCache: true,
493+
);
494+
495+
$response = $client->request('GET', 'http://example.com/foo-bar');
496+
self::assertSame(200, $response->getStatusCode());
497+
self::assertSame('foo', $response->getContent());
498+
499+
$response = $client->request('GET', 'http://example.com/foo-bar');
500+
self::assertSame(200, $response->getStatusCode());
501+
self::assertSame('bar', $response->getContent());
502+
}
503+
504+
public function testAPrivateCacheStoresAResponseWithAuthenticationHeader()
505+
{
506+
$mockClient = new MockHttpClient([
507+
new MockResponse('foo', [
508+
'http_code' => 200,
509+
'response_headers' => [
510+
'Cache-Control' => 'max-age=300',
511+
'Set-Cookie' => 'foo=bar',
512+
],
513+
]),
514+
new MockResponse('should not be served'),
515+
]);
516+
517+
$client = new CachingHttpClient(
518+
$mockClient,
519+
$this->cacheAdapter,
520+
sharedCache: false,
521+
);
522+
523+
$response = $client->request('GET', 'http://example.com/foo-bar');
524+
self::assertSame(200, $response->getStatusCode());
525+
self::assertSame('foo', $response->getContent());
526+
527+
$response = $client->request('GET', 'http://example.com/foo-bar');
528+
self::assertSame(200, $response->getStatusCode());
529+
self::assertSame('foo', $response->getContent());
530+
}
531+
476532
public function testCacheMissAfterInvalidation()
477533
{
478534
$mockClient = new MockHttpClient([

0 commit comments

Comments
 (0)