示例#1
0
 def __init__(self, environ):
     script_name = base.get_script_name(environ)
     path_info = force_unicode(environ.get('PATH_INFO', '/'))
     if not path_info or path_info == script_name:
         # Sometimes PATH_INFO exists, but is empty (e.g. accessing
         # the SCRIPT_NAME URL without a trailing slash). We really need to
         # operate as if they'd requested '/'. Not amazingly nice to force
         # the path like this, but should be harmless.
         #
         # (The comparison of path_info to script_name is to work around an
         # apparent bug in flup 1.0.1. See Django ticket #8490).
         path_info = '/'
     self.environ = environ
     self.path_info = path_info
     self.path = '%s%s' % (script_name, path_info)
     self.META = environ
     self.META['PATH_INFO'] = path_info
     self.META['SCRIPT_NAME'] = script_name
     self.method = environ['REQUEST_METHOD'].upper()
     self._post_parse_error = False
     try:
         content_length = int(self.environ.get('CONTENT_LENGTH'))
     except (ValueError, TypeError):
         content_length = 0
     self._stream = LimitedStream(self.environ['wsgi.input'], content_length)
     self._read_started = False
示例#2
0
    def __call__(self, environ, start_response):
        # Set up middleware if needed. We couldn't do this earlier, because
        # settings weren't available.
        if self._request_middleware is None:
            with self.initLock:
                try:
                    # Check that middleware is still uninitialised.
                    if self._request_middleware is None:
                        self.load_middleware()
                except:
                    # Unload whatever middleware we got
                    self._request_middleware = None
                    raise

        set_script_prefix(base.get_script_name(environ))
        signals.request_started.send(sender=self.__class__)
        try:
            try:
                request = self.request_class(environ)
            except UnicodeDecodeError:
                logger.warning('Bad Request (UnicodeDecodeError)',
                    exc_info=sys.exc_info(),
                    extra={
                        'status_code': 400,
                    }
                )
                response = http.HttpResponseBadRequest()
            else:
                response = self.get_response(request)
        finally:
            signals.request_finished.send(sender=self.__class__)

        try:
            status_text = STATUS_CODE_TEXT[response.status_code]
        except KeyError:
            status_text = 'UNKNOWN STATUS CODE'
        status = '%s %s' % (response.status_code, status_text)
        response_headers = [(str(k), str(v)) for k, v in response.items()]
        for c in response.cookies.values():
            response_headers.append((b'Set-Cookie', str(c.output(header=''))))
        start_response(smart_str(status), response_headers)
        return response