summaryrefslogtreecommitdiff
path: root/django/utils/http.py
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@m4x.org>2013-09-05 14:38:59 -0500
committerClaude Paroz <claude@2xlibre.net>2013-09-10 21:29:31 +0200
commitf9f792eb04dfd48d93682070583f473166b490ae (patch)
treec66c3d474e808d664b3b24b94fdc3d5344295c4f /django/utils/http.py
parent960f5bc75901f76e9b4b356f98b22b494fb47611 (diff)
[1.6.x] Took advantage of django.utils.six.moves.urllib.*.
Backport of 6a6428a36 from master.
Diffstat (limited to 'django/utils/http.py')
-rw-r--r--django/utils/http.py23
1 files changed, 10 insertions, 13 deletions
diff --git a/django/utils/http.py b/django/utils/http.py
index 85af230dd2..571a179830 100644
--- a/django/utils/http.py
+++ b/django/utils/http.py
@@ -5,12 +5,6 @@ import calendar
import datetime
import re
import sys
-try:
- from urllib import parse as urllib_parse
-except ImportError: # Python 2
- import urllib as urllib_parse
- import urlparse
- urllib_parse.urlparse = urlparse.urlparse
from binascii import Error as BinasciiError
from email.utils import formatdate
@@ -19,6 +13,9 @@ from django.utils.datastructures import MultiValueDict
from django.utils.encoding import force_str, force_text
from django.utils.functional import allow_lazy
from django.utils import six
+from django.utils.six.moves.urllib.parse import (
+ quote, quote_plus, unquote, unquote_plus, urlparse,
+ urlencode as original_urlencode)
ETAG_MATCH = re.compile(r'(?:W/)?"((?:\\.|[^"])*)"')
@@ -40,7 +37,7 @@ def urlquote(url, safe='/'):
can safely be used as part of an argument to a subsequent iri_to_uri() call
without double-quoting occurring.
"""
- return force_text(urllib_parse.quote(force_str(url), force_str(safe)))
+ return force_text(quote(force_str(url), force_str(safe)))
urlquote = allow_lazy(urlquote, six.text_type)
def urlquote_plus(url, safe=''):
@@ -50,7 +47,7 @@ def urlquote_plus(url, safe=''):
returned string can safely be used as part of an argument to a subsequent
iri_to_uri() call without double-quoting occurring.
"""
- return force_text(urllib_parse.quote_plus(force_str(url), force_str(safe)))
+ return force_text(quote_plus(force_str(url), force_str(safe)))
urlquote_plus = allow_lazy(urlquote_plus, six.text_type)
def urlunquote(quoted_url):
@@ -58,7 +55,7 @@ def urlunquote(quoted_url):
A wrapper for Python's urllib.unquote() function that can operate on
the result of django.utils.http.urlquote().
"""
- return force_text(urllib_parse.unquote(force_str(quoted_url)))
+ return force_text(unquote(force_str(quoted_url)))
urlunquote = allow_lazy(urlunquote, six.text_type)
def urlunquote_plus(quoted_url):
@@ -66,7 +63,7 @@ def urlunquote_plus(quoted_url):
A wrapper for Python's urllib.unquote_plus() function that can operate on
the result of django.utils.http.urlquote_plus().
"""
- return force_text(urllib_parse.unquote_plus(force_str(quoted_url)))
+ return force_text(unquote_plus(force_str(quoted_url)))
urlunquote_plus = allow_lazy(urlunquote_plus, six.text_type)
def urlencode(query, doseq=0):
@@ -79,7 +76,7 @@ def urlencode(query, doseq=0):
query = query.lists()
elif hasattr(query, 'items'):
query = query.items()
- return urllib_parse.urlencode(
+ return original_urlencode(
[(force_str(k),
[force_str(i) for i in v] if isinstance(v, (list,tuple)) else force_str(v))
for k, v in query],
@@ -244,7 +241,7 @@ def same_origin(url1, url2):
"""
Checks if two URLs are 'same-origin'
"""
- p1, p2 = urllib_parse.urlparse(url1), urllib_parse.urlparse(url2)
+ p1, p2 = urlparse(url1), urlparse(url2)
try:
return (p1.scheme, p1.hostname, p1.port) == (p2.scheme, p2.hostname, p2.port)
except ValueError:
@@ -259,6 +256,6 @@ def is_safe_url(url, host=None):
"""
if not url:
return False
- url_info = urllib_parse.urlparse(url)
+ url_info = urlparse(url)
return (not url_info.netloc or url_info.netloc == host) and \
(not url_info.scheme or url_info.scheme in ['http', 'https'])