Skip to content

Commit 03c8da2

Browse files
committed
Merge branch 'lpouwelse-change/11-prevent-multiple-option-requests'
2 parents 347e147 + e346ac5 commit 03c8da2

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
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",
6-
"version": "100.0.4",
6+
"version": "100.0.5",
77
"license": [
88
"OSL-3.0",
99
"AFL-3.0"

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 Request Max Age</label>
31+
<comment>Enables Access-Control-Max-Age response header for CORS requests (max age in seconds)</comment>
32+
</field>
2833
</group>
2934
</section>
3035
</system>

0 commit comments

Comments
 (0)