Module: OpenURI

Defined in:
lib/open_uri_w_redirect_to_https.rb

Constant Summary collapse

RedirectHTTPToHTTPS =
{ mutex: Mutex.new, default: false }

Class Method Summary collapse

Class Method Details

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

open_uri patch that also accepts the :redirect_to_https option; when set to true, redirections from HTTP to HTTPS are allowed; for example:

open('http://github.com', redirect_to_https: true)

you can set the dynamic or global default using redirect_to_https= or w_redirect_to_https



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/open_uri_w_redirect_to_https.rb', line 79

def open_uri(name, *rest, &b)
  r = (o = rest.find { |x| Hash === x }) && o.delete(:redirect_to_https)
  Thread.current[:__open_uri_w_redirect_to_https__] = \
    r.nil? ? redirect_to_https? : r
  b2 = -> io {
    Thread.current[:__open_uri_w_redirect_to_https__] = nil; b[io]
  }
  begin
    open_uri_orig name, *rest, &(b ? b2 : nil)
  ensure
    Thread.current[:__open_uri_w_redirect_to_https__] = nil
  end
end

.open_uri_origObject



20
# File 'lib/open_uri_w_redirect_to_https.rb', line 20

alias_method :open_uri_orig     , :open_uri

.redirect_to_https=(val) ⇒ Object

set the open_uri :redirect_to_https global default



24
25
26
# File 'lib/open_uri_w_redirect_to_https.rb', line 24

def redirect_to_https=(val)
  (x = RedirectHTTPToHTTPS)[:mutex].synchronize { x[:default] = val }
end

.redirect_to_https?Boolean

get the open_uri :redirect_to_https dynamic or global default



30
31
32
33
34
35
# File 'lib/open_uri_w_redirect_to_https.rb', line 30

def redirect_to_https?
  (x = RedirectHTTPToHTTPS)[:mutex].synchronize do
    t = Thread.current[:__open_uri_w_redirect_to_https_default__]
    t.nil? ? x[:default] : t
  end
end

.redirectable?(uri1, uri2) ⇒ Boolean

redirectable? patch that uses a thread-local variable to determine whether HTTP to HTTPS redirection should be allowed (as well)

unless the dynamic or global :redirect_to_https setting is set to :always, only the behaviour of calls through open_uri will be changed (as per argument or dynamic or global setting)



60
61
62
63
64
65
66
67
# File 'lib/open_uri_w_redirect_to_https.rb', line 60

def redirectable?(uri1, uri2)
  if  redirect_to_https? == :always ||
      Thread.current[:__open_uri_w_redirect_to_https__]
    redirectable_w_redirect_to_https? uri1, uri2
  else
    redirectable_orig? uri1, uri2
  end
end

.redirectable_orig?Object



21
# File 'lib/open_uri_w_redirect_to_https.rb', line 21

alias_method :redirectable_orig?, :redirectable?

.w_redirect_to_https(val = true, &b) ⇒ Object

dynamically thread-scoped open_uri :redirect_to_https default; for example:

w_redirect_to_https { open('http://github.com' }


43
44
45
46
47
48
49
50
51
# File 'lib/open_uri_w_redirect_to_https.rb', line 43

def w_redirect_to_https(val = true, &b)
  old = Thread.current[:__open_uri_w_redirect_to_https_default__]
  Thread.current[:__open_uri_w_redirect_to_https_default__] = val
  begin
    b[]
  ensure
    Thread.current[:__open_uri_w_redirect_to_https_default__] = old
  end
end