Skip to content

[HttpCache] fix: do not cache OPTIONS request #20205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Symfony/Component/HttpFoundation/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -1473,6 +1473,16 @@ public function isMethodSafe()
return in_array($this->getMethod(), array('GET', 'HEAD', 'OPTIONS', 'TRACE'));
}

/**
* Checks whether the method is cachaeble or not.
*
* @return bool
*/
public function isMethodCacheable()
{
return in_array($this->getMethod(), array('GET', 'HEAD'));
}

/**
* Returns the request body content.
*
Expand Down
26 changes: 26 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1948,6 +1948,32 @@ public function methodSafeProvider()
array('CONNECT', false),
);
}

/**
* @dataProvider methodCacheableProvider
*/
public function testMethodCacheable($method, $chacheable)
{
$request = new Request();
$request->setMethod($method);
$this->assertEquals($chacheable, $request->isMethodCacheable());
}

public function methodCacheableProvider()
{
return array(
array('HEAD', true),
array('GET', true),
array('POST', false),
array('PUT', false),
array('PATCH', false),
array('DELETE', false),
array('PURGE', false),
array('OPTIONS', false),
array('TRACE', false),
array('CONNECT', false),
);
}
}

class RequestContentProxy extends Request
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ

if (!$request->isMethodSafe()) {
$response = $this->invalidate($request, $catch);
} elseif ($request->headers->has('expect')) {
} elseif ($request->headers->has('expect') || !$request->isMethodCacheable()) {
$response = $this->pass($request, $catch);
} else {
$response = $this->lookup($request, $catch);
Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,21 @@ public function testEsiCacheRemoveValidationHeadersIfEmbeddedResponses()
$this->assertNull($this->response->getETag());
$this->assertNull($this->response->getLastModified());
}

public function testDoesNotCacheOptionsRequest()
{
$this->setNextResponse(200, array('Cache-Control' => 'public, s-maxage=60'), 'get');
$this->request('GET', '/');
$this->assertHttpKernelIsCalled();

$this->setNextResponse(200, array('Cache-Control' => 'public, s-maxage=60'), 'options');
$this->request('OPTIONS', '/');
$this->assertHttpKernelIsCalled();

$this->request('GET', '/');
$this->assertHttpKernelIsNotCalled();
$this->assertSame('get', $this->response->getContent());
}
}

class TestKernel implements HttpKernelInterface
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpKernel/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": ">=5.3.9",
"symfony/event-dispatcher": "~2.6,>=2.6.7",
"symfony/http-foundation": "~2.7.15|~2.8.8",
"symfony/http-foundation": "~2.7.20|~2.8.13",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you have to change this for 2.7.x-dev

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also fails. I guess it has to use my branch?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

~2.7.20|~2.8.13 should be correct

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please note that in that case it is okay that the deps=high tests are failing (because the feature isn't merged to higher branches yet). The deps=low tests must pass though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok good to know 👍 Now I understand. Changed it back.

"symfony/debug": "~2.6,>=2.6.2",
"psr/log": "~1.0"
},
Expand Down