Skip to content

Commit 2c120fd

Browse files
authored
[Bug]: Cors headers not set when cache was not enabled (#925)
* Fix: Cors header where set only when cache enabled * Apply php-cs-fixer changes --------- Co-authored-by: mcop1 <mcop1@users.noreply.github.com>
1 parent f88cb83 commit 2c120fd

File tree

4 files changed

+85
-34
lines changed

4 files changed

+85
-34
lines changed

src/Controller/WebserviceController.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use Pimcore\Bundle\DataHubBundle\Service\CheckConsumerPermissionsService;
3434
use Pimcore\Bundle\DataHubBundle\Service\FileUploadService;
3535
use Pimcore\Bundle\DataHubBundle\Service\OutputCacheService;
36+
use Pimcore\Bundle\DataHubBundle\Service\ResponseServiceInterface;
3637
use Pimcore\Cache\RuntimeCache;
3738
use Pimcore\Controller\FrontendController;
3839
use Pimcore\Helper\LongRunningHelper;
@@ -90,7 +91,8 @@ public function webonyxAction(
9091
LocaleServiceInterface $localeService,
9192
Factory $modelFactory,
9293
Request $request,
93-
LongRunningHelper $longRunningHelper
94+
LongRunningHelper $longRunningHelper,
95+
ResponseServiceInterface $responseService
9496
) {
9597
$clientname = $request->attributes->getString('clientname');
9698
$variableValues = null;
@@ -107,6 +109,8 @@ public function webonyxAction(
107109
if ($response = $this->cacheService->load($request)) {
108110
Logger::debug('Loading response from cache');
109111

112+
$responseService->addCorsHeaders($response);
113+
110114
return $response;
111115
}
112116

@@ -226,7 +230,10 @@ public function webonyxAction(
226230
}
227231

228232
$response = new JsonResponse($output);
233+
234+
$responseService->removeCorsHeaders($response);
229235
$this->cacheService->save($request, $response);
236+
$responseService->addCorsHeaders($response);
230237

231238
return $response;
232239
}

src/Service/OutputCacheService.php

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,7 @@ public function load(Request $request)
7171

7272
$cacheKey = $this->computeKey($request);
7373

74-
$response = $this->loadFromCache($cacheKey);
75-
if ($response) {
76-
$this->addCorsHeaders($response);
77-
}
78-
79-
return $response;
74+
return $this->loadFromCache($cacheKey);
8075
}
8176

8277
/**
@@ -89,42 +84,15 @@ public function save(Request $request, JsonResponse $response, $extraTags = []):
8984
$clientname = $request->attributes->getString('clientname');
9085
$extraTags = array_merge(['output', 'datahub', $clientname], $extraTags);
9186

92-
$this->removeCorsHeaders($response);
9387
$cacheKey = $this->computeKey($request);
9488

9589
$event = new OutputCachePreSaveEvent($request, $response);
9690
$this->eventDispatcher->dispatch($event, OutputCacheEvents::PRE_SAVE);
9791

9892
$this->saveToCache($cacheKey, $response, $extraTags);
99-
100-
$this->addCorsHeaders($response);
10193
}
10294
}
10395

104-
/**
105-
* Removes CORS headers including Access-Control-Allow-Origin that should not be cached.
106-
*/
107-
protected function removeCorsHeaders(JsonResponse $response): void
108-
{
109-
$response->headers->remove('Access-Control-Allow-Origin');
110-
$response->headers->remove('Access-Control-Allow-Credentials');
111-
$response->headers->remove('Access-Control-Allow-Methods');
112-
$response->headers->remove('Access-Control-Allow-Headers');
113-
}
114-
115-
protected function addCorsHeaders(JsonResponse $response): void
116-
{
117-
$origin = '*';
118-
if (!empty($_SERVER['HTTP_ORIGIN'])) {
119-
$origin = $_SERVER['HTTP_ORIGIN'];
120-
}
121-
122-
$response->headers->set('Access-Control-Allow-Origin', $origin);
123-
$response->headers->set('Access-Control-Allow-Credentials', 'true');
124-
$response->headers->set('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
125-
$response->headers->set('Access-Control-Allow-Headers', 'Origin, Content-Type, X-Auth-Token');
126-
}
127-
12896
/**
12997
* @param string $key
13098
*

src/Service/ResponseService.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Pimcore
7+
*
8+
* This source file is available under two different licenses:
9+
* - GNU General Public License version 3 (GPLv3)
10+
* - Pimcore Commercial License (PCL)
11+
* Full copyright and license information is available in
12+
* LICENSE.md which is distributed with this source code.
13+
*
14+
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
15+
* @license http://www.pimcore.org/license GPLv3 and PCL
16+
*/
17+
18+
namespace Pimcore\Bundle\DataHubBundle\Service;
19+
20+
use Symfony\Component\HttpFoundation\JsonResponse;
21+
22+
/** @internal */
23+
final class ResponseService implements ResponseServiceInterface
24+
{
25+
/**
26+
* Removes CORS headers including Access-Control-Allow-Origin that should not be cached.
27+
*/
28+
public function removeCorsHeaders(JsonResponse $response): void
29+
{
30+
$response->headers->remove('Access-Control-Allow-Origin');
31+
$response->headers->remove('Access-Control-Allow-Credentials');
32+
$response->headers->remove('Access-Control-Allow-Methods');
33+
$response->headers->remove('Access-Control-Allow-Headers');
34+
}
35+
36+
public function addCorsHeaders(JsonResponse $response): void
37+
{
38+
$origin = '*';
39+
if (!empty($_SERVER['HTTP_ORIGIN'])) {
40+
$origin = $_SERVER['HTTP_ORIGIN'];
41+
}
42+
43+
$response->headers->set('Access-Control-Allow-Origin', $origin);
44+
$response->headers->set('Access-Control-Allow-Credentials', 'true');
45+
$response->headers->set('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
46+
$response->headers->set('Access-Control-Allow-Headers', 'Origin, Content-Type, X-Auth-Token');
47+
}
48+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Pimcore
7+
*
8+
* This source file is available under two different licenses:
9+
* - GNU General Public License version 3 (GPLv3)
10+
* - Pimcore Commercial License (PCL)
11+
* Full copyright and license information is available in
12+
* LICENSE.md which is distributed with this source code.
13+
*
14+
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
15+
* @license http://www.pimcore.org/license GPLv3 and PCL
16+
*/
17+
18+
namespace Pimcore\Bundle\DataHubBundle\Service;
19+
20+
use Symfony\Component\HttpFoundation\JsonResponse;
21+
22+
/** @internal */
23+
interface ResponseServiceInterface
24+
{
25+
public function removeCorsHeaders(JsonResponse $response): void;
26+
27+
public function addCorsHeaders(JsonResponse $response): void;
28+
}

0 commit comments

Comments
 (0)