def web_socket_do_extra_handshake(request): # Send simple response header. This test implements the handshake manually, # so that we can send the header in the same packet as the close frame. msg = (b'HTTP/1.1 101 Switching Protocols:\x0D\x0A' b'Connection: Upgrade\x0D\x0A' b'Upgrade: WebSocket\x0D\x0A' b'Set-Cookie: ws_test=test\x0D\x0A' b'Sec-WebSocket-Origin: %s\x0D\x0A' b'Sec-WebSocket-Accept: %s\x0D\x0A\x0D\x0A') % ( request.ws_origin.encode('UTF-8'), hybi.compute_accept_from_unicode( request.headers_in.get(common.SEC_WEBSOCKET_KEY_HEADER))) # Create a clean close frame. close_body = stream.create_closing_handshake_body(1001, 'PASS') close_frame = stream.create_close_frame(close_body) # Concatenate the header and the close frame and write them to the socket. request.connection.write(msg + close_frame) # Wait for the responding close frame from the user agent. It's not possible # to use the stream methods at this point because the stream hasn't been # established from pywebsocket's point of view. Instead just read the # correct number of bytes. # Warning: reading the wrong number of bytes here will make the test # flaky. MASK_LENGTH = 4 request.connection.read(len(close_frame) + MASK_LENGTH) # Close the socket without pywebsocket sending its own handshake response. raise AbortedByUserException('Abort the connection')
def web_socket_do_extra_handshake(request): # Missing 'Upgrade: websocket\r\n' msg = (b'HTTP/1.1 101 Switching Protocols\r\n' b'Connection: Upgrade\r\n' b'Sec-WebSocket-Accept: %s\r\n' b'\r\n') % compute_accept_from_unicode(request.headers_in['Sec-WebSocket-Key']) request.connection.write(msg) # Prevents pywebsocket from sending its own handshake message. raise handshake.AbortedByUserException('Abort the connection')
def web_socket_do_extra_handshake(request): msg = b"HTTP/1.1 101 Switching Protocols\r\n" msg += b"Upgrade: websocket\r\n" msg += b"Connection: Upgrade\r\n" msg += b"Sec-WebSocket-Accept: " msg += compute_accept_from_unicode(request.headers_in["Sec-WebSocket-Key"]) msg += b"\xa5:\r\n" msg += b"\r\n" request.connection.write(msg) print(msg) raise handshake.AbortedByUserException("Abort the connection") # Prevents pywebsocket from sending its own handshake message.
def web_socket_do_extra_handshake(request): accept = compute_accept_from_unicode(request.headers_in['Sec-WebSocket-Key']) message = (b'HTTP/1.1 101 Switching Protocols\r\n' b'Upgrade: websocket\r\n' b'Connection: Upgrade\r\n' b'Sec-WebSocket-Accept: %s\r\n' b'Sec-WebSocket-Protocol: sip \r\n' b'\r\n' % accept) request.connection.write(message) # Prevent pywebsocket from sending its own handshake message. raise handshake.AbortedByUserException('Close the connection')
def web_socket_do_extra_handshake(request): msg = b'HTTP/1.1 101 Switching Protocols\n' # Does not end with "\r\n". msg += b'Upgrade: websocket\r\n' msg += b'Connection: Upgrade\r\n' msg += b'Sec-WebSocket-Accept: ' msg += compute_accept_from_unicode(request.headers_in['Sec-WebSocket-Key']) msg += b'\r\n\r\n' request.connection.write(msg) print(msg) raise handshake.AbortedByUserException( 'Abort the connection' ) # Prevents pywebsocket from sending its own handshake message.
def web_socket_do_extra_handshake(request): message = b'HTTP/1.1 101 Switching Protocols\r\n' message += b'Upgrade: websocket\r\n' message += b'Connection: Upgrade\r\n' message += b'Sec-WebSocket-Accept: ' message += compute_accept_from_unicode( request.headers_in['Sec-WebSocket-Key']) message += 'Sec-WebSocket-Protocol: Non-áscii-value\r\n'.encode() message += b'\r\n\r\n' request.connection.write(message) raise handshake.AbortedByUserException( 'Abort the connection' ) # Prevents pywebsocket from sending its own handshake message.
def web_socket_do_extra_handshake(request): message = b'HTTP/1.1 101 Switching Protocols\r\n' message += b'Upgrade: websocket\r\n' message += b'Connection: Upgrade\r\n' message += b'WebSocket-Origin: http://localhost:8880\r\n' message += b'WebSocket-Location: ws://localhost:8880/bogus\r\n' message += b'Sec-WebSocket-Accept: ' message += compute_accept_from_unicode( request.headers_in['Sec-WebSocket-Key']) message += b'\r\n\r\n' request.connection.write(message) raise handshake.AbortedByUserException( 'Abort the connection' ) # Prevents pywebsocket from sending its own handshake message.
def web_socket_do_extra_handshake(request): # This will cause the handshake to fail because it pushes the length of the # status line past 1024 characters msg = b'.' * 1024 msg += b'HTTP/1.1 101 Switching Protocols\r\n' msg += b'Upgrade: websocket\r\n' msg += b'Connection: Upgrade\r\n' msg += b'Sec-WebSocket-Accept: ' msg += compute_accept_from_unicode(request.headers_in['Sec-WebSocket-Key']) msg += b'\r\n\r\n' request.connection.write(msg) raise handshake.AbortedByUserException( 'abort the connection' ) # Prevents pywebsocket from sending its own handshake message.
def web_socket_do_extra_handshake(request): frame = stream.create_text_frame('Frame-contains-thirty-one-bytes') message = frame message += 'HTTP/1.1 101 Switching Protocols non-ascií\r\n'.encode() message += b'Upgrade: websocket\r\n' message += b'Connection: Upgrade\r\n' message += b'Sec-WebSocket-Accept: ' message += compute_accept_from_unicode( request.headers_in['Sec-WebSocket-Key']) message += b'\r\n\r\n' request.connection.write(message) raise handshake.AbortedByUserException( 'Abort the connection' ) # Prevents pywebsocket from sending its own handshake message.
def web_socket_do_extra_handshake(request): msg = b'\0HTTP/1.1 101 Switching Protocols\r\n' msg += b'Upgrade: websocket\r\n' msg += b'Connection: Upgrade\r\n' msg += b'Sec-WebSocket-Accept: ' msg += compute_accept_from_unicode(request.headers_in['Sec-WebSocket-Key']) msg += b'\r\n\r\n' request.connection.write(msg) # continue writing data until the client disconnects while True: time.sleep(1) numFrames = 1024 // len(msg) # write over 1024 bytes including the above handshake for i in range(0, numFrames): request.connection.write(msg)
def web_socket_do_extra_handshake(request): resources = request.ws_resource.split('?', 1) parameter = None message = b'' if len(resources) == 2: parameter = resources[1] message += b'HTTP/' message += parameter.encode() message += b' 101 Switching Protocols\r\n' message += b'Upgrade: websocket\r\n' message += b'Connection: Upgrade\r\n' message += b'Sec-WebSocket-Accept: ' message += compute_accept_from_unicode(request.headers_in['Sec-WebSocket-Key']) message += b'\r\n\r\n' request.connection.write(message) raise handshake.AbortedByUserException('Abort the connection') # Prevents pywebsocket from sending its own handshake message.
def web_socket_do_extra_handshake(request): resources = request.ws_resource.split('?', 1) parameters = None if len(resources) == 2: parameters = parse.unquote(resources[1]) message = b'HTTP/1.1 101 Switching Protocols\r\n' message += b'Upgrade: websocket\r\n' message += b'Connection: Upgrade\r\n' message += b'Sec-WebSocket-Accept: ' message += compute_accept_from_unicode(request.headers_in['Sec-WebSocket-Key']) message += b'\r\nSec-WebSocket-Extensions: x-webkit-deflate-frame' if parameters: message += ('; %s\r\n' % parameters).encode() else: message += b'\r\n' message += b'\r\n' request.connection.write(message) raise handshake.AbortedByUserException('Abort the connection') # Prevents pywebsocket from sending its own handshake message.
def web_socket_do_extra_handshake(request): resources = request.ws_resource.split('?', 1) parameters = None if len(resources) == 2: parameters = parse.unquote(resources[1]) message_parameters = (b'; %s\r\n' % parameters.encode('utf-8') if parameters else b'\r\n') message = (b'HTTP/1.1 101 Switching Protocols\r\n' b'Upgrade: websocket\r\n' b'Connection: Upgrade\r\n' b'Sec-WebSocket-Accept: %s\r\n' b'Sec-WebSocket-Extensions: permessage-deflate' b'%s\r\n') % (compute_accept_from_unicode( request.headers_in['Sec-WebSocket-Key']), message_parameters) request.connection.write(message) # Prevents pywebsocket from sending its own handshake message. raise handshake.AbortedByUserException('Abort the connection')
def web_socket_do_extra_handshake(request): # This simulates a broken server that sends a WebSocket frame before the # handshake, and more frames afterwards. It is important that if this # happens the client does not buffer all the frames as the server continues # to send more data - it should abort after reading a reasonable number of # bytes (set arbitrarily to 1024). msg = b'HTTP/1.1 101 Switching \0Protocols\r\n' msg += b'Upgrade: websocket\r\n' msg += b'Connection: Upgrade\r\n' msg += b'Sec-WebSocket-Accept: ' msg += compute_accept_from_unicode(request.headers_in['Sec-WebSocket-Key']) msg += b'\r\n\r\n' request.connection.write(msg) # continue writing data until the client disconnects while True: time.sleep(1) numFrames = 1024 // len( msg) # write over 1024 bytes including the above handshake for i in range(0, numFrames): request.connection.write(msg)
def web_socket_do_extra_handshake(request): # Send simple response header. This test implements the handshake # manually. It's not clear why. msg = (b'HTTP/1.1 101 Switching Protocols:\x0D\x0A' b'Connection: Upgrade\x0D\x0A' b'Upgrade: WebSocket\x0D\x0A' b'Set-Cookie: ws_test=test\x0D\x0A' b'Sec-WebSocket-Origin: %s\x0D\x0A' b'Sec-WebSocket-Accept: %s\x0D\x0A\x0D\x0A') % ( request.ws_origin.encode('UTF-8'), hybi.compute_accept_from_unicode( request.headers_in.get(common.SEC_WEBSOCKET_KEY_HEADER))) request.connection.write(msg) # Send a clean close frame. body = stream.create_closing_handshake_body(1000, '') request.connection.write(stream.create_close_frame(body)) # Wait for the responding close frame from the user agent. It's not possible # to use the stream methods at this point because the stream hasn't been # established from pywebsocket's point of view. Instead just read the # appropriate number of bytes and assume they are correct. request.connection.read(8) # Close the socket without pywebsocket sending its own handshake response. raise AbortedByUserException('Abort the connection')