mirror of
https://github.com/django/django.git
synced 2025-10-25 22:56:12 +00:00
Fixed #15181 -- Ensure that special characters are escaped when querying for the URL of an uploaded file. Thanks to e.generalov for the report and patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@15409 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -156,6 +156,24 @@ def iri_to_uri(iri):
|
||||
return iri
|
||||
return urllib.quote(smart_str(iri), safe="/#%[]=:;$&()+,!?*@'~")
|
||||
|
||||
def filepath_to_uri(path):
|
||||
"""Convert an file system path to a URI portion that is suitable for
|
||||
inclusion in a URL.
|
||||
|
||||
We are assuming input is either UTF-8 or unicode already.
|
||||
|
||||
This method will encode certain chars that would normally be recognized as
|
||||
special chars for URIs. Note that this method does not encode the '
|
||||
character, as it is a valid character within URIs. See
|
||||
encodeURIComponent() JavaScript function for more details.
|
||||
|
||||
Returns an ASCII string containing the encoded result.
|
||||
"""
|
||||
if path is None:
|
||||
return path
|
||||
# I know about `os.sep` and `os.altsep` but I want to leave
|
||||
# some flexibility for hardcoding separators.
|
||||
return urllib.quote(smart_str(path).replace("\\", "/"), safe="/~!*()'")
|
||||
|
||||
# The encoding of the default system locale but falls back to the
|
||||
# given fallback encoding if the encoding is unsupported by python or could
|
||||
|
||||
Reference in New Issue
Block a user