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