Skip to content

Commit

Permalink
wsgi: fix Input.readline on Python 3
Browse files Browse the repository at this point in the history
Previously, we would compare the last item of a byte string
with a newline in a native string. On Python 3, getting a
single item from a byte string give you an integer (which
will not be equal to any string), so readline would return
the entire request body.

While we're at it, fix the return type when the caller requests
that zero bytes be read.
  • Loading branch information
tipabu authored and jstasiak committed Feb 28, 2019
1 parent f0bc79e commit 53001f5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions eventlet/wsgi.py
Expand Up @@ -157,7 +157,7 @@ def _chunked_read(self, rfile, length=None, use_readline=False):
self.is_hundred_continue_response_sent = True
try:
if length == 0:
return ""
return b""

if length and length < 0:
length = None
Expand Down Expand Up @@ -190,7 +190,7 @@ def _chunked_read(self, rfile, length=None, use_readline=False):
length -= datalen
if length == 0:
break
if use_readline and data[-1] == "\n":
if use_readline and data[-1:] == b"\n":
break
else:
try:
Expand Down
13 changes: 13 additions & 0 deletions tests/wsgi_test.py
Expand Up @@ -1773,6 +1773,9 @@ def application(self, env, start_response):
elif pi == "/lines":
for x in input:
response.append(x)
elif pi == "/readline":
response.extend(iter(input.readline, b''))
response.append(('\nread %d lines' % len(response)).encode())
elif pi == "/ping":
input.read()
response.append(b"pong")
Expand Down Expand Up @@ -1876,6 +1879,16 @@ def test_chunked_readline(self):
self.assertEqual(read_http(fd).body, b'this is chunked\nline 2\nline3')
fd.close()

def test_chunked_readline_from_input(self):
body = self.body()
req = "POST /readline HTTP/1.1\r\nContent-Length: %s\r\n" \
"transfer-encoding: Chunked\r\n\r\n%s" % (len(body), body)

fd = self.connect()
fd.sendall(req.encode())
self.assertEqual(read_http(fd).body, b'this is chunked\nline 2\nline3\nread 3 lines')
fd.close()

def test_chunked_readline_wsgi_override_minimum_chunk_size(self):

fd = self.connect()
Expand Down

0 comments on commit 53001f5

Please sign in to comment.