Sha256: 44ea5962c1c8c9a70faa852c1d73e37cf4236ab0acf5cc8ec645814f04d6d02f

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 KB

Contents

# Patch to allow open-uri to follow safe (http to https) and unsafe redirects (https to http).
# Original gist URL:
# https://gist.github.com/1271420
#
# Relevant issue:
# http://redmine.ruby-lang.org/issues/3719
#
# Source here:
# https://github.com/ruby/ruby/blob/trunk/lib/open-uri.rb

module OpenURI
  class <<self
    alias_method :open_uri_original, :open_uri
    alias_method :redirectable_cautious?, :redirectable?

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

    def redirectable_unsafe?(uri1, uri2)
      !redirectable_safe?(uri1, uri2)
    end
  end

  # The original open_uri takes *args but then doesn't do anything with them.
  # Assume we can only handle a hash.
  def self.open_uri(name, options = {})
    redirectable_unsafe = options.delete :allow_unsafe_redirections
    redirectable_safe   = options.delete :allow_safe_redirections

    if redirectable_unsafe
      class <<self
        remove_method :redirectable?
        alias_method :redirectable?, :redirectable_unsafe?
      end
    elsif redirectable_safe
      class <<self
        remove_method :redirectable?
        alias_method :redirectable?, :redirectable_safe?
      end
    else
      class <<self
        remove_method :redirectable?
        alias_method :redirectable?, :redirectable_cautious?
      end
    end

    self.open_uri_original name, options
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
metainspector-1.12.1 lib/meta_inspector/open_uri.rb
metainspector-1.12.0 lib/meta_inspector/open_uri.rb