A copyright violation detector running on Wikimedia Cloud Services https://tools.wmflabs.org/copyvios/
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

43 linhas
1.2 KiB

  1. # -*- coding: utf-8 -*-
  2. import base64
  3. from Cookie import BaseCookie
  4. from datetime import datetime, timedelta
  5. from os import path
  6. class _CookieManager(BaseCookie):
  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 AttributeError:
  12. super(_CookieManager, self).__init__()
  13. def value_decode(self, value):
  14. try:
  15. return base64.b64decode(value).decode("utf8")
  16. except (TypeError, UnicodeDecodeError):
  17. return u"False"
  18. def value_encode(self, value):
  19. return base64.b64encode(value.encode("utf8"))
  20. @property
  21. def path(self):
  22. return self._path
  23. def parse_cookies(context, environ):
  24. return _CookieManager(environ)
  25. def set_cookie(headers, cookies, key, value, days=0):
  26. cookies[key] = value
  27. if days:
  28. expires = datetime.utcnow() + timedelta(days=days)
  29. cookies[key]["expires"] = expires.strftime("%a, %d %b %Y %H:%M:%S GMT")
  30. cookies[key]["path"] = cookies.path
  31. headers.append(("Set-Cookie", cookies[key].OutputString()))
  32. def delete_cookie(headers, cookies, key):
  33. set_cookie(headers, cookies, key, "", days=-1)