request.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. import cgi
  2. import codecs
  3. import copy
  4. import re
  5. from io import BytesIO
  6. from itertools import chain
  7. from urllib.parse import quote, urlencode, urljoin, urlsplit
  8. from django.conf import settings
  9. from django.core import signing
  10. from django.core.exceptions import (
  11. DisallowedHost, ImproperlyConfigured, RequestDataTooBig,
  12. )
  13. from django.core.files import uploadhandler
  14. from django.http.multipartparser import MultiPartParser, MultiPartParserError
  15. from django.utils.datastructures import (
  16. CaseInsensitiveMapping, ImmutableList, MultiValueDict,
  17. )
  18. from django.utils.encoding import escape_uri_path, iri_to_uri
  19. from django.utils.functional import cached_property
  20. from django.utils.http import is_same_domain, limited_parse_qsl
  21. RAISE_ERROR = object()
  22. host_validation_re = re.compile(r"^([a-z0-9.-]+|\[[a-f0-9]*:[a-f0-9\.:]+\])(:\d+)?$")
  23. class UnreadablePostError(OSError):
  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:
  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.content_type = None
  51. self.content_params = None
  52. def __repr__(self):
  53. if self.method is None or not self.get_full_path():
  54. return '<%s>' % self.__class__.__name__
  55. return '<%s: %s %r>' % (self.__class__.__name__, self.method, self.get_full_path())
  56. @cached_property
  57. def headers(self):
  58. return HttpHeaders(self.META)
  59. def _set_content_type_params(self, meta):
  60. """Set content_type, content_params, and encoding."""
  61. self.content_type, self.content_params = cgi.parse_header(meta.get('CONTENT_TYPE', ''))
  62. if 'charset' in self.content_params:
  63. try:
  64. codecs.lookup(self.content_params['charset'])
  65. except LookupError:
  66. pass
  67. else:
  68. self.encoding = self.content_params['charset']
  69. def _get_raw_host(self):
  70. """
  71. Return the HTTP host using the environment or request headers. Skip
  72. allowed hosts protection, so may return an insecure host.
  73. """
  74. # We try three options, in order of decreasing preference.
  75. if settings.USE_X_FORWARDED_HOST and (
  76. 'HTTP_X_FORWARDED_HOST' in self.META):
  77. host = self.META['HTTP_X_FORWARDED_HOST']
  78. elif 'HTTP_HOST' in self.META:
  79. host = self.META['HTTP_HOST']
  80. else:
  81. # Reconstruct the host using the algorithm from PEP 333.
  82. host = self.META['SERVER_NAME']
  83. server_port = self.get_port()
  84. if server_port != ('443' if self.is_secure() else '80'):
  85. host = '%s:%s' % (host, server_port)
  86. return host
  87. def get_host(self):
  88. """Return the HTTP host using the environment or request headers."""
  89. host = self._get_raw_host()
  90. # Allow variants of localhost if ALLOWED_HOSTS is empty and DEBUG=True.
  91. allowed_hosts = settings.ALLOWED_HOSTS
  92. if settings.DEBUG and not allowed_hosts:
  93. allowed_hosts = ['localhost', '127.0.0.1', '[::1]']
  94. domain, port = split_domain_port(host)
  95. if domain and validate_host(domain, allowed_hosts):
  96. return host
  97. else:
  98. msg = "Invalid HTTP_HOST header: %r." % host
  99. if domain:
  100. msg += " You may need to add %r to ALLOWED_HOSTS." % domain
  101. else:
  102. msg += " The domain name provided is not valid according to RFC 1034/1035."
  103. raise DisallowedHost(msg)
  104. def get_port(self):
  105. """Return the port number for the request as a string."""
  106. if settings.USE_X_FORWARDED_PORT and 'HTTP_X_FORWARDED_PORT' in self.META:
  107. port = self.META['HTTP_X_FORWARDED_PORT']
  108. else:
  109. port = self.META['SERVER_PORT']
  110. return str(port)
  111. def get_full_path(self, force_append_slash=False):
  112. return self._get_full_path(self.path, force_append_slash)
  113. def get_full_path_info(self, force_append_slash=False):
  114. return self._get_full_path(self.path_info, force_append_slash)
  115. def _get_full_path(self, path, force_append_slash):
  116. # RFC 3986 requires query string arguments to be in the ASCII range.
  117. # Rather than crash if this doesn't happen, we encode defensively.
  118. return '%s%s%s' % (
  119. escape_uri_path(path),
  120. '/' if force_append_slash and not path.endswith('/') else '',
  121. ('?' + iri_to_uri(self.META.get('QUERY_STRING', ''))) if self.META.get('QUERY_STRING', '') else ''
  122. )
  123. def get_signed_cookie(self, key, default=RAISE_ERROR, salt='', max_age=None):
  124. """
  125. Attempt to return a signed cookie. If the signature fails or the
  126. cookie has expired, raise an exception, unless the `default` argument
  127. is provided, in which case return that value.
  128. """
  129. try:
  130. cookie_value = self.COOKIES[key]
  131. except KeyError:
  132. if default is not RAISE_ERROR:
  133. return default
  134. else:
  135. raise
  136. try:
  137. value = signing.get_cookie_signer(salt=key + salt).unsign(
  138. cookie_value, max_age=max_age)
  139. except signing.BadSignature:
  140. if default is not RAISE_ERROR:
  141. return default
  142. else:
  143. raise
  144. return value
  145. def get_raw_uri(self):
  146. """
  147. Return an absolute URI from variables available in this request. Skip
  148. allowed hosts protection, so may return insecure URI.
  149. """
  150. return '{scheme}://{host}{path}'.format(
  151. scheme=self.scheme,
  152. host=self._get_raw_host(),
  153. path=self.get_full_path(),
  154. )
  155. def build_absolute_uri(self, location=None):
  156. """
  157. Build an absolute URI from the location and the variables available in
  158. this request. If no ``location`` is specified, build the absolute URI
  159. using request.get_full_path(). If the location is absolute, convert it
  160. to an RFC 3987 compliant URI and return it. If location is relative or
  161. is scheme-relative (i.e., ``//example.com/``), urljoin() it to a base
  162. URL constructed from the request variables.
  163. """
  164. if location is None:
  165. # Make it an absolute url (but schemeless and domainless) for the
  166. # edge case that the path starts with '//'.
  167. location = '//%s' % self.get_full_path()
  168. bits = urlsplit(location)
  169. if not (bits.scheme and bits.netloc):
  170. # Handle the simple, most common case. If the location is absolute
  171. # and a scheme or host (netloc) isn't provided, skip an expensive
  172. # urljoin() as long as no path segments are '.' or '..'.
  173. if (bits.path.startswith('/') and not bits.scheme and not bits.netloc and
  174. '/./' not in bits.path and '/../' not in bits.path):
  175. # If location starts with '//' but has no netloc, reuse the
  176. # schema and netloc from the current request. Strip the double
  177. # slashes and continue as if it wasn't specified.
  178. if location.startswith('//'):
  179. location = location[2:]
  180. location = self._current_scheme_host + location
  181. else:
  182. # Join the constructed URL with the provided location, which
  183. # allows the provided location to apply query strings to the
  184. # base path.
  185. location = urljoin(self._current_scheme_host + self.path, location)
  186. return iri_to_uri(location)
  187. @cached_property
  188. def _current_scheme_host(self):
  189. return '{}://{}'.format(self.scheme, self.get_host())
  190. def _get_scheme(self):
  191. """
  192. Hook for subclasses like WSGIRequest to implement. Return 'http' by
  193. default.
  194. """
  195. return 'http'
  196. @property
  197. def scheme(self):
  198. if settings.SECURE_PROXY_SSL_HEADER:
  199. try:
  200. header, secure_value = settings.SECURE_PROXY_SSL_HEADER
  201. except ValueError:
  202. raise ImproperlyConfigured(
  203. 'The SECURE_PROXY_SSL_HEADER setting must be a tuple containing two values.'
  204. )
  205. header_value = self.META.get(header)
  206. if header_value is not None:
  207. return 'https' if header_value == secure_value else 'http'
  208. return self._get_scheme()
  209. def is_secure(self):
  210. return self.scheme == 'https'
  211. def is_ajax(self):
  212. return self.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'
  213. @property
  214. def encoding(self):
  215. return self._encoding
  216. @encoding.setter
  217. def encoding(self, val):
  218. """
  219. Set the encoding used for GET/POST accesses. If the GET or POST
  220. dictionary has already been created, remove and recreate it on the
  221. next access (so that it is decoded correctly).
  222. """
  223. self._encoding = val
  224. if hasattr(self, 'GET'):
  225. del self.GET
  226. if hasattr(self, '_post'):
  227. del self._post
  228. def _initialize_handlers(self):
  229. self._upload_handlers = [uploadhandler.load_handler(handler, self)
  230. for handler in settings.FILE_UPLOAD_HANDLERS]
  231. @property
  232. def upload_handlers(self):
  233. if not self._upload_handlers:
  234. # If there are no upload handlers defined, initialize them from settings.
  235. self._initialize_handlers()
  236. return self._upload_handlers
  237. @upload_handlers.setter
  238. def upload_handlers(self, upload_handlers):
  239. if hasattr(self, '_files'):
  240. raise AttributeError("You cannot set the upload handlers after the upload has been processed.")
  241. self._upload_handlers = upload_handlers
  242. def parse_file_upload(self, META, post_data):
  243. """Return a tuple of (POST QueryDict, FILES MultiValueDict)."""
  244. self.upload_handlers = ImmutableList(
  245. self.upload_handlers,
  246. warning="You cannot alter upload handlers after the upload has been processed."
  247. )
  248. parser = MultiPartParser(META, post_data, self.upload_handlers, self.encoding)
  249. return parser.parse()
  250. @property
  251. def body(self):
  252. if not hasattr(self, '_body'):
  253. if self._read_started:
  254. raise RawPostDataException("You cannot access body after reading from request's data stream")
  255. # Limit the maximum request data size that will be handled in-memory.
  256. if (settings.DATA_UPLOAD_MAX_MEMORY_SIZE is not None and
  257. int(self.META.get('CONTENT_LENGTH') or 0) > settings.DATA_UPLOAD_MAX_MEMORY_SIZE):
  258. raise RequestDataTooBig('Request body exceeded settings.DATA_UPLOAD_MAX_MEMORY_SIZE.')
  259. try:
  260. self._body = self.read()
  261. except OSError as e:
  262. raise UnreadablePostError(*e.args) from e
  263. self._stream = BytesIO(self._body)
  264. return self._body
  265. def _mark_post_parse_error(self):
  266. self._post = QueryDict()
  267. self._files = MultiValueDict()
  268. def _load_post_and_files(self):
  269. """Populate self._post and self._files if the content-type is a form type"""
  270. if self.method != 'POST':
  271. self._post, self._files = QueryDict(encoding=self._encoding), MultiValueDict()
  272. return
  273. if self._read_started and not hasattr(self, '_body'):
  274. self._mark_post_parse_error()
  275. return
  276. if self.content_type == 'multipart/form-data':
  277. if hasattr(self, '_body'):
  278. # Use already read data
  279. data = BytesIO(self._body)
  280. else:
  281. data = self
  282. try:
  283. self._post, self._files = self.parse_file_upload(self.META, data)
  284. except MultiPartParserError:
  285. # An error occurred while parsing POST data. Since when
  286. # formatting the error the request handler might access
  287. # self.POST, set self._post and self._file to prevent
  288. # attempts to parse POST data again.
  289. self._mark_post_parse_error()
  290. raise
  291. elif self.content_type == 'application/x-www-form-urlencoded':
  292. self._post, self._files = QueryDict(self.body, encoding=self._encoding), MultiValueDict()
  293. else:
  294. self._post, self._files = QueryDict(encoding=self._encoding), MultiValueDict()
  295. def close(self):
  296. if hasattr(self, '_files'):
  297. for f in chain.from_iterable(l[1] for l in self._files.lists()):
  298. f.close()
  299. # File-like and iterator interface.
  300. #
  301. # Expects self._stream to be set to an appropriate source of bytes by
  302. # a corresponding request subclass (e.g. WSGIRequest).
  303. # Also when request data has already been read by request.POST or
  304. # request.body, self._stream points to a BytesIO instance
  305. # containing that data.
  306. def read(self, *args, **kwargs):
  307. self._read_started = True
  308. try:
  309. return self._stream.read(*args, **kwargs)
  310. except OSError as e:
  311. raise UnreadablePostError(*e.args) from e
  312. def readline(self, *args, **kwargs):
  313. self._read_started = True
  314. try:
  315. return self._stream.readline(*args, **kwargs)
  316. except OSError as e:
  317. raise UnreadablePostError(*e.args) from e
  318. def __iter__(self):
  319. return iter(self.readline, b'')
  320. def readlines(self):
  321. return list(self)
  322. class HttpHeaders(CaseInsensitiveMapping):
  323. HTTP_PREFIX = 'HTTP_'
  324. # PEP 333 gives two headers which aren't prepended with HTTP_.
  325. UNPREFIXED_HEADERS = {'CONTENT_TYPE', 'CONTENT_LENGTH'}
  326. def __init__(self, environ):
  327. headers = {}
  328. for header, value in environ.items():
  329. name = self.parse_header_name(header)
  330. if name:
  331. headers[name] = value
  332. super().__init__(headers)
  333. def __getitem__(self, key):
  334. """Allow header lookup using underscores in place of hyphens."""
  335. return super().__getitem__(key.replace('_', '-'))
  336. @classmethod
  337. def parse_header_name(cls, header):
  338. if header.startswith(cls.HTTP_PREFIX):
  339. header = header[len(cls.HTTP_PREFIX):]
  340. elif header not in cls.UNPREFIXED_HEADERS:
  341. return None
  342. return header.replace('_', '-').title()
  343. class QueryDict(MultiValueDict):
  344. """
  345. A specialized MultiValueDict which represents a query string.
  346. A QueryDict can be used to represent GET or POST data. It subclasses
  347. MultiValueDict since keys in such data can be repeated, for instance
  348. in the data from a form with a <select multiple> field.
  349. By default QueryDicts are immutable, though the copy() method
  350. will always return a mutable copy.
  351. Both keys and values set on this class are converted from the given encoding
  352. (DEFAULT_CHARSET by default) to str.
  353. """
  354. # These are both reset in __init__, but is specified here at the class
  355. # level so that unpickling will have valid values
  356. _mutable = True
  357. _encoding = None
  358. def __init__(self, query_string=None, mutable=False, encoding=None):
  359. super().__init__()
  360. self.encoding = encoding or settings.DEFAULT_CHARSET
  361. query_string = query_string or ''
  362. parse_qsl_kwargs = {
  363. 'keep_blank_values': True,
  364. 'fields_limit': settings.DATA_UPLOAD_MAX_NUMBER_FIELDS,
  365. 'encoding': self.encoding,
  366. }
  367. if isinstance(query_string, bytes):
  368. # query_string normally contains URL-encoded data, a subset of ASCII.
  369. try:
  370. query_string = query_string.decode(self.encoding)
  371. except UnicodeDecodeError:
  372. # ... but some user agents are misbehaving :-(
  373. query_string = query_string.decode('iso-8859-1')
  374. for key, value in limited_parse_qsl(query_string, **parse_qsl_kwargs):
  375. self.appendlist(key, value)
  376. self._mutable = mutable
  377. @classmethod
  378. def fromkeys(cls, iterable, value='', mutable=False, encoding=None):
  379. """
  380. Return a new QueryDict with keys (may be repeated) from an iterable and
  381. values from value.
  382. """
  383. q = cls('', mutable=True, encoding=encoding)
  384. for key in iterable:
  385. q.appendlist(key, value)
  386. if not mutable:
  387. q._mutable = False
  388. return q
  389. @property
  390. def encoding(self):
  391. if self._encoding is None:
  392. self._encoding = settings.DEFAULT_CHARSET
  393. return self._encoding
  394. @encoding.setter
  395. def encoding(self, value):
  396. self._encoding = value
  397. def _assert_mutable(self):
  398. if not self._mutable:
  399. raise AttributeError("This QueryDict instance is immutable")
  400. def __setitem__(self, key, value):
  401. self._assert_mutable()
  402. key = bytes_to_text(key, self.encoding)
  403. value = bytes_to_text(value, self.encoding)
  404. super().__setitem__(key, value)
  405. def __delitem__(self, key):
  406. self._assert_mutable()
  407. super().__delitem__(key)
  408. def __copy__(self):
  409. result = self.__class__('', mutable=True, encoding=self.encoding)
  410. for key, value in self.lists():
  411. result.setlist(key, value)
  412. return result
  413. def __deepcopy__(self, memo):
  414. result = self.__class__('', mutable=True, encoding=self.encoding)
  415. memo[id(self)] = result
  416. for key, value in self.lists():
  417. result.setlist(copy.deepcopy(key, memo), copy.deepcopy(value, memo))
  418. return result
  419. def setlist(self, key, list_):
  420. self._assert_mutable()
  421. key = bytes_to_text(key, self.encoding)
  422. list_ = [bytes_to_text(elt, self.encoding) for elt in list_]
  423. super().setlist(key, list_)
  424. def setlistdefault(self, key, default_list=None):
  425. self._assert_mutable()
  426. return super().setlistdefault(key, default_list)
  427. def appendlist(self, key, value):
  428. self._assert_mutable()
  429. key = bytes_to_text(key, self.encoding)
  430. value = bytes_to_text(value, self.encoding)
  431. super().appendlist(key, value)
  432. def pop(self, key, *args):
  433. self._assert_mutable()
  434. return super().pop(key, *args)
  435. def popitem(self):
  436. self._assert_mutable()
  437. return super().popitem()
  438. def clear(self):
  439. self._assert_mutable()
  440. super().clear()
  441. def setdefault(self, key, default=None):
  442. self._assert_mutable()
  443. key = bytes_to_text(key, self.encoding)
  444. default = bytes_to_text(default, self.encoding)
  445. return super().setdefault(key, default)
  446. def copy(self):
  447. """Return a mutable copy of this object."""
  448. return self.__deepcopy__({})
  449. def urlencode(self, safe=None):
  450. """
  451. Return an encoded string of all query string arguments.
  452. `safe` specifies characters which don't require quoting, for example::
  453. >>> q = QueryDict(mutable=True)
  454. >>> q['next'] = '/a&b/'
  455. >>> q.urlencode()
  456. 'next=%2Fa%26b%2F'
  457. >>> q.urlencode(safe='/')
  458. 'next=/a%26b/'
  459. """
  460. output = []
  461. if safe:
  462. safe = safe.encode(self.encoding)
  463. def encode(k, v):
  464. return '%s=%s' % ((quote(k, safe), quote(v, safe)))
  465. else:
  466. def encode(k, v):
  467. return urlencode({k: v})
  468. for k, list_ in self.lists():
  469. output.extend(
  470. encode(k.encode(self.encoding), str(v).encode(self.encoding))
  471. for v in list_
  472. )
  473. return '&'.join(output)
  474. # It's neither necessary nor appropriate to use
  475. # django.utils.encoding.force_str() for parsing URLs and form inputs. Thus,
  476. # this slightly more restricted function, used by QueryDict.
  477. def bytes_to_text(s, encoding):
  478. """
  479. Convert bytes objects to strings, using the given encoding. Illegally
  480. encoded input characters are replaced with Unicode "unknown" codepoint
  481. (\ufffd).
  482. Return any non-bytes objects without change.
  483. """
  484. if isinstance(s, bytes):
  485. return str(s, encoding, 'replace')
  486. else:
  487. return s
  488. def split_domain_port(host):
  489. """
  490. Return a (domain, port) tuple from a given host.
  491. Returned domain is lowercased. If the host is invalid, the domain will be
  492. empty.
  493. """
  494. host = host.lower()
  495. if not host_validation_re.match(host):
  496. return '', ''
  497. if host[-1] == ']':
  498. # It's an IPv6 address without a port.
  499. return host, ''
  500. bits = host.rsplit(':', 1)
  501. domain, port = bits if len(bits) == 2 else (bits[0], '')
  502. # Remove a trailing dot (if present) from the domain.
  503. domain = domain[:-1] if domain.endswith('.') else domain
  504. return domain, port
  505. def validate_host(host, allowed_hosts):
  506. """
  507. Validate the given host for this site.
  508. Check that the host looks valid and matches a host or host pattern in the
  509. given list of ``allowed_hosts``. Any pattern beginning with a period
  510. matches a domain and all its subdomains (e.g. ``.example.com`` matches
  511. ``example.com`` and any subdomain), ``*`` matches anything, and anything
  512. else must match exactly.
  513. Note: This function assumes that the given host is lowercased and has
  514. already had the port, if any, stripped off.
  515. Return ``True`` for a valid host, ``False`` otherwise.
  516. """
  517. return any(pattern == '*' or is_same_domain(host, pattern) for pattern in allowed_hosts)