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): # 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): # A new frame is arrived before the previous fragmented frame has finished. request.connection.write( stream.create_text_frame( 'This message ', opcode=common.OPCODE_TEXT, fin=0)) request.connection.write( stream.create_text_frame( 'should be ignored.', opcode=common.OPCODE_TEXT, fin=1)) # Not OPCODE_CONTINUATION.
def web_socket_transfer_data(request): # Fragmented control frame is prohibited. The client must abort the # connection. request.connection.write( stream.create_text_frame('This message should be ignored.', opcode=common.OPCODE_PING, fin=0))
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): message = b"This message should be ignored." if bit == 1: frame = stream.create_header(common.OPCODE_TEXT, len(message), 1, 1, 0, 0, 0) + message elif bit == 2: frame = stream.create_header(common.OPCODE_TEXT, len(message), 1, 0, 1, 0, 0) + message elif bit == 3: frame = stream.create_header(common.OPCODE_TEXT, len(message), 1, 0, 0, 1, 0) + message else: frame = stream.create_text_frame('FAIL: Invalid bit number: %d' % bit) request.connection.write(frame)
def web_socket_transfer_data(request): message = "This message should be ignored." if bit == 1: frame = stream.create_header(common.OPCODE_TEXT, len(message), 1, 1, 0, 0, 0) + message elif bit == 2: frame = stream.create_header(common.OPCODE_TEXT, len(message), 1, 0, 1, 0, 0) + message elif bit == 3: frame = stream.create_header(common.OPCODE_TEXT, len(message), 1, 0, 0, 1, 0) + message else: frame = stream.create_text_frame('FAIL: Invalid bit number: %d' % bit) request.connection.write(frame)
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' message += 'Upgrade: websocket\r\n' message += 'Connection: Upgrade\r\n' message += 'Sec-WebSocket-Accept: %s\r\n' % compute_accept(request.headers_in['Sec-WebSocket-Key'])[0] message += '\r\n' request.connection.write(message) raise handshake.AbortedByUserException('Abort the connection') # Prevents pywebsocket from sending its own handshake message.
def web_socket_transfer_data(request): text = b'This message should be ignored.' opcode = common.OPCODE_TEXT if bit == 1: frame = stream.create_header(opcode, len(text), 1, 1, 0, 0, 0) + text elif bit == 2: frame = stream.create_header(opcode, len(text), 1, 0, 1, 0, 0) + text elif bit == 3: frame = stream.create_header(opcode, len(text), 1, 0, 0, 1, 0) + text else: frame = stream.create_text_frame('FAIL: Invalid bit number: %d' % bit) request.connection.write(frame)
def web_socket_transfer_data(request): text = 'This message should be ignored.' opcode = common.OPCODE_TEXT if bit == 1: frame = stream.create_header(opcode, len(text), 1, 1, 0, 0, 0) + text elif bit == 2: frame = stream.create_header(opcode, len(text), 1, 0, 1, 0, 0) + text elif bit == 3: frame = stream.create_header(opcode, len(text), 1, 0, 0, 1, 0) + text else: frame = stream.create_text_frame('FAIL: Invalid bit number: %d' % bit) request.connection.write(frame)
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' message += 'Upgrade: websocket\r\n' message += 'Connection: Upgrade\r\n' message += 'Sec-WebSocket-Accept: %s\r\n' % compute_accept( request.headers_in['Sec-WebSocket-Key'])[0] message += '\r\n' request.connection.write(message) raise handshake.AbortedByUserException( 'Abort the connection' ) # Prevents pywebsocket from sending its own handshake message.
def web_socket_transfer_data(request): match = re.search(r'\?bit=(\d+)$', request.ws_resource) if match is None: msgutil.send_message(request, 'FAIL: Query value is incorrect or missing') return bit = int(match.group(1)) message = "This message should be ignored." if bit == 1: frame = stream.create_header(common.OPCODE_TEXT, len(message), 1, 1, 0, 0, 0) + message elif bit == 2: frame = stream.create_header(common.OPCODE_TEXT, len(message), 1, 0, 1, 0, 0) + message elif bit == 3: frame = stream.create_header(common.OPCODE_TEXT, len(message), 1, 0, 0, 1, 0) + message else: frame = stream.create_text_frame('FAIL: Invalid bit number: %d' % bit) request.connection.write(frame)
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). frame = stream.create_text_frame('\0Frame-contains-thirty-two-bytes') msg = frame msg += 'HTTP/1.1 101 Switching Protocols\r\n' msg += 'Upgrade: websocket\r\n' msg += 'Connection: Upgrade\r\n' msg += 'Sec-WebSocket-Accept: %s\r\n' % compute_accept(request.headers_in['Sec-WebSocket-Key'])[0] msg += '\r\n' request.connection.write(msg) # continue writing data until the client disconnects while True: time.sleep(1) numFrames = 1024 / len(frame) # write over 1024 bytes including the above handshake for i in range(0, numFrames): request.connection.write(frame)
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). frame = stream.create_text_frame('\0Frame-contains-thirty-two-bytes') msg = frame msg += (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'\r\n') % compute_accept_from_unicode(request.headers_in['Sec-WebSocket-Key']) request.connection.write(msg) # continue writing data until the client disconnects while True: time.sleep(1) # write over 1024 bytes including the above handshake numFrames = 1024 // len(frame) for i in range(0, numFrames): request.connection.write(frame)
def web_socket_transfer_data(request): # send 2 messages in one packet. request.connection.write( stream.create_text_frame("first message") + stream.create_text_frame("second message"))
def web_socket_transfer_data(request): # Fragmented control frame is prohibited. The client must abort the connection. request.connection.write(stream.create_text_frame('This message should be ignored.', opcode=common.OPCODE_PING, fin=0))
def web_socket_transfer_data(request): # All control frames must have a payload length of 125 bytes or less. message = 'X' * 126 request.connection.write(stream.create_text_frame(message, opcode=common.OPCODE_PING, fin=1))
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('The Masked Message', mask=True))
def web_socket_transfer_data(request): # send 2 messages in one packet. request.connection.write(stream.create_text_frame("first message") + stream.create_text_frame("second message"))
def web_socket_transfer_data(request): # A new frame is arrived before the previous fragmented frame has finished. request.connection.write(stream.create_text_frame('This message ', opcode=common.OPCODE_TEXT, fin=0)) request.connection.write(stream.create_text_frame('should be ignored.', opcode=common.OPCODE_TEXT, fin=1)) # Not OPCODE_CONTINUATION.
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('The Masked Message', mask=True))
def web_socket_transfer_data(request): # All control frames must have a payload length of 125 bytes or less. message = 'X' * 126 request.connection.write( stream.create_text_frame(message, opcode=common.OPCODE_PING, fin=1))