Skip to content
This repository was archived by the owner on May 23, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

[![Build Status](https://travis-ci.org/ozee31/cakephp-cors.svg?branch=master)](https://travis-ci.org/ozee31/cakephp-cors)

It's a pull request from [ozee31/cakephp-cors](https://github.com/ozee31/cakephp-cors).

A CakePHP (4+) plugin for activate cors domain in your application with [Middleware](http://book.cakephp.org/3.0/en/controllers/middleware.html).

[Learn more about CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ozee31/cakephp-cors",
"description": "A CakePHP (3.3.x) plugin for activate cors domain in your application",
"description": "A CakePHP 4 plugin for activate cors domain in your application",
"type": "cakephp-plugin",
"require": {
"php": ">=7.2.0",
Expand Down
16 changes: 15 additions & 1 deletion src/Routing/Middleware/CorsMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Cake\Http\Response;

class CorsMiddleware implements MiddlewareInterface
{
Expand All @@ -16,7 +17,20 @@ class CorsMiddleware implements MiddlewareInterface
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$response = $handler->handle($request);
if (strtoupper($request->getMethod()) === 'OPTIONS') {
if (!array_intersect($request->getHeader("Access-Control-Request-Method"), Configure::read('Cors.AllowMethods'))) {
$response = new Response([
'status' => 403,
'body' => 'Method Forbidden'
]);
} else {
$response = new Response([
'status' => 200
]);
}
} else {
$response = $handler->handle($request);
}

$response = $this->addHeaders($request, $response);

Expand Down