Opened 14 years ago
Closed 14 years ago
#14024 closed (duplicate)
Application freezes on access to request.POST under WSGIRequest+multipart/form-data
Reported by: | Piotr Czachur | Owned by: | nobody |
---|---|---|---|
Component: | HTTP handling | Version: | dev |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
==How to reproduce==
- manage.py runserver
- send POST using multipart/form-data encoding
- access request.raw_post_data, before request.POST was accessed, for example in first middleware's process_request()
- access request.POST -- here application freezes
==Why it happens==
Accessing WSGIRequest.raw_post_data reads data from WSGIRequest.environwsgi.input stream, which afterwards is exhausted.
Accessing request.POST calls WSGIRequest._load_post_and_files(), which calls MultiPartParser.parse(), which tries to parse already exhausted stream from WSGIRequest.environwsgi.input.
How to fix (fix is working, I don't claim it's a good way)
--- core/handlers/wsgi.py (revision 13353) +++ core/handlers/wsgi.py (local) @@ -132,7 +132,7 @@ # Populates self._post and self._files if self.method == 'POST': if self.environ.get('CONTENT_TYPE', '').startswith('multipart'): - self._raw_post_data = '' + self._get_raw_post_data() try: self._post, self._files = self.parse_file_upload(self.META, self.environ['wsgi.input']) except: @@ -204,7 +204,8 @@ safe_copyfileobj(self.environ['wsgi.input'], buf, size=content_length) self._raw_post_data = buf.getvalue() - buf.close() + buf.seek(0) + self.environ['wsgi.input'] = buf return self._raw_post_data GET = property(_get_get, _set_get)
Note:
See TracTickets
for help on using tickets.
Problem sounds the same as #12522, though proposed fixes are different.