Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions Lib/telnetlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,18 +431,20 @@ def process_rawq(self):
buf = [b'', b'']
try:
while self.rawq:
c = self.rawq_getchar()
if not self.iacseq:
if c == theNULL:
continue
if c == b"\021":
continue
if c != IAC:
buf[self.sb] = buf[self.sb] + c
continue
slice = self._next_nonIAC_slice()
if slice:
buf[self.sb] = buf[self.sb] + slice
else:
self.iacseq += c
c = self.rawq_getchar()
if c == theNULL:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can theNull be replaced with b'\0'?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think theNull should be replaced by NOOPT (they're equal), but it's another topic. This comparison comes from the initial implementation, I've only reindented.

continue
elif c == b"\021":
continue
else:
self.iacseq += c
elif len(self.iacseq) == 1:
c = self.rawq_getchar()
# 'IAC: IAC CMD [OPTION only for WILL/WONT/DO/DONT]'
if c in (DO, DONT, WILL, WONT):
self.iacseq += c
Expand All @@ -469,6 +471,7 @@ def process_rawq(self):
# unless we did a WILL/DO before.
self.msg('IAC %d not recognized' % ord(c))
elif len(self.iacseq) == 2:
c = self.rawq_getchar()
cmd = self.iacseq[1:2]
self.iacseq = b''
opt = c
Expand All @@ -492,6 +495,28 @@ def process_rawq(self):
self.cookedq = self.cookedq + buf[0]
self.sbdataq = self.sbdataq + buf[1]

def _next_nonIAC_slice(self):
"""Return next non-IAC characters from raw queue.
Assumes the caller checked the raw queue is not empty.
"""
next_i = self.irawq
max_i = len(self.rawq)
while next_i < max_i:
c = self.rawq[next_i]
if c == theNULL[0] or c == 0x11 or c == IAC[0]:
break
next_i += 1

if next_i == self.irawq:
return None
else:
slice = self.rawq[self.irawq:next_i]
self.irawq = next_i
if next_i == max_i:
self.rawq = b''
self.irawq = 0
return slice

def rawq_getchar(self):
"""Get next char from raw queue.

Expand Down Expand Up @@ -522,7 +547,7 @@ def fill_rawq(self):
self.irawq = 0
# The buffer size should be fairly small so as to avoid quadratic
# behavior in process_rawq() above
buf = self.sock.recv(50)
buf = self.sock.recv(4096)
self.msg("recv %r", buf)
self.eof = (not buf)
self.rawq = self.rawq + buf
Expand Down