This commit is contained in:
Hiroki Ohtani
2014-10-14 15:21:31 +09:00
parent d18abf3083
commit 572367d0c1
3 changed files with 26 additions and 8 deletions

View File

@@ -20,9 +20,14 @@ for case in range(1, count+1):
try: try:
ws = websocket.create_connection(url) ws = websocket.create_connection(url)
opcode, msg = ws.recv_data() 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): if opcode in (websocket.ABNF.OPCODE_TEXT, websocket.ABNF.OPCODE_BINARY):
ws.send(msg, opcode) ws.send(msg, opcode)
success += 1 success += 1
except UnicodeDecodeError:
# this case is ok.
success += 1
except Exception as e: except Exception as e:
failed += 1 failed += 1
print("[Faield] Test Case: " + str(case)) print("[Faield] Test Case: " + str(case))

View File

@@ -75,6 +75,9 @@ class ABNF(object):
self.data = data self.data = data
self.get_mask_key = os.urandom self.get_mask_key = os.urandom
if rsv1 or rsv2 or rsv3:
raise NotImplementedError("rsv is not implemented, yet")
def __str__(self): def __str__(self):
return "fin=" + str(self.fin) \ return "fin=" + str(self.fin) \
+ " opcode=" + str(self.opcode) \ + " opcode=" + str(self.opcode) \

View File

@@ -723,7 +723,8 @@ class WebSocket(object):
self.send_close() self.send_close()
return (frame.opcode, frame.data) return (frame.opcode, frame.data)
elif frame.opcode == ABNF.OPCODE_PING: elif frame.opcode == ABNF.OPCODE_PING:
self.pong(frame.data) if len(frame.data) < 126:
self.pong(frame.data)
if control_frame: if control_frame:
return (frame.opcode, frame.data) return (frame.opcode, frame.data)
elif frame.opcode == ABNF.OPCODE_PONG: elif frame.opcode == ABNF.OPCODE_PONG:
@@ -746,17 +747,24 @@ class WebSocket(object):
# 'NoneType' object has no attribute 'opcode' # 'NoneType' object has no attribute 'opcode'
raise WebSocketException("Not a valid frame %s" % frame) raise WebSocketException("Not a valid frame %s" % frame)
elif frame.opcode in (ABNF.OPCODE_TEXT, ABNF.OPCODE_BINARY, ABNF.OPCODE_CONT): 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") 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: if frame.fin or self.fire_cont_frame:
data = self._cont_data data = self._cont_data
self._cont_data = None self._cont_data = None
return data return data
elif frame.opcode == ABNF.OPCODE_CLOSE: elif frame.opcode == ABNF.OPCODE_CLOSE:
self.send_close() self.send_close()
return (frame.opcode, frame) return (frame.opcode, frame)
@@ -844,11 +852,13 @@ class WebSocket(object):
except: except:
pass pass
self._closeInternal() self.shutdown()
def _closeInternal(self): def shutdown(self):
"close socket, immediately."
if self.sock: if self.sock:
self.sock.close() self.sock.close()
self.sock = None
def _send(self, data): def _send(self, data):
if isinstance(data, six.text_type): if isinstance(data, six.text_type):