2010-06-18 13:48:42 -07:00
|
|
|
from mod_pywebsocket import msgutil
|
|
|
|
|
|
|
|
import time
|
|
|
|
import sys
|
2011-12-15 15:19:51 -08:00
|
|
|
import struct
|
2010-06-18 13:48:42 -07:00
|
|
|
|
|
|
|
# see the list of tests in test_websocket.html
|
|
|
|
|
|
|
|
def web_socket_do_extra_handshake(request):
|
2011-05-21 18:27:52 -07:00
|
|
|
# must set request.ws_protocol to the selected version from ws_requested_protocols
|
2011-08-03 09:42:02 -07:00
|
|
|
for x in request.ws_requested_protocols:
|
|
|
|
if x != "test-does-not-exist":
|
|
|
|
request.ws_protocol = x
|
|
|
|
break
|
2011-05-21 18:27:52 -07:00
|
|
|
|
2011-06-16 11:27:11 -07:00
|
|
|
if request.ws_protocol == "test-2.1":
|
2011-08-03 12:15:25 -07:00
|
|
|
time.sleep(3)
|
2011-06-16 11:27:11 -07:00
|
|
|
elif request.ws_protocol == "test-9":
|
2011-08-03 12:15:25 -07:00
|
|
|
time.sleep(3)
|
2011-06-16 11:27:11 -07:00
|
|
|
elif request.ws_protocol == "test-10":
|
2011-08-03 12:15:25 -07:00
|
|
|
time.sleep(3)
|
2011-06-16 11:27:11 -07:00
|
|
|
elif request.ws_protocol == "test-19":
|
|
|
|
raise ValueError('Aborting (test-19)')
|
|
|
|
elif request.ws_protocol == "test-20" or request.ws_protocol == "test-17":
|
2011-08-03 12:15:25 -07:00
|
|
|
time.sleep(3)
|
2011-06-16 11:27:11 -07:00
|
|
|
elif request.ws_protocol == "test-22":
|
2011-08-03 12:15:25 -07:00
|
|
|
# The timeout is 5 seconds
|
|
|
|
time.sleep(13)
|
2011-12-15 15:23:00 -08:00
|
|
|
elif request.ws_protocol == "test-41b":
|
|
|
|
request.sts = "max-age=100"
|
2010-06-18 13:48:42 -07:00
|
|
|
else:
|
|
|
|
pass
|
|
|
|
|
2012-05-17 10:56:56 -07:00
|
|
|
# Behave according to recommendation of RFC 6455, section # 5.5.1:
|
|
|
|
# "When sending a Close frame in response, the endpoint typically echos the
|
|
|
|
# status code it received."
|
|
|
|
# - Without this, pywebsocket replies with 1000 to any close code.
|
|
|
|
#
|
|
|
|
# Note that this function is only called when the client initiates the close
|
|
|
|
def web_socket_passive_closing_handshake(request):
|
|
|
|
if request.ws_close_code == 1005:
|
|
|
|
return None, None
|
|
|
|
else:
|
|
|
|
return request.ws_close_code, request.ws_close_reason
|
|
|
|
|
|
|
|
|
2010-06-18 13:48:42 -07:00
|
|
|
def web_socket_transfer_data(request):
|
2011-06-16 11:27:11 -07:00
|
|
|
if request.ws_protocol == "test-2.1" or request.ws_protocol == "test-2.2":
|
2010-06-27 14:09:29 -07:00
|
|
|
msgutil.close_connection(request)
|
2011-06-16 11:27:11 -07:00
|
|
|
elif request.ws_protocol == "test-6":
|
2010-06-27 14:09:29 -07:00
|
|
|
resp = "wrong message"
|
|
|
|
if msgutil.receive_message(request) == "1":
|
|
|
|
resp = "2"
|
|
|
|
msgutil.send_message(request, resp.decode('utf-8'))
|
|
|
|
resp = "wrong message"
|
|
|
|
if msgutil.receive_message(request) == "3":
|
|
|
|
resp = "4"
|
|
|
|
msgutil.send_message(request, resp.decode('utf-8'))
|
|
|
|
resp = "wrong message"
|
|
|
|
if msgutil.receive_message(request) == "5":
|
|
|
|
resp = "あいうえお"
|
|
|
|
msgutil.send_message(request, resp.decode('utf-8'))
|
2011-05-21 18:27:52 -07:00
|
|
|
msgutil.close_connection(request)
|
2011-06-16 11:27:11 -07:00
|
|
|
elif request.ws_protocol == "test-7":
|
2011-08-03 17:32:23 -07:00
|
|
|
msgutil.send_message(request, "test-7 data")
|
2011-06-16 11:27:11 -07:00
|
|
|
elif request.ws_protocol == "test-10":
|
2010-06-18 13:48:42 -07:00
|
|
|
msgutil.close_connection(request)
|
2011-06-16 11:27:11 -07:00
|
|
|
elif request.ws_protocol == "test-11":
|
2010-06-18 13:48:42 -07:00
|
|
|
resp = "wrong message"
|
|
|
|
if msgutil.receive_message(request) == "client data":
|
|
|
|
resp = "server data"
|
|
|
|
msgutil.send_message(request, resp.decode('utf-8'))
|
2011-06-16 11:27:11 -07:00
|
|
|
elif request.ws_protocol == "test-12":
|
2012-07-09 17:31:01 -07:00
|
|
|
msg = msgutil.receive_message(request)
|
|
|
|
if msg == u'a\ufffdb':
|
|
|
|
# converted unpaired surrogate in UTF-16 to UTF-8 OK
|
|
|
|
msgutil.send_message(request, "SUCCESS")
|
|
|
|
else:
|
|
|
|
msgutil.send_message(request, "FAIL got '" + msg
|
|
|
|
+ "' instead of string with replacement char'")
|
2011-06-16 11:27:11 -07:00
|
|
|
elif request.ws_protocol == "test-13":
|
2010-06-18 13:48:42 -07:00
|
|
|
# first one binary message containing the byte 0x61 ('a')
|
|
|
|
request.connection.write('\xff\x01\x61')
|
|
|
|
# after a bad utf8 message
|
|
|
|
request.connection.write('\x01\x61\xff')
|
|
|
|
msgutil.close_connection(request)
|
2011-06-16 11:27:11 -07:00
|
|
|
elif request.ws_protocol == "test-14":
|
2011-05-21 18:27:52 -07:00
|
|
|
msgutil.close_connection(request)
|
2010-06-18 13:48:42 -07:00
|
|
|
msgutil.send_message(request, "server data")
|
2011-06-16 11:27:11 -07:00
|
|
|
elif request.ws_protocol == "test-15":
|
2011-12-20 00:20:12 -08:00
|
|
|
# DISABLED: close_connection hasn't supported 2nd 'abort' argument for a
|
|
|
|
# long time. Passing extra arg was causing exception, which conveniently
|
|
|
|
# caused abort :) but as of pywebsocket v606 raising an exception here no
|
|
|
|
# longer aborts, and there's no obvious way to close TCP connection w/o
|
|
|
|
# sending websocket CLOSE.
|
|
|
|
raise RuntimeError("test-15 should be disabled for now")
|
|
|
|
#msgutil.close_connection(request, True) # OBSOLETE 2nd arg
|
2010-06-27 14:09:29 -07:00
|
|
|
return
|
2011-06-16 11:27:11 -07:00
|
|
|
elif request.ws_protocol == "test-17" or request.ws_protocol == "test-21":
|
2011-08-03 12:15:25 -07:00
|
|
|
time.sleep(2)
|
2010-06-18 13:48:42 -07:00
|
|
|
resp = "wrong message"
|
2010-06-27 14:09:29 -07:00
|
|
|
if msgutil.receive_message(request) == "client data":
|
|
|
|
resp = "server data"
|
2010-06-18 13:48:42 -07:00
|
|
|
msgutil.send_message(request, resp.decode('utf-8'))
|
2011-08-03 12:15:25 -07:00
|
|
|
time.sleep(2)
|
2010-06-27 14:09:29 -07:00
|
|
|
msgutil.close_connection(request)
|
2011-06-16 11:27:11 -07:00
|
|
|
elif request.ws_protocol == "test-20":
|
2010-06-27 14:09:29 -07:00
|
|
|
msgutil.send_message(request, "server data")
|
2010-06-18 13:48:42 -07:00
|
|
|
msgutil.close_connection(request)
|
2011-08-03 12:15:25 -07:00
|
|
|
elif request.ws_protocol == "test-34":
|
|
|
|
request.ws_stream.close_connection(1001, "going away now")
|
|
|
|
elif request.ws_protocol == "test-35a":
|
|
|
|
while not request.client_terminated:
|
|
|
|
msgutil.receive_message(request)
|
|
|
|
global test35code
|
|
|
|
test35code = request.ws_close_code
|
|
|
|
global test35reason
|
|
|
|
test35reason = request.ws_close_reason
|
|
|
|
elif request.ws_protocol == "test-35b":
|
|
|
|
request.ws_stream.close_connection(test35code + 1, test35reason)
|
|
|
|
elif request.ws_protocol == "test-37b":
|
|
|
|
while not request.client_terminated:
|
|
|
|
msgutil.receive_message(request)
|
|
|
|
global test37code
|
|
|
|
test37code = request.ws_close_code
|
|
|
|
global test37reason
|
|
|
|
test37reason = request.ws_close_reason
|
|
|
|
elif request.ws_protocol == "test-37c":
|
|
|
|
request.ws_stream.close_connection(test37code, test37reason)
|
2011-09-20 15:13:43 -07:00
|
|
|
elif request.ws_protocol == "test-42":
|
|
|
|
# Echo back 3 messages
|
|
|
|
msgutil.send_message(request,
|
|
|
|
msgutil.receive_message(request))
|
|
|
|
msgutil.send_message(request,
|
|
|
|
msgutil.receive_message(request))
|
|
|
|
msgutil.send_message(request,
|
|
|
|
msgutil.receive_message(request))
|
2011-12-15 15:19:51 -08:00
|
|
|
elif request.ws_protocol == "test-44":
|
|
|
|
rcv = msgutil.receive_message(request)
|
|
|
|
# check we received correct binary msg
|
|
|
|
if len(rcv) == 3 \
|
|
|
|
and ord(rcv[0]) == 5 and ord(rcv[1]) == 0 and ord(rcv[2]) == 7:
|
|
|
|
# reply with binary msg 0x04
|
|
|
|
msgutil.send_message(request, struct.pack("cc", chr(0), chr(4)), True, True)
|
|
|
|
else:
|
|
|
|
msgutil.send_message(request, "incorrect binary msg received!")
|
|
|
|
elif request.ws_protocol == "test-45":
|
|
|
|
rcv = msgutil.receive_message(request)
|
|
|
|
# check we received correct binary msg
|
|
|
|
if rcv == "flob":
|
|
|
|
# send back same blob as binary msg
|
|
|
|
msgutil.send_message(request, rcv, True, True)
|
|
|
|
else:
|
|
|
|
msgutil.send_message(request, "incorrect binary msg received: '" + rcv + "'")
|
2011-12-20 00:20:12 -08:00
|
|
|
elif request.ws_protocol == "test-46":
|
|
|
|
msgutil.send_message(request, "client must drop this if close was called")
|
2011-12-15 15:19:51 -08:00
|
|
|
|
2010-06-18 13:48:42 -07:00
|
|
|
while not request.client_terminated:
|
|
|
|
msgutil.receive_message(request)
|