[Http] Refactor header parsing from WS to HTTP
This commit is contained in:
parent
e6e194736a
commit
48413cfbad
@ -1,17 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace Ratchet\WebSocket\Guzzle\Http\Message;
|
|
||||||
use Guzzle\Http\Message\RequestFactory as GuzzleRequestFactory;
|
|
||||||
use Guzzle\Http\EntityBody;
|
|
||||||
|
|
||||||
class RequestFactory extends GuzzleRequestFactory {
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function create($method, $url, $headers = null, $body = '') {
|
|
||||||
$c = $this->entityEnclosingRequestClass;
|
|
||||||
$request = new $c($method, $url, $headers);
|
|
||||||
$request->setBody(EntityBody::factory($body));
|
|
||||||
|
|
||||||
return $request;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,56 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace Ratchet\WebSocket;
|
|
||||||
use Ratchet\MessageInterface;
|
|
||||||
use Ratchet\ConnectionInterface;
|
|
||||||
use Ratchet\WebSocket\Guzzle\Http\Message\RequestFactory;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This class receives streaming data from a client request
|
|
||||||
* and parses HTTP headers, returning a Guzzle 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 \Guzzle\Http\Message\RequestInterface|null
|
|
||||||
* @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 = RequestFactory::getInstance()->fromMessage($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);
|
|
||||||
}
|
|
||||||
}
|
|
15
WsServer.php
15
WsServer.php
@ -2,12 +2,11 @@
|
|||||||
namespace Ratchet\WebSocket;
|
namespace Ratchet\WebSocket;
|
||||||
use Ratchet\MessageComponentInterface;
|
use Ratchet\MessageComponentInterface;
|
||||||
use Ratchet\ConnectionInterface;
|
use Ratchet\ConnectionInterface;
|
||||||
|
use Ratchet\Http\HttpServerInterface;
|
||||||
|
use Guzzle\Http\Message\RequestInterface;
|
||||||
|
use Guzzle\Http\Message\Response;
|
||||||
use Ratchet\WebSocket\Version;
|
use Ratchet\WebSocket\Version;
|
||||||
use Ratchet\WebSocket\Encoding\ToggleableValidator;
|
use Ratchet\WebSocket\Encoding\ToggleableValidator;
|
||||||
use Guzzle\Http\Message\Response;
|
|
||||||
|
|
||||||
use Guzzle\Http\Message\RequestInterface;
|
|
||||||
use Ratchet\Http\HttpServerInterface;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The adapter to handle WebSocket requests/responses
|
* The adapter to handle WebSocket requests/responses
|
||||||
@ -16,13 +15,6 @@ use Ratchet\Http\HttpServerInterface;
|
|||||||
* @link http://dev.w3.org/html5/websockets/
|
* @link http://dev.w3.org/html5/websockets/
|
||||||
*/
|
*/
|
||||||
class WsServer implements HttpServerInterface {
|
class WsServer implements HttpServerInterface {
|
||||||
/**
|
|
||||||
* 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
|
|
||||||
*/
|
|
||||||
public $reqParser;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Manage the various WebSocket versions to support
|
* Manage the various WebSocket versions to support
|
||||||
* @var VersionManager
|
* @var VersionManager
|
||||||
@ -65,7 +57,6 @@ class WsServer implements HttpServerInterface {
|
|||||||
* If you want to enable sub-protocols have your component implement WsServerInterface as well
|
* If you want to enable sub-protocols have your component implement WsServerInterface as well
|
||||||
*/
|
*/
|
||||||
public function __construct(MessageComponentInterface $component) {
|
public function __construct(MessageComponentInterface $component) {
|
||||||
$this->reqParser = new HttpRequestParser;
|
|
||||||
$this->versioner = new VersionManager;
|
$this->versioner = new VersionManager;
|
||||||
$this->validator = new ToggleableValidator;
|
$this->validator = new ToggleableValidator;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user