differentiate between None and empty return

This commit is contained in:
Jakob Ketterl 2019-09-26 00:24:55 +01:00
parent 2d1bcf221c
commit 6cb7e65231

View File

@ -14,6 +14,10 @@ class IncompleteRead(Exception):
pass pass
class Drained(Exception):
pass
class WebSocketConnection(object): class WebSocketConnection(object):
connections = [] connections = []
@ -120,7 +124,9 @@ class WebSocketConnection(object):
def protected_read(self, num): def protected_read(self, num):
data = self.handler.rfile.read(num) data = self.handler.rfile.read(num)
if data is None or len(data) != num: if data is None:
raise Drained()
if len(data) != num:
raise IncompleteRead() raise IncompleteRead()
return data return data
@ -134,7 +140,7 @@ class WebSocketConnection(object):
(read, _, _) = select.select([self.interruptPipeRecv, self.handler.rfile], [], []) (read, _, _) = select.select([self.interruptPipeRecv, self.handler.rfile], [], [])
if self.handler.rfile in read: if self.handler.rfile in read:
available = True available = True
while available: while self.open and available:
try: try:
header = self.protected_read(2) header = self.protected_read(2)
opcode = header[0] & 0x0F opcode = header[0] & 0x0F
@ -158,8 +164,11 @@ class WebSocketConnection(object):
self.open = False self.open = False
else: else:
logger.warning("unsupported opcode: {0}".format(opcode)) logger.warning("unsupported opcode: {0}".format(opcode))
except IncompleteRead: except Drained:
available = False available = False
except IncompleteRead:
logger.warning("incomplete read on websocket; closing connection")
self.open = False
except TimeoutError: except TimeoutError:
logger.warning("websocket timed out; closing connection") logger.warning("websocket timed out; closing connection")
self.open = False self.open = False