def web_socket_transfer_data(request): # pywebsocket does not mask message by default. We need to build a frame manually to mask it. request.connection.write(stream.create_text_frame('First message', mask=False)) request.connection.write(stream.create_text_frame('Fragmented ', opcode=common.OPCODE_TEXT, fin=0, mask=False)) request.connection.write(stream.create_text_frame('message', opcode=common.OPCODE_CONTINUATION, fin=1, mask=False)) request.connection.write(stream.create_text_frame('', mask=False)) msgutil.send_message(request, 'END') # Wait for the client to start closing handshake. # To receive a close frame, we must use an internal method of request.ws_stream. opcode, payload, final, reserved1, reserved2, reserved3 = request.ws_stream._receive_frame() assert opcode == common.OPCODE_CLOSE assert final assert not reserved1 assert not reserved2 assert not reserved3 # Send a masked close frame. Clients should be able to handle this frame and # the WebSocket object should be closed cleanly. request.connection.write(stream.create_close_frame('', mask=False)) # Prevents pywebsocket from starting its own closing handshake. raise handshake.AbortedByUserException('Abort the connection')
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_transfer_data(request): # pywebsocket does not mask message by default. We need to build a frame # manually to mask it. request.connection.write( stream.create_text_frame('First message', mask=False)) request.connection.write( stream.create_text_frame( 'Fragmented ', opcode=common.OPCODE_TEXT, fin=0, mask=False)) request.connection.write( stream.create_text_frame( 'message', opcode=common.OPCODE_CONTINUATION, fin=1, mask=False)) request.connection.write(stream.create_text_frame('', mask=False)) msgutil.send_message(request, 'END') # Wait for the client to start closing handshake. To receive a close frame, # we must use an internal method of request.ws_stream. opcode, payload, final, reserved1, reserved2, reserved3 = \ request.ws_stream._receive_frame() assert opcode == common.OPCODE_CLOSE assert final assert not reserved1 assert not reserved2 assert not reserved3 # Send a masked close frame. Clients should be able to handle this frame # and the WebSocket object should be closed cleanly. request.connection.write(stream.create_close_frame('', mask=False)) # Prevents pywebsocket from starting its own closing handshake. raise handshake.AbortedByUserException('Abort the connection')
def web_socket_transfer_data(request): while True: body = stream.create_closing_handshake_body(1000, '') text_frame = stream.create_text_frame("(none)") close_frame = stream.create_close_frame(body) request.ws_stream._write(text_frame + close_frame) request.server_terminated = True line = request.ws_stream.receive_message() return
def web_socket_transfer_data(request): line = request.ws_stream.receive_message() if line is None: return if line == '-': data = '' elif line == '--': data = 'X' else: code, reason = line.split(' ', 1) data = struct.pack('!H', int(code)) + reason.encode('utf-8') request.connection.write(stream.create_close_frame(data))
def web_socket_transfer_data(request): while True: line = request.ws_stream.receive_message() if line is None: return code, reason = line.split(" ", 1) if code is None or reason is None: return data = struct.pack("!H", int(code)) + reason.encode("UTF-8") request.connection.write(stream.create_close_frame(data)) # Suppress to re-respond client responding close frame. raise Exception("customized server initiated closing handshake")
def web_socket_do_extra_handshake(request): # Send simple response header. This test implements the handshake # manually. It's not clear why. request.connection.write('HTTP/1.1 101 Switching Protocols:\x0D\x0AConnection: Upgrade\x0D\x0AUpgrade: WebSocket\x0D\x0ASet-Cookie: ws_test=test\x0D\x0ASec-WebSocket-Origin: '+request.ws_origin+'\x0D\x0ASec-WebSocket-Accept: '+hybi.compute_accept(request.headers_in.get(common.SEC_WEBSOCKET_KEY_HEADER))[0]+'\x0D\x0A\x0D\x0A') # 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')
def web_socket_transfer_data(request): # Wait for a close frame sent from the client. close_frame = request.ws_stream.receive_bytes(6) msgutil.send_message(request, 'Client should ignore this message') # Send only first two bytes of the received frame. The remaining four bytes are # "masking key", which changes every time the test runs. data = struct.pack('!H', 1000) + 'close_frame[:2]=%r' % close_frame[:2] request.connection.write(stream.create_close_frame(data)) # Tell pywebsocket we have sent a close frame to the client, so it can close # the connection. request.server_terminated = True
def web_socket_do_extra_handshake(request): # Send simple response header. This test implements the handshake # manually. It's not clear why. request.connection.write( 'HTTP/1.1 101 Switching Protocols:\x0D\x0AConnection: Upgrade\x0D\x0AUpgrade: WebSocket\x0D\x0ASet-Cookie: ws_test=test\x0D\x0ASec-WebSocket-Origin: ' + request.ws_origin + '\x0D\x0ASec-WebSocket-Accept: ' + hybi.compute_accept( request.headers_in.get(common.SEC_WEBSOCKET_KEY_HEADER))[0] + '\x0D\x0A\x0D\x0A') # 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')
def web_socket_transfer_data(request): # Wait for a close frame sent from the client. close_frame = request.ws_stream.receive_bytes(6) msgutil.send_message(request, 'Client should ignore this message') # Send only first two bytes of the received frame. The remaining four bytes are # "masking key", which changes every time the test runs. data = struct.pack('!H', 1000) + 'close_frame[:2]=%r' % close_frame[:2] request.connection.write(stream.create_close_frame(data)) # If the following assertion fails, AssertionError will be raised, # which will prevent pywebsocket from sending a close frame. # In this case, the client will fail to finish closing handshake, thus # closeEvent.wasClean will become false. assert close_frame[:2] == '\x88\x82' # Pretend we have received a close frame from the client. # After this function exits, pywebsocket will send a close frame automatically. request.client_terminated = True
def web_socket_transfer_data(request): # Wait for a close frame sent from the client. close_frame = request.ws_stream.receive_bytes(6) msgutil.send_message(request, 'Client should ignore this message') # Send only first two bytes of the received frame. The remaining four bytes are # "masking key", which changes every time the test runs. data = struct.pack('!H', 1000) + 'close_frame[:2]=%r' % close_frame[:2] request.connection.write(stream.create_close_frame(data)) # If the following assertion fails, AssertionError will be raised, # which will prevent pywebsocket from sending a close frame. # In this case, the client will fail to finish closing handshake, thus # closeEvent.wasClean will become false. assert close_frame[:2] == '\x88\x80' # Pretend we have received a close frame from the client. # After this function exits, pywebsocket will send a close frame automatically. request.client_terminated = True
def web_socket_passive_closing_handshake(request): code = struct.pack("!H", 3004) packet = stream.create_close_frame(code + "split test".encode("utf-8")) request.connection.write(packet[:1]) request.connection.write(packet[1:]) raise handshake.AbortedByUserException("Abort the connection")
def web_socket_passive_closing_handshake(request): code = struct.pack('!H', 3004) packet = stream.create_close_frame(code + 'split test'.encode('utf-8')) request.connection.write(packet[:1]) request.connection.write(packet[1:]) raise handshake.AbortedByUserException('Abort the connection')
def web_socket_passive_closing_handshake(request): code = struct.pack('!H', 1000) packet = stream.create_close_frame(code + 'split test'.encode('utf-8')) request.connection.write(packet[:1]) request.connection.write(packet[1:]) raise handshake.AbortedByUserException('Abort the connection')