[Refactor] Fixing stuff for Chris
Fixing sub protocol agreement between server and client
This commit is contained in:
		
							parent
							
								
									8a2b9153ab
								
							
						
					
					
						commit
						c6a801f1ef
					
				@ -23,23 +23,18 @@ class Hixie76 implements VersionInterface {
 | 
			
		||||
    /**
 | 
			
		||||
     * @param string
 | 
			
		||||
     * @return string
 | 
			
		||||
     * @todo Unhack this mess...or wait for Hixie to die (HURRY UP APPLE)
 | 
			
		||||
     */
 | 
			
		||||
    public function handshake(RequestInterface $request) {
 | 
			
		||||
        $body = $this->sign($request->getHeader('Sec-WebSocket-Key1'), $request->getHeader('Sec-WebSocket-Key2'), $request->getBody());
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        $headers = array(
 | 
			
		||||
            'Upgrade'                => 'WebSocket'
 | 
			
		||||
          , 'Connection'             => 'Upgrade'
 | 
			
		||||
          , 'Sec-WebSocket-Origin'   => $request->getHeader('Origin')
 | 
			
		||||
          , 'Sec-WebSocket-Location' => 'ws://' . $request->getHeader('Host') . $request->getPath()
 | 
			
		||||
        );
 | 
			
		||||
        if ($request->getHeader('Sec-WebSocket-Protocol')) {
 | 
			
		||||
            $headers['Sec-WebSocket-Protocol'] = $request->getHeader('Sec-WebSocket-Protocol');
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        $response = new \Guzzle\Http\Message\Response('101', $headers, $body);        
 | 
			
		||||
        return $response;
 | 
			
		||||
 | 
			
		||||
        return new \Guzzle\Http\Message\Response('101', $headers, $body);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function newMessage() {
 | 
			
		||||
@ -55,18 +50,18 @@ class Hixie76 implements VersionInterface {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function generateKeyNumber($key) {
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        if (substr_count($key, ' ') == 0) {
 | 
			
		||||
            return '';
 | 
			
		||||
        }
 | 
			
		||||
        $int = preg_replace('[\D]', '', $key) / substr_count($key, ' ');
 | 
			
		||||
        return (is_int($int)) ? $int : '';
 | 
			
		||||
        
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function sign($key1, $key2, $code) {
 | 
			
		||||
 
 | 
			
		||||
 | 
			
		||||
        return md5(
 | 
			
		||||
            pack('N', $this->generateKeyNumber($key1))
 | 
			
		||||
          . pack('N', $this->generateKeyNumber($key2))
 | 
			
		||||
 | 
			
		||||
@ -79,6 +79,25 @@ class WebSocketComponent implements MessageComponentInterface {
 | 
			
		||||
            $response = $from->WebSocket->version->handshake($from->WebSocket->headers);
 | 
			
		||||
            $from->WebSocket->handshake = true;
 | 
			
		||||
            
 | 
			
		||||
            
 | 
			
		||||
  	
 | 
			
		||||
           // This block is to be moved/changed later
 | 
			
		||||
  	
 | 
			
		||||
            $agreed_protocols    = array();
 | 
			
		||||
            
 | 
			
		||||
            $requested_protocols = $from->WebSocket->headers->getTokenizedHeader('Sec-WebSocket-Protocol', ',');
 | 
			
		||||
            
 | 
			
		||||
            foreach ($this->accepted_subprotocols as $sub_protocol) {  	
 | 
			
		||||
                if (false !== $requested_protocols->hasValue($sub_protocol)) {  	
 | 
			
		||||
                    $agreed_protocols[] = $sub_protocol;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            if (count($agreed_protocols) > 0) {
 | 
			
		||||
            
 | 
			
		||||
                // $response['Sec-WebSocket-Protocol'] = implode(',', $agreed_protocols);
 | 
			
		||||
                $response->setHeader('Sec-WebSocket-Protocol', implode(',', $agreed_protocols));
 | 
			
		||||
            }
 | 
			
		||||
            $header = (string)$response;
 | 
			
		||||
            
 | 
			
		||||
            $comp = $this->_factory->newComposite();
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user