mirror of
https://github.com/samsonjs/http-cookie.git
synced 2026-04-27 14:57:46 +00:00
YAMLServer#load: Add backward compatibility for reading YAML in the old format.
This commit is contained in:
parent
073d83b40c
commit
e72b0fdbc5
1 changed files with 31 additions and 8 deletions
|
|
@ -11,19 +11,42 @@ class HTTP::CookieJar::YAMLSaver < HTTP::CookieJar::AbstractSaver
|
||||||
def load(io, jar)
|
def load(io, jar)
|
||||||
begin
|
begin
|
||||||
data = YAML.load(io)
|
data = YAML.load(io)
|
||||||
|
rescue ArgumentError => e
|
||||||
|
case e.message
|
||||||
|
when %r{\Aundefined class/module Mechanize::}
|
||||||
|
# backward compatibility with Mechanize::Cookie
|
||||||
|
begin
|
||||||
|
io.rewind # hopefully
|
||||||
|
yaml = io.read
|
||||||
|
# a gross hack
|
||||||
|
yaml.gsub!(%r{^( [^ ].*:) !ruby/object:Mechanize::Cookie$}, "\\1")
|
||||||
|
data = YAML.load(yaml)
|
||||||
|
rescue Errno::ESPIPE
|
||||||
|
@logger.warn "could not rewind the stream for conversion" if @logger
|
||||||
rescue ArgumentError
|
rescue ArgumentError
|
||||||
@logger.warn "unloadable YAML cookie data discarded" if @logger
|
end
|
||||||
return
|
end
|
||||||
end
|
|
||||||
|
|
||||||
unless data.instance_of?(Array)
|
|
||||||
@logger.warn "incompatible YAML cookie data discarded" if @logger
|
|
||||||
return
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
case data
|
||||||
|
when Array
|
||||||
data.each { |cookie|
|
data.each { |cookie|
|
||||||
jar.add(cookie)
|
jar.add(cookie)
|
||||||
}
|
}
|
||||||
|
when Hash
|
||||||
|
# backward compatibility with Mechanize::Cookie
|
||||||
|
data.each { |domain, paths|
|
||||||
|
paths.each { |path, names|
|
||||||
|
names.each { |cookie_name, cookie_hash|
|
||||||
|
cookie = HTTP::Cookie.new(cookie_hash)
|
||||||
|
jar.add(cookie)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
@logger.warn "incompatible YAML cookie data discarded" if @logger
|
||||||
|
return
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue