Skip to content
Closed
Show file tree
Hide file tree
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
4 changes: 1 addition & 3 deletions Lib/cgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,11 +663,9 @@ def read_multi(self, environ, keep_blank_values, strict_parsing):

def read_single(self):
"""Internal: read an atomic part."""
self.read_lines()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I just tested this patch locally. Seems like with some clients, the webserver will not get a EOF if the client sent content-length header. So the this will result in an indefinite block on fp.readline(). We should honor the content-length, and not try to read more than that in any case.

if self.length >= 0:
self.read_binary()
self.skip_lines()
else:
self.read_lines()
self.file.seek(0)

bufsize = 8*1024 # I/O buffering size for copy to file
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_cgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,18 @@ def test_fieldstorage_part_content_length(self):
self.assertEqual(fs.list[0].name, 'submit-name')
self.assertEqual(fs.list[0].value, 'Larry')

def test_content_length_no_content_disposition(self):
body = b'{"test":123}'
env = {
'CONTENT_LENGTH': len(body),
'REQUEST_METHOD': 'POST',
'CONTENT_TYPE': 'application/json',
'wsgi.input': BytesIO(body),
}

form = cgi.FieldStorage(fp=env['wsgi.input'], environ=env)
self.assertEqual(form.file.read(), body.decode(form.encoding))

def test_field_storage_multipart_no_content_length(self):
fp = BytesIO(b"""--MyBoundary
Content-Disposition: form-data; name="my-arg"; filename="foo"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixes a bug in :mod:`cgi` when a request has a "Content-Length" header but
without "Content-Disposition" headers.