2009-05-08 17:51:05 +08:00
|
|
|
import codecs
|
2012-07-20 21:36:52 +08:00
|
|
|
import datetime
|
|
|
|
import locale
|
2015-01-28 20:35:27 +08:00
|
|
|
from decimal import Decimal
|
2017-02-07 21:55:44 +08:00
|
|
|
from urllib.parse import quote
|
2009-02-12 04:13:17 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils.functional import Promise
|
2007-04-04 14:34:19 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2007-11-04 11:37:04 +08:00
|
|
|
class DjangoUnicodeDecodeError(UnicodeDecodeError):
|
|
|
|
def __init__(self, obj, *args):
|
|
|
|
self.obj = obj
|
2017-02-02 04:50:18 +08:00
|
|
|
super().__init__(*args)
|
2007-11-04 11:37:04 +08:00
|
|
|
|
|
|
|
def __str__(self):
|
2017-02-02 04:50:18 +08:00
|
|
|
return "%s. You passed in %r (%s)" % (
|
|
|
|
super().__str__(),
|
|
|
|
self.obj,
|
|
|
|
type(self.obj),
|
|
|
|
)
|
2007-11-04 11:37:04 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2017-01-26 17:08:08 +08:00
|
|
|
def smart_str(s, encoding="utf-8", strings_only=False, errors="strict"):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
"""
|
2017-01-21 05:04:05 +08:00
|
|
|
Return a string representing 's'. Treat bytestrings using the 'encoding'
|
|
|
|
codec.
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
|
|
|
If strings_only is True, don't convert (some) non-string-like objects.
|
|
|
|
"""
|
2007-04-04 14:34:19 +08:00
|
|
|
if isinstance(s, Promise):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
# The input is the result of a gettext_lazy() call.
|
|
|
|
return s
|
2017-01-26 17:08:08 +08:00
|
|
|
return force_str(s, encoding, strings_only, errors)
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2016-12-29 23:27:49 +08:00
|
|
|
_PROTECTED_TYPES = (
|
|
|
|
type(None),
|
|
|
|
int,
|
|
|
|
float,
|
|
|
|
Decimal,
|
|
|
|
datetime.datetime,
|
|
|
|
datetime.date,
|
|
|
|
datetime.time,
|
2016-03-29 06:33:29 +08:00
|
|
|
)
|
2014-06-08 11:36:59 +08:00
|
|
|
|
|
|
|
|
2009-04-13 20:35:49 +08:00
|
|
|
def is_protected_type(obj):
|
|
|
|
"""Determine if the object instance is of a protected type.
|
|
|
|
|
|
|
|
Objects of protected types are preserved as-is when passed to
|
2017-01-26 17:08:08 +08:00
|
|
|
force_str(strings_only=True).
|
2009-04-13 20:35:49 +08:00
|
|
|
"""
|
2014-06-08 11:36:59 +08:00
|
|
|
return isinstance(obj, _PROTECTED_TYPES)
|
2009-04-13 20:35:49 +08:00
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2017-01-26 17:08:08 +08:00
|
|
|
def force_str(s, encoding="utf-8", strings_only=False, errors="strict"):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
"""
|
2017-01-26 17:08:08 +08:00
|
|
|
Similar to smart_str(), except that lazy instances are resolved to
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
strings, rather than kept as lazy objects.
|
|
|
|
|
|
|
|
If strings_only is True, don't convert (some) non-string-like objects.
|
|
|
|
"""
|
2013-09-06 23:34:32 +08:00
|
|
|
# Handle the common case first for performance reasons.
|
2016-12-29 23:27:49 +08:00
|
|
|
if issubclass(type(s), str):
|
2010-09-27 23:25:08 +08:00
|
|
|
return s
|
2009-04-13 20:35:49 +08:00
|
|
|
if strings_only and is_protected_type(s):
|
2007-04-04 14:34:19 +08:00
|
|
|
return s
|
2007-11-04 11:37:04 +08:00
|
|
|
try:
|
2017-02-01 07:31:17 +08:00
|
|
|
if isinstance(s, bytes):
|
|
|
|
s = str(s, encoding, errors)
|
2012-07-24 06:28:29 +08:00
|
|
|
else:
|
2017-02-01 07:31:17 +08:00
|
|
|
s = str(s)
|
2012-04-29 00:09:37 +08:00
|
|
|
except UnicodeDecodeError as e:
|
2017-02-04 08:38:33 +08:00
|
|
|
raise DjangoUnicodeDecodeError(s, *e.args)
|
2007-04-04 14:34:19 +08:00
|
|
|
return s
|
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2012-07-21 16:00:10 +08:00
|
|
|
def smart_bytes(s, encoding="utf-8", strings_only=False, errors="strict"):
|
2007-04-04 14:34:19 +08:00
|
|
|
"""
|
2017-01-25 04:32:33 +08:00
|
|
|
Return a bytestring version of 's', encoded as specified in 'encoding'.
|
2007-04-04 14:34:19 +08:00
|
|
|
|
2012-08-18 22:36:55 +08:00
|
|
|
If strings_only is True, don't convert (some) non-string-like objects.
|
|
|
|
"""
|
|
|
|
if isinstance(s, Promise):
|
|
|
|
# The input is the result of a gettext_lazy() call.
|
|
|
|
return s
|
|
|
|
return force_bytes(s, encoding, strings_only, errors)
|
|
|
|
|
|
|
|
|
|
|
|
def force_bytes(s, encoding="utf-8", strings_only=False, errors="strict"):
|
|
|
|
"""
|
|
|
|
Similar to smart_bytes, except that lazy instances are resolved to
|
|
|
|
strings, rather than kept as lazy objects.
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
If strings_only is True, don't convert (some) non-string-like objects.
|
2007-04-04 14:34:19 +08:00
|
|
|
"""
|
2013-09-06 23:34:32 +08:00
|
|
|
# Handle the common case first for performance reasons.
|
2012-07-21 16:00:10 +08:00
|
|
|
if isinstance(s, bytes):
|
|
|
|
if encoding == "utf-8":
|
|
|
|
return s
|
|
|
|
else:
|
|
|
|
return s.decode("utf-8", errors).encode(encoding, errors)
|
2013-09-06 23:28:28 +08:00
|
|
|
if strings_only and is_protected_type(s):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return s
|
2016-12-29 23:27:49 +08:00
|
|
|
if isinstance(s, memoryview):
|
2013-09-06 23:34:32 +08:00
|
|
|
return bytes(s)
|
2018-07-09 14:26:22 +08:00
|
|
|
return str(s).encode(encoding, errors)
|
2012-07-21 16:00:10 +08:00
|
|
|
|
2016-11-13 01:11:23 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def iri_to_uri(iri):
|
|
|
|
"""
|
|
|
|
Convert an Internationalized Resource Identifier (IRI) portion to a URI
|
|
|
|
portion that is suitable for inclusion in a URL.
|
|
|
|
|
2017-01-24 00:44:25 +08:00
|
|
|
This is the algorithm from section 3.1 of RFC 3987, slightly simplified
|
|
|
|
since the input is assumed to be a string rather than an arbitrary byte
|
|
|
|
stream.
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2017-01-24 00:44:25 +08:00
|
|
|
Take an IRI (string or UTF-8 bytes, e.g. '/I ♥ Django/' or
|
|
|
|
b'/I \xe2\x99\xa5 Django/') and return a string containing the encoded
|
|
|
|
result with ASCII chars only (e.g. '/I%20%E2%99%A5%20Django/').
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
"""
|
2010-01-04 02:06:27 +08:00
|
|
|
# The list of safe characters here is constructed from the "reserved" and
|
|
|
|
# "unreserved" characters specified in sections 2.2 and 2.3 of RFC 3986:
|
|
|
|
# reserved = gen-delims / sub-delims
|
|
|
|
# gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
|
|
|
|
# sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
|
|
|
|
# / "*" / "+" / "," / ";" / "="
|
|
|
|
# unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
|
2017-11-01 00:05:54 +08:00
|
|
|
# Of the unreserved characters, urllib.parse.quote() already considers all
|
|
|
|
# but the ~ safe.
|
2010-01-04 02:06:27 +08:00
|
|
|
# The % character is also added to the list of safe characters here, as the
|
|
|
|
# end of section 3.1 of RFC 3987 specifically mentions that % must not be
|
|
|
|
# converted.
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
if iri is None:
|
|
|
|
return iri
|
2017-01-24 00:44:25 +08:00
|
|
|
elif isinstance(iri, Promise):
|
|
|
|
iri = str(iri)
|
|
|
|
return quote(iri, safe="/#%[]=:;$&()+,!?*@'~")
|
2007-04-04 14:34:19 +08:00
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2017-02-07 21:55:44 +08:00
|
|
|
# List of byte values that uri_to_iri() decodes from percent encoding.
|
|
|
|
# First, the unreserved characters from RFC 3986:
|
|
|
|
_ascii_ranges = [[45, 46, 95, 126], range(65, 91), range(97, 123)]
|
|
|
|
_hextobyte = {
|
|
|
|
(fmt % char).encode(): bytes((char,))
|
|
|
|
for ascii_range in _ascii_ranges
|
|
|
|
for char in ascii_range
|
|
|
|
for fmt in ["%02x", "%02X"]
|
|
|
|
}
|
|
|
|
# And then everything above 128, because bytes ≥ 128 are part of multibyte
|
2020-04-18 22:46:05 +08:00
|
|
|
# Unicode characters.
|
2017-02-07 21:55:44 +08:00
|
|
|
_hexdig = "0123456789ABCDEFabcdef"
|
|
|
|
_hextobyte.update(
|
|
|
|
{(a + b).encode(): bytes.fromhex(a + b) for a in _hexdig[8:] for b in _hexdig}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2014-07-22 20:25:22 +08:00
|
|
|
def uri_to_iri(uri):
|
|
|
|
"""
|
2017-01-25 04:32:33 +08:00
|
|
|
Convert a Uniform Resource Identifier(URI) into an Internationalized
|
2014-07-22 20:25:22 +08:00
|
|
|
Resource Identifier(IRI).
|
|
|
|
|
2017-02-07 21:55:44 +08:00
|
|
|
This is the algorithm from section 3.2 of RFC 3987, excluding step 4.
|
2014-07-22 20:25:22 +08:00
|
|
|
|
2017-01-25 04:32:33 +08:00
|
|
|
Take an URI in ASCII bytes (e.g. '/I%20%E2%99%A5%20Django/') and return
|
2017-02-07 21:55:44 +08:00
|
|
|
a string containing the encoded result (e.g. '/I%20♥%20Django/').
|
2014-07-22 20:25:22 +08:00
|
|
|
"""
|
|
|
|
if uri is None:
|
|
|
|
return uri
|
|
|
|
uri = force_bytes(uri)
|
2021-05-29 18:51:14 +08:00
|
|
|
# Fast selective unquote: First, split on '%' and then starting with the
|
2017-02-07 21:55:44 +08:00
|
|
|
# second block, decode the first 2 bytes if they represent a hex code to
|
|
|
|
# decode. The rest of the block is the part after '%AB', not containing
|
|
|
|
# any '%'. Add that to the output without further processing.
|
|
|
|
bits = uri.split(b"%")
|
|
|
|
if len(bits) == 1:
|
|
|
|
iri = uri
|
|
|
|
else:
|
|
|
|
parts = [bits[0]]
|
|
|
|
append = parts.append
|
|
|
|
hextobyte = _hextobyte
|
|
|
|
for item in bits[1:]:
|
|
|
|
hex = item[:2]
|
|
|
|
if hex in hextobyte:
|
|
|
|
append(hextobyte[item[:2]])
|
|
|
|
append(item[2:])
|
|
|
|
else:
|
|
|
|
append(b"%")
|
|
|
|
append(item)
|
|
|
|
iri = b"".join(parts)
|
2017-02-08 01:05:47 +08:00
|
|
|
return repercent_broken_unicode(iri).decode()
|
2014-07-22 20:25:22 +08:00
|
|
|
|
|
|
|
|
2014-10-31 23:43:34 +08:00
|
|
|
def escape_uri_path(path):
|
|
|
|
"""
|
|
|
|
Escape the unsafe characters from the path portion of a Uniform Resource
|
|
|
|
Identifier (URI).
|
|
|
|
"""
|
|
|
|
# These are the "reserved" and "unreserved" characters specified in
|
|
|
|
# sections 2.2 and 2.3 of RFC 2396:
|
|
|
|
# reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ","
|
|
|
|
# unreserved = alphanum | mark
|
|
|
|
# mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
|
2015-11-03 17:55:10 +08:00
|
|
|
# The list of safe characters here is constructed subtracting ";", "=",
|
2014-10-31 23:43:34 +08:00
|
|
|
# and "?" according to section 3.3 of RFC 2396.
|
|
|
|
# The reason for not subtracting and escaping "/" is that we are escaping
|
|
|
|
# the entire path, not a path segment.
|
2017-01-26 16:37:07 +08:00
|
|
|
return quote(path, safe="/:@&+$,-_.!~*'()")
|
2014-10-31 23:43:34 +08:00
|
|
|
|
|
|
|
|
2019-07-03 01:32:17 +08:00
|
|
|
def punycode(domain):
|
|
|
|
"""Return the Punycode of the given domain if it's non-ASCII."""
|
|
|
|
return domain.encode("idna").decode("ascii")
|
|
|
|
|
|
|
|
|
2014-07-22 20:25:22 +08:00
|
|
|
def repercent_broken_unicode(path):
|
|
|
|
"""
|
|
|
|
As per section 3.2 of RFC 3987, step three of converting a URI into an IRI,
|
2017-01-25 04:32:33 +08:00
|
|
|
repercent-encode any octet produced that is not part of a strictly legal
|
|
|
|
UTF-8 octet sequence.
|
2014-07-22 20:25:22 +08:00
|
|
|
"""
|
2019-07-19 23:04:53 +08:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
path.decode()
|
|
|
|
except UnicodeDecodeError as e:
|
|
|
|
# CVE-2019-14235: A recursion shouldn't be used since the exception
|
|
|
|
# handling uses massive amounts of memory
|
|
|
|
repercent = quote(path[e.start : e.end], safe=b"/#%[]=:;$&()+,!?*@'~")
|
2019-08-28 15:20:46 +08:00
|
|
|
path = path[: e.start] + repercent.encode() + path[e.end :]
|
2019-07-19 23:04:53 +08:00
|
|
|
else:
|
|
|
|
return path
|
2014-07-22 20:25:22 +08:00
|
|
|
|
|
|
|
|
2011-02-04 22:43:10 +08:00
|
|
|
def filepath_to_uri(path):
|
2012-12-17 03:34:41 +08:00
|
|
|
"""Convert a file system path to a URI portion that is suitable for
|
2011-02-04 22:43:10 +08:00
|
|
|
inclusion in a URL.
|
|
|
|
|
2017-01-25 04:32:33 +08:00
|
|
|
Encode certain chars that would normally be recognized as special chars
|
|
|
|
for URIs. Do not encode the ' character, as it is a valid character
|
|
|
|
within URIs. See the encodeURIComponent() JavaScript function for details.
|
2011-02-04 22:43:10 +08:00
|
|
|
"""
|
|
|
|
if path is None:
|
|
|
|
return path
|
|
|
|
# I know about `os.sep` and `os.altsep` but I want to leave
|
|
|
|
# some flexibility for hardcoding separators.
|
2019-10-30 04:15:18 +08:00
|
|
|
return quote(str(path).replace("\\", "/"), safe="/~!*()'")
|
2009-05-08 17:51:05 +08:00
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2013-03-03 05:41:08 +08:00
|
|
|
def get_system_encoding():
|
|
|
|
"""
|
2022-03-08 20:17:05 +08:00
|
|
|
The encoding for the character type functions. Fallback to 'ascii' if the
|
2017-01-25 04:32:33 +08:00
|
|
|
#encoding is unsupported by Python or could not be determined. See tickets
|
|
|
|
#10335 and #5846.
|
2013-03-03 05:41:08 +08:00
|
|
|
"""
|
|
|
|
try:
|
2022-03-08 20:17:05 +08:00
|
|
|
encoding = locale.getlocale()[1] or "ascii"
|
2013-03-03 05:41:08 +08:00
|
|
|
codecs.lookup(encoding)
|
|
|
|
except Exception:
|
|
|
|
encoding = "ascii"
|
|
|
|
return encoding
|
|
|
|
|
2016-11-13 01:11:23 +08:00
|
|
|
|
2013-03-03 05:41:08 +08:00
|
|
|
DEFAULT_LOCALE_ENCODING = get_system_encoding()
|