request.py 25 KB

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