-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Cors header where set only when cache enabled
- Loading branch information
Showing
4 changed files
with
91 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license GPLv3 and PCL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\DataHubBundle\Service; | ||
|
||
use Pimcore\Bundle\DataHubBundle\Event\GraphQL\Model\OutputCachePreLoadEvent; | ||
use Pimcore\Bundle\DataHubBundle\Event\GraphQL\Model\OutputCachePreSaveEvent; | ||
use Pimcore\Bundle\DataHubBundle\Event\GraphQL\OutputCacheEvents; | ||
use Pimcore\Logger; | ||
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** @internal */ | ||
final class ResponseService implements ResponseServiceInterface | ||
{ | ||
/** | ||
* Removes CORS headers including Access-Control-Allow-Origin that should not be cached. | ||
*/ | ||
public function removeCorsHeaders(JsonResponse $response): void | ||
{ | ||
$response->headers->remove('Access-Control-Allow-Origin'); | ||
$response->headers->remove('Access-Control-Allow-Credentials'); | ||
$response->headers->remove('Access-Control-Allow-Methods'); | ||
$response->headers->remove('Access-Control-Allow-Headers'); | ||
} | ||
|
||
public function addCorsHeaders(JsonResponse $response): void | ||
{ | ||
$origin = '*'; | ||
if (!empty($_SERVER['HTTP_ORIGIN'])) { | ||
$origin = $_SERVER['HTTP_ORIGIN']; | ||
} | ||
|
||
$response->headers->set('Access-Control-Allow-Origin', $origin); | ||
$response->headers->set('Access-Control-Allow-Credentials', 'true'); | ||
$response->headers->set('Access-Control-Allow-Methods', 'GET, POST, OPTIONS'); | ||
$response->headers->set('Access-Control-Allow-Headers', 'Origin, Content-Type, X-Auth-Token'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license GPLv3 and PCL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\DataHubBundle\Service; | ||
|
||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
|
||
/** @internal */ | ||
interface ResponseServiceInterface | ||
{ | ||
public function removeCorsHeaders(JsonResponse $response): void; | ||
public function addCorsHeaders(JsonResponse $response): void; | ||
} |