Ticket #8622: file_upload_error_handling.diff

File file_upload_error_handling.diff, 4.2 KB (added by vung, 16 years ago)
  • django/core/handlers/wsgi.py

     
    9292        self.META['PATH_INFO'] = path_info
    9393        self.META['SCRIPT_NAME'] = script_name
    9494        self.method = environ['REQUEST_METHOD'].upper()
     95        self._post_parse_error = False
    9596
    9697    def __repr__(self):
    9798        # Since this is called as part of error handling, we need to be very
     
    100101            get = pformat(self.GET)
    101102        except:
    102103            get = '<could not parse>'
     104        if self._post_parse_error:
     105            post = '<POST data unavailable>'
     106        else:
     107            try:
     108                post = pformat(self.POST)
     109            except:
     110                post = '<could not parse>'
    103111        try:
    104             post = pformat(self.POST)
    105         except:
    106             post = '<could not parse>'
    107         try:
    108112            cookies = pformat(self.COOKIES)
    109113        except:
    110114            cookies = '<could not parse>'
     
    127131        if self.method == 'POST':
    128132            if self.environ.get('CONTENT_TYPE', '').startswith('multipart'):
    129133                self._raw_post_data = ''
    130                 self._post, self._files = self.parse_file_upload(self.META, self.environ['wsgi.input'])
     134                try:
     135                    self._post, self._files = self.parse_file_upload(self.META, self.environ['wsgi.input'])
     136                except:
     137                    # An error occured while parsing POST data.  Since when
     138                    # formatting the error the request handler might access
     139                    # self.POST, set self._post and self._file to prevent
     140                    # attempts to parse POST data again.
     141                    self._post = http.QueryDict('')
     142                    self._files = datastructures.MultiValueDict()
     143                    # Mark that an error occured.  This allows self.__repr__ to
     144                    # be explicit about it instead of simply representing an
     145                    # empty POST
     146                    self._post_parse_error = True
     147                    raise
    131148            else:
    132149                self._post, self._files = http.QueryDict(self.raw_post_data, encoding=self._encoding), datastructures.MultiValueDict()
    133150        else:
  • django/core/handlers/modpython.py

     
    3535            # a common start character for URL patterns. So this is a little
    3636            # naughty, but also pretty harmless.
    3737            self.path_info = u'/'
     38        self._post_parse_error = False
    3839
    3940    def __repr__(self):
    4041        # Since this is called as part of error handling, we need to be very
     
    4344            get = pformat(self.GET)
    4445        except:
    4546            get = '<could not parse>'
     47        if self._post_parse_error:
     48            post = '<POST data unavailable>'
     49        else:
     50            try:
     51                post = pformat(self.POST)
     52            except:
     53                post = '<could not parse>'
    4654        try:
    47             post = pformat(self.POST)
    48         except:
    49             post = '<could not parse>'
    50         try:
    5155            cookies = pformat(self.COOKIES)
    5256        except:
    5357            cookies = '<could not parse>'
     
    7377        "Populates self._post and self._files"
    7478        if 'content-type' in self._req.headers_in and self._req.headers_in['content-type'].startswith('multipart'):
    7579            self._raw_post_data = ''
    76             self._post, self._files = self.parse_file_upload(self.META, self._req)
     80            try:
     81                self._post, self._files = self.parse_file_upload(self.META, self._req)
     82            except:
     83                # This is the same situation found in
     84                # django.core.handlers.wsgi.WSGIHandler
     85                self._post = http.QueryDict('')
     86                self._files = datastructures.MultiValueDict()
     87                self._post_parse_error = True
     88                raise
    7789        else:
    7890            self._post, self._files = http.QueryDict(self.raw_post_data, encoding=self._encoding), datastructures.MultiValueDict()
    7991
Back to Top