Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
6b834337
Commit
6b834337
authored
Jul 03, 2014
by
Philippe Gaultier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move methods
parent
6493cb87
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
47 deletions
+49
-47
Cors.php
framework/filters/Cors.php
+49
-47
No files found.
framework/filters/Cors.php
View file @
6b834337
...
...
@@ -90,7 +90,7 @@ class Cors extends ActionFilter
{
$this
->
request
=
Yii
::
$app
->
getRequest
();
$this
->
response
=
Yii
::
$app
->
getResponse
();
$this
->
overrideSettings
(
$action
);
$this
->
override
Default
Settings
(
$action
);
$requestCorsHeaders
=
$this
->
extractHeaders
(
$this
->
request
);
$responseCorsHeaders
=
$this
->
prepareHeaders
(
$requestCorsHeaders
);
...
...
@@ -100,10 +100,10 @@ class Cors extends ActionFilter
}
/**
* Override settings for
current
action
* Override settings for
specific
action
* @param \yii\base\Action $action the action settings to override
*/
public
function
overrideSettings
(
$action
)
public
function
override
Default
Settings
(
$action
)
{
if
(
isset
(
$this
->
actions
[
$action
->
id
]))
{
$actionParams
=
$this
->
actions
[
$action
->
id
];
...
...
@@ -135,6 +135,37 @@ class Cors extends ActionFilter
}
/**
* For each CORS headers create the specific response
* @param array $requestHeaders CORS headers we have detected
* @return array CORS headers ready to be sent
*/
public
function
prepareHeaders
(
$requestHeaders
)
{
$responseHeaders
=
[];
// handle Origin
if
(
isset
(
$requestHeaders
[
'Origin'
]))
{
if
((
in_array
(
'*'
,
$this
->
cors
[
'Origin'
])
===
true
)
||
(
in_array
(
$requestHeaders
[
'Origin'
],
$this
->
cors
[
'Origin'
])))
{
$responseHeaders
[
'Access-Control-Allow-Origin'
]
=
$requestHeaders
[
'Origin'
];
}
}
$this
->
prepareAllowHeaders
(
'Method'
,
$requestHeaders
,
$responseHeaders
);
$this
->
prepareAllowHeaders
(
'Headers'
,
$requestHeaders
,
$responseHeaders
);
if
(
$this
->
cors
[
'Access-Control-Allow-Credentials'
]
===
true
)
{
$responseHeaders
[
'Access-Control-Allow-Credentials'
]
=
'true'
;
}
elseif
(
$this
->
cors
[
'Access-Control-Allow-Credentials'
]
===
false
)
{
$responseHeaders
[
'Access-Control-Allow-Credentials'
]
=
'false'
;
}
if
((
$_SERVER
[
'REQUEST_METHOD'
]
===
'OPTIONS'
)
&&
(
$this
->
cors
[
'Access-Control-Max-Age'
]
!==
null
))
{
$responseHeaders
[
'Access-Control-Max-Age'
]
=
$this
->
cors
[
'Access-Control-Max-Age'
];
}
return
$responseHeaders
;
}
/**
* Handle classic CORS request to avoid duplicate code
* @param string $type the kind of headers we would handle
* @param array $requestHeaders CORS headers request by client
...
...
@@ -173,6 +204,21 @@ class Cors extends ActionFilter
}
/**
* Adds the CORS headers to the response
* @param Response $response
* @param array CORS headers which have been compouted
*/
public
function
addCorsHeaders
(
$response
,
$headers
)
{
if
(
empty
(
$headers
)
===
false
)
{
$responseHeaders
=
$response
->
getHeaders
();
foreach
(
$headers
as
$field
=>
$value
)
{
$responseHeaders
->
set
(
$field
,
$value
);
}
}
}
/**
* Convert any string (including php headers with HTTP prefix) to header format like :
* * X-PINGOTHER -> X-Pingother
* * HTTP_X_PINGOTHER -> X-Pingother
...
...
@@ -195,48 +241,4 @@ class Cors extends ActionFilter
{
return
'HTTP_'
.
strtoupper
(
str_replace
([
' '
,
'-'
],
[
'_'
,
'_'
],
$string
));
}
/**
* For each CORS headers create the specific response
* @param array $requestHeaders CORS headers we have detected
* @return array CORS headers ready to be sent
*/
public
function
prepareHeaders
(
$requestHeaders
)
{
$responseHeaders
=
[];
// handle Origin
if
(
isset
(
$requestHeaders
[
'Origin'
]))
{
if
((
in_array
(
'*'
,
$this
->
cors
[
'Origin'
])
===
true
)
||
(
in_array
(
$requestHeaders
[
'Origin'
],
$this
->
cors
[
'Origin'
])))
{
$responseHeaders
[
'Access-Control-Allow-Origin'
]
=
$requestHeaders
[
'Origin'
];
}
}
$this
->
prepareAllowHeaders
(
'Method'
,
$requestHeaders
,
$responseHeaders
);
$this
->
prepareAllowHeaders
(
'Headers'
,
$requestHeaders
,
$responseHeaders
);
if
(
$this
->
cors
[
'Access-Control-Allow-Credentials'
]
===
true
)
{
$responseHeaders
[
'Access-Control-Allow-Credentials'
]
=
'true'
;
}
elseif
(
$this
->
cors
[
'Access-Control-Allow-Credentials'
]
===
false
)
{
$responseHeaders
[
'Access-Control-Allow-Credentials'
]
=
'false'
;
}
if
((
$_SERVER
[
'REQUEST_METHOD'
]
===
'OPTIONS'
)
&&
(
$this
->
cors
[
'Access-Control-Max-Age'
]
!==
null
))
{
$responseHeaders
[
'Access-Control-Max-Age'
]
=
$this
->
cors
[
'Access-Control-Max-Age'
];
}
return
$responseHeaders
;
}
/**
* Adds the CORS headers to the response
* @param Response $response
* @param array CORS headers which have been compouted
*/
public
function
addCorsHeaders
(
$response
,
$headers
)
{
if
(
empty
(
$headers
)
===
false
)
{
$responseHeaders
=
$response
->
getHeaders
();
foreach
(
$headers
as
$field
=>
$value
)
{
$responseHeaders
->
set
(
$field
,
$value
);
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment