_LWPCookieJar.py 6.4 KB

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