﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
14024	Application freezes on access to request.POST under WSGIRequest+multipart/form-data	Piotr Czachur	nobody	"
==How to reproduce==
 1. manage.py runserver
 2. send POST using multipart/form-data encoding
 2. access request.raw_post_data, '''before request.POST was accessed''', for example in first middleware's process_request()
 3. access request.POST -- here application freezes

==Why it happens==

Accessing WSGIRequest.raw_post_data reads data from WSGIRequest.environ['wsgi.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.environ['wsgi.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)"		closed	HTTP handling	dev		duplicate			Unreviewed	1	0	0	0	0	0
