_LWPCookieJar.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. """Load / save to libwww-perl (LWP) format files.
  2. Actually, the format is slightly extended from that used by LWP's
  3. (libwww-perl's) HTTP::Cookies, to avoid losing some RFC 2965 information
  4. not recorded by LWP.
  5. It uses the version string "2.0", though really there isn't an LWP Cookies
  6. 2.0 format. This indicates that there is extra information in here
  7. (domain_dot and # port_spec) while still being compatible with
  8. libwww-perl, I hope.
  9. """
  10. import time, re
  11. from cookielib import (_warn_unhandled_exception, FileCookieJar, LoadError,
  12. Cookie, MISSING_FILENAME_TEXT,
  13. join_header_words, split_header_words,
  14. iso2time, time2isoz)
  15. def lwp_cookie_str(cookie):
  16. """Return string representation of Cookie in the LWP cookie file format.
  17. Actually, the format is extended a bit -- see module docstring.
  18. """
  19. h = [(cookie.name, cookie.value),
  20. ("path", cookie.path),
  21. ("domain", cookie.domain)]
  22. if cookie.port is not None: h.append(("port", cookie.port))
  23. if cookie.path_specified: h.append(("path_spec", None))
  24. if cookie.port_specified: h.append(("port_spec", None))
  25. if cookie.domain_initial_dot: h.append(("domain_dot", None))
  26. if cookie.secure: h.append(("secure", None))
  27. if cookie.expires: h.append(("expires",
  28. time2isoz(float(cookie.expires))))
  29. if cookie.discard: h.append(("discard", None))
  30. if cookie.comment: h.append(("comment", cookie.comment))
  31. if cookie.comment_url: h.append(("commenturl", cookie.comment_url))
  32. keys = cookie._rest.keys()
  33. keys.sort()
  34. for k in keys:
  35. h.append((k, str(cookie._rest[k])))
  36. h.append(("version", str(cookie.version)))
  37. return join_header_words([h])
  38. class LWPCookieJar(FileCookieJar):
  39. """
  40. The LWPCookieJar saves a sequence of "Set-Cookie3" lines.
  41. "Set-Cookie3" is the format used by the libwww-perl library, not known
  42. to be compatible with any browser, but which is easy to read and
  43. doesn't lose information about RFC 2965 cookies.
  44. Additional methods
  45. as_lwp_str(ignore_discard=True, ignore_expired=True)
  46. """
  47. def as_lwp_str(self, ignore_discard=True, ignore_expires=True):
  48. """Return cookies as a string of "\\n"-separated "Set-Cookie3" headers.
  49. ignore_discard and ignore_expires: see docstring for FileCookieJar.save
  50. """
  51. now = time.time()
  52. r = []
  53. for cookie in self:
  54. if not ignore_discard and cookie.discard:
  55. continue
  56. if not ignore_expires and cookie.is_expired(now):
  57. continue
  58. r.append("Set-Cookie3: %s" % lwp_cookie_str(cookie))
  59. return "\n".join(r+[""])
  60. def save(self, filename=None, ignore_discard=False, ignore_expires=False):
  61. if filename is None:
  62. if self.filename is not None: filename = self.filename
  63. else: raise ValueError(MISSING_FILENAME_TEXT)
  64. f = open(filename, "w")
  65. try:
  66. # There really isn't an LWP Cookies 2.0 format, but this indicates
  67. # that there is extra information in here (domain_dot and
  68. # port_spec) while still being compatible with libwww-perl, I hope.
  69. f.write("#LWP-Cookies-2.0\n")
  70. f.write(self.as_lwp_str(ignore_discard, ignore_expires))
  71. finally:
  72. f.close()
  73. def _really_load(self, f, filename, ignore_discard, ignore_expires):
  74. magic = f.readline()
  75. if not re.search(self.magic_re, magic):
  76. msg = ("%r does not look like a Set-Cookie3 (LWP) format "
  77. "file" % filename)
  78. raise LoadError(msg)
  79. now = time.time()
  80. header = "Set-Cookie3:"
  81. boolean_attrs = ("port_spec", "path_spec", "domain_dot",
  82. "secure", "discard")
  83. value_attrs = ("version",
  84. "port", "path", "domain",
  85. "expires",
  86. "comment", "commenturl")
  87. try:
  88. while 1:
  89. line = f.readline()
  90. if line == "": break
  91. if not line.startswith(header):
  92. continue
  93. line = line[len(header):].strip()
  94. for data in split_header_words([line]):
  95. name, value = data[0]
  96. standard = {}
  97. rest = {}
  98. for k in boolean_attrs:
  99. standard[k] = False
  100. for k, v in data[1:]:
  101. if k is not None:
  102. lc = k.lower()
  103. else:
  104. lc = None
  105. # don't lose case distinction for unknown fields
  106. if (lc in value_attrs) or (lc in boolean_attrs):
  107. k = lc
  108. if k in boolean_attrs:
  109. if v is None: v = True
  110. standard[k] = v
  111. elif k in value_attrs:
  112. standard[k] = v
  113. else:
  114. rest[k] = v
  115. h = standard.get
  116. expires = h("expires")
  117. discard = h("discard")
  118. if expires is not None:
  119. expires = iso2time(expires)
  120. if expires is None:
  121. discard = True
  122. domain = h("domain")
  123. domain_specified = domain.startswith(".")
  124. c = Cookie(h("version"), name, value,
  125. h("port"), h("port_spec"),
  126. domain, domain_specified, h("domain_dot"),
  127. h("path"), h("path_spec"),
  128. h("secure"),
  129. expires,
  130. discard,
  131. h("comment"),
  132. h("commenturl"),
  133. rest)
  134. if not ignore_discard and c.discard:
  135. continue
  136. if not ignore_expires and c.is_expired(now):
  137. continue
  138. self.set_cookie(c)
  139. except IOError:
  140. raise
  141. except Exception:
  142. _warn_unhandled_exception()
  143. raise LoadError("invalid Set-Cookie3 format file %r: %r" %
  144. (filename, line))