Debian Codegraph @

django/http/request.py

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