tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

simple_handshake_wsh.py (1659B)


      1 #!/usr/bin/python
      2 
      3 from pywebsocket3 import common, stream
      4 from pywebsocket3.handshake import AbortedByUserException, hybi
      5 
      6 
      7 def web_socket_do_extra_handshake(request):
      8    # Send simple response header. This test implements the handshake manually,
      9    # so that we can send the header in the same packet as the close frame.
     10    msg = (b'HTTP/1.1 101 Switching Protocols:\x0D\x0A'
     11           b'Connection: Upgrade\x0D\x0A'
     12           b'Upgrade: WebSocket\x0D\x0A'
     13           b'Set-Cookie: ws_test=test\x0D\x0A'
     14           b'Sec-WebSocket-Origin: %s\x0D\x0A'
     15           b'Sec-WebSocket-Accept: %s\x0D\x0A\x0D\x0A') % (request.ws_origin.encode(
     16               'UTF-8'), hybi.compute_accept_from_unicode(request.headers_in.get(common.SEC_WEBSOCKET_KEY_HEADER)))
     17    # Create a clean close frame.
     18    close_body = stream.create_closing_handshake_body(1001, 'PASS')
     19    close_frame = stream.create_close_frame(close_body)
     20    # Concatenate the header and the close frame and write them to the socket.
     21    request.connection.write(msg + close_frame)
     22    # Wait for the responding close frame from the user agent. It's not possible
     23    # to use the stream methods at this point because the stream hasn't been
     24    # established from pywebsocket's point of view. Instead just read the
     25    # correct number of bytes.
     26    # Warning: reading the wrong number of bytes here will make the test
     27    # flaky.
     28    MASK_LENGTH = 4
     29    request.connection.read(len(close_frame) + MASK_LENGTH)
     30    # Close the socket without pywebsocket sending its own handshake response.
     31    raise AbortedByUserException('Abort the connection')
     32 
     33 
     34 def web_socket_transfer_data(request):
     35    pass