2006-07-22 00:20:22 +08:00
|
|
|
import os
|
2008-03-17 21:49:04 +08:00
|
|
|
from Cookie import SimpleCookie, CookieError
|
2005-07-13 09:25:57 +08:00
|
|
|
from pprint import pformat
|
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
|
|
|
from urllib import urlencode
|
2007-09-14 13:28:00 +08:00
|
|
|
from urlparse import urljoin
|
2008-03-08 11:46:33 +08:00
|
|
|
try:
|
|
|
|
# The mod_python version is more efficient, so try importing it first.
|
|
|
|
from mod_python.util import parse_qsl
|
|
|
|
except ImportError:
|
|
|
|
from cgi import parse_qsl
|
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
from django.utils.datastructures import MultiValueDict, ImmutableList
|
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
|
|
|
from django.utils.encoding import smart_str, iri_to_uri, force_unicode
|
2008-07-01 23:10:51 +08:00
|
|
|
from django.http.multipartparser import MultiPartParser
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core.files import uploadhandler
|
2007-11-11 11:55:44 +08:00
|
|
|
from utils import *
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2006-06-20 12:06:16 +08:00
|
|
|
RESERVED_CHARS="!*'();:@&=+$,/?%#[]"
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Http404(Exception):
|
|
|
|
pass
|
|
|
|
|
2006-06-20 11:48:31 +08:00
|
|
|
class HttpRequest(object):
|
2008-03-08 11:46:33 +08:00
|
|
|
"""A basic HTTP request."""
|
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
|
|
|
|
|
|
|
# The encoding used in GET/POST dicts. None means use default setting.
|
|
|
|
_encoding = None
|
2008-07-01 23:10:51 +08:00
|
|
|
_upload_handlers = []
|
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
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
def __init__(self):
|
|
|
|
self.GET, self.POST, self.COOKIES, self.META, self.FILES = {}, {}, {}, {}, {}
|
|
|
|
self.path = ''
|
2008-07-21 15:57:10 +08:00
|
|
|
self.path_info = ''
|
2006-06-20 11:48:31 +08:00
|
|
|
self.method = None
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '<HttpRequest\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' % \
|
|
|
|
(pformat(self.GET), pformat(self.POST), pformat(self.COOKIES),
|
|
|
|
pformat(self.META))
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
for d in (self.POST, self.GET):
|
2007-04-26 21:30:48 +08:00
|
|
|
if key in d:
|
2005-07-13 09:25:57 +08:00
|
|
|
return d[key]
|
|
|
|
raise KeyError, "%s not found in either POST or GET" % key
|
|
|
|
|
2007-09-11 22:04:40 +08:00
|
|
|
def has_key(self, key):
|
2007-04-26 21:30:48 +08:00
|
|
|
return key in self.GET or key in self.POST
|
2005-09-28 00:33:25 +08:00
|
|
|
|
2007-09-11 22:04:40 +08:00
|
|
|
__contains__ = has_key
|
2007-09-14 13:28:00 +08:00
|
|
|
|
2007-09-16 01:46:03 +08:00
|
|
|
def get_host(self):
|
2008-03-08 11:46:33 +08:00
|
|
|
"""Returns the HTTP host using the environment or request headers."""
|
2007-09-16 01:46:03 +08:00
|
|
|
# We try three options, in order of decreasing preference.
|
2007-10-04 06:21:43 +08:00
|
|
|
if 'HTTP_X_FORWARDED_HOST' in self.META:
|
|
|
|
host = self.META['HTTP_X_FORWARDED_HOST']
|
|
|
|
elif 'HTTP_HOST' in self.META:
|
2007-09-16 01:46:03 +08:00
|
|
|
host = self.META['HTTP_HOST']
|
|
|
|
else:
|
|
|
|
# Reconstruct the host using the algorithm from PEP 333.
|
|
|
|
host = self.META['SERVER_NAME']
|
|
|
|
server_port = self.META['SERVER_PORT']
|
|
|
|
if server_port != (self.is_secure() and 443 or 80):
|
|
|
|
host = '%s:%s' % (host, server_port)
|
|
|
|
return host
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
def get_full_path(self):
|
|
|
|
return ''
|
2006-08-09 23:40:24 +08:00
|
|
|
|
2007-09-14 13:28:00 +08:00
|
|
|
def build_absolute_uri(self, location=None):
|
|
|
|
"""
|
|
|
|
Builds an absolute URI from the location and the variables available in
|
|
|
|
this request. If no location is specified, the absolute URI is built on
|
|
|
|
``request.get_full_path()``.
|
|
|
|
"""
|
|
|
|
if not location:
|
2007-09-14 13:39:59 +08:00
|
|
|
location = self.get_full_path()
|
2007-09-14 13:28:00 +08:00
|
|
|
if not ':' in location:
|
|
|
|
current_uri = '%s://%s%s' % (self.is_secure() and 'https' or 'http',
|
2007-09-16 01:46:03 +08:00
|
|
|
self.get_host(), self.path)
|
2007-09-14 13:28:00 +08:00
|
|
|
location = urljoin(current_uri, location)
|
|
|
|
return location
|
|
|
|
|
2006-07-22 00:20:22 +08:00
|
|
|
def is_secure(self):
|
|
|
|
return os.environ.get("HTTPS") == "on"
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2008-03-20 15:16:16 +08:00
|
|
|
def is_ajax(self):
|
|
|
|
return self.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'
|
|
|
|
|
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
|
|
|
def _set_encoding(self, val):
|
|
|
|
"""
|
|
|
|
Sets the encoding used for GET/POST accesses. If the GET or POST
|
|
|
|
dictionary has already been created, it is removed and recreated on the
|
|
|
|
next access (so that it is decoded correctly).
|
|
|
|
"""
|
|
|
|
self._encoding = val
|
|
|
|
if hasattr(self, '_get'):
|
|
|
|
del self._get
|
|
|
|
if hasattr(self, '_post'):
|
|
|
|
del self._post
|
|
|
|
|
|
|
|
def _get_encoding(self):
|
|
|
|
return self._encoding
|
|
|
|
|
|
|
|
encoding = property(_get_encoding, _set_encoding)
|
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
def _initialize_handlers(self):
|
|
|
|
self._upload_handlers = [uploadhandler.load_handler(handler, self)
|
|
|
|
for handler in settings.FILE_UPLOAD_HANDLERS]
|
|
|
|
|
|
|
|
def _set_upload_handlers(self, upload_handlers):
|
|
|
|
if hasattr(self, '_files'):
|
|
|
|
raise AttributeError("You cannot set the upload handlers after the upload has been processed.")
|
|
|
|
self._upload_handlers = upload_handlers
|
|
|
|
|
|
|
|
def _get_upload_handlers(self):
|
|
|
|
if not self._upload_handlers:
|
|
|
|
# If thre are no upload handlers defined, initialize them from settings.
|
|
|
|
self._initialize_handlers()
|
|
|
|
return self._upload_handlers
|
|
|
|
|
|
|
|
upload_handlers = property(_get_upload_handlers, _set_upload_handlers)
|
|
|
|
|
|
|
|
def parse_file_upload(self, META, post_data):
|
|
|
|
"""Returns a tuple of (POST QueryDict, FILES MultiValueDict)."""
|
|
|
|
self.upload_handlers = ImmutableList(
|
|
|
|
self.upload_handlers,
|
|
|
|
warning = "You cannot alter upload handlers after the upload has been processed."
|
|
|
|
)
|
|
|
|
parser = MultiPartParser(META, post_data, self.upload_handlers, self.encoding)
|
|
|
|
return parser.parse()
|
2008-03-08 11:46:33 +08:00
|
|
|
|
2005-11-30 10:35:06 +08:00
|
|
|
class QueryDict(MultiValueDict):
|
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
|
|
|
"""
|
|
|
|
A specialized MultiValueDict that takes a query string when initialized.
|
|
|
|
This is immutable unless you create a copy of it.
|
|
|
|
|
|
|
|
Values retrieved from this class are converted from the given encoding
|
|
|
|
(DEFAULT_CHARSET by default) to unicode.
|
|
|
|
"""
|
|
|
|
def __init__(self, query_string, mutable=False, encoding=None):
|
2005-11-30 12:08:46 +08:00
|
|
|
MultiValueDict.__init__(self)
|
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
|
|
|
if not encoding:
|
|
|
|
# *Important*: do not import settings any earlier because of note
|
|
|
|
# in core.handlers.modpython.
|
|
|
|
from django.conf import settings
|
2007-07-11 14:55:12 +08:00
|
|
|
encoding = settings.DEFAULT_CHARSET
|
|
|
|
self.encoding = encoding
|
2005-11-30 12:08:46 +08:00
|
|
|
self._mutable = True
|
2005-11-30 12:13:21 +08:00
|
|
|
for key, value in parse_qsl((query_string or ''), True): # keep_blank_values=True
|
2008-03-08 11:46:33 +08:00
|
|
|
self.appendlist(force_unicode(key, encoding, errors='replace'),
|
|
|
|
force_unicode(value, encoding, errors='replace'))
|
2006-03-29 01:31:04 +08:00
|
|
|
self._mutable = mutable
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-30 12:08:46 +08:00
|
|
|
def _assert_mutable(self):
|
2005-07-13 09:25:57 +08:00
|
|
|
if not self._mutable:
|
2008-03-08 11:46:33 +08:00
|
|
|
raise AttributeError("This QueryDict instance is immutable")
|
2005-11-30 12:08:46 +08:00
|
|
|
|
2006-03-29 01:31:04 +08:00
|
|
|
def __setitem__(self, key, value):
|
2005-11-30 12:08:46 +08:00
|
|
|
self._assert_mutable()
|
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
|
|
|
key = str_to_unicode(key, self.encoding)
|
|
|
|
value = str_to_unicode(value, self.encoding)
|
2005-11-30 12:08:46 +08:00
|
|
|
MultiValueDict.__setitem__(self, key, value)
|
2006-03-29 01:31:04 +08:00
|
|
|
|
2007-05-11 16:22:06 +08:00
|
|
|
def __delitem__(self, key):
|
|
|
|
self._assert_mutable()
|
|
|
|
super(QueryDict, self).__delitem__(key)
|
|
|
|
|
2006-03-29 01:31:04 +08:00
|
|
|
def __copy__(self):
|
|
|
|
result = self.__class__('', mutable=True)
|
|
|
|
for key, value in dict.items(self):
|
|
|
|
dict.__setitem__(result, key, value)
|
|
|
|
return result
|
|
|
|
|
2008-02-27 15:58:00 +08:00
|
|
|
def __deepcopy__(self, memo):
|
2006-03-29 01:31:04 +08:00
|
|
|
import copy
|
|
|
|
result = self.__class__('', mutable=True)
|
|
|
|
memo[id(self)] = result
|
|
|
|
for key, value in dict.items(self):
|
|
|
|
dict.__setitem__(result, copy.deepcopy(key, memo), copy.deepcopy(value, memo))
|
|
|
|
return result
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def setlist(self, key, list_):
|
2005-11-30 12:08:46 +08:00
|
|
|
self._assert_mutable()
|
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
|
|
|
key = str_to_unicode(key, self.encoding)
|
|
|
|
list_ = [str_to_unicode(elt, self.encoding) for elt in list_]
|
2005-11-30 12:08:46 +08:00
|
|
|
MultiValueDict.setlist(self, key, list_)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
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
|
|
|
def setlistdefault(self, key, default_list=()):
|
|
|
|
self._assert_mutable()
|
|
|
|
if key not in self:
|
|
|
|
self.setlist(key, default_list)
|
|
|
|
return MultiValueDict.getlist(self, key)
|
|
|
|
|
2005-11-30 12:08:46 +08:00
|
|
|
def appendlist(self, key, value):
|
|
|
|
self._assert_mutable()
|
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
|
|
|
key = str_to_unicode(key, self.encoding)
|
|
|
|
value = str_to_unicode(value, self.encoding)
|
2005-11-30 12:08:46 +08:00
|
|
|
MultiValueDict.appendlist(self, key, value)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-30 12:08:46 +08:00
|
|
|
def update(self, other_dict):
|
|
|
|
self._assert_mutable()
|
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
|
|
|
f = lambda s: str_to_unicode(s, self.encoding)
|
|
|
|
d = dict([(f(k), f(v)) for k, v in other_dict.items()])
|
|
|
|
MultiValueDict.update(self, d)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2007-05-20 02:34:00 +08:00
|
|
|
def pop(self, key, *args):
|
2005-11-30 12:08:46 +08:00
|
|
|
self._assert_mutable()
|
2007-05-20 02:34:00 +08:00
|
|
|
return MultiValueDict.pop(self, key, *args)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-30 12:08:46 +08:00
|
|
|
def popitem(self):
|
|
|
|
self._assert_mutable()
|
|
|
|
return MultiValueDict.popitem(self)
|
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
self._assert_mutable()
|
|
|
|
MultiValueDict.clear(self)
|
|
|
|
|
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
|
|
|
def setdefault(self, key, default=None):
|
2005-11-30 12:08:46 +08:00
|
|
|
self._assert_mutable()
|
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
|
|
|
key = str_to_unicode(key, self.encoding)
|
|
|
|
default = str_to_unicode(default, self.encoding)
|
|
|
|
return MultiValueDict.setdefault(self, key, default)
|
2005-11-30 12:08:46 +08:00
|
|
|
|
|
|
|
def copy(self):
|
2008-03-08 11:46:33 +08:00
|
|
|
"""Returns a mutable copy of this object."""
|
2008-02-27 15:58:00 +08:00
|
|
|
return self.__deepcopy__({})
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-09-03 03:39:47 +08:00
|
|
|
def urlencode(self):
|
|
|
|
output = []
|
2005-11-30 12:08:46 +08:00
|
|
|
for k, list_ in self.lists():
|
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
|
|
|
k = smart_str(k, self.encoding)
|
|
|
|
output.extend([urlencode({k: smart_str(v, self.encoding)}) for v in list_])
|
2005-09-03 03:39:47 +08:00
|
|
|
return '&'.join(output)
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
def parse_cookie(cookie):
|
|
|
|
if cookie == '':
|
|
|
|
return {}
|
2008-03-17 21:49:04 +08:00
|
|
|
try:
|
|
|
|
c = SimpleCookie()
|
|
|
|
c.load(cookie)
|
|
|
|
except CookieError:
|
|
|
|
# Invalid cookie
|
|
|
|
return {}
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
cookiedict = {}
|
|
|
|
for key in c.keys():
|
|
|
|
cookiedict[key] = c.get(key).value
|
|
|
|
return cookiedict
|
|
|
|
|
2006-04-10 07:54:34 +08:00
|
|
|
class HttpResponse(object):
|
2008-03-08 11:46:33 +08:00
|
|
|
"""A basic HTTP response, with content and dictionary-accessed headers."""
|
2007-03-30 14:46:36 +08:00
|
|
|
|
|
|
|
status_code = 200
|
|
|
|
|
2007-08-11 17:37:42 +08:00
|
|
|
def __init__(self, content='', mimetype=None, status=None,
|
|
|
|
content_type=None):
|
2006-04-10 08:01:59 +08:00
|
|
|
from django.conf import settings
|
|
|
|
self._charset = settings.DEFAULT_CHARSET
|
2007-08-11 17:37:42 +08:00
|
|
|
if mimetype:
|
|
|
|
content_type = mimetype # For backwards compatibility
|
|
|
|
if not content_type:
|
|
|
|
content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE,
|
|
|
|
settings.DEFAULT_CHARSET)
|
2007-02-10 11:56:21 +08:00
|
|
|
if not isinstance(content, basestring) and hasattr(content, '__iter__'):
|
2006-09-22 20:32:00 +08:00
|
|
|
self._container = content
|
2006-04-10 07:54:34 +08:00
|
|
|
self._is_string = False
|
|
|
|
else:
|
2006-09-22 20:32:00 +08:00
|
|
|
self._container = [content]
|
2006-04-10 07:54:34 +08:00
|
|
|
self._is_string = True
|
2005-07-13 09:25:57 +08:00
|
|
|
self.cookies = SimpleCookie()
|
2007-06-27 21:02:38 +08:00
|
|
|
if status:
|
|
|
|
self.status_code = status
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2007-10-20 13:58:48 +08:00
|
|
|
# _headers is a mapping of the lower-case name to the original case of
|
|
|
|
# the header (required for working with legacy systems) and the header
|
|
|
|
# value.
|
|
|
|
self._headers = {'content-type': ('Content-Type', content_type)}
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
def __str__(self):
|
2008-03-08 11:46:33 +08:00
|
|
|
"""Full HTTP message, including headers."""
|
2005-07-13 09:25:57 +08:00
|
|
|
return '\n'.join(['%s: %s' % (key, value)
|
2007-10-20 13:58:48 +08:00
|
|
|
for key, value in self._headers.values()]) \
|
2005-07-13 09:25:57 +08:00
|
|
|
+ '\n\n' + self.content
|
|
|
|
|
2007-12-17 16:05:51 +08:00
|
|
|
def _convert_to_ascii(self, *values):
|
2008-03-08 11:46:33 +08:00
|
|
|
"""Converts all values to ascii strings."""
|
2007-12-17 16:05:51 +08:00
|
|
|
for value in values:
|
|
|
|
if isinstance(value, unicode):
|
|
|
|
try:
|
|
|
|
yield value.encode('us-ascii')
|
|
|
|
except UnicodeError, e:
|
|
|
|
e.reason += ', HTTP response headers must be in US-ASCII format'
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
yield str(value)
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
def __setitem__(self, header, value):
|
2007-12-17 16:05:51 +08:00
|
|
|
header, value = self._convert_to_ascii(header, value)
|
2007-10-20 13:58:48 +08:00
|
|
|
self._headers[header.lower()] = (header, value)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def __delitem__(self, header):
|
|
|
|
try:
|
2007-09-15 05:53:13 +08:00
|
|
|
del self._headers[header.lower()]
|
2005-07-13 09:25:57 +08:00
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __getitem__(self, header):
|
2007-10-20 13:58:48 +08:00
|
|
|
return self._headers[header.lower()][1]
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def has_header(self, header):
|
2008-03-08 11:46:33 +08:00
|
|
|
"""Case-insensitive check for a header."""
|
2007-09-15 05:53:13 +08:00
|
|
|
return self._headers.has_key(header.lower())
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2007-09-15 05:41:48 +08:00
|
|
|
__contains__ = has_header
|
2007-10-20 11:32:21 +08:00
|
|
|
|
2007-09-15 06:33:56 +08:00
|
|
|
def items(self):
|
2007-10-20 13:58:48 +08:00
|
|
|
return self._headers.values()
|
2007-10-20 11:32:21 +08:00
|
|
|
|
2007-09-15 06:33:56 +08:00
|
|
|
def get(self, header, alternate):
|
2007-10-20 13:58:48 +08:00
|
|
|
return self._headers.get(header.lower(), (None, alternate))[1]
|
2007-09-15 05:41:48 +08:00
|
|
|
|
2008-03-08 11:46:33 +08:00
|
|
|
def set_cookie(self, key, value='', max_age=None, expires=None, path='/',
|
|
|
|
domain=None, secure=False):
|
2005-07-13 09:25:57 +08:00
|
|
|
self.cookies[key] = value
|
2008-03-08 11:31:42 +08:00
|
|
|
if max_age is not None:
|
|
|
|
self.cookies[key]['max-age'] = max_age
|
|
|
|
if expires is not None:
|
|
|
|
self.cookies[key]['expires'] = expires
|
|
|
|
if path is not None:
|
|
|
|
self.cookies[key]['path'] = path
|
|
|
|
if domain is not None:
|
|
|
|
self.cookies[key]['domain'] = domain
|
|
|
|
if secure:
|
|
|
|
self.cookies[key]['secure'] = True
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2006-08-09 23:40:24 +08:00
|
|
|
def delete_cookie(self, key, path='/', domain=None):
|
2007-10-20 15:01:12 +08:00
|
|
|
self.set_cookie(key, max_age=0, path=path, domain=domain,
|
2008-03-08 11:46:33 +08:00
|
|
|
expires='Thu, 01-Jan-1970 00:00:00 GMT')
|
2005-09-23 09:17:39 +08:00
|
|
|
|
2006-04-10 07:54:34 +08:00
|
|
|
def _get_content(self):
|
2007-10-20 14:50:16 +08:00
|
|
|
if self.has_header('Content-Encoding'):
|
|
|
|
return ''.join(self._container)
|
|
|
|
return smart_str(''.join(self._container), self._charset)
|
2006-04-10 07:54:34 +08:00
|
|
|
|
|
|
|
def _set_content(self, value):
|
2006-09-22 20:32:00 +08:00
|
|
|
self._container = [value]
|
2006-04-10 07:54:34 +08:00
|
|
|
self._is_string = True
|
|
|
|
|
|
|
|
content = property(_get_content, _set_content)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2006-09-22 20:32:00 +08:00
|
|
|
def __iter__(self):
|
2007-09-15 03:55:24 +08:00
|
|
|
self._iterator = iter(self._container)
|
2007-06-22 15:15:04 +08:00
|
|
|
return self
|
|
|
|
|
|
|
|
def next(self):
|
|
|
|
chunk = self._iterator.next()
|
|
|
|
if isinstance(chunk, unicode):
|
|
|
|
chunk = chunk.encode(self._charset)
|
2007-12-05 17:28:56 +08:00
|
|
|
return str(chunk)
|
2006-09-22 20:32:00 +08:00
|
|
|
|
|
|
|
def close(self):
|
|
|
|
if hasattr(self._container, 'close'):
|
|
|
|
self._container.close()
|
2006-04-11 21:43:29 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
# The remaining methods partially implement the file-like object interface.
|
|
|
|
# See http://docs.python.org/lib/bltin-file-objects.html
|
|
|
|
def write(self, content):
|
2006-04-10 07:54:34 +08:00
|
|
|
if not self._is_string:
|
2008-03-08 11:46:33 +08:00
|
|
|
raise Exception("This %s instance is not writable" % self.__class__)
|
2006-09-22 20:32:00 +08:00
|
|
|
self._container.append(content)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def flush(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def tell(self):
|
2006-04-10 07:54:34 +08:00
|
|
|
if not self._is_string:
|
2008-03-08 11:46:33 +08:00
|
|
|
raise Exception("This %s instance cannot tell its position" % self.__class__)
|
2006-09-22 20:32:00 +08:00
|
|
|
return sum([len(chunk) for chunk in self._container])
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
class HttpResponseRedirect(HttpResponse):
|
2007-03-30 14:46:36 +08:00
|
|
|
status_code = 302
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
def __init__(self, redirect_to):
|
|
|
|
HttpResponse.__init__(self)
|
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['Location'] = iri_to_uri(redirect_to)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2006-01-04 07:57:14 +08:00
|
|
|
class HttpResponsePermanentRedirect(HttpResponse):
|
2007-03-30 14:46:36 +08:00
|
|
|
status_code = 301
|
|
|
|
|
2006-01-04 07:57:14 +08:00
|
|
|
def __init__(self, redirect_to):
|
|
|
|
HttpResponse.__init__(self)
|
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['Location'] = iri_to_uri(redirect_to)
|
2006-01-04 07:57:14 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
class HttpResponseNotModified(HttpResponse):
|
2007-03-30 14:46:36 +08:00
|
|
|
status_code = 304
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2007-03-30 14:48:19 +08:00
|
|
|
class HttpResponseBadRequest(HttpResponse):
|
|
|
|
status_code = 400
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
class HttpResponseNotFound(HttpResponse):
|
2007-03-30 14:46:36 +08:00
|
|
|
status_code = 404
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
class HttpResponseForbidden(HttpResponse):
|
2007-03-30 14:46:36 +08:00
|
|
|
status_code = 403
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2006-06-19 10:04:37 +08:00
|
|
|
class HttpResponseNotAllowed(HttpResponse):
|
2007-03-30 14:46:36 +08:00
|
|
|
status_code = 405
|
|
|
|
|
2006-06-19 10:04:37 +08:00
|
|
|
def __init__(self, permitted_methods):
|
|
|
|
HttpResponse.__init__(self)
|
|
|
|
self['Allow'] = ', '.join(permitted_methods)
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
class HttpResponseGone(HttpResponse):
|
2007-03-30 14:46:36 +08:00
|
|
|
status_code = 410
|
|
|
|
|
2005-09-01 13:01:47 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
HttpResponse.__init__(self, *args, **kwargs)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
class HttpResponseServerError(HttpResponse):
|
2007-03-30 14:46:36 +08:00
|
|
|
status_code = 500
|
|
|
|
|
2005-09-01 13:01:47 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
HttpResponse.__init__(self, *args, **kwargs)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2007-09-16 01:46:03 +08:00
|
|
|
# A backwards compatible alias for HttpRequest.get_host.
|
2006-05-02 09:31:56 +08:00
|
|
|
def get_host(request):
|
2007-09-16 01:46:03 +08:00
|
|
|
return request.get_host()
|
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
|
|
|
|
|
|
|
# It's neither necessary nor appropriate to use
|
|
|
|
# django.utils.encoding.smart_unicode for parsing URLs and form inputs. Thus,
|
|
|
|
# this slightly more restricted function.
|
|
|
|
def str_to_unicode(s, encoding):
|
|
|
|
"""
|
2008-03-08 11:46:33 +08:00
|
|
|
Converts basestring objects to unicode, using the given encoding. Illegally
|
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
|
|
|
encoded input characters are replaced with Unicode "unknown" codepoint
|
|
|
|
(\ufffd).
|
|
|
|
|
|
|
|
Returns any non-basestring objects without change.
|
|
|
|
"""
|
|
|
|
if isinstance(s, str):
|
|
|
|
return unicode(s, encoding, 'replace')
|
|
|
|
else:
|
|
|
|
return s
|
2008-07-21 15:57:10 +08:00
|
|
|
|