Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 from mod_pywebsocket import msgutil
3 import time
4 import sys
5 import struct
7 # see the list of tests in test_websocket.html
9 def web_socket_do_extra_handshake(request):
10 # must set request.ws_protocol to the selected version from ws_requested_protocols
11 for x in request.ws_requested_protocols:
12 if x != "test-does-not-exist":
13 request.ws_protocol = x
14 break
16 if request.ws_protocol == "test-2.1":
17 time.sleep(3)
18 elif request.ws_protocol == "test-9":
19 time.sleep(3)
20 elif request.ws_protocol == "test-10":
21 time.sleep(3)
22 elif request.ws_protocol == "test-19":
23 raise ValueError('Aborting (test-19)')
24 elif request.ws_protocol == "test-20" or request.ws_protocol == "test-17":
25 time.sleep(3)
26 elif request.ws_protocol == "test-22":
27 # The timeout is 5 seconds
28 time.sleep(13)
29 elif request.ws_protocol == "test-41b":
30 request.sts = "max-age=100"
31 else:
32 pass
34 # Behave according to recommendation of RFC 6455, section # 5.5.1:
35 # "When sending a Close frame in response, the endpoint typically echos the
36 # status code it received."
37 # - Without this, pywebsocket replies with 1000 to any close code.
38 #
39 # Note that this function is only called when the client initiates the close
40 def web_socket_passive_closing_handshake(request):
41 if request.ws_close_code == 1005:
42 return None, None
43 else:
44 return request.ws_close_code, request.ws_close_reason
47 def web_socket_transfer_data(request):
48 if request.ws_protocol == "test-2.1" or request.ws_protocol == "test-2.2":
49 msgutil.close_connection(request)
50 elif request.ws_protocol == "test-6":
51 resp = "wrong message"
52 if msgutil.receive_message(request) == "1":
53 resp = "2"
54 msgutil.send_message(request, resp.decode('utf-8'))
55 resp = "wrong message"
56 if msgutil.receive_message(request) == "3":
57 resp = "4"
58 msgutil.send_message(request, resp.decode('utf-8'))
59 resp = "wrong message"
60 if msgutil.receive_message(request) == "5":
61 resp = "あいうえお"
62 msgutil.send_message(request, resp.decode('utf-8'))
63 msgutil.close_connection(request)
64 elif request.ws_protocol == "test-7":
65 msgutil.send_message(request, "test-7 data")
66 elif request.ws_protocol == "test-10":
67 msgutil.close_connection(request)
68 elif request.ws_protocol == "test-11":
69 resp = "wrong message"
70 if msgutil.receive_message(request) == "client data":
71 resp = "server data"
72 msgutil.send_message(request, resp.decode('utf-8'))
73 elif request.ws_protocol == "test-12":
74 msg = msgutil.receive_message(request)
75 if msg == u'a\ufffdb':
76 # converted unpaired surrogate in UTF-16 to UTF-8 OK
77 msgutil.send_message(request, "SUCCESS")
78 else:
79 msgutil.send_message(request, "FAIL got '" + msg
80 + "' instead of string with replacement char'")
81 elif request.ws_protocol == "test-13":
82 # first one binary message containing the byte 0x61 ('a')
83 request.connection.write('\xff\x01\x61')
84 # after a bad utf8 message
85 request.connection.write('\x01\x61\xff')
86 msgutil.close_connection(request)
87 elif request.ws_protocol == "test-14":
88 msgutil.close_connection(request)
89 msgutil.send_message(request, "server data")
90 elif request.ws_protocol == "test-15":
91 # DISABLED: close_connection hasn't supported 2nd 'abort' argument for a
92 # long time. Passing extra arg was causing exception, which conveniently
93 # caused abort :) but as of pywebsocket v606 raising an exception here no
94 # longer aborts, and there's no obvious way to close TCP connection w/o
95 # sending websocket CLOSE.
96 raise RuntimeError("test-15 should be disabled for now")
97 #msgutil.close_connection(request, True) # OBSOLETE 2nd arg
98 return
99 elif request.ws_protocol == "test-17" or request.ws_protocol == "test-21":
100 time.sleep(2)
101 resp = "wrong message"
102 if msgutil.receive_message(request) == "client data":
103 resp = "server data"
104 msgutil.send_message(request, resp.decode('utf-8'))
105 time.sleep(2)
106 msgutil.close_connection(request)
107 elif request.ws_protocol == "test-20":
108 msgutil.send_message(request, "server data")
109 msgutil.close_connection(request)
110 elif request.ws_protocol == "test-34":
111 request.ws_stream.close_connection(1001, "going away now")
112 elif request.ws_protocol == "test-35a":
113 while not request.client_terminated:
114 msgutil.receive_message(request)
115 global test35code
116 test35code = request.ws_close_code
117 global test35reason
118 test35reason = request.ws_close_reason
119 elif request.ws_protocol == "test-35b":
120 request.ws_stream.close_connection(test35code + 1, test35reason)
121 elif request.ws_protocol == "test-37b":
122 while not request.client_terminated:
123 msgutil.receive_message(request)
124 global test37code
125 test37code = request.ws_close_code
126 global test37reason
127 test37reason = request.ws_close_reason
128 elif request.ws_protocol == "test-37c":
129 request.ws_stream.close_connection(test37code, test37reason)
130 elif request.ws_protocol == "test-42":
131 # Echo back 3 messages
132 msgutil.send_message(request,
133 msgutil.receive_message(request))
134 msgutil.send_message(request,
135 msgutil.receive_message(request))
136 msgutil.send_message(request,
137 msgutil.receive_message(request))
138 elif request.ws_protocol == "test-44":
139 rcv = msgutil.receive_message(request)
140 # check we received correct binary msg
141 if len(rcv) == 3 \
142 and ord(rcv[0]) == 5 and ord(rcv[1]) == 0 and ord(rcv[2]) == 7:
143 # reply with binary msg 0x04
144 msgutil.send_message(request, struct.pack("cc", chr(0), chr(4)), True, True)
145 else:
146 msgutil.send_message(request, "incorrect binary msg received!")
147 elif request.ws_protocol == "test-45":
148 rcv = msgutil.receive_message(request)
149 # check we received correct binary msg
150 if rcv == "flob":
151 # send back same blob as binary msg
152 msgutil.send_message(request, rcv, True, True)
153 else:
154 msgutil.send_message(request, "incorrect binary msg received: '" + rcv + "'")
155 elif request.ws_protocol == "test-46":
156 msgutil.send_message(request, "client must drop this if close was called")
158 while not request.client_terminated:
159 msgutil.receive_message(request)