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.
 
 
 
 
 

49 lines
1.6 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. def __init__(self, environ):
  8. self._path = path.split(environ["PATH_INFO"])[0]
  9. try:
  10. super(_CookieManager, self).__init__(environ["HTTP_COOKIE"])
  11. except (CookieError, AttributeError):
  12. super(_CookieManager, self).__init__()
  13. for cookie in self.keys():
  14. if self[cookie].value is False:
  15. del self[cookie]
  16. def value_decode(self, value):
  17. unquoted = super(_CookieManager, self).value_decode(value)[0]
  18. try:
  19. return base64.b64decode(unquoted).decode("utf8"), value
  20. except (TypeError, UnicodeDecodeError):
  21. return False, "False"
  22. def value_encode(self, value):
  23. encoded = base64.b64encode(value.encode("utf8"))
  24. quoted = super(_CookieManager, self).value_encode(encoded)[1]
  25. return value, encoded
  26. @property
  27. def path(self):
  28. return self._path
  29. def parse_cookies(context, environ):
  30. return _CookieManager(environ)
  31. def set_cookie(headers, cookies, key, value, days=0):
  32. cookies[key] = value
  33. if days:
  34. expires = datetime.utcnow() + timedelta(days=days)
  35. cookies[key]["expires"] = expires.strftime("%a, %d %b %Y %H:%M:%S GMT")
  36. cookies[key]["path"] = cookies.path
  37. headers.append(("Set-Cookie", cookies[key].OutputString()))
  38. def delete_cookie(headers, cookies, key):
  39. set_cookie(headers, cookies, key, "", days=-1)