2006-07-22 00:20:22 +08:00
|
|
|
import os
|
2005-07-13 09:25:57 +08:00
|
|
|
from Cookie import SimpleCookie
|
|
|
|
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
|
2007-08-12 20:02:08 +08:00
|
|
|
from django.utils.datastructures import MultiValueDict, FileDict
|
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
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2006-06-20 12:06:16 +08:00
|
|
|
RESERVED_CHARS="!*'();:@&=+$,/?%#[]"
|
|
|
|
|
2005-11-30 10:36:26 +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
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Http404(Exception):
|
|
|
|
pass
|
|
|
|
|
2006-06-20 11:48:31 +08:00
|
|
|
class HttpRequest(object):
|
2005-07-13 09:25:57 +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
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
def __init__(self):
|
|
|
|
self.GET, self.POST, self.COOKIES, self.META, self.FILES = {}, {}, {}, {}, {}
|
|
|
|
self.path = ''
|
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
|
|
|
|
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',
|
|
|
|
get_host(self), self.path)
|
|
|
|
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
|
|
|
|
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)
|
|
|
|
|
2005-07-18 13:52:06 +08:00
|
|
|
def parse_file_upload(header_dict, post_data):
|
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
|
|
|
"Returns a tuple of (POST QueryDict, FILES MultiValueDict)"
|
2005-07-13 09:25:57 +08:00
|
|
|
import email, email.Message
|
|
|
|
from cgi import parse_header
|
2005-07-18 13:52:06 +08:00
|
|
|
raw_message = '\r\n'.join(['%s:%s' % pair for pair in header_dict.items()])
|
|
|
|
raw_message += '\r\n\r\n' + post_data
|
2005-07-13 09:25:57 +08:00
|
|
|
msg = email.message_from_string(raw_message)
|
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
|
|
|
POST = QueryDict('', mutable=True)
|
2005-11-30 10:35:06 +08:00
|
|
|
FILES = MultiValueDict()
|
2005-07-13 09:25:57 +08:00
|
|
|
for submessage in msg.get_payload():
|
2007-02-27 02:52:15 +08:00
|
|
|
if submessage and isinstance(submessage, email.Message.Message):
|
2005-07-13 09:25:57 +08:00
|
|
|
name_dict = parse_header(submessage['Content-Disposition'])[1]
|
|
|
|
# name_dict is something like {'name': 'file', 'filename': 'test.txt'} for file uploads
|
|
|
|
# or {'name': 'blah'} for POST fields
|
|
|
|
# We assume all uploaded files have a 'filename' set.
|
2007-04-26 21:30:48 +08:00
|
|
|
if 'filename' in name_dict:
|
2005-07-13 09:25:57 +08:00
|
|
|
assert type([]) != type(submessage.get_payload()), "Nested MIME messages are not supported"
|
|
|
|
if not name_dict['filename'].strip():
|
|
|
|
continue
|
|
|
|
# IE submits the full path, so trim everything but the basename.
|
2007-08-12 20:01:31 +08:00
|
|
|
# (We can't use os.path.basename because that uses the server's
|
|
|
|
# directory separator, which may not be the same as the
|
|
|
|
# client's one.)
|
2005-07-13 09:25:57 +08:00
|
|
|
filename = name_dict['filename'][name_dict['filename'].rfind("\\")+1:]
|
2007-08-12 20:02:08 +08:00
|
|
|
FILES.appendlist(name_dict['name'], FileDict({
|
2005-07-13 09:25:57 +08:00
|
|
|
'filename': filename,
|
2007-04-26 21:30:48 +08:00
|
|
|
'content-type': 'Content-Type' in submessage and submessage['Content-Type'] or None,
|
2005-07-13 09:25:57 +08:00
|
|
|
'content': submessage.get_payload(),
|
2007-08-12 20:02:08 +08:00
|
|
|
}))
|
2005-07-13 09:25:57 +08:00
|
|
|
else:
|
|
|
|
POST.appendlist(name_dict['name'], submessage.get_payload())
|
|
|
|
return POST, FILES
|
|
|
|
|
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
|
2007-07-11 14:55:12 +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:
|
|
|
|
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
|
|
|
|
|
|
|
|
def __deepcopy__(self, memo={}):
|
|
|
|
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):
|
|
|
|
"Returns a mutable copy of this object."
|
2006-03-29 01:31:04 +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 {}
|
|
|
|
c = SimpleCookie()
|
|
|
|
c.load(cookie)
|
|
|
|
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):
|
2005-07-13 09:25:57 +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
|
2007-08-11 17:37:42 +08:00
|
|
|
self.headers = {'Content-Type': content_type}
|
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
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
"Full HTTP message, including headers"
|
|
|
|
return '\n'.join(['%s: %s' % (key, value)
|
|
|
|
for key, value in self.headers.items()]) \
|
|
|
|
+ '\n\n' + self.content
|
|
|
|
|
|
|
|
def __setitem__(self, header, value):
|
|
|
|
self.headers[header] = value
|
|
|
|
|
|
|
|
def __delitem__(self, header):
|
|
|
|
try:
|
|
|
|
del self.headers[header]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __getitem__(self, header):
|
|
|
|
return self.headers[header]
|
|
|
|
|
|
|
|
def has_header(self, header):
|
|
|
|
"Case-insensitive check for a header"
|
|
|
|
header = header.lower()
|
|
|
|
for key in self.headers.keys():
|
|
|
|
if key.lower() == header:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2005-11-01 09:02:07 +08:00
|
|
|
def set_cookie(self, key, value='', max_age=None, expires=None, path='/', domain=None, secure=None):
|
2005-07-13 09:25:57 +08:00
|
|
|
self.cookies[key] = value
|
2005-11-01 09:02:07 +08:00
|
|
|
for var in ('max_age', 'path', 'domain', 'secure', 'expires'):
|
2005-07-13 09:25:57 +08:00
|
|
|
val = locals()[var]
|
|
|
|
if val is not None:
|
|
|
|
self.cookies[key][var.replace('_', '-')] = val
|
|
|
|
|
2006-08-09 23:40:24 +08:00
|
|
|
def delete_cookie(self, key, path='/', domain=None):
|
|
|
|
self.cookies[key] = ''
|
|
|
|
if path is not None:
|
|
|
|
self.cookies[key]['path'] = path
|
|
|
|
if domain is not None:
|
2006-11-30 00:44:10 +08:00
|
|
|
self.cookies[key]['domain'] = domain
|
2006-08-09 23:40:24 +08:00
|
|
|
self.cookies[key]['expires'] = 0
|
|
|
|
self.cookies[key]['max-age'] = 0
|
2005-09-23 09:17:39 +08:00
|
|
|
|
2006-04-10 07:54:34 +08:00
|
|
|
def _get_content(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
|
|
|
content = smart_str(''.join(self._container), self._charset)
|
2006-04-10 07:54:34 +08:00
|
|
|
return content
|
|
|
|
|
|
|
|
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-06-22 15:15:04 +08:00
|
|
|
self._iterator = self._container.__iter__()
|
|
|
|
return self
|
|
|
|
|
|
|
|
def next(self):
|
|
|
|
chunk = self._iterator.next()
|
|
|
|
if isinstance(chunk, unicode):
|
|
|
|
chunk = chunk.encode(self._charset)
|
|
|
|
return 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:
|
|
|
|
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:
|
|
|
|
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
|
|
|
|
|
|
|
def get_host(request):
|
2006-06-20 11:48:31 +08:00
|
|
|
"Gets the HTTP host from the environment or request headers."
|
2007-09-14 13:39:42 +08:00
|
|
|
# We try three options, in order of decreasing preference.
|
2006-05-02 09:31:56 +08:00
|
|
|
host = request.META.get('HTTP_X_FORWARDED_HOST', '')
|
2007-09-14 13:39:42 +08:00
|
|
|
if 'HTTP_HOST' in request.META:
|
|
|
|
host = request.META['HTTP_HOST']
|
|
|
|
else:
|
|
|
|
# Reconstruct the host using the algorithm from PEP 333.
|
|
|
|
host = request.META['SERVER_NAME']
|
|
|
|
server_port = request.META['SERVER_PORT']
|
|
|
|
if server_port != (request.is_secure() and 443 or 80):
|
|
|
|
host = '%s:%s' % (host, server_port)
|
2006-05-02 09:31:56 +08:00
|
|
|
return 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):
|
|
|
|
"""
|
|
|
|
Convert basestring objects to unicode, using the given encoding. Illegaly
|
|
|
|
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
|
|
|
|
|