A copyright violation detector running on Wikimedia Cloud Services https://tools.wmflabs.org/copyvios/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

55 lines
1.7 KiB

  1. # -*- coding: utf-8 -*-
  2. import base64
  3. from Cookie import CookieError, SimpleCookie
  4. from datetime import datetime, timedelta
  5. from os import path
  6. class _CookieManager(SimpleCookie):
  7. MAGIC = "--ets1"
  8. def __init__(self, environ):
  9. self._path = path.split(environ["PATH_INFO"])[0]
  10. try:
  11. super(_CookieManager, self).__init__(environ["HTTP_COOKIE"])
  12. except (CookieError, KeyError):
  13. super(_CookieManager, self).__init__()
  14. for cookie in self.keys():
  15. if self[cookie].value is False:
  16. del self[cookie]
  17. def value_decode(self, value):
  18. unquoted = super(_CookieManager, self).value_decode(value)[0]
  19. try:
  20. decoded = base64.b64decode(unquoted).decode("utf8")
  21. except (TypeError, UnicodeDecodeError):
  22. return False, "False"
  23. if decoded.startswith(self.MAGIC):
  24. return decoded[len(self.MAGIC):], value
  25. return False, "False"
  26. def value_encode(self, value):
  27. encoded = base64.b64encode(self.MAGIC + value.encode("utf8"))
  28. quoted = super(_CookieManager, self).value_encode(encoded)[1]
  29. return value, quoted
  30. @property
  31. def path(self):
  32. return self._path
  33. def parse_cookies(environ):
  34. return _CookieManager(environ)
  35. def set_cookie(headers, cookies, key, value, days=0):
  36. cookies[key] = value
  37. if days:
  38. expires = datetime.utcnow() + timedelta(days=days)
  39. cookies[key]["expires"] = expires.strftime("%a, %d %b %Y %H:%M:%S GMT")
  40. cookies[key]["path"] = cookies.path
  41. headers.append(("Set-Cookie", cookies[key].OutputString()))
  42. def delete_cookie(headers, cookies, key):
  43. set_cookie(headers, cookies, key, u"", days=-1)
  44. del cookies[key]