diff options
author | marvin-borner@live.com | 2018-04-10 21:50:16 +0200 |
---|---|---|
committer | marvin-borner@live.com | 2018-04-10 21:54:48 +0200 |
commit | fc9401f04a3aca5abb22f87ebc210de8afe11d32 (patch) | |
tree | b0b310f3581764ec3955f4e496a05137a32951c3 /assets/php/vendor/cboden/ratchet/src/Ratchet/Http | |
parent | 286d643180672f20526f3dc3bd19d7b751e2fa97 (diff) |
Initial Commit
Diffstat (limited to 'assets/php/vendor/cboden/ratchet/src/Ratchet/Http')
7 files changed, 355 insertions, 0 deletions
diff --git a/assets/php/vendor/cboden/ratchet/src/Ratchet/Http/CloseResponseTrait.php b/assets/php/vendor/cboden/ratchet/src/Ratchet/Http/CloseResponseTrait.php new file mode 100644 index 0000000..abdf5c4 --- /dev/null +++ b/assets/php/vendor/cboden/ratchet/src/Ratchet/Http/CloseResponseTrait.php @@ -0,0 +1,22 @@ +<?php +namespace Ratchet\Http; +use Ratchet\ConnectionInterface; +use GuzzleHttp\Psr7 as gPsr; +use GuzzleHttp\Psr7\Response; + +trait CloseResponseTrait { + /** + * Close a connection with an HTTP response + * @param \Ratchet\ConnectionInterface $conn + * @param int $code HTTP status code + * @return null + */ + private function close(ConnectionInterface $conn, $code = 400, array $additional_headers = []) { + $response = new Response($code, array_merge([ + 'X-Powered-By' => \Ratchet\VERSION + ], $additional_headers)); + + $conn->send(gPsr\str($response)); + $conn->close(); + } +}
\ No newline at end of file diff --git a/assets/php/vendor/cboden/ratchet/src/Ratchet/Http/HttpRequestParser.php b/assets/php/vendor/cboden/ratchet/src/Ratchet/Http/HttpRequestParser.php new file mode 100644 index 0000000..9c44114 --- /dev/null +++ b/assets/php/vendor/cboden/ratchet/src/Ratchet/Http/HttpRequestParser.php @@ -0,0 +1,64 @@ +<?php +namespace Ratchet\Http; +use Ratchet\MessageInterface; +use Ratchet\ConnectionInterface; +use GuzzleHttp\Psr7 as gPsr; + +/** + * This class receives streaming data from a client request + * and parses HTTP headers, returning a PSR-7 Request object + * once it's been buffered + */ +class HttpRequestParser implements MessageInterface { + const EOM = "\r\n\r\n"; + + /** + * The maximum number of bytes the request can be + * This is a security measure to prevent attacks + * @var int + */ + public $maxSize = 4096; + + /** + * @param \Ratchet\ConnectionInterface $context + * @param string $data Data stream to buffer + * @return \Psr\Http\Message\RequestInterface + * @throws \OverflowException If the message buffer has become too large + */ + public function onMessage(ConnectionInterface $context, $data) { + if (!isset($context->httpBuffer)) { + $context->httpBuffer = ''; + } + + $context->httpBuffer .= $data; + + if (strlen($context->httpBuffer) > (int)$this->maxSize) { + throw new \OverflowException("Maximum buffer size of {$this->maxSize} exceeded parsing HTTP header"); + } + + if ($this->isEom($context->httpBuffer)) { + $request = $this->parse($context->httpBuffer); + + unset($context->httpBuffer); + + return $request; + } + } + + /** + * Determine if the message has been buffered as per the HTTP specification + * @param string $message + * @return boolean + */ + public function isEom($message) { + return (boolean)strpos($message, static::EOM); + } + + /** + * @param string $headers + * @return \Psr\Http\Message\RequestInterface + */ + public function parse($headers) { + return gPsr\parse_request($headers); + } +} diff --git a/assets/php/vendor/cboden/ratchet/src/Ratchet/Http/HttpServer.php b/assets/php/vendor/cboden/ratchet/src/Ratchet/Http/HttpServer.php new file mode 100644 index 0000000..bbd8d53 --- /dev/null +++ b/assets/php/vendor/cboden/ratchet/src/Ratchet/Http/HttpServer.php @@ -0,0 +1,76 @@ +<?php +namespace Ratchet\Http; +use Ratchet\MessageComponentInterface; +use Ratchet\ConnectionInterface; + +class HttpServer implements MessageComponentInterface { + use CloseResponseTrait; + + /** + * Buffers incoming HTTP requests returning a Guzzle Request when coalesced + * @var HttpRequestParser + * @note May not expose this in the future, may do through facade methods + */ + protected $_reqParser; + + /** + * @var \Ratchet\Http\HttpServerInterface + */ + protected $_httpServer; + + /** + * @param HttpServerInterface + */ + public function __construct(HttpServerInterface $component) { + $this->_httpServer = $component; + $this->_reqParser = new HttpRequestParser; + } + + /** + * {@inheritdoc} + */ + public function onOpen(ConnectionInterface $conn) { + $conn->httpHeadersReceived = false; + } + + /** + * {@inheritdoc} + */ + public function onMessage(ConnectionInterface $from, $msg) { + if (true !== $from->httpHeadersReceived) { + try { + if (null === ($request = $this->_reqParser->onMessage($from, $msg))) { + return; + } + } catch (\OverflowException $oe) { + return $this->close($from, 413); + } + + $from->httpHeadersReceived = true; + + return $this->_httpServer->onOpen($from, $request); + } + + $this->_httpServer->onMessage($from, $msg); + } + + /** + * {@inheritdoc} + */ + public function onClose(ConnectionInterface $conn) { + if ($conn->httpHeadersReceived) { + $this->_httpServer->onClose($conn); + } + } + + /** + * {@inheritdoc} + */ + public function onError(ConnectionInterface $conn, \Exception $e) { + if ($conn->httpHeadersReceived) { + $this->_httpServer->onError($conn, $e); + } else { + $this->close($conn, 500); + } + } +} diff --git a/assets/php/vendor/cboden/ratchet/src/Ratchet/Http/HttpServerInterface.php b/assets/php/vendor/cboden/ratchet/src/Ratchet/Http/HttpServerInterface.php new file mode 100644 index 0000000..2c37c49 --- /dev/null +++ b/assets/php/vendor/cboden/ratchet/src/Ratchet/Http/HttpServerInterface.php @@ -0,0 +1,14 @@ +<?php +namespace Ratchet\Http; +use Ratchet\MessageComponentInterface; +use Ratchet\ConnectionInterface; +use Psr\Http\Message\RequestInterface; + +interface HttpServerInterface extends MessageComponentInterface { + /** + * @param \Ratchet\ConnectionInterface $conn + * @param \Psr\Http\Message\RequestInterface $request null is default because PHP won't let me overload; don't pass null!!! + * @throws \UnexpectedValueException if a RequestInterface is not passed + */ + public function onOpen(ConnectionInterface $conn, RequestInterface $request = null); +} diff --git a/assets/php/vendor/cboden/ratchet/src/Ratchet/Http/NoOpHttpServerController.php b/assets/php/vendor/cboden/ratchet/src/Ratchet/Http/NoOpHttpServerController.php new file mode 100644 index 0000000..4f72e66 --- /dev/null +++ b/assets/php/vendor/cboden/ratchet/src/Ratchet/Http/NoOpHttpServerController.php @@ -0,0 +1,18 @@ +<?php +namespace Ratchet\Http; +use Ratchet\ConnectionInterface; +use Psr\Http\Message\RequestInterface; + +class NoOpHttpServerController implements HttpServerInterface { + public function onOpen(ConnectionInterface $conn, RequestInterface $request = null) { + } + + public function onMessage(ConnectionInterface $from, $msg) { + } + + public function onClose(ConnectionInterface $conn) { + } + + public function onError(ConnectionInterface $conn, \Exception $e) { + } +} diff --git a/assets/php/vendor/cboden/ratchet/src/Ratchet/Http/OriginCheck.php b/assets/php/vendor/cboden/ratchet/src/Ratchet/Http/OriginCheck.php new file mode 100644 index 0000000..2bdc0f7 --- /dev/null +++ b/assets/php/vendor/cboden/ratchet/src/Ratchet/Http/OriginCheck.php @@ -0,0 +1,65 @@ +<?php +namespace Ratchet\Http; +use Ratchet\ConnectionInterface; +use Ratchet\MessageComponentInterface; +use Psr\Http\Message\RequestInterface; + +/** + * A middleware to ensure JavaScript clients connecting are from the expected domain. + * This protects other websites from open WebSocket connections to your application. + * Note: This can be spoofed from non-web browser clients + */ +class OriginCheck implements HttpServerInterface { + use CloseResponseTrait; + + /** + * @var \Ratchet\MessageComponentInterface + */ + protected $_component; + + public $allowedOrigins = []; + + /** + * @param MessageComponentInterface $component Component/Application to decorate + * @param array $allowed An array of allowed domains that are allowed to connect from + */ + public function __construct(MessageComponentInterface $component, array $allowed = []) { + $this->_component = $component; + $this->allowedOrigins += $allowed; + } + + /** + * {@inheritdoc} + */ + public function onOpen(ConnectionInterface $conn, RequestInterface $request = null) { + $header = (string)$request->getHeader('Origin')[0]; + $origin = parse_url($header, PHP_URL_HOST) ?: $header; + + if (!in_array($origin, $this->allowedOrigins)) { + return $this->close($conn, 403); + } + + return $this->_component->onOpen($conn, $request); + } + + /** + * {@inheritdoc} + */ + function onMessage(ConnectionInterface $from, $msg) { + return $this->_component->onMessage($from, $msg); + } + + /** + * {@inheritdoc} + */ + function onClose(ConnectionInterface $conn) { + return $this->_component->onClose($conn); + } + + /** + * {@inheritdoc} + */ + function onError(ConnectionInterface $conn, \Exception $e) { + return $this->_component->onError($conn, $e); + } +}
\ No newline at end of file diff --git a/assets/php/vendor/cboden/ratchet/src/Ratchet/Http/Router.php b/assets/php/vendor/cboden/ratchet/src/Ratchet/Http/Router.php new file mode 100644 index 0000000..df7fe82 --- /dev/null +++ b/assets/php/vendor/cboden/ratchet/src/Ratchet/Http/Router.php @@ -0,0 +1,96 @@ +<?php +namespace Ratchet\Http; +use Ratchet\ConnectionInterface; +use Psr\Http\Message\RequestInterface; +use Symfony\Component\Routing\Matcher\UrlMatcherInterface; +use Symfony\Component\Routing\Exception\MethodNotAllowedException; +use Symfony\Component\Routing\Exception\ResourceNotFoundException; +use GuzzleHttp\Psr7 as gPsr; + +class Router implements HttpServerInterface { + use CloseResponseTrait; + + /** + * @var \Symfony\Component\Routing\Matcher\UrlMatcherInterface + */ + protected $_matcher; + + private $_noopController; + + public function __construct(UrlMatcherInterface $matcher) { + $this->_matcher = $matcher; + $this->_noopController = new NoOpHttpServerController; + } + + /** + * {@inheritdoc} + * @throws \UnexpectedValueException If a controller is not \Ratchet\Http\HttpServerInterface + */ + public function onOpen(ConnectionInterface $conn, RequestInterface $request = null) { + if (null === $request) { + throw new \UnexpectedValueException('$request can not be null'); + } + + $conn->controller = $this->_noopController; + + $uri = $request->getUri(); + + $context = $this->_matcher->getContext(); + $context->setMethod($request->getMethod()); + $context->setHost($uri->getHost()); + + try { + $route = $this->_matcher->match($uri->getPath()); + } catch (MethodNotAllowedException $nae) { + return $this->close($conn, 405, array('Allow' => $nae->getAllowedMethods())); + } catch (ResourceNotFoundException $nfe) { + return $this->close($conn, 404); + } + + if (is_string($route['_controller']) && class_exists($route['_controller'])) { + $route['_controller'] = new $route['_controller']; + } + + if (!($route['_controller'] instanceof HttpServerInterface)) { + throw new \UnexpectedValueException('All routes must implement Ratchet\Http\HttpServerInterface'); + } + + $parameters = []; + foreach($route as $key => $value) { + if ((is_string($key)) && ('_' !== substr($key, 0, 1))) { + $parameters[$key] = $value; + } + } + $parameters = array_merge($parameters, gPsr\parse_query($uri->getQuery() ?: '')); + + $request = $request->withUri($uri->withQuery(gPsr\build_query($parameters))); + + $conn->controller = $route['_controller']; + $conn->controller->onOpen($conn, $request); + } + + /** + * {@inheritdoc} + */ + public function onMessage(ConnectionInterface $from, $msg) { + $from->controller->onMessage($from, $msg); + } + + /** + * {@inheritdoc} + */ + public function onClose(ConnectionInterface $conn) { + if (isset($conn->controller)) { + $conn->controller->onClose($conn); + } + } + + /** + * {@inheritdoc} + */ + public function onError(ConnectionInterface $conn, \Exception $e) { + if (isset($conn->controller)) { + $conn->controller->onError($conn, $e); + } + } +} |