mirror of
https://github.com/samsonjs/http-cookie.git
synced 2026-04-27 14:57:46 +00:00
Move cookies.txt parser and serializer from HTTP::CookieJar to HTTP::Cookie.
Fix a bug where anything after '#' in cookies.txt are removed.
This commit is contained in:
parent
fdcdae34be
commit
c727d39076
2 changed files with 46 additions and 28 deletions
|
|
@ -15,6 +15,8 @@ class HTTP::Cookie
|
||||||
secure
|
secure
|
||||||
expires created_at accessed_at
|
expires created_at accessed_at
|
||||||
]
|
]
|
||||||
|
True = "TRUE"
|
||||||
|
False = "FALSE"
|
||||||
|
|
||||||
# In Ruby < 1.9.3 URI() does not accept an URI object.
|
# In Ruby < 1.9.3 URI() does not accept an URI object.
|
||||||
if RUBY_VERSION < "1.9.3"
|
if RUBY_VERSION < "1.9.3"
|
||||||
|
|
@ -226,6 +228,35 @@ class HTTP::Cookie
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Parses a line from cookies.txt and returns a cookie object if
|
||||||
|
# the line represents a cookie record or returns nil otherwise.
|
||||||
|
def parse_cookiestxt_line(line)
|
||||||
|
return nil if line.match(/^#/)
|
||||||
|
|
||||||
|
domain,
|
||||||
|
s_for_domain, # Whether this cookie is for domain
|
||||||
|
path, # Path for which the cookie is relevant
|
||||||
|
s_secure, # Requires a secure connection
|
||||||
|
s_expires, # Time the cookie expires (Unix epoch time)
|
||||||
|
name, value = line.split("\t")
|
||||||
|
return nil if value.nil?
|
||||||
|
|
||||||
|
value.chomp!
|
||||||
|
|
||||||
|
if (expires_seconds = s_expires.to_i).nonzero?
|
||||||
|
expires = Time.at(expires_seconds)
|
||||||
|
return nil if expires < Time.now
|
||||||
|
end
|
||||||
|
|
||||||
|
HTTP::Cookie.new(name, value,
|
||||||
|
:domain => domain,
|
||||||
|
:for_domain => s_for_domain == True,
|
||||||
|
:path => path,
|
||||||
|
:secure => s_secure == True,
|
||||||
|
:expires => expires,
|
||||||
|
:version => 0)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Sets the domain attribute. A leading dot in +domain+ implies
|
# Sets the domain attribute. A leading dot in +domain+ implies
|
||||||
|
|
@ -317,6 +348,19 @@ class HTTP::Cookie
|
||||||
end
|
end
|
||||||
include Comparable
|
include Comparable
|
||||||
|
|
||||||
|
# Serializes the cookie into a cookies.txt line.
|
||||||
|
def to_cookiestxt_line(linefeed = "\n")
|
||||||
|
[
|
||||||
|
@domain,
|
||||||
|
@for_domain ? True : False,
|
||||||
|
@path,
|
||||||
|
@secure ? True : False,
|
||||||
|
@expires.to_i.to_s,
|
||||||
|
@name,
|
||||||
|
@value
|
||||||
|
].join("\t") << linefeed
|
||||||
|
end
|
||||||
|
|
||||||
# YAML serialization helper for Syck.
|
# YAML serialization helper for Syck.
|
||||||
def to_yaml_properties
|
def to_yaml_properties
|
||||||
PERSISTENT_PROPERTIES.map { |name| "@#{name}" }
|
PERSISTENT_PROPERTIES.map { |name| "@#{name}" }
|
||||||
|
|
|
||||||
|
|
@ -152,25 +152,7 @@ class HTTP::CookieJar
|
||||||
now = Time.now
|
now = Time.now
|
||||||
|
|
||||||
io.each_line do |line|
|
io.each_line do |line|
|
||||||
line.chomp!
|
c = HTTP::Cookie.parse_cookiestxt_line(line) and add(c)
|
||||||
line.gsub!(/#.+/, '')
|
|
||||||
fields = line.split("\t")
|
|
||||||
|
|
||||||
next if fields.length != 7
|
|
||||||
|
|
||||||
expires_seconds = fields[4].to_i
|
|
||||||
expires = (expires_seconds == 0) ? nil : Time.at(expires_seconds)
|
|
||||||
next if expires and (expires < now)
|
|
||||||
|
|
||||||
c = HTTP::Cookie.new(fields[5], fields[6])
|
|
||||||
c.domain = fields[0]
|
|
||||||
c.for_domain = (fields[1] == "TRUE") # Whether this cookie is for domain
|
|
||||||
c.path = fields[2] # Path for which the cookie is relevant
|
|
||||||
c.secure = (fields[3] == "TRUE") # Requires a secure connection
|
|
||||||
c.expires = expires # Time the cookie expires.
|
|
||||||
c.version = 0 # Conforms to Netscape cookie spec.
|
|
||||||
|
|
||||||
add(c)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@jar
|
@jar
|
||||||
|
|
@ -179,15 +161,7 @@ class HTTP::CookieJar
|
||||||
# Write cookies to Mozilla cookies.txt-style IO stream
|
# Write cookies to Mozilla cookies.txt-style IO stream
|
||||||
def dump_cookiestxt(io)
|
def dump_cookiestxt(io)
|
||||||
to_a.each do |cookie|
|
to_a.each do |cookie|
|
||||||
io.puts([
|
io.print cookie.to_cookiestxt_line
|
||||||
cookie.domain,
|
|
||||||
cookie.for_domain? ? "TRUE" : "FALSE",
|
|
||||||
cookie.path,
|
|
||||||
cookie.secure ? "TRUE" : "FALSE",
|
|
||||||
cookie.expires.to_i.to_s,
|
|
||||||
cookie.name,
|
|
||||||
cookie.value
|
|
||||||
].join("\t"))
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue