2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2010-10-04 23:12:39 +08:00
|
|
|
import sys
|
2012-05-06 01:47:03 +08:00
|
|
|
from io import BytesIO
|
2010-10-04 23:12:39 +08:00
|
|
|
from threading import Lock
|
2007-09-28 01:01:34 +08:00
|
|
|
|
2007-12-02 10:22:19 +08:00
|
|
|
from django import http
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.core import signals
|
2008-07-21 15:57:10 +08:00
|
|
|
from django.core.handlers import base
|
|
|
|
from django.core.urlresolvers import set_script_prefix
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.utils import datastructures
|
2012-07-21 16:00:10 +08:00
|
|
|
from django.utils.encoding import force_text, smart_bytes, iri_to_uri
|
2010-10-06 23:02:26 +08:00
|
|
|
from django.utils.log import getLogger
|
2005-07-18 14:30:26 +08:00
|
|
|
|
2010-10-06 23:02:26 +08:00
|
|
|
logger = getLogger('django.request')
|
2010-10-04 23:12:39 +08:00
|
|
|
|
|
|
|
|
2011-09-10 00:57:35 +08:00
|
|
|
# See http://www.iana.org/assignments/http-status-codes
|
2005-07-23 02:12:05 +08:00
|
|
|
STATUS_CODE_TEXT = {
|
2005-09-13 11:21:44 +08:00
|
|
|
100: 'CONTINUE',
|
|
|
|
101: 'SWITCHING PROTOCOLS',
|
2011-09-10 00:57:35 +08:00
|
|
|
102: 'PROCESSING',
|
2005-07-23 02:12:05 +08:00
|
|
|
200: 'OK',
|
2005-09-13 11:21:44 +08:00
|
|
|
201: 'CREATED',
|
|
|
|
202: 'ACCEPTED',
|
|
|
|
203: 'NON-AUTHORITATIVE INFORMATION',
|
|
|
|
204: 'NO CONTENT',
|
|
|
|
205: 'RESET CONTENT',
|
|
|
|
206: 'PARTIAL CONTENT',
|
2011-09-10 00:57:35 +08:00
|
|
|
207: 'MULTI-STATUS',
|
|
|
|
208: 'ALREADY REPORTED',
|
|
|
|
226: 'IM USED',
|
2005-09-13 11:21:44 +08:00
|
|
|
300: 'MULTIPLE CHOICES',
|
|
|
|
301: 'MOVED PERMANENTLY',
|
|
|
|
302: 'FOUND',
|
|
|
|
303: 'SEE OTHER',
|
|
|
|
304: 'NOT MODIFIED',
|
|
|
|
305: 'USE PROXY',
|
|
|
|
306: 'RESERVED',
|
|
|
|
307: 'TEMPORARY REDIRECT',
|
|
|
|
400: 'BAD REQUEST',
|
|
|
|
401: 'UNAUTHORIZED',
|
|
|
|
402: 'PAYMENT REQUIRED',
|
|
|
|
403: 'FORBIDDEN',
|
2005-07-23 02:12:05 +08:00
|
|
|
404: 'NOT FOUND',
|
2005-09-13 11:21:44 +08:00
|
|
|
405: 'METHOD NOT ALLOWED',
|
|
|
|
406: 'NOT ACCEPTABLE',
|
|
|
|
407: 'PROXY AUTHENTICATION REQUIRED',
|
|
|
|
408: 'REQUEST TIMEOUT',
|
|
|
|
409: 'CONFLICT',
|
|
|
|
410: 'GONE',
|
|
|
|
411: 'LENGTH REQUIRED',
|
|
|
|
412: 'PRECONDITION FAILED',
|
|
|
|
413: 'REQUEST ENTITY TOO LARGE',
|
|
|
|
414: 'REQUEST-URI TOO LONG',
|
|
|
|
415: 'UNSUPPORTED MEDIA TYPE',
|
|
|
|
416: 'REQUESTED RANGE NOT SATISFIABLE',
|
|
|
|
417: 'EXPECTATION FAILED',
|
2011-09-10 00:57:35 +08:00
|
|
|
422: 'UNPROCESSABLE ENTITY',
|
|
|
|
423: 'LOCKED',
|
|
|
|
424: 'FAILED DEPENDENCY',
|
|
|
|
426: 'UPGRADE REQUIRED',
|
2005-07-23 02:12:05 +08:00
|
|
|
500: 'INTERNAL SERVER ERROR',
|
2005-09-13 11:21:44 +08:00
|
|
|
501: 'NOT IMPLEMENTED',
|
|
|
|
502: 'BAD GATEWAY',
|
|
|
|
503: 'SERVICE UNAVAILABLE',
|
|
|
|
504: 'GATEWAY TIMEOUT',
|
|
|
|
505: 'HTTP VERSION NOT SUPPORTED',
|
2011-09-10 00:57:35 +08:00
|
|
|
506: 'VARIANT ALSO NEGOTIATES',
|
|
|
|
507: 'INSUFFICIENT STORAGE',
|
|
|
|
508: 'LOOP DETECTED',
|
|
|
|
510: 'NOT EXTENDED',
|
2005-07-23 02:12:05 +08:00
|
|
|
}
|
|
|
|
|
2010-10-30 00:39:25 +08:00
|
|
|
class LimitedStream(object):
|
|
|
|
'''
|
|
|
|
LimitedStream wraps another stream in order to not allow reading from it
|
|
|
|
past specified amount of bytes.
|
|
|
|
'''
|
|
|
|
def __init__(self, stream, limit, buf_size=64 * 1024 * 1024):
|
|
|
|
self.stream = stream
|
|
|
|
self.remaining = limit
|
2012-05-19 23:43:34 +08:00
|
|
|
self.buffer = b''
|
2010-10-30 00:39:25 +08:00
|
|
|
self.buf_size = buf_size
|
|
|
|
|
|
|
|
def _read_limited(self, size=None):
|
|
|
|
if size is None or size > self.remaining:
|
|
|
|
size = self.remaining
|
|
|
|
if size == 0:
|
2012-05-19 23:43:34 +08:00
|
|
|
return b''
|
2010-10-30 00:39:25 +08:00
|
|
|
result = self.stream.read(size)
|
|
|
|
self.remaining -= len(result)
|
|
|
|
return result
|
|
|
|
|
|
|
|
def read(self, size=None):
|
|
|
|
if size is None:
|
|
|
|
result = self.buffer + self._read_limited()
|
2012-05-19 23:43:34 +08:00
|
|
|
self.buffer = b''
|
2010-10-30 00:39:25 +08:00
|
|
|
elif size < len(self.buffer):
|
|
|
|
result = self.buffer[:size]
|
|
|
|
self.buffer = self.buffer[size:]
|
|
|
|
else: # size >= len(self.buffer)
|
|
|
|
result = self.buffer + self._read_limited(size - len(self.buffer))
|
2012-05-19 23:43:34 +08:00
|
|
|
self.buffer = b''
|
2010-10-30 00:39:25 +08:00
|
|
|
return result
|
|
|
|
|
|
|
|
def readline(self, size=None):
|
2012-05-19 23:43:34 +08:00
|
|
|
while b'\n' not in self.buffer and \
|
2011-01-16 15:31:35 +08:00
|
|
|
(size is None or len(self.buffer) < size):
|
2010-10-30 00:39:25 +08:00
|
|
|
if size:
|
2011-01-16 15:31:35 +08:00
|
|
|
# since size is not None here, len(self.buffer) < size
|
2010-10-30 00:39:25 +08:00
|
|
|
chunk = self._read_limited(size - len(self.buffer))
|
|
|
|
else:
|
|
|
|
chunk = self._read_limited()
|
|
|
|
if not chunk:
|
|
|
|
break
|
|
|
|
self.buffer += chunk
|
2012-05-06 01:47:03 +08:00
|
|
|
sio = BytesIO(self.buffer)
|
2010-10-30 00:39:25 +08:00
|
|
|
if size:
|
|
|
|
line = sio.readline(size)
|
|
|
|
else:
|
|
|
|
line = sio.readline()
|
|
|
|
self.buffer = sio.read()
|
|
|
|
return line
|
2006-09-23 21:53:02 +08:00
|
|
|
|
2011-10-22 12:30:10 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class WSGIRequest(http.HttpRequest):
|
2005-07-18 14:30:26 +08:00
|
|
|
def __init__(self, environ):
|
2008-07-21 15:57:10 +08:00
|
|
|
script_name = base.get_script_name(environ)
|
2012-07-21 16:00:10 +08:00
|
|
|
path_info = force_text(environ.get('PATH_INFO', '/'))
|
2008-08-26 10:42:39 +08:00
|
|
|
if not path_info or path_info == script_name:
|
2008-07-22 06:13:56 +08:00
|
|
|
# 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.
|
2008-08-26 10:42:39 +08:00
|
|
|
#
|
|
|
|
# (The comparison of path_info to script_name is to work around an
|
2011-12-17 04:21:16 +08:00
|
|
|
# apparent bug in flup 1.0.1. See Django ticket #8490).
|
2012-06-08 00:08:47 +08:00
|
|
|
path_info = '/'
|
2005-07-18 14:30:26 +08:00
|
|
|
self.environ = environ
|
2008-07-21 15:57:10 +08:00
|
|
|
self.path_info = path_info
|
|
|
|
self.path = '%s%s' % (script_name, path_info)
|
2006-09-28 09:56:02 +08:00
|
|
|
self.META = environ
|
2008-07-21 15:57:10 +08:00
|
|
|
self.META['PATH_INFO'] = path_info
|
|
|
|
self.META['SCRIPT_NAME'] = script_name
|
2006-06-20 11:48:31 +08:00
|
|
|
self.method = environ['REQUEST_METHOD'].upper()
|
2008-08-31 03:56:14 +08:00
|
|
|
self._post_parse_error = False
|
2011-06-28 18:17:56 +08:00
|
|
|
try:
|
|
|
|
content_length = int(self.environ.get('CONTENT_LENGTH'))
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
content_length = 0
|
|
|
|
self._stream = LimitedStream(self.environ['wsgi.input'], content_length)
|
2010-10-30 00:39:25 +08:00
|
|
|
self._read_started = False
|
2005-07-18 14:30:26 +08:00
|
|
|
|
|
|
|
def get_full_path(self):
|
2009-03-08 17:58:40 +08:00
|
|
|
# RFC 3986 requires query string arguments to be in the ASCII range.
|
|
|
|
# Rather than crash if this doesn't happen, we encode defensively.
|
|
|
|
return '%s%s' % (self.path, self.environ.get('QUERY_STRING', '') and ('?' + iri_to_uri(self.environ.get('QUERY_STRING', ''))) or '')
|
2005-07-18 14:30:26 +08:00
|
|
|
|
2011-12-17 06:06:06 +08:00
|
|
|
def _is_secure(self):
|
|
|
|
return 'wsgi.url_scheme' in self.environ and self.environ['wsgi.url_scheme'] == 'https'
|
2006-07-22 00:20:22 +08:00
|
|
|
|
2005-07-18 14:30:26 +08:00
|
|
|
def _get_request(self):
|
|
|
|
if not hasattr(self, '_request'):
|
2006-05-02 09:31:56 +08:00
|
|
|
self._request = datastructures.MergeDict(self.POST, self.GET)
|
2005-07-18 14:30:26 +08:00
|
|
|
return self._request
|
|
|
|
|
|
|
|
def _get_get(self):
|
|
|
|
if not hasattr(self, '_get'):
|
2005-11-27 02:41:29 +08:00
|
|
|
# The WSGI spec says 'QUERY_STRING' may be absent.
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
self._get = http.QueryDict(self.environ.get('QUERY_STRING', ''), encoding=self._encoding)
|
2005-07-18 14:30:26 +08:00
|
|
|
return self._get
|
|
|
|
|
|
|
|
def _set_get(self, get):
|
|
|
|
self._get = get
|
|
|
|
|
|
|
|
def _get_post(self):
|
|
|
|
if not hasattr(self, '_post'):
|
|
|
|
self._load_post_and_files()
|
|
|
|
return self._post
|
|
|
|
|
|
|
|
def _set_post(self, post):
|
|
|
|
self._post = post
|
|
|
|
|
|
|
|
def _get_cookies(self):
|
|
|
|
if not hasattr(self, '_cookies'):
|
2006-05-02 09:31:56 +08:00
|
|
|
self._cookies = http.parse_cookie(self.environ.get('HTTP_COOKIE', ''))
|
2005-07-18 14:30:26 +08:00
|
|
|
return self._cookies
|
|
|
|
|
|
|
|
def _set_cookies(self, cookies):
|
|
|
|
self._cookies = cookies
|
|
|
|
|
|
|
|
def _get_files(self):
|
|
|
|
if not hasattr(self, '_files'):
|
|
|
|
self._load_post_and_files()
|
|
|
|
return self._files
|
|
|
|
|
|
|
|
GET = property(_get_get, _set_get)
|
|
|
|
POST = property(_get_post, _set_post)
|
|
|
|
COOKIES = property(_get_cookies, _set_cookies)
|
|
|
|
FILES = property(_get_files)
|
|
|
|
REQUEST = property(_get_request)
|
|
|
|
|
2011-10-22 12:30:10 +08:00
|
|
|
|
2008-07-21 15:57:10 +08:00
|
|
|
class WSGIHandler(base.BaseHandler):
|
2007-08-12 18:24:05 +08:00
|
|
|
initLock = Lock()
|
2007-10-20 15:42:34 +08:00
|
|
|
request_class = WSGIRequest
|
2007-08-12 18:24:05 +08:00
|
|
|
|
2005-07-18 14:30:26 +08:00
|
|
|
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:
|
2012-06-23 23:11:15 +08:00
|
|
|
with self.initLock:
|
2011-01-15 13:54:55 +08:00
|
|
|
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
|
2005-07-18 14:30:26 +08:00
|
|
|
|
2008-07-21 15:57:10 +08:00
|
|
|
set_script_prefix(base.get_script_name(environ))
|
2008-08-06 23:32:46 +08:00
|
|
|
signals.request_started.send(sender=self.__class__)
|
2005-07-18 14:30:26 +08:00
|
|
|
try:
|
2007-10-20 15:42:34 +08:00
|
|
|
try:
|
|
|
|
request = self.request_class(environ)
|
|
|
|
except UnicodeDecodeError:
|
2011-03-26 11:53:37 +08:00
|
|
|
logger.warning('Bad Request (UnicodeDecodeError)',
|
2010-10-04 23:12:39 +08:00
|
|
|
exc_info=sys.exc_info(),
|
|
|
|
extra={
|
|
|
|
'status_code': 400,
|
|
|
|
}
|
|
|
|
)
|
2007-10-20 15:42:34 +08:00
|
|
|
response = http.HttpResponseBadRequest()
|
|
|
|
else:
|
|
|
|
response = self.get_response(request)
|
2006-05-02 09:31:56 +08:00
|
|
|
finally:
|
2008-08-06 23:32:46 +08:00
|
|
|
signals.request_finished.send(sender=self.__class__)
|
2006-02-20 12:36:17 +08:00
|
|
|
|
2005-07-23 02:12:05 +08:00
|
|
|
try:
|
|
|
|
status_text = STATUS_CODE_TEXT[response.status_code]
|
|
|
|
except KeyError:
|
|
|
|
status_text = 'UNKNOWN STATUS CODE'
|
|
|
|
status = '%s %s' % (response.status_code, status_text)
|
2007-09-15 06:33:56 +08:00
|
|
|
response_headers = [(str(k), str(v)) for k, v in response.items()]
|
2005-08-16 06:47:41 +08:00
|
|
|
for c in response.cookies.values():
|
2012-08-03 21:18:13 +08:00
|
|
|
response_headers.append((str('Set-Cookie'), str(c.output(header=''))))
|
2012-07-21 16:00:10 +08:00
|
|
|
start_response(smart_bytes(status), response_headers)
|
2006-09-22 20:32:00 +08:00
|
|
|
return response
|