request.py 25 KB

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