2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2012-10-30 03:33:00 +08:00
|
|
|
import codecs
|
2012-09-21 03:03:24 +08:00
|
|
|
import logging
|
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-08-30 04:40:51 +08:00
|
|
|
from django.utils.encoding import force_str, force_text, iri_to_uri
|
2005-07-18 14:30:26 +08:00
|
|
|
|
2013-05-19 18:58:13 +08:00
|
|
|
# For backwards compatibility -- lots of code uses this in the wild!
|
|
|
|
from django.http.response import REASON_PHRASES as STATUS_CODE_TEXT
|
2010-10-04 23:12:39 +08:00
|
|
|
|
2013-05-19 18:58:13 +08:00
|
|
|
logger = logging.getLogger('django.request')
|
2010-10-04 23:12:39 +08:00
|
|
|
|
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-12-17 17:49:26 +08:00
|
|
|
path_info = base.get_path_info(environ)
|
2013-03-24 17:57:38 +08:00
|
|
|
if not path_info:
|
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.
|
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
|
2013-03-31 15:54:52 +08:00
|
|
|
self.path = '%s/%s' % (script_name.rstrip('/'), path_info.lstrip('/'))
|
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()
|
2012-10-30 03:33:00 +08:00
|
|
|
_, content_params = self._parse_content_type(self.META.get('CONTENT_TYPE', ''))
|
|
|
|
if 'charset' in content_params:
|
|
|
|
try:
|
|
|
|
codecs.lookup(content_params['charset'])
|
|
|
|
except LookupError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.encoding = content_params['charset']
|
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
|
|
|
|
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
|
|
|
|
2012-10-30 03:33:00 +08:00
|
|
|
def _parse_content_type(self, ctype):
|
|
|
|
"""
|
|
|
|
Media Types parsing according to RFC 2616, section 3.7.
|
|
|
|
|
|
|
|
Returns the data type and parameters. For example:
|
|
|
|
Input: "text/plain; charset=iso-8859-1"
|
|
|
|
Output: ('text/plain', {'charset': 'iso-8859-1'})
|
|
|
|
"""
|
|
|
|
content_type, _, params = ctype.partition(';')
|
|
|
|
content_params = {}
|
|
|
|
for parameter in params.split(';'):
|
|
|
|
k, _, v = parameter.strip().partition('=')
|
|
|
|
content_params[k] = v
|
|
|
|
return content_type, content_params
|
|
|
|
|
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:
|
2012-09-09 19:00:10 +08:00
|
|
|
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)
|
2012-12-30 22:19:22 +08:00
|
|
|
|
|
|
|
response._handler_class = self.__class__
|
2006-02-20 12:36:17 +08:00
|
|
|
|
2013-05-19 18:58:13 +08:00
|
|
|
status = '%s %s' % (response.status_code, response.reason_phrase)
|
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-08-30 04:40:51 +08:00
|
|
|
start_response(force_str(status), response_headers)
|
2006-09-22 20:32:00 +08:00
|
|
|
return response
|