private HandshakeResponse buildHandshakeResponse ( WebSocketConnection conn , String clientKey ) throws WebSocketException { if ( log . isDebugEnabled ( ) ) { log . debug ( "buildHandshakeResponse: {} client key: {}" , conn , clientKey ) ; } byte [ ] accept ; try { // performs the accept creation routine from RFC6455 @see <a href="http://tools.ietf.org/html/rfc6455">RFC6455</a> // concatenate the key and magic string, then SHA1 hash and base64 encode MessageDigest md = MessageDigest . getInstance ( "SHA1" ) ; accept = Base64 . encode ( md . digest ( ( clientKey + Constants . WEBSOCKET_MAGIC_STRING ) . getBytes ( ) ) ) ; } catch ( NoSuchAlgorithmException e ) { throw new WebSocketException ( "Algorithm is missing" ) ; } // make up reply data... IoBuffer buf = IoBuffer . allocate ( 308 ) ; buf . setAutoExpand ( true ) ; buf . put ( "HTTP/1.1 101 Switching Protocols" . getBytes ( ) ) ; buf . put ( Constants . CRLF ) ; buf . put ( "Upgrade: websocket" . getBytes ( ) ) ; buf . put ( Constants . CRLF ) ; buf . put ( "Connection: Upgrade" . getBytes ( ) ) ; buf . put ( Constants . CRLF ) ; buf . put ( "Server: Red5" . getBytes ( ) ) ; buf . put ( Constants . CRLF ) ; buf . put ( "Sec-WebSocket-Version-Server: 13" . getBytes ( ) ) ; buf . put ( Constants . CRLF ) ; buf . put ( String . format ( "Sec-WebSocket-Origin: %s" , conn . getOrigin ( ) ) . getBytes ( ) ) ; buf . put ( Constants . CRLF ) ; buf . put ( String . format ( "Sec-WebSocket-Location: %s" , conn . getHost ( ) ) . getBytes ( ) ) ; buf . put ( Constants . CRLF ) ; // send back extensions if enabled if ( conn . hasExtensions ( ) ) { buf . put ( String . format ( "Sec-WebSocket-Extensions: %s" , conn . getExtensionsAsString ( ) ) . getBytes ( ) ) ; buf . put ( Constants . CRLF ) ; } // send back protocol if enabled if ( conn . hasProtocol ( ) ) { buf . put ( String . format ( "Sec-WebSocket-Protocol: %s" , conn . getProtocol ( ) ) . getBytes ( ) ) ; buf . put ( Constants . CRLF ) ; } buf . put ( String . format ( "Sec-WebSocket-Accept: %s" , new String ( accept ) ) . getBytes ( ) ) ; buf . put ( Constants . CRLF ) ; buf . put ( Constants . CRLF ) ; // if any bytes follow this crlf, the follow-up data will be corrupted if ( log . isTraceEnabled ( ) ) { log . trace ( "Handshake response size: {}" , buf . limit ( ) ) ; } return new HandshakeResponse ( buf ) ; }
Build a handshake response based on the given client key .