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.
 
 
 
 
 

46 lines
1.5 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. def value_decode(self, value):
  14. unquoted = super(_CookieManager, self).value_decode(value)[0]
  15. try:
  16. return base64.b64decode(unquoted).decode("utf8"), value
  17. except (TypeError, UnicodeDecodeError):
  18. return u"False", "False"
  19. def value_encode(self, value):
  20. encoded = base64.b64encode(value.encode("utf8"))
  21. quoted = super(_CookieManager, self).value_encode(encoded)[1]
  22. return value, encoded
  23. @property
  24. def path(self):
  25. return self._path
  26. def parse_cookies(context, environ):
  27. return _CookieManager(environ)
  28. def set_cookie(headers, cookies, key, value, days=0):
  29. cookies[key] = value
  30. if days:
  31. expires = datetime.utcnow() + timedelta(days=days)
  32. cookies[key]["expires"] = expires.strftime("%a, %d %b %Y %H:%M:%S GMT")
  33. cookies[key]["path"] = cookies.path
  34. headers.append(("Set-Cookie", cookies[key].OutputString()))
  35. def delete_cookie(headers, cookies, key):
  36. set_cookie(headers, cookies, key, "", days=-1)