From 572367d0c1c6cc6a4fb749bfa4009af64a91c0dc Mon Sep 17 00:00:00 2001 From: Hiroki Ohtani Date: Tue, 14 Oct 2014 15:21:31 +0900 Subject: [PATCH] refs #117 --- compliance/test_fuzzingclient.py | 5 +++++ websocket/_abnf.py | 3 +++ websocket/_core.py | 26 ++++++++++++++++++-------- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/compliance/test_fuzzingclient.py b/compliance/test_fuzzingclient.py index 4e42075..038a2c6 100644 --- a/compliance/test_fuzzingclient.py +++ b/compliance/test_fuzzingclient.py @@ -20,9 +20,14 @@ for case in range(1, count+1): try: ws = websocket.create_connection(url) opcode, msg = ws.recv_data() + if opcode == websocket.ABNF.OPCODE_TEXT: + msg.decode("utf-8") if opcode in (websocket.ABNF.OPCODE_TEXT, websocket.ABNF.OPCODE_BINARY): ws.send(msg, opcode) success += 1 + except UnicodeDecodeError: + # this case is ok. + success += 1 except Exception as e: failed += 1 print("[Faield] Test Case: " + str(case)) diff --git a/websocket/_abnf.py b/websocket/_abnf.py index a92f0d1..8252f4c 100644 --- a/websocket/_abnf.py +++ b/websocket/_abnf.py @@ -75,6 +75,9 @@ class ABNF(object): self.data = data self.get_mask_key = os.urandom + if rsv1 or rsv2 or rsv3: + raise NotImplementedError("rsv is not implemented, yet") + def __str__(self): return "fin=" + str(self.fin) \ + " opcode=" + str(self.opcode) \ diff --git a/websocket/_core.py b/websocket/_core.py index 88ae9db..49e8d49 100644 --- a/websocket/_core.py +++ b/websocket/_core.py @@ -723,7 +723,8 @@ class WebSocket(object): self.send_close() return (frame.opcode, frame.data) elif frame.opcode == ABNF.OPCODE_PING: - self.pong(frame.data) + if len(frame.data) < 126: + self.pong(frame.data) if control_frame: return (frame.opcode, frame.data) elif frame.opcode == ABNF.OPCODE_PONG: @@ -746,17 +747,24 @@ class WebSocket(object): # 'NoneType' object has no attribute 'opcode' raise WebSocketException("Not a valid frame %s" % frame) elif frame.opcode in (ABNF.OPCODE_TEXT, ABNF.OPCODE_BINARY, ABNF.OPCODE_CONT): - if frame.opcode == ABNF.OPCODE_CONT and not self._cont_data: + if frame.opcode == ABNF.OPCODE_CONT and not self._recving_frames: raise WebSocketException("Illegal frame") - if self._cont_data: - self._cont_data[1].data += frame.data - else: - self._cont_data = [frame.opcode, frame] + if self._cont_data: + self._cont_data[1] += frame.data + else: + if frame.opcode in (ABNF.OPCODE_TEXT, ABNF.OPCODE_BINARY): + self._recving_frames = frame.opcode + self._cont_data = [frame.opcode, frame.data] + + if frame.fin: + self._recving_frames = None + if frame.fin or self.fire_cont_frame: data = self._cont_data self._cont_data = None return data + elif frame.opcode == ABNF.OPCODE_CLOSE: self.send_close() return (frame.opcode, frame) @@ -844,11 +852,13 @@ class WebSocket(object): except: pass - self._closeInternal() + self.shutdown() - def _closeInternal(self): + def shutdown(self): + "close socket, immediately." if self.sock: self.sock.close() + self.sock = None def _send(self, data): if isinstance(data, six.text_type):