Sha256: 8439b2e6fdac23449e31c16f51cc63626efe8b51d7a23b2c5fda590d767bfddc

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

# Patch to allow open-uri to follow safe (http to https) and unsafe redirections (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

1 entries across 1 versions & 1 rubygems

Version Path
open_uri_redirections-0.0.1 lib/open-uri/redirections_patch.rb