From 1b44924cb0ae40509a9e224277860d89078f8bca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Tue, 18 Aug 2015 14:56:36 +0200 Subject: [PATCH] replace 'open_uri_redirections' gem with a single freedom_patches file --- Gemfile | 2 - Gemfile.lock | 2 - lib/freedom_patches/open_uri_redirections.rb | 98 ++++++++++++++++++++ 3 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 lib/freedom_patches/open_uri_redirections.rb diff --git a/Gemfile b/Gemfile index c6f88f8b81b..98cdfdd7900 100644 --- a/Gemfile +++ b/Gemfile @@ -53,8 +53,6 @@ gem 'fast_xs' gem 'fast_xor' -gem 'open_uri_redirections' - # while we sort out https://github.com/sdsykes/fastimage/pull/46 gem 'fastimage_discourse', require: 'fastimage' gem 'aws-sdk', require: false diff --git a/Gemfile.lock b/Gemfile.lock index fd507523c8d..5ce6b8971a1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -214,7 +214,6 @@ GEM multi_json (~> 1.11) mustache nokogiri (~> 1.6.6) - open_uri_redirections (0.2.1) openid-redis-store (0.0.2) redis ruby-openid @@ -444,7 +443,6 @@ DEPENDENCIES omniauth-openid omniauth-twitter onebox - open_uri_redirections openid-redis-store pg pry-nav diff --git a/lib/freedom_patches/open_uri_redirections.rb b/lib/freedom_patches/open_uri_redirections.rb new file mode 100644 index 00000000000..78bb47b1f37 --- /dev/null +++ b/lib/freedom_patches/open_uri_redirections.rb @@ -0,0 +1,98 @@ +##### +# 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 +# +# Thread-safe implementation adapted from: +# https://github.com/obfusk/open_uri_w_redirect_to_https +# +# Copy and pasted from: +# https://github.com/open-uri-redirections/open_uri_redirections +# + +require "open-uri" + +module OpenURI + class < HTTPS redirections. + # * :all will allow HTTP => HTTPS and HTTPS => HTTP redirections. + # + # OpenURI::open can receive different kinds of arguments, like a string for + # the mode or an integer for the permissions, and then a hash with options + # like UserAgent, etc. + # + # To find the :allow_redirections option, we look for this options hash. + # + def self.open_uri(name, *rest, &block) + Thread.current[:__open_uri_redirections__] = allow_redirections(rest) + + block2 = lambda do |io| + Thread.current[:__open_uri_redirections__] = nil + block[io] + end + + begin + open_uri_original name, *rest, &(block ? block2 : nil) + ensure + Thread.current[:__open_uri_redirections__] = nil + end + end + + private + + def self.allow_redirections(args) + options = first_hash_argument(args) + options.delete :allow_redirections if options + end + + def self.first_hash_argument(arguments) + arguments.select { |arg| arg.is_a? Hash }.first + end + + def self.http_to_https?(uri1, uri2) + schemes_from([uri1, uri2]) == %w(http https) + end + + def self.https_to_http?(uri1, uri2) + schemes_from([uri1, uri2]) == %w(https http) + end + + def self.schemes_from(uris) + uris.map { |u| u.scheme.downcase } + end +end