Skip to content

Commit cfa0af5

Browse files
authored
Handle OPTIONS cacheable (#74)
* Handle OPTIONS cacheable * Refactor options * Avoid duplicate headers
1 parent 10270c4 commit cfa0af5

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

src/Cors.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,16 @@ public function __construct(HttpKernelInterface $app, array $options = array())
4545

4646
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
4747
{
48-
if ($this->cors->isPreflightRequest($request)) {
49-
return $this->cors->handlePreflightRequest($request);
48+
if ($request->getMethod() === 'OPTIONS') {
49+
if ($this->cors->isPreflightRequest($request)) {
50+
$response = $this->cors->handlePreflightRequest($request);
51+
} else {
52+
$response = $this->app->handle($request, $type, $catch);
53+
}
54+
55+
$this->cors->varyHeader($response, 'Access-Control-Request-Method');
56+
57+
return $response;
5058
}
5159

5260
$response = $this->app->handle($request, $type, $catch);

src/CorsService.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ public function isCorsRequest(Request $request)
6969

7070
public function isPreflightRequest(Request $request)
7171
{
72-
return $this->isCorsRequest($request)
73-
&& $request->getMethod() === 'OPTIONS'
74-
&& $request->headers->has('Access-Control-Request-Method');
72+
return $request->getMethod() === 'OPTIONS' && $request->headers->has('Access-Control-Request-Method');
7573
}
7674

7775
public function handlePreflightRequest(Request $request)
@@ -213,11 +211,11 @@ private function configureMaxAge(Response $response, Request $request)
213211
}
214212
}
215213

216-
private function varyHeader(Response $response, $header)
214+
public function varyHeader(Response $response, $header)
217215
{
218216
if (!$response->headers->has('Vary')) {
219217
$response->headers->set('Vary', $header);
220-
} else {
218+
} elseif (!in_array($header, explode(', ', $response->headers->get('Vary')))) {
221219
$response->headers->set('Vary', $response->headers->get('Vary') . ', ' . $header);
222220
}
223221
}

tests/CorsTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function it_returns_allow_headers_header_on_allow_all_headers_request_cre
105105

106106
$this->assertEquals(204, $response->getStatusCode());
107107
$this->assertEquals('Foo, BAR', $response->headers->get('Access-Control-Allow-Headers'));
108-
$this->assertEquals('Access-Control-Request-Headers', $response->headers->get('Vary'));
108+
$this->assertStringContainsString('Access-Control-Request-Headers', $response->headers->get('Vary'));
109109
}
110110

111111
/**
@@ -304,6 +304,20 @@ public function it_returns_access_control_headers_on_cors_request_with_pattern_o
304304
$this->assertEquals('Origin', $response->headers->get('Vary'));
305305
}
306306

307+
/**
308+
* @test
309+
*/
310+
public function it_adds_vary_headers_on_preflight_non_preflight_options()
311+
{
312+
$app = $this->createStackedApp();
313+
$request = new Request();
314+
$request->setMethod('OPTIONS');
315+
316+
$response = $app->handle($request);
317+
318+
$this->assertEquals('Access-Control-Request-Method', $response->headers->get('Vary'));
319+
}
320+
307321
/**
308322
* @test
309323
*/
@@ -316,6 +330,7 @@ public function it_returns_access_control_headers_on_valid_preflight_request()
316330

317331
$this->assertTrue($response->headers->has('Access-Control-Allow-Origin'));
318332
$this->assertEquals('http://localhost', $response->headers->get('Access-Control-Allow-Origin'));
333+
$this->assertEquals('Access-Control-Request-Method', $response->headers->get('Vary'));
319334
}
320335

321336
/**

0 commit comments

Comments
 (0)