Skip to content

Commit

Permalink
Added CORS middleware
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Wright <tom@inflatablecookie.com>
  • Loading branch information
betterthanclay committed Dec 8, 2023
1 parent a9f8792 commit ba37406
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
* Added CORS Middleware
* Added priority ordering to incoming Middleware

## v0.2.11 (2023-11-28)
* Improved ob_flush handling in transport

Expand Down
106 changes: 106 additions & 0 deletions src/Middleware/Cors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

/**
* @package Harvest
* @license http://opensource.org/licenses/MIT
*/

declare(strict_types=1);

namespace DecodeLabs\Harvest\Middleware;

use DecodeLabs\Genesis;
use DecodeLabs\Harvest\PriorityProvider;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\MiddlewareInterface as Middleware;
use Psr\Http\Server\RequestHandlerInterface as Handler;

class Cors implements
Middleware,
PriorityProvider
{
/**
* @var array<string>
*/
protected array $allow = [];

/**
* Init with allow list
*
* @param array<string> $allow
*/
public function __construct(
array $allow = []
) {
$this->allow = $allow;
}

/**
* Get default priority
*/
public function getPriority(): int
{
return -1;
}

/**
* Process middleware
*/
public function process(
Request $request,
Handler $next
): Response {
$response = $next->handle($request);

// Check header
if ($response->hasHeader('Access-Control-Allow-Origin')) {
return $response;
}

// Env mode
if (class_exists(Genesis::class)) {
$development = Genesis::$environment->isDevelopment();
} else {
$development = false;
}

// Check origin
$allow = false;
$origin = $request->getHeaderLine('Origin');

if (empty($this->allow)) {
if (!$development) {
return $response;
}

$allow = true;
} else {
if (empty($origin)) {
return $response;
}

foreach ($this->allow as $allow) {
if ($allow === '*') {
$allow = true;
break;
}

if ($allow === $origin) {
$allow = true;
break;
}
}
}

// Add header
if ($allow) {
$response = $response->withHeader(
'Access-Control-Allow-Origin',
$origin
);
}

return $response;
}
}
13 changes: 12 additions & 1 deletion src/Middleware/Https.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,24 @@

use DecodeLabs\Genesis;
use DecodeLabs\Harvest;
use DecodeLabs\Harvest\PriorityProvider;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\MiddlewareInterface as Middleware;
use Psr\Http\Server\RequestHandlerInterface as Handler;

class Https implements Middleware
class Https implements
Middleware,
PriorityProvider
{
/**
* Get default priority
*/
public function getPriority(): int
{
return -1;
}

/**
* Process middleware
*/
Expand Down

0 comments on commit ba37406

Please sign in to comment.