You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

request.py 20 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. from __future__ import unicode_literals
  2. import copy
  3. import re
  4. import sys
  5. from io import BytesIO
  6. from itertools import chain
  7. from django.conf import settings
  8. from django.core import signing
  9. from django.core.exceptions import DisallowedHost, ImproperlyConfigured
  10. from django.core.files import uploadhandler
  11. from django.http.multipartparser import MultiPartParser, MultiPartParserError
  12. from django.utils import six
  13. from django.utils.datastructures import ImmutableList, MultiValueDict
  14. from django.utils.encoding import (
  15. escape_uri_path, force_bytes, force_str, force_text, iri_to_uri,
  16. )
  17. from django.utils.http import is_same_domain
  18. from django.utils.six.moves.urllib.parse import (
  19. parse_qsl, quote, urlencode, urljoin, urlsplit,
  20. )
  21. RAISE_ERROR = object()
  22. host_validation_re = re.compile(r"^([a-z0-9.-]+|\[[a-f0-9]*:[a-f0-9:]+\])(:\d+)?$")
  23. class UnreadablePostError(IOError):
  24. pass
  25. class RawPostDataException(Exception):
  26. """
  27. You cannot access raw_post_data from a request that has
  28. multipart/* POST data if it has been accessed via POST,
  29. FILES, etc..
  30. """
  31. pass
  32. class HttpRequest(object):
  33. """A basic HTTP request."""
  34. # The encoding used in GET/POST dicts. None means use default setting.
  35. _encoding = None
  36. _upload_handlers = []
  37. def __init__(self):
  38. # WARNING: The `WSGIRequest` subclass doesn't call `super`.
  39. # Any variable assignment made here should also happen in
  40. # `WSGIRequest.__init__()`.
  41. self.GET = QueryDict(mutable=True)
  42. self.POST = QueryDict(mutable=True)
  43. self.COOKIES = {}
  44. self.META = {}
  45. self.FILES = MultiValueDict()
  46. self.path = ''
  47. self.path_info = ''
  48. self.method = None
  49. self.resolver_match = None
  50. self._post_parse_error = False
  51. def __repr__(self):
  52. if self.method is None or not self.get_full_path():
  53. return force_str('<%s>' % self.__class__.__name__)
  54. return force_str(
  55. '<%s: %s %r>' % (self.__class__.__name__, self.method, force_str(self.get_full_path()))
  56. )
  57. def _get_raw_host(self):
  58. """
  59. Return the HTTP host using the environment or request headers. Skip
  60. allowed hosts protection, so may return an insecure host.
  61. """
  62. # We try three options, in order of decreasing preference.
  63. if settings.USE_X_FORWARDED_HOST and (
  64. 'HTTP_X_FORWARDED_HOST' in self.META):
  65. host = self.META['HTTP_X_FORWARDED_HOST']
  66. elif 'HTTP_HOST' in self.META:
  67. host = self.META['HTTP_HOST']
  68. else:
  69. # Reconstruct the host using the algorithm from PEP 333.
  70. host = self.META['SERVER_NAME']
  71. server_port = self.get_port()
  72. if server_port != ('443' if self.is_secure() else '80'):
  73. host = '%s:%s' % (host, server_port)
  74. return host
  75. def get_host(self):
  76. """Return the HTTP host using the environment or request headers."""
  77. host = self._get_raw_host()
  78. # There is no hostname validation when DEBUG=True
  79. if settings.DEBUG:
  80. return host
  81. domain, port = split_domain_port(host)
  82. if domain and validate_host(domain, settings.ALLOWED_HOSTS):
  83. return host
  84. else:
  85. msg = "Invalid HTTP_HOST header: %r." % host
  86. if domain:
  87. msg += " You may need to add %r to ALLOWED_HOSTS." % domain
  88. else:
  89. msg += " The domain name provided is not valid according to RFC 1034/1035."
  90. raise DisallowedHost(msg)
  91. def get_port(self):
  92. """Return the port number for the request as a string."""
  93. if settings.USE_X_FORWARDED_PORT and 'HTTP_X_FORWARDED_PORT' in self.META:
  94. port = self.META['HTTP_X_FORWARDED_PORT']
  95. else:
  96. port = self.META['SERVER_PORT']
  97. return str(port)
  98. def get_full_path(self, force_append_slash=False):
  99. # RFC 3986 requires query string arguments to be in the ASCII range.
  100. # Rather than crash if this doesn't happen, we encode defensively.
  101. return '%s%s%s' % (
  102. escape_uri_path(self.path),
  103. '/' if force_append_slash and not self.path.endswith('/') else '',
  104. ('?' + iri_to_uri(self.META.get('QUERY_STRING', ''))) if self.META.get('QUERY_STRING', '') else ''
  105. )
  106. def get_signed_cookie(self, key, default=RAISE_ERROR, salt='', max_age=None):
  107. """
  108. Attempts to return a signed cookie. If the signature fails or the
  109. cookie has expired, raises an exception... unless you provide the
  110. default argument in which case that value will be returned instead.
  111. """
  112. try:
  113. cookie_value = self.COOKIES[key]
  114. except KeyError:
  115. if default is not RAISE_ERROR:
  116. return default
  117. else:
  118. raise
  119. try:
  120. value = signing.get_cookie_signer(salt=key + salt).unsign(
  121. cookie_value, max_age=max_age)
  122. except signing.BadSignature:
  123. if default is not RAISE_ERROR:
  124. return default
  125. else:
  126. raise
  127. return value
  128. def get_raw_uri(self):
  129. """
  130. Return an absolute URI from variables available in this request. Skip
  131. allowed hosts protection, so may return insecure URI.
  132. """
  133. return '{scheme}://{host}{path}'.format(
  134. scheme=self.scheme,
  135. host=self._get_raw_host(),
  136. path=self.get_full_path(),
  137. )
  138. def build_absolute_uri(self, location=None):
  139. """
  140. Builds an absolute URI from the location and the variables available in
  141. this request. If no ``location`` is specified, the absolute URI is
  142. built on ``request.get_full_path()``. Anyway, if the location is
  143. absolute, it is simply converted to an RFC 3987 compliant URI and
  144. returned and if location is relative or is scheme-relative (i.e.,
  145. ``//example.com/``), it is urljoined to a base URL constructed from the
  146. request variables.
  147. """
  148. if location is None:
  149. # Make it an absolute url (but schemeless and domainless) for the
  150. # edge case that the path starts with '//'.
  151. location = '//%s' % self.get_full_path()
  152. bits = urlsplit(location)
  153. if not (bits.scheme and bits.netloc):
  154. current_uri = '{scheme}://{host}{path}'.format(scheme=self.scheme,
  155. host=self.get_host(),
  156. path=self.path)
  157. # Join the constructed URL with the provided location, which will
  158. # allow the provided ``location`` to apply query strings to the
  159. # base path as well as override the host, if it begins with //
  160. location = urljoin(current_uri, location)
  161. return iri_to_uri(location)
  162. def _get_scheme(self):
  163. """
  164. Hook for subclasses like WSGIRequest to implement. Returns 'http' by
  165. default.
  166. """
  167. return 'http'
  168. @property
  169. def scheme(self):
  170. if settings.SECURE_PROXY_SSL_HEADER:
  171. try:
  172. header, value = settings.SECURE_PROXY_SSL_HEADER
  173. except ValueError:
  174. raise ImproperlyConfigured(
  175. 'The SECURE_PROXY_SSL_HEADER setting must be a tuple containing two values.'
  176. )
  177. if self.META.get(header) == value:
  178. return 'https'
  179. return self._get_scheme()
  180. def is_secure(self):
  181. return self.scheme == 'https'
  182. def is_ajax(self):
  183. return self.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'
  184. @property
  185. def encoding(self):
  186. return self._encoding
  187. @encoding.setter
  188. def encoding(self, val):
  189. """
  190. Sets the encoding used for GET/POST accesses. If the GET or POST
  191. dictionary has already been created, it is removed and recreated on the
  192. next access (so that it is decoded correctly).
  193. """
  194. self._encoding = val
  195. if hasattr(self, '_get'):
  196. del self._get
  197. if hasattr(self, '_post'):
  198. del self._post
  199. def _initialize_handlers(self):
  200. self._upload_handlers = [uploadhandler.load_handler(handler, self)
  201. for handler in settings.FILE_UPLOAD_HANDLERS]
  202. @property
  203. def upload_handlers(self):
  204. if not self._upload_handlers:
  205. # If there are no upload handlers defined, initialize them from settings.
  206. self._initialize_handlers()
  207. return self._upload_handlers
  208. @upload_handlers.setter
  209. def upload_handlers(self, upload_handlers):
  210. if hasattr(self, '_files'):
  211. raise AttributeError("You cannot set the upload handlers after the upload has been processed.")
  212. self._upload_handlers = upload_handlers
  213. def parse_file_upload(self, META, post_data):
  214. """Returns a tuple of (POST QueryDict, FILES MultiValueDict)."""
  215. self.upload_handlers = ImmutableList(
  216. self.upload_handlers,
  217. warning="You cannot alter upload handlers after the upload has been processed."
  218. )
  219. parser = MultiPartParser(META, post_data, self.upload_handlers, self.encoding)
  220. return parser.parse()
  221. @property
  222. def body(self):
  223. if not hasattr(self, '_body'):
  224. if self._read_started:
  225. raise RawPostDataException("You cannot access body after reading from request's data stream")
  226. try:
  227. self._body = self.read()
  228. except IOError as e:
  229. six.reraise(UnreadablePostError, UnreadablePostError(*e.args), sys.exc_info()[2])
  230. self._stream = BytesIO(self._body)
  231. return self._body
  232. def _mark_post_parse_error(self):
  233. self._post = QueryDict('')
  234. self._files = MultiValueDict()
  235. self._post_parse_error = True
  236. def _load_post_and_files(self):
  237. """Populate self._post and self._files if the content-type is a form type"""
  238. if self.method != 'POST':
  239. self._post, self._files = QueryDict('', encoding=self._encoding), MultiValueDict()
  240. return
  241. if self._read_started and not hasattr(self, '_body'):
  242. self._mark_post_parse_error()
  243. return
  244. if self.META.get('CONTENT_TYPE', '').startswith('multipart/form-data'):
  245. if hasattr(self, '_body'):
  246. # Use already read data
  247. data = BytesIO(self._body)
  248. else:
  249. data = self
  250. try:
  251. self._post, self._files = self.parse_file_upload(self.META, data)
  252. except MultiPartParserError:
  253. # An error occurred while parsing POST data. Since when
  254. # formatting the error the request handler might access
  255. # self.POST, set self._post and self._file to prevent
  256. # attempts to parse POST data again.
  257. # Mark that an error occurred. This allows self.__repr__ to
  258. # be explicit about it instead of simply representing an
  259. # empty POST
  260. self._mark_post_parse_error()
  261. raise
  262. elif self.META.get('CONTENT_TYPE', '').startswith('application/x-www-form-urlencoded'):
  263. self._post, self._files = QueryDict(self.body, encoding=self._encoding), MultiValueDict()
  264. else:
  265. self._post, self._files = QueryDict('', encoding=self._encoding), MultiValueDict()
  266. def close(self):
  267. if hasattr(self, '_files'):
  268. for f in chain.from_iterable(l[1] for l in self._files.lists()):
  269. f.close()
  270. # File-like and iterator interface.
  271. #
  272. # Expects self._stream to be set to an appropriate source of bytes by
  273. # a corresponding request subclass (e.g. WSGIRequest).
  274. # Also when request data has already been read by request.POST or
  275. # request.body, self._stream points to a BytesIO instance
  276. # containing that data.
  277. def read(self, *args, **kwargs):
  278. self._read_started = True
  279. try:
  280. return self._stream.read(*args, **kwargs)
  281. except IOError as e:
  282. six.reraise(UnreadablePostError, UnreadablePostError(*e.args), sys.exc_info()[2])
  283. def readline(self, *args, **kwargs):
  284. self._read_started = True
  285. try:
  286. return self._stream.readline(*args, **kwargs)
  287. except IOError as e:
  288. six.reraise(UnreadablePostError, UnreadablePostError(*e.args), sys.exc_info()[2])
  289. def xreadlines(self):
  290. while True:
  291. buf = self.readline()
  292. if not buf:
  293. break
  294. yield buf
  295. __iter__ = xreadlines
  296. def readlines(self):
  297. return list(iter(self))
  298. class QueryDict(MultiValueDict):
  299. """
  300. A specialized MultiValueDict which represents a query string.
  301. A QueryDict can be used to represent GET or POST data. It subclasses
  302. MultiValueDict since keys in such data can be repeated, for instance
  303. in the data from a form with a <select multiple> field.
  304. By default QueryDicts are immutable, though the copy() method
  305. will always return a mutable copy.
  306. Both keys and values set on this class are converted from the given encoding
  307. (DEFAULT_CHARSET by default) to unicode.
  308. """
  309. # These are both reset in __init__, but is specified here at the class
  310. # level so that unpickling will have valid values
  311. _mutable = True
  312. _encoding = None
  313. def __init__(self, query_string=None, mutable=False, encoding=None):
  314. super(QueryDict, self).__init__()
  315. if not encoding:
  316. encoding = settings.DEFAULT_CHARSET
  317. self.encoding = encoding
  318. if six.PY3:
  319. if isinstance(query_string, bytes):
  320. # query_string normally contains URL-encoded data, a subset of ASCII.
  321. try:
  322. query_string = query_string.decode(encoding)
  323. except UnicodeDecodeError:
  324. # ... but some user agents are misbehaving :-(
  325. query_string = query_string.decode('iso-8859-1')
  326. for key, value in parse_qsl(query_string or '',
  327. keep_blank_values=True,
  328. encoding=encoding):
  329. self.appendlist(key, value)
  330. else:
  331. for key, value in parse_qsl(query_string or '',
  332. keep_blank_values=True):
  333. try:
  334. value = value.decode(encoding)
  335. except UnicodeDecodeError:
  336. value = value.decode('iso-8859-1')
  337. self.appendlist(force_text(key, encoding, errors='replace'),
  338. value)
  339. self._mutable = mutable
  340. @property
  341. def encoding(self):
  342. if self._encoding is None:
  343. self._encoding = settings.DEFAULT_CHARSET
  344. return self._encoding
  345. @encoding.setter
  346. def encoding(self, value):
  347. self._encoding = value
  348. def _assert_mutable(self):
  349. if not self._mutable:
  350. raise AttributeError("This QueryDict instance is immutable")
  351. def __setitem__(self, key, value):
  352. self._assert_mutable()
  353. key = bytes_to_text(key, self.encoding)
  354. value = bytes_to_text(value, self.encoding)
  355. super(QueryDict, self).__setitem__(key, value)
  356. def __delitem__(self, key):
  357. self._assert_mutable()
  358. super(QueryDict, self).__delitem__(key)
  359. def __copy__(self):
  360. result = self.__class__('', mutable=True, encoding=self.encoding)
  361. for key, value in six.iterlists(self):
  362. result.setlist(key, value)
  363. return result
  364. def __deepcopy__(self, memo):
  365. result = self.__class__('', mutable=True, encoding=self.encoding)
  366. memo[id(self)] = result
  367. for key, value in six.iterlists(self):
  368. result.setlist(copy.deepcopy(key, memo), copy.deepcopy(value, memo))
  369. return result
  370. def setlist(self, key, list_):
  371. self._assert_mutable()
  372. key = bytes_to_text(key, self.encoding)
  373. list_ = [bytes_to_text(elt, self.encoding) for elt in list_]
  374. super(QueryDict, self).setlist(key, list_)
  375. def setlistdefault(self, key, default_list=None):
  376. self._assert_mutable()
  377. return super(QueryDict, self).setlistdefault(key, default_list)
  378. def appendlist(self, key, value):
  379. self._assert_mutable()
  380. key = bytes_to_text(key, self.encoding)
  381. value = bytes_to_text(value, self.encoding)
  382. super(QueryDict, self).appendlist(key, value)
  383. def pop(self, key, *args):
  384. self._assert_mutable()
  385. return super(QueryDict, self).pop(key, *args)
  386. def popitem(self):
  387. self._assert_mutable()
  388. return super(QueryDict, self).popitem()
  389. def clear(self):
  390. self._assert_mutable()
  391. super(QueryDict, self).clear()
  392. def setdefault(self, key, default=None):
  393. self._assert_mutable()
  394. key = bytes_to_text(key, self.encoding)
  395. default = bytes_to_text(default, self.encoding)
  396. return super(QueryDict, self).setdefault(key, default)
  397. def copy(self):
  398. """Returns a mutable copy of this object."""
  399. return self.__deepcopy__({})
  400. def urlencode(self, safe=None):
  401. """
  402. Returns an encoded string of all query string arguments.
  403. :arg safe: Used to specify characters which do not require quoting, for
  404. example::
  405. >>> q = QueryDict('', mutable=True)
  406. >>> q['next'] = '/a&b/'
  407. >>> q.urlencode()
  408. 'next=%2Fa%26b%2F'
  409. >>> q.urlencode(safe='/')
  410. 'next=/a%26b/'
  411. """
  412. output = []
  413. if safe:
  414. safe = force_bytes(safe, self.encoding)
  415. encode = lambda k, v: '%s=%s' % ((quote(k, safe), quote(v, safe)))
  416. else:
  417. encode = lambda k, v: urlencode({k: v})
  418. for k, list_ in self.lists():
  419. k = force_bytes(k, self.encoding)
  420. output.extend(encode(k, force_bytes(v, self.encoding))
  421. for v in list_)
  422. return '&'.join(output)
  423. # It's neither necessary nor appropriate to use
  424. # django.utils.encoding.smart_text for parsing URLs and form inputs. Thus,
  425. # this slightly more restricted function, used by QueryDict.
  426. def bytes_to_text(s, encoding):
  427. """
  428. Converts basestring objects to unicode, using the given encoding. Illegally
  429. encoded input characters are replaced with Unicode "unknown" codepoint
  430. (\ufffd).
  431. Returns any non-basestring objects without change.
  432. """
  433. if isinstance(s, bytes):
  434. return six.text_type(s, encoding, 'replace')
  435. else:
  436. return s
  437. def split_domain_port(host):
  438. """
  439. Return a (domain, port) tuple from a given host.
  440. Returned domain is lower-cased. If the host is invalid, the domain will be
  441. empty.
  442. """
  443. host = host.lower()
  444. if not host_validation_re.match(host):
  445. return '', ''
  446. if host[-1] == ']':
  447. # It's an IPv6 address without a port.
  448. return host, ''
  449. bits = host.rsplit(':', 1)
  450. if len(bits) == 2:
  451. return tuple(bits)
  452. return bits[0], ''
  453. def validate_host(host, allowed_hosts):
  454. """
  455. Validate the given host for this site.
  456. Check that the host looks valid and matches a host or host pattern in the
  457. given list of ``allowed_hosts``. Any pattern beginning with a period
  458. matches a domain and all its subdomains (e.g. ``.example.com`` matches
  459. ``example.com`` and any subdomain), ``*`` matches anything, and anything
  460. else must match exactly.
  461. Note: This function assumes that the given host is lower-cased and has
  462. already had the port, if any, stripped off.
  463. Return ``True`` for a valid host, ``False`` otherwise.
  464. """
  465. host = host[:-1] if host.endswith('.') else host
  466. for pattern in allowed_hosts:
  467. if pattern == '*' or is_same_domain(host, pattern):
  468. return True
  469. return False