summaryrefslogtreecommitdiff
path: root/django/utils/encoding.py
diff options
context:
space:
mode:
Diffstat (limited to 'django/utils/encoding.py')
-rw-r--r--django/utils/encoding.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/django/utils/encoding.py b/django/utils/encoding.py
index 3abee09c52..39d548a9ce 100644
--- a/django/utils/encoding.py
+++ b/django/utils/encoding.py
@@ -226,6 +226,23 @@ def uri_to_iri(uri):
return repercent_broken_unicode(iri).decode('utf-8')
+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 = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
+ # The list of safe characters here is constructed substracting ";", "=",
+ # 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.
+ return quote(force_bytes(path), safe=b"/:@&+$,-_.!~*'()")
+
+
def repercent_broken_unicode(path):
"""
As per section 3.2 of RFC 3987, step three of converting a URI into an IRI,