Skip to content

Commit e8f4b06

Browse files
committed
Merge branch 'change/11-prevent-multiple-option-requests' of https://github.com/lpouwelse/magento-2-cors-requests into lpouwelse-change/11-prevent-multiple-option-requests
2 parents 347e147 + a1046e5 commit e8f4b06

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

Plugin/CorsHeadersPlugin.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ protected function getEnableAmp()
7171
\Magento\Store\Model\ScopeInterface::SCOPE_STORE);
7272
}
7373

74+
/**
75+
* Get the Access-Control-Max-Age
76+
* @return string
77+
*/
78+
protected function getMaxAge()
79+
{
80+
return (int) $this->scopeConfig->getValue('web/corsRequests/max_age',
81+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE);
82+
}
83+
7484
/**
7585
* Triggers before original dispatch
7686
* This method triggers before original \Magento\Webapi\Controller\Rest::dispatch and set version
@@ -92,6 +102,9 @@ public function beforeDispatch(
92102
if ($this->getEnableAmp()) {
93103
$this->response->setHeader('AMP-Access-Control-Allow-Source-Origin', rtrim($originUrl,"/"), true);
94104
}
105+
if ((int)$this->getMaxAge() > 0) {
106+
$this->response->setHeader('Access-Control-Max-Age', $this->getMaxAge(), true);
107+
}
95108
}
96109
}
97110

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "splashlab/magento-2-cors-requests",
2+
"name": "lpouwelse/magento-2-cors-requests",
33
"description": "Enabling cross-origin resource sharing (CORS) requests to Magento 2 API from configured Origin domain",
44
"homepage": "https://github.com/splashlab/magento-2-cors-requests",
55
"type": "magento2-module",

etc/adminhtml/system.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
2626
<comment>Enables AMP-Access-Control-Allow-Source-Origin response header for AMP CORS requests</comment>
2727
</field>
28+
<field id="max_age" translate="label" type="text" sortOrder="40" showInDefault="1" showInWebsite="1"
29+
showInStore="1">
30+
<label>CORS Control Max age</label>
31+
<comment>Enables Access-Control-Max-Age response header for AMP CORS requests (max age in seconds)</comment>
32+
</field>
2833
</group>
2934
</section>
3035
</system>

0 commit comments

Comments
 (0)