[ruby/open-uri] Set default for max_redirects and add exception class
https://github.com/ruby/open-uri/commit/dcdcb885cc
This commit is contained in:
parent
d97479f9c9
commit
41c00bc97e
@ -108,7 +108,7 @@ module OpenURI
|
|||||||
:ftp_active_mode => false,
|
:ftp_active_mode => false,
|
||||||
:redirect => true,
|
:redirect => true,
|
||||||
:encoding => nil,
|
:encoding => nil,
|
||||||
:max_redirects => nil,
|
:max_redirects => 64,
|
||||||
}
|
}
|
||||||
|
|
||||||
def OpenURI.check_options(options) # :nodoc:
|
def OpenURI.check_options(options) # :nodoc:
|
||||||
@ -240,7 +240,7 @@ module OpenURI
|
|||||||
uri = redirect
|
uri = redirect
|
||||||
raise "HTTP redirection loop: #{uri}" if uri_set.include? uri.to_s
|
raise "HTTP redirection loop: #{uri}" if uri_set.include? uri.to_s
|
||||||
uri_set[uri.to_s] = true
|
uri_set[uri.to_s] = true
|
||||||
raise "Too many redirects" if max_redirects && uri_set.size > max_redirects
|
raise TooManyRedirects.new("Too many redirects", buf.io) if max_redirects && uri_set.size > max_redirects
|
||||||
else
|
else
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
@ -395,6 +395,9 @@ module OpenURI
|
|||||||
attr_reader :uri
|
attr_reader :uri
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class TooManyRedirects < HTTPError
|
||||||
|
end
|
||||||
|
|
||||||
class Buffer # :nodoc: all
|
class Buffer # :nodoc: all
|
||||||
def initialize
|
def initialize
|
||||||
@io = StringIO.new
|
@io = StringIO.new
|
||||||
|
@ -572,7 +572,7 @@ class TestOpenURI < Test::Unit::TestCase
|
|||||||
srv.mount_proc("/r1/") {|req, res| res.status = 301; res["location"] = "#{url}/r2"; res.body = "r1" }
|
srv.mount_proc("/r1/") {|req, res| res.status = 301; res["location"] = "#{url}/r2"; res.body = "r1" }
|
||||||
srv.mount_proc("/r2/") {|req, res| res.status = 301; res["location"] = "#{url}/r3"; res.body = "r2" }
|
srv.mount_proc("/r2/") {|req, res| res.status = 301; res["location"] = "#{url}/r3"; res.body = "r2" }
|
||||||
srv.mount_proc("/r3/") {|req, res| res.body = "r3" }
|
srv.mount_proc("/r3/") {|req, res| res.body = "r3" }
|
||||||
exc = assert_raise(RuntimeError) { URI.open("#{url}/r1/", max_redirects: 1) {} }
|
exc = assert_raise(OpenURI::TooManyRedirects) { URI.open("#{url}/r1/", max_redirects: 1) {} }
|
||||||
assert_equal("Too many redirects", exc.message)
|
assert_equal("Too many redirects", exc.message)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user