PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/django/utils/http.py

https://gitlab.com/mayakarya/django
Python | 385 lines | 342 code | 10 blank | 33 comment | 2 complexity | e06529359235982b8066a81def19857b MD5 | raw file
  1. from __future__ import unicode_literals
  2. import base64
  3. import calendar
  4. import datetime
  5. import re
  6. import sys
  7. import unicodedata
  8. from binascii import Error as BinasciiError
  9. from email.utils import formatdate
  10. from django.core.exceptions import TooManyFieldsSent
  11. from django.utils import six
  12. from django.utils.datastructures import MultiValueDict
  13. from django.utils.encoding import force_bytes, force_str, force_text
  14. from django.utils.functional import keep_lazy_text
  15. from django.utils.six.moves.urllib.parse import (
  16. quote, quote_plus, unquote, unquote_plus, urlencode as original_urlencode,
  17. urlparse,
  18. )
  19. ETAG_MATCH = re.compile(r'(?:W/)?"((?:\\.|[^"])*)"')
  20. MONTHS = 'jan feb mar apr may jun jul aug sep oct nov dec'.split()
  21. __D = r'(?P<day>\d{2})'
  22. __D2 = r'(?P<day>[ \d]\d)'
  23. __M = r'(?P<mon>\w{3})'
  24. __Y = r'(?P<year>\d{4})'
  25. __Y2 = r'(?P<year>\d{2})'
  26. __T = r'(?P<hour>\d{2}):(?P<min>\d{2}):(?P<sec>\d{2})'
  27. RFC1123_DATE = re.compile(r'^\w{3}, %s %s %s %s GMT$' % (__D, __M, __Y, __T))
  28. RFC850_DATE = re.compile(r'^\w{6,9}, %s-%s-%s %s GMT$' % (__D, __M, __Y2, __T))
  29. ASCTIME_DATE = re.compile(r'^\w{3} %s %s %s %s$' % (__M, __D2, __T, __Y))
  30. RFC3986_GENDELIMS = str(":/?#[]@")
  31. RFC3986_SUBDELIMS = str("!$&'()*+,;=")
  32. FIELDS_MATCH = re.compile('[&;]')
  33. @keep_lazy_text
  34. def urlquote(url, safe='/'):
  35. """
  36. A version of Python's urllib.quote() function that can operate on unicode
  37. strings. The url is first UTF-8 encoded before quoting. The returned string
  38. can safely be used as part of an argument to a subsequent iri_to_uri() call
  39. without double-quoting occurring.
  40. """
  41. return force_text(quote(force_str(url), force_str(safe)))
  42. @keep_lazy_text
  43. def urlquote_plus(url, safe=''):
  44. """
  45. A version of Python's urllib.quote_plus() function that can operate on
  46. unicode strings. The url is first UTF-8 encoded before quoting. The
  47. returned string can safely be used as part of an argument to a subsequent
  48. iri_to_uri() call without double-quoting occurring.
  49. """
  50. return force_text(quote_plus(force_str(url), force_str(safe)))
  51. @keep_lazy_text
  52. def urlunquote(quoted_url):
  53. """
  54. A wrapper for Python's urllib.unquote() function that can operate on
  55. the result of django.utils.http.urlquote().
  56. """
  57. return force_text(unquote(force_str(quoted_url)))
  58. @keep_lazy_text
  59. def urlunquote_plus(quoted_url):
  60. """
  61. A wrapper for Python's urllib.unquote_plus() function that can operate on
  62. the result of django.utils.http.urlquote_plus().
  63. """
  64. return force_text(unquote_plus(force_str(quoted_url)))
  65. def urlencode(query, doseq=0):
  66. """
  67. A version of Python's urllib.urlencode() function that can operate on
  68. unicode strings. The parameters are first cast to UTF-8 encoded strings and
  69. then encoded as per normal.
  70. """
  71. if isinstance(query, MultiValueDict):
  72. query = query.lists()
  73. elif hasattr(query, 'items'):
  74. query = query.items()
  75. return original_urlencode(
  76. [(force_str(k),
  77. [force_str(i) for i in v] if isinstance(v, (list, tuple)) else force_str(v))
  78. for k, v in query],
  79. doseq)
  80. def cookie_date(epoch_seconds=None):
  81. """
  82. Formats the time to ensure compatibility with Netscape's cookie standard.
  83. Accepts a floating point number expressed in seconds since the epoch, in
  84. UTC - such as that outputted by time.time(). If set to None, defaults to
  85. the current time.
  86. Outputs a string in the format 'Wdy, DD-Mon-YYYY HH:MM:SS GMT'.
  87. """
  88. rfcdate = formatdate(epoch_seconds)
  89. return '%s-%s-%s GMT' % (rfcdate[:7], rfcdate[8:11], rfcdate[12:25])
  90. def http_date(epoch_seconds=None):
  91. """
  92. Formats the time to match the RFC1123 date format as specified by HTTP
  93. RFC7231 section 7.1.1.1.
  94. Accepts a floating point number expressed in seconds since the epoch, in
  95. UTC - such as that outputted by time.time(). If set to None, defaults to
  96. the current time.
  97. Outputs a string in the format 'Wdy, DD Mon YYYY HH:MM:SS GMT'.
  98. """
  99. return formatdate(epoch_seconds, usegmt=True)
  100. def parse_http_date(date):
  101. """
  102. Parses a date format as specified by HTTP RFC7231 section 7.1.1.1.
  103. The three formats allowed by the RFC are accepted, even if only the first
  104. one is still in widespread use.
  105. Returns an integer expressed in seconds since the epoch, in UTC.
  106. """
  107. # emails.Util.parsedate does the job for RFC1123 dates; unfortunately
  108. # RFC7231 makes it mandatory to support RFC850 dates too. So we roll
  109. # our own RFC-compliant parsing.
  110. for regex in RFC1123_DATE, RFC850_DATE, ASCTIME_DATE:
  111. m = regex.match(date)
  112. if m is not None:
  113. break
  114. else:
  115. raise ValueError("%r is not in a valid HTTP date format" % date)
  116. try:
  117. year = int(m.group('year'))
  118. if year < 100:
  119. if year < 70:
  120. year += 2000
  121. else:
  122. year += 1900
  123. month = MONTHS.index(m.group('mon').lower()) + 1
  124. day = int(m.group('day'))
  125. hour = int(m.group('hour'))
  126. min = int(m.group('min'))
  127. sec = int(m.group('sec'))
  128. result = datetime.datetime(year, month, day, hour, min, sec)
  129. return calendar.timegm(result.utctimetuple())
  130. except Exception:
  131. six.reraise(ValueError, ValueError("%r is not a valid date" % date), sys.exc_info()[2])
  132. def parse_http_date_safe(date):
  133. """
  134. Same as parse_http_date, but returns None if the input is invalid.
  135. """
  136. try:
  137. return parse_http_date(date)
  138. except Exception:
  139. pass
  140. # Base 36 functions: useful for generating compact URLs
  141. def base36_to_int(s):
  142. """
  143. Converts a base 36 string to an ``int``. Raises ``ValueError` if the
  144. input won't fit into an int.
  145. """
  146. # To prevent overconsumption of server resources, reject any
  147. # base36 string that is long than 13 base36 digits (13 digits
  148. # is sufficient to base36-encode any 64-bit integer)
  149. if len(s) > 13:
  150. raise ValueError("Base36 input too large")
  151. value = int(s, 36)
  152. # ... then do a final check that the value will fit into an int to avoid
  153. # returning a long (#15067). The long type was removed in Python 3.
  154. if six.PY2 and value > sys.maxint:
  155. raise ValueError("Base36 input too large")
  156. return value
  157. def int_to_base36(i):
  158. """
  159. Converts an integer to a base36 string
  160. """
  161. char_set = '0123456789abcdefghijklmnopqrstuvwxyz'
  162. if i < 0:
  163. raise ValueError("Negative base36 conversion input.")
  164. if six.PY2:
  165. if not isinstance(i, six.integer_types):
  166. raise TypeError("Non-integer base36 conversion input.")
  167. if i > sys.maxint:
  168. raise ValueError("Base36 conversion input too large.")
  169. if i < 36:
  170. return char_set[i]
  171. b36 = ''
  172. while i != 0:
  173. i, n = divmod(i, 36)
  174. b36 = char_set[n] + b36
  175. return b36
  176. def urlsafe_base64_encode(s):
  177. """
  178. Encodes a bytestring in base64 for use in URLs, stripping any trailing
  179. equal signs.
  180. """
  181. return base64.urlsafe_b64encode(s).rstrip(b'\n=')
  182. def urlsafe_base64_decode(s):
  183. """
  184. Decodes a base64 encoded string, adding back any trailing equal signs that
  185. might have been stripped.
  186. """
  187. s = force_bytes(s)
  188. try:
  189. return base64.urlsafe_b64decode(s.ljust(len(s) + len(s) % 4, b'='))
  190. except (LookupError, BinasciiError) as e:
  191. raise ValueError(e)
  192. def parse_etags(etag_str):
  193. """
  194. Parses a string with one or several etags passed in If-None-Match and
  195. If-Match headers by the rules in RFC 2616. Returns a list of etags
  196. without surrounding double quotes (") and unescaped from \<CHAR>.
  197. """
  198. etags = ETAG_MATCH.findall(etag_str)
  199. if not etags:
  200. # etag_str has wrong format, treat it as an opaque string then
  201. return [etag_str]
  202. etags = [e.encode('ascii').decode('unicode_escape') for e in etags]
  203. return etags
  204. def quote_etag(etag):
  205. """
  206. Wraps a string in double quotes escaping contents as necessary.
  207. """
  208. return '"%s"' % etag.replace('\\', '\\\\').replace('"', '\\"')
  209. def unquote_etag(etag):
  210. """
  211. Unquote an ETag string; i.e. revert quote_etag().
  212. """
  213. return etag.strip('"').replace('\\"', '"').replace('\\\\', '\\') if etag else etag
  214. def is_same_domain(host, pattern):
  215. """
  216. Return ``True`` if the host is either an exact match or a match
  217. to the wildcard pattern.
  218. Any pattern beginning with a period matches a domain and all of its
  219. subdomains. (e.g. ``.example.com`` matches ``example.com`` and
  220. ``foo.example.com``). Anything else is an exact string match.
  221. """
  222. if not pattern:
  223. return False
  224. pattern = pattern.lower()
  225. return (
  226. pattern[0] == '.' and (host.endswith(pattern) or host == pattern[1:]) or
  227. pattern == host
  228. )
  229. def is_safe_url(url, host=None, require_https=False):
  230. """
  231. Return ``True`` if the url is a safe redirection (i.e. it doesn't point to
  232. a different host and uses a safe scheme).
  233. Always returns ``False`` on an empty url.
  234. If ``require_https`` is ``True``, only 'https' will be considered a valid
  235. scheme, as opposed to 'http' and 'https' with the default, ``False``.
  236. """
  237. if url is not None:
  238. url = url.strip()
  239. if not url:
  240. return False
  241. if six.PY2:
  242. try:
  243. url = force_text(url)
  244. except UnicodeDecodeError:
  245. return False
  246. # Chrome treats \ completely as / in paths but it could be part of some
  247. # basic auth credentials so we need to check both URLs.
  248. return (_is_safe_url(url, host, require_https=require_https) and
  249. _is_safe_url(url.replace('\\', '/'), host, require_https=require_https))
  250. def _is_safe_url(url, host, require_https=False):
  251. # Chrome considers any URL with more than two slashes to be absolute, but
  252. # urlparse is not so flexible. Treat any url with three slashes as unsafe.
  253. if url.startswith('///'):
  254. return False
  255. url_info = urlparse(url)
  256. # Forbid URLs like http:///example.com - with a scheme, but without a hostname.
  257. # In that URL, example.com is not the hostname but, a path component. However,
  258. # Chrome will still consider example.com to be the hostname, so we must not
  259. # allow this syntax.
  260. if not url_info.netloc and url_info.scheme:
  261. return False
  262. # Forbid URLs that start with control characters. Some browsers (like
  263. # Chrome) ignore quite a few control characters at the start of a
  264. # URL and might consider the URL as scheme relative.
  265. if unicodedata.category(url[0])[0] == 'C':
  266. return False
  267. scheme = url_info.scheme
  268. # Consider URLs without a scheme (e.g. //example.com/p) to be http.
  269. if not url_info.scheme and url_info.netloc:
  270. scheme = 'http'
  271. valid_schemes = ['https'] if require_https else ['http', 'https']
  272. return ((not url_info.netloc or url_info.netloc == host) and
  273. (not scheme or scheme in valid_schemes))
  274. def limited_parse_qsl(qs, keep_blank_values=False, encoding='utf-8',
  275. errors='replace', fields_limit=None):
  276. """
  277. Return a list of key/value tuples parsed from query string.
  278. Copied from urlparse with an additional "fields_limit" argument.
  279. Copyright (C) 2013 Python Software Foundation (see LICENSE.python).
  280. Arguments:
  281. qs: percent-encoded query string to be parsed
  282. keep_blank_values: flag indicating whether blank values in
  283. percent-encoded queries should be treated as blank strings. A
  284. true value indicates that blanks should be retained as blank
  285. strings. The default false value indicates that blank values
  286. are to be ignored and treated as if they were not included.
  287. encoding and errors: specify how to decode percent-encoded sequences
  288. into Unicode characters, as accepted by the bytes.decode() method.
  289. fields_limit: maximum number of fields parsed or an exception
  290. is raised. None means no limit and is the default.
  291. """
  292. if fields_limit:
  293. pairs = FIELDS_MATCH.split(qs, fields_limit)
  294. if len(pairs) > fields_limit:
  295. raise TooManyFieldsSent(
  296. 'The number of GET/POST parameters exceeded '
  297. 'settings.DATA_UPLOAD_MAX_NUMBER_FIELDS.'
  298. )
  299. else:
  300. pairs = FIELDS_MATCH.split(qs)
  301. r = []
  302. for name_value in pairs:
  303. if not name_value:
  304. continue
  305. nv = name_value.split(str('='), 1)
  306. if len(nv) != 2:
  307. # Handle case of a control-name with no equal sign
  308. if keep_blank_values:
  309. nv.append('')
  310. else:
  311. continue
  312. if len(nv[1]) or keep_blank_values:
  313. if six.PY3:
  314. name = nv[0].replace('+', ' ')
  315. name = unquote(name, encoding=encoding, errors=errors)
  316. value = nv[1].replace('+', ' ')
  317. value = unquote(value, encoding=encoding, errors=errors)
  318. else:
  319. name = unquote(nv[0].replace(b'+', b' '))
  320. value = unquote(nv[1].replace(b'+', b' '))
  321. r.append((name, value))
  322. return r