PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Editor/Python/Lib/_MozillaCookieJar.py

https://gitlab.com/dahbearz/CRYENGINE
Python | 149 lines | 140 code | 6 blank | 3 comment | 12 complexity | 53a021925cd7801fb4ef37bd52840b73 MD5 | raw file
  1. """Mozilla / Netscape cookie loading / saving."""
  2. import re, time
  3. from cookielib import (_warn_unhandled_exception, FileCookieJar, LoadError,
  4. Cookie, MISSING_FILENAME_TEXT)
  5. class MozillaCookieJar(FileCookieJar):
  6. """
  7. WARNING: you may want to backup your browser's cookies file if you use
  8. this class to save cookies. I *think* it works, but there have been
  9. bugs in the past!
  10. This class differs from CookieJar only in the format it uses to save and
  11. load cookies to and from a file. This class uses the Mozilla/Netscape
  12. `cookies.txt' format. lynx uses this file format, too.
  13. Don't expect cookies saved while the browser is running to be noticed by
  14. the browser (in fact, Mozilla on unix will overwrite your saved cookies if
  15. you change them on disk while it's running; on Windows, you probably can't
  16. save at all while the browser is running).
  17. Note that the Mozilla/Netscape format will downgrade RFC2965 cookies to
  18. Netscape cookies on saving.
  19. In particular, the cookie version and port number information is lost,
  20. together with information about whether or not Path, Port and Discard were
  21. specified by the Set-Cookie2 (or Set-Cookie) header, and whether or not the
  22. domain as set in the HTTP header started with a dot (yes, I'm aware some
  23. domains in Netscape files start with a dot and some don't -- trust me, you
  24. really don't want to know any more about this).
  25. Note that though Mozilla and Netscape use the same format, they use
  26. slightly different headers. The class saves cookies using the Netscape
  27. header by default (Mozilla can cope with that).
  28. """
  29. magic_re = "#( Netscape)? HTTP Cookie File"
  30. header = """\
  31. # Netscape HTTP Cookie File
  32. # http://www.netscape.com/newsref/std/cookie_spec.html
  33. # This is a generated file! Do not edit.
  34. """
  35. def _really_load(self, f, filename, ignore_discard, ignore_expires):
  36. now = time.time()
  37. magic = f.readline()
  38. if not re.search(self.magic_re, magic):
  39. f.close()
  40. raise LoadError(
  41. "%r does not look like a Netscape format cookies file" %
  42. filename)
  43. try:
  44. while 1:
  45. line = f.readline()
  46. if line == "": break
  47. # last field may be absent, so keep any trailing tab
  48. if line.endswith("\n"): line = line[:-1]
  49. # skip comments and blank lines XXX what is $ for?
  50. if (line.strip().startswith(("#", "$")) or
  51. line.strip() == ""):
  52. continue
  53. domain, domain_specified, path, secure, expires, name, value = \
  54. line.split("\t")
  55. secure = (secure == "TRUE")
  56. domain_specified = (domain_specified == "TRUE")
  57. if name == "":
  58. # cookies.txt regards 'Set-Cookie: foo' as a cookie
  59. # with no name, whereas cookielib regards it as a
  60. # cookie with no value.
  61. name = value
  62. value = None
  63. initial_dot = domain.startswith(".")
  64. assert domain_specified == initial_dot
  65. discard = False
  66. if expires == "":
  67. expires = None
  68. discard = True
  69. # assume path_specified is false
  70. c = Cookie(0, name, value,
  71. None, False,
  72. domain, domain_specified, initial_dot,
  73. path, False,
  74. secure,
  75. expires,
  76. discard,
  77. None,
  78. None,
  79. {})
  80. if not ignore_discard and c.discard:
  81. continue
  82. if not ignore_expires and c.is_expired(now):
  83. continue
  84. self.set_cookie(c)
  85. except IOError:
  86. raise
  87. except Exception:
  88. _warn_unhandled_exception()
  89. raise LoadError("invalid Netscape format cookies file %r: %r" %
  90. (filename, line))
  91. def save(self, filename=None, ignore_discard=False, ignore_expires=False):
  92. if filename is None:
  93. if self.filename is not None: filename = self.filename
  94. else: raise ValueError(MISSING_FILENAME_TEXT)
  95. f = open(filename, "w")
  96. try:
  97. f.write(self.header)
  98. now = time.time()
  99. for cookie in self:
  100. if not ignore_discard and cookie.discard:
  101. continue
  102. if not ignore_expires and cookie.is_expired(now):
  103. continue
  104. if cookie.secure: secure = "TRUE"
  105. else: secure = "FALSE"
  106. if cookie.domain.startswith("."): initial_dot = "TRUE"
  107. else: initial_dot = "FALSE"
  108. if cookie.expires is not None:
  109. expires = str(cookie.expires)
  110. else:
  111. expires = ""
  112. if cookie.value is None:
  113. # cookies.txt regards 'Set-Cookie: foo' as a cookie
  114. # with no name, whereas cookielib regards it as a
  115. # cookie with no value.
  116. name = ""
  117. value = cookie.name
  118. else:
  119. name = cookie.name
  120. value = cookie.value
  121. f.write(
  122. "\t".join([cookie.domain, initial_dot, cookie.path,
  123. secure, expires, name, value])+
  124. "\n")
  125. finally:
  126. f.close()