Skip to content

Commit

Permalink
FIX: Support for skipping redirects on certain domains (like steam)
Browse files Browse the repository at this point in the history
  • Loading branch information
eviltrout committed Jun 26, 2017
1 parent 8967d50 commit db485ae
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
10 changes: 6 additions & 4 deletions lib/final_destination.rb
Expand Up @@ -20,6 +20,7 @@ def initialize(url, opts=nil)
nil
end
end
@ignored = [Discourse.base_url_no_prefix] + (@opts[:ignore_redirects] || [])
@limit = @opts[:max_redirects]
@status = :ready
@cookie = nil
Expand Down Expand Up @@ -63,10 +64,11 @@ def resolve
return nil
end

# Always allow current base url
if hostname_matches?(Discourse.base_url_no_prefix)
@status = :resolved
return @uri
@ignored.each do |host|
if hostname_matches?(host)
@status = :resolved
return @uri
end
end

return nil unless validate_uri
Expand Down
6 changes: 5 additions & 1 deletion lib/oneboxer.rb
Expand Up @@ -17,6 +17,10 @@ def changed?
end
end

def self.ignore_redirects
@ignore_redirects ||= ['http://store.steampowered.com']
end

def self.preview(url, options=nil)
options ||= {}
invalidate(url) if options[:invalidate_oneboxes]
Expand Down Expand Up @@ -144,7 +148,7 @@ def self.onebox_cache_key(url)
def self.onebox_raw(url)

Rails.cache.fetch(onebox_cache_key(url), expires_in: 1.day) do
fd = FinalDestination.new(url)
fd = FinalDestination.new(url, ignore_redirects: ignore_redirects)
uri = fd.resolve
return blank_onebox if uri.blank? || SiteSetting.onebox_domains_blacklist.include?(uri.hostname)
options = {
Expand Down
13 changes: 12 additions & 1 deletion spec/components/final_destination_spec.rb
Expand Up @@ -4,7 +4,10 @@
describe FinalDestination do

let(:opts) do
{ # avoid IP lookups in test
{
ignore_redirects: ['https://ignore-me.com'],

# avoid IP lookups in test
lookup_ip: lambda do |host|
case host
when 'eviltrout.com' then '52.84.143.152'
Expand All @@ -13,6 +16,7 @@
when 'some_thing.example.com' then '104.25.152.10'
when 'private-host.com' then '192.168.10.1'
when 'internal-ipv6.com' then '2001:abc:de:01:3:3d0:6a65:c2bf'
when 'ignore-me.com' then '53.84.143.152'
else
as_ip = IPAddr.new(host) rescue nil
raise "couldn't lookup #{host}" if as_ip.nil?
Expand Down Expand Up @@ -64,6 +68,13 @@ def fd(url)
end
end

it "ignores redirects" do
final = FinalDestination.new('https://ignore-me.com/some-url', opts)
expect(final.resolve.to_s).to eq('https://ignore-me.com/some-url')
expect(final.redirected?).to eq(false)
expect(final.status).to eq(:resolved)
end

context "underscores in URLs" do
before do
stub_request(:head, 'https://some_thing.example.com').to_return(doc_response)
Expand Down

0 comments on commit db485ae

Please sign in to comment.