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

/django/utils/http.py

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