Custom error responses for failed handshake
Including React SocketClient for client tests Use re-entrants in test server
This commit is contained in:
		
							parent
							
								
									3fb32b2827
								
							
						
					
					
						commit
						af15a56cb4
					
				| @ -25,6 +25,7 @@ | ||||
|         "guzzlehttp/psr7": "^1.0" | ||||
|     }, | ||||
|     "require-dev": { | ||||
|         "react/http": "^0.4.1" | ||||
|         "react/http": "^0.4.1", | ||||
|         "react/socket-client": "^0.4.3" | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -1,8 +1,7 @@ | ||||
| <?php | ||||
| namespace Ratchet\RFC6455\Handshake; | ||||
| 
 | ||||
| use GuzzleHttp\Psr7\Response; | ||||
| use Psr\Http\Message\RequestInterface; | ||||
| use GuzzleHttp\Psr7\Response; | ||||
| use Ratchet\RFC6455\Encoding\ValidatorInterface; | ||||
| 
 | ||||
| /** | ||||
| @ -30,9 +29,7 @@ class Negotiator implements NegotiatorInterface { | ||||
|      * {@inheritdoc} | ||||
|      */ | ||||
|     public function isProtocol(RequestInterface $request) { | ||||
|         $version = (int)(string)$request->getHeader('Sec-WebSocket-Version'); | ||||
| 
 | ||||
|         return ($this->getVersionNumber() === $version); | ||||
|         return $this->verifier->verifyVersion($request->getHeader('Sec-WebSocket-Version')); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
| @ -46,26 +43,46 @@ class Negotiator implements NegotiatorInterface { | ||||
|      * {@inheritdoc} | ||||
|      */ | ||||
|     public function handshake(RequestInterface $request) { | ||||
|         if (true !== $this->verifier->verifyAll($request)) { | ||||
|         if (true !== $this->verifier->verifyMethod($request->getMethod())) { | ||||
|             return new Response(405); | ||||
|         } | ||||
| 
 | ||||
|         if (true !== $this->verifier->verifyHTTPVersion($request->getProtocolVersion())) { | ||||
|             return new Response(505); | ||||
|         } | ||||
| 
 | ||||
|         if (true !== $this->verifier->verifyRequestURI($request->getUri()->getPath())) { | ||||
|             return new Response(400); | ||||
|         } | ||||
| 
 | ||||
|         if (true !== $this->verifier->verifyHost($request->getHeader('Host'))) { | ||||
|             return new Response(400); | ||||
|         } | ||||
| 
 | ||||
|         if (true !== $this->verifier->verifyUpgradeRequest($request->getHeader('Upgrade'))) { | ||||
|             return new Response(400, [], '1.1', null, 'Upgrade header MUST be provided'); | ||||
|         } | ||||
| 
 | ||||
|         if (true !== $this->verifier->verifyConnection($request->getHeader('Connection'))) { | ||||
|             return new Response(400, [], '1.1', null, 'Connection header MUST be provided'); | ||||
|         } | ||||
| 
 | ||||
|         if (true !== $this->verifier->verifyKey($request->getHeader('Sec-WebSocket-Key'))) { | ||||
|             return new Response(400, [], '1.1', null, 'Invalid Sec-WebSocket-Key'); | ||||
|         } | ||||
| 
 | ||||
|         if (true !== $this->verifier->verifyVersion($request->getHeader('Sec-WebSocket-Version'))) { | ||||
|             return new Response(426, ['Sec-WebSocket-Version' => $this->getVersionNumber()]); | ||||
|         } | ||||
| 
 | ||||
|         return new Response(101, [ | ||||
|             'Upgrade'              => 'websocket' | ||||
|           , 'Connection'           => 'Upgrade' | ||||
|           , 'Sec-WebSocket-Accept' => $this->sign((string)$request->getHeader('Sec-WebSocket-Key')[0]) | ||||
|           , 'X-Powered-By'         => 'Ratchet' | ||||
|         ]); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @deprecated - The logic belons somewhere else | ||||
|      * @param \Ratchet\WebSocket\Version\RFC6455\Connection $from | ||||
|      * @param string                                        $data | ||||
|      */ | ||||
| //    public function onMessage(ConnectionInterface $from, $data) {
 | ||||
| //
 | ||||
| //    }
 | ||||
| 
 | ||||
|     /** | ||||
|      * Used when doing the handshake to encode the key, verifying client/server are speaking the same language | ||||
|      * @param  string $key | ||||
|  | ||||
| @ -1,8 +1,6 @@ | ||||
| <?php | ||||
| namespace Ratchet\RFC6455\Handshake; | ||||
| 
 | ||||
| use Psr\Http\Message\RequestInterface; | ||||
| use Psr\Http\Message\ResponseInterface; | ||||
| 
 | ||||
| /** | ||||
|  * A standard interface for interacting with the various version of the WebSocket protocol | ||||
| @ -27,7 +25,7 @@ interface NegotiatorInterface { | ||||
|     /** | ||||
|      * Perform the handshake and return the response headers | ||||
|      * @param RequestInterface $request | ||||
|      * @return ResponseInterface | ||||
|      * @return \Psr\Http\Message\ResponseInterface | ||||
|      */ | ||||
|     function handshake(RequestInterface $request); | ||||
| 
 | ||||
|  | ||||
| @ -64,16 +64,16 @@ $loop   = \React\EventLoop\Factory::create(); | ||||
| $socket = new \React\Socket\Server($loop); | ||||
| $server = new \React\Http\Server($socket); | ||||
| 
 | ||||
| $server->on('request', function (\React\Http\Request $request, \React\Http\Response $response) { | ||||
|     $conn = new ConnectionContext($response); | ||||
| $encodingValidator = new \Ratchet\RFC6455\Encoding\Validator; | ||||
| $negotiator = new \Ratchet\RFC6455\Handshake\Negotiator($encodingValidator); | ||||
| $ms = new \Ratchet\RFC6455\Messaging\Streaming\MessageStreamer($encodingValidator); | ||||
| 
 | ||||
|     $encodingValidator = new \Ratchet\RFC6455\Encoding\Validator; | ||||
| $server->on('request', function (\React\Http\Request $request, \React\Http\Response $response) use ($negotiator, $ms) { | ||||
|     $conn = new ConnectionContext($response); | ||||
| 
 | ||||
|     // make the React Request a Psr7 request (not perfect)
 | ||||
|     $psrRequest = new \GuzzleHttp\Psr7\Request($request->getMethod(), $request->getPath(), $request->getHeaders()); | ||||
| 
 | ||||
|     $negotiator = new \Ratchet\RFC6455\Handshake\Negotiator($encodingValidator); | ||||
| 
 | ||||
|     $negotiatorResponse = $negotiator->handshake($psrRequest); | ||||
| 
 | ||||
|     $response->writeHead( | ||||
| @ -89,8 +89,6 @@ $server->on('request', function (\React\Http\Request $request, \React\Http\Respo | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     $ms = new \Ratchet\RFC6455\Messaging\Streaming\MessageStreamer($encodingValidator); | ||||
| 
 | ||||
|     $request->on('data', function ($data) use ($ms, $conn) { | ||||
|         $ms->onData($data, $conn); | ||||
|     }); | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Chris Boden
						Chris Boden