Module: OpenURI

Defined in:
lib/berkshelf/core_ext/openuri.rb

Overview

Patch to allow open-uri to follow safe (http to https) and unsafe redirections (https to http).

Original gist URL: gist.github.com/1271420

Relevant issue: redmine.ruby-lang.org/issues/3719

Source here: github.com/ruby/ruby/blob/trunk/lib/open-uri.rb

Class Method Summary collapse

Class Method Details

.open_uri(name, *rest, &block) ⇒ Object

Patches the original open_uri method to follow all redirects



28
29
30
31
32
33
34
35
# File 'lib/berkshelf/core_ext/openuri.rb', line 28

def self.open_uri(name, *rest, &block)
  class << self
    remove_method :redirectable?
    alias_method  :redirectable?, :redirectable_all?
  end

  self.open_uri_original(name, *rest, &block)
end

.open_uri_originalObject



16
# File 'lib/berkshelf/core_ext/openuri.rb', line 16

alias_method :open_uri_original, :open_uri

.redirectable_all?(uri1, uri2) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/berkshelf/core_ext/openuri.rb', line 22

def redirectable_all?(uri1, uri2)
  redirectable_safe?(uri1, uri2) || (uri1.scheme.downcase == "https" && uri2.scheme.downcase == "http")
end

.redirectable_safe?(uri1, uri2) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/berkshelf/core_ext/openuri.rb', line 18

def redirectable_safe?(uri1, uri2)
  uri1.scheme.downcase == uri2.scheme.downcase || (uri1.scheme.downcase == "http" && uri2.scheme.downcase == "https")
end