Skip to content

Commit

Permalink
PR fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
domino91 committed Oct 14, 2024
1 parent 88380cc commit 88585d8
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/Hub/Middleware/CorsMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,30 @@
use Psr\Http\Message\ServerRequestInterface;
use React\Http\Message\Response;

class CorsMiddleware
final class CorsMiddleware
{
public function __construct(
private ?string $corsOrigin
)
{
if (!$this->corsOrigin) {
$this->corsOrigin = 'null';
}
private readonly ?string $corsOrigin = '*',
) {
}

public function __invoke(
ServerRequestInterface $request,
callable $next
): ResponseInterface
{
callable $next
): ResponseInterface {
$response = $next($request);

return $response->withAddedHeader('Access-Control-Allow-Origin', $this->corsOrigin)
$corsOrigin = $this->corsOrigin ?? $this->getOrigin($request);

return $response->withAddedHeader('Access-Control-Allow-Origin', $corsOrigin)
->withAddedHeader('Access-Control-Allow-Headers', '*')
->withAddedHeader('Access-Control-Allow-Methods', '*')
->withAddedHeader('Access-Control-Allow-Credentials', 'true')
->withStatus(Response::STATUS_OK);
}
}

private function getOrigin(ServerRequestInterface $request): string

Check warning on line 31 in src/Hub/Middleware/CorsMiddleware.php

View check run for this annotation

Codecov / codecov/patch

src/Hub/Middleware/CorsMiddleware.php#L31

Added line #L31 was not covered by tests
{
return $request->getHeaderLine('Origin') ?: 'null';

Check warning on line 33 in src/Hub/Middleware/CorsMiddleware.php

View check run for this annotation

Codecov / codecov/patch

src/Hub/Middleware/CorsMiddleware.php#L33

Added line #L33 was not covered by tests
}
}

0 comments on commit 88585d8

Please sign in to comment.