http.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. from __future__ import unicode_literals
  2. import base64
  3. import calendar
  4. import datetime
  5. import re
  6. import sys
  7. from binascii import Error as BinasciiError
  8. from email.utils import formatdate
  9. from django.utils.datastructures import MultiValueDict
  10. from django.utils.encoding import force_str, force_text
  11. from django.utils.functional import allow_lazy
  12. from django.utils import six
  13. from django.utils.six.moves.urllib.parse import (
  14. quote, quote_plus, unquote, unquote_plus, urlparse,
  15. urlencode as original_urlencode)
  16. ETAG_MATCH = re.compile(r'(?:W/)?"((?:\\.|[^"])*)"')
  17. MONTHS = 'jan feb mar apr may jun jul aug sep oct nov dec'.split()
  18. __D = r'(?P<day>\d{2})'
  19. __D2 = r'(?P<day>[ \d]\d)'
  20. __M = r'(?P<mon>\w{3})'
  21. __Y = r'(?P<year>\d{4})'
  22. __Y2 = r'(?P<year>\d{2})'
  23. __T = r'(?P<hour>\d{2}):(?P<min>\d{2}):(?P<sec>\d{2})'
  24. RFC1123_DATE = re.compile(r'^\w{3}, %s %s %s %s GMT$' % (__D, __M, __Y, __T))
  25. RFC850_DATE = re.compile(r'^\w{6,9}, %s-%s-%s %s GMT$' % (__D, __M, __Y2, __T))
  26. ASCTIME_DATE = re.compile(r'^\w{3} %s %s %s %s$' % (__M, __D2, __T, __Y))
  27. def urlquote(url, safe='/'):
  28. """
  29. A version of Python's urllib.quote() function that can operate on unicode
  30. strings. The url is first UTF-8 encoded before quoting. The returned string
  31. can safely be used as part of an argument to a subsequent iri_to_uri() call
  32. without double-quoting occurring.
  33. """
  34. return force_text(quote(force_str(url), force_str(safe)))
  35. urlquote = allow_lazy(urlquote, six.text_type)
  36. def urlquote_plus(url, safe=''):
  37. """
  38. A version of Python's urllib.quote_plus() function that can operate on
  39. unicode strings. The url is first UTF-8 encoded before quoting. The
  40. returned string can safely be used as part of an argument to a subsequent
  41. iri_to_uri() call without double-quoting occurring.
  42. """
  43. return force_text(quote_plus(force_str(url), force_str(safe)))
  44. urlquote_plus = allow_lazy(urlquote_plus, six.text_type)
  45. def urlunquote(quoted_url):
  46. """
  47. A wrapper for Python's urllib.unquote() function that can operate on
  48. the result of django.utils.http.urlquote().
  49. """
  50. return force_text(unquote(force_str(quoted_url)))
  51. urlunquote = allow_lazy(urlunquote, six.text_type)
  52. def urlunquote_plus(quoted_url):
  53. """
  54. A wrapper for Python's urllib.unquote_plus() function that can operate on
  55. the result of django.utils.http.urlquote_plus().
  56. """
  57. return force_text(unquote_plus(force_str(quoted_url)))
  58. urlunquote_plus = allow_lazy(urlunquote_plus, six.text_type)
  59. def urlencode(query, doseq=0):
  60. """
  61. A version of Python's urllib.urlencode() function that can operate on
  62. unicode strings. The parameters are first cast to UTF-8 encoded strings and
  63. then encoded as per normal.
  64. """
  65. if isinstance(query, MultiValueDict):
  66. query = query.lists()
  67. elif hasattr(query, 'items'):
  68. query = query.items()
  69. return original_urlencode(
  70. [(force_str(k),
  71. [force_str(i) for i in v] if isinstance(v, (list, tuple)) else force_str(v))
  72. for k, v in query],
  73. doseq)
  74. def cookie_date(epoch_seconds=None):
  75. """
  76. Formats the time to ensure compatibility with Netscape's cookie standard.
  77. Accepts a floating point number expressed in seconds since the epoch, in
  78. UTC - such as that outputted by time.time(). If set to None, defaults to
  79. the current time.
  80. Outputs a string in the format 'Wdy, DD-Mon-YYYY HH:MM:SS GMT'.
  81. """
  82. rfcdate = formatdate(epoch_seconds)
  83. return '%s-%s-%s GMT' % (rfcdate[:7], rfcdate[8:11], rfcdate[12:25])
  84. def http_date(epoch_seconds=None):
  85. """
  86. Formats the time to match the RFC1123 date format as specified by HTTP
  87. RFC2616 section 3.3.1.
  88. Accepts a floating point number expressed in seconds since the epoch, in
  89. UTC - such as that outputted by time.time(). If set to None, defaults to
  90. the current time.
  91. Outputs a string in the format 'Wdy, DD Mon YYYY HH:MM:SS GMT'.
  92. """
  93. return formatdate(epoch_seconds, usegmt=True)
  94. def parse_http_date(date):
  95. """
  96. Parses a date format as specified by HTTP RFC2616 section 3.3.1.
  97. The three formats allowed by the RFC are accepted, even if only the first
  98. one is still in widespread use.
  99. Returns an integer expressed in seconds since the epoch, in UTC.
  100. """
  101. # emails.Util.parsedate does the job for RFC1123 dates; unfortunately
  102. # RFC2616 makes it mandatory to support RFC850 dates too. So we roll
  103. # our own RFC-compliant parsing.
  104. for regex in RFC1123_DATE, RFC850_DATE, ASCTIME_DATE:
  105. m = regex.match(date)
  106. if m is not None:
  107. break
  108. else:
  109. raise ValueError("%r is not in a valid HTTP date format" % date)
  110. try:
  111. year = int(m.group('year'))
  112. if year < 100:
  113. if year < 70:
  114. year += 2000
  115. else:
  116. year += 1900
  117. month = MONTHS.index(m.group('mon').lower()) + 1
  118. day = int(m.group('day'))
  119. hour = int(m.group('hour'))
  120. min = int(m.group('min'))
  121. sec = int(m.group('sec'))
  122. result = datetime.datetime(year, month, day, hour, min, sec)
  123. return calendar.timegm(result.utctimetuple())
  124. except Exception:
  125. six.reraise(ValueError, ValueError("%r is not a valid date" % date), sys.exc_info()[2])
  126. def parse_http_date_safe(date):
  127. """
  128. Same as parse_http_date, but returns None if the input is invalid.
  129. """
  130. try:
  131. return parse_http_date(date)
  132. except Exception:
  133. pass
  134. # Base 36 functions: useful for generating compact URLs
  135. def base36_to_int(s):
  136. """
  137. Converts a base 36 string to an ``int``. Raises ``ValueError` if the
  138. input won't fit into an int.
  139. """
  140. # To prevent overconsumption of server resources, reject any
  141. # base36 string that is long than 13 base36 digits (13 digits
  142. # is sufficient to base36-encode any 64-bit integer)
  143. if len(s) > 13:
  144. raise ValueError("Base36 input too large")
  145. value = int(s, 36)
  146. # ... then do a final check that the value will fit into an int to avoid
  147. # returning a long (#15067). The long type was removed in Python 3.
  148. if six.PY2 and value > sys.maxint:
  149. raise ValueError("Base36 input too large")
  150. return value
  151. def int_to_base36(i):
  152. """
  153. Converts an integer to a base36 string
  154. """
  155. digits = "0123456789abcdefghijklmnopqrstuvwxyz"
  156. factor = 0
  157. if i < 0:
  158. raise ValueError("Negative base36 conversion input.")
  159. if six.PY2:
  160. if not isinstance(i, six.integer_types):
  161. raise TypeError("Non-integer base36 conversion input.")
  162. if i > sys.maxint:
  163. raise ValueError("Base36 conversion input too large.")
  164. # Find starting factor
  165. while True:
  166. factor += 1
  167. if i < 36 ** factor:
  168. factor -= 1
  169. break
  170. base36 = []
  171. # Construct base36 representation
  172. while factor >= 0:
  173. j = 36 ** factor
  174. base36.append(digits[i // j])
  175. i = i % j
  176. factor -= 1
  177. return ''.join(base36)
  178. def urlsafe_base64_encode(s):
  179. """
  180. Encodes a bytestring in base64 for use in URLs, stripping any trailing
  181. equal signs.
  182. """
  183. return base64.urlsafe_b64encode(s).rstrip(b'\n=')
  184. def urlsafe_base64_decode(s):
  185. """
  186. Decodes a base64 encoded string, adding back any trailing equal signs that
  187. might have been stripped.
  188. """
  189. s = s.encode('utf-8') # base64encode should only return ASCII.
  190. try:
  191. return base64.urlsafe_b64decode(s.ljust(len(s) + len(s) % 4, b'='))
  192. except (LookupError, BinasciiError) as e:
  193. raise ValueError(e)
  194. def parse_etags(etag_str):
  195. """
  196. Parses a string with one or several etags passed in If-None-Match and
  197. If-Match headers by the rules in RFC 2616. Returns a list of etags
  198. without surrounding double quotes (") and unescaped from \<CHAR>.
  199. """
  200. etags = ETAG_MATCH.findall(etag_str)
  201. if not etags:
  202. # etag_str has wrong format, treat it as an opaque string then
  203. return [etag_str]
  204. etags = [e.encode('ascii').decode('unicode_escape') for e in etags]
  205. return etags
  206. def quote_etag(etag):
  207. """
  208. Wraps a string in double quotes escaping contents as necessary.
  209. """
  210. return '"%s"' % etag.replace('\\', '\\\\').replace('"', '\\"')
  211. def same_origin(url1, url2):
  212. """
  213. Checks if two URLs are 'same-origin'
  214. """
  215. p1, p2 = urlparse(url1), urlparse(url2)
  216. try:
  217. return (p1.scheme, p1.hostname, p1.port) == (p2.scheme, p2.hostname, p2.port)
  218. except ValueError:
  219. return False
  220. def is_safe_url(url, host=None):
  221. """
  222. Return ``True`` if the url is a safe redirection (i.e. it doesn't point to
  223. a different host and uses a safe scheme).
  224. Always returns ``False`` on an empty url.
  225. """
  226. if not url:
  227. return False
  228. # Chrome treats \ completely as /
  229. url = url.replace('\\', '/')
  230. # Chrome considers any URL with more than two slashes to be absolute, but
  231. # urlparse is not so flexible. Treat any url with three slashes as unsafe.
  232. if url.startswith('///'):
  233. return False
  234. url_info = urlparse(url)
  235. # Forbid URLs like http:///example.com - with a scheme, but without a hostname.
  236. # In that URL, example.com is not the hostname but, a path component. However,
  237. # Chrome will still consider example.com to be the hostname, so we must not
  238. # allow this syntax.
  239. if not url_info.netloc and url_info.scheme:
  240. return False
  241. return ((not url_info.netloc or url_info.netloc == host) and
  242. (not url_info.scheme or url_info.scheme in ['http', 'https']))