This commit is contained in:
Marcel Pociot 2018-11-23 15:09:54 +01:00
parent 1cfe2c8776
commit a632b43b56
1 changed files with 13 additions and 8 deletions

View File

@ -8,7 +8,8 @@ use Ratchet\Http\HttpServerInterface;
use Ratchet\MessageComponentInterface;
use Psr\Http\Message\RequestInterface;
class OriginCheck implements HttpServerInterface {
class OriginCheck implements HttpServerInterface
{
use CloseResponseTrait;
@ -17,13 +18,14 @@ class OriginCheck implements HttpServerInterface {
protected $allowedOrigins = [];
public function __construct(MessageComponentInterface $component, array $allowedOrigins = []) {
public function __construct(MessageComponentInterface $component, array $allowedOrigins = [])
{
$this->_component = $component;
$this->allowedOrigins = $allowedOrigins;
}
public function onOpen(ConnectionInterface $connection, RequestInterface $request = null) {
public function onOpen(ConnectionInterface $connection, RequestInterface $request = null)
{
if ($request->hasHeader('Origin')) {
$this->verifyOrigin($connection, $request);
}
@ -31,15 +33,18 @@ class OriginCheck implements HttpServerInterface {
return $this->_component->onOpen($connection, $request);
}
function onMessage(ConnectionInterface $from, $msg) {
function onMessage(ConnectionInterface $from, $msg)
{
return $this->_component->onMessage($from, $msg);
}
function onClose(ConnectionInterface $connection) {
function onClose(ConnectionInterface $connection)
{
return $this->_component->onClose($connection);
}
function onError(ConnectionInterface $connection, \Exception $e) {
function onError(ConnectionInterface $connection, \Exception $e)
{
return $this->_component->onError($connection, $e);
}
@ -48,7 +53,7 @@ class OriginCheck implements HttpServerInterface {
$header = (string)$request->getHeader('Origin')[0];
$origin = parse_url($header, PHP_URL_HOST) ?: $header;
if (! empty($this->allowedOrigins) && !in_array($origin, $this->allowedOrigins)) {
if (!empty($this->allowedOrigins) && !in_array($origin, $this->allowedOrigins)) {
return $this->close($connection, 403);
}
}