FIX: Support for skipping redirects on certain domains (like steam)

This commit is contained in:
Robin Ward 2017-06-26 15:38:23 -04:00
parent 8967d50dc2
commit db485ae0da
3 changed files with 23 additions and 6 deletions

View File

@ -20,6 +20,7 @@ class FinalDestination
nil
end
end
@ignored = [Discourse.base_url_no_prefix] + (@opts[:ignore_redirects] || [])
@limit = @opts[:max_redirects]
@status = :ready
@cookie = nil
@ -63,10 +64,11 @@ class FinalDestination
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

View File

@ -17,6 +17,10 @@ module Oneboxer
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]
@ -144,7 +148,7 @@ module Oneboxer
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 = {

View File

@ -4,7 +4,10 @@ require 'final_destination'
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'
@ -13,6 +16,7 @@ describe FinalDestination do
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?
@ -64,6 +68,13 @@ describe FinalDestination do
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)