mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 20:23:44 +08:00
FEATURE: allow FinalDestination to use custom user agent for specific hosts
This commit is contained in:
parent
7d25d65ccb
commit
00c406520e
|
@ -37,6 +37,7 @@ class FinalDestination
|
||||||
@opts = opts || {}
|
@opts = opts || {}
|
||||||
@force_get_hosts = @opts[:force_get_hosts] || []
|
@force_get_hosts = @opts[:force_get_hosts] || []
|
||||||
@preserve_fragment_url_hosts = @opts[:preserve_fragment_url_hosts] || []
|
@preserve_fragment_url_hosts = @opts[:preserve_fragment_url_hosts] || []
|
||||||
|
@force_custom_user_agent_hosts = @opts[:force_custom_user_agent_hosts] || []
|
||||||
@opts[:max_redirects] ||= 5
|
@opts[:max_redirects] ||= 5
|
||||||
@opts[:lookup_ip] ||= lambda { |host| FinalDestination.lookup_ip(host) }
|
@opts[:lookup_ip] ||= lambda { |host| FinalDestination.lookup_ip(host) }
|
||||||
|
|
||||||
|
@ -66,6 +67,7 @@ class FinalDestination
|
||||||
@timeout = @opts[:timeout] || nil
|
@timeout = @opts[:timeout] || nil
|
||||||
@preserve_fragment_url = @preserve_fragment_url_hosts.any? { |host| hostname_matches?(host) }
|
@preserve_fragment_url = @preserve_fragment_url_hosts.any? { |host| hostname_matches?(host) }
|
||||||
@validate_uri = @opts.fetch(:validate_uri) { true }
|
@validate_uri = @opts.fetch(:validate_uri) { true }
|
||||||
|
@user_agent = @force_custom_user_agent_hosts.any? { |host| hostname_matches?(host) } ? Onebox.options.user_agent : "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.connection_timeout
|
def self.connection_timeout
|
||||||
|
@ -82,7 +84,7 @@ class FinalDestination
|
||||||
|
|
||||||
def request_headers
|
def request_headers
|
||||||
result = {
|
result = {
|
||||||
"User-Agent" => "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",
|
"User-Agent" => @user_agent,
|
||||||
"Accept" => "*/*",
|
"Accept" => "*/*",
|
||||||
"Host" => @uri.hostname
|
"Host" => @uri.hostname
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,10 @@ module Oneboxer
|
||||||
@force_get_hosts ||= ['http://us.battle.net']
|
@force_get_hosts ||= ['http://us.battle.net']
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.force_custom_user_agent_hosts
|
||||||
|
@force_custom_user_agent_hosts ||= ['http://codepen.io']
|
||||||
|
end
|
||||||
|
|
||||||
def self.allowed_post_types
|
def self.allowed_post_types
|
||||||
@allowed_post_types ||= [Post.types[:regular], Post.types[:moderator_action]]
|
@allowed_post_types ||= [Post.types[:regular], Post.types[:moderator_action]]
|
||||||
end
|
end
|
||||||
|
@ -270,7 +274,12 @@ module Oneboxer
|
||||||
|
|
||||||
def self.external_onebox(url)
|
def self.external_onebox(url)
|
||||||
Rails.cache.fetch(onebox_cache_key(url), expires_in: 1.day) do
|
Rails.cache.fetch(onebox_cache_key(url), expires_in: 1.day) do
|
||||||
fd = FinalDestination.new(url, ignore_redirects: ignore_redirects, ignore_hostnames: blacklisted_domains, force_get_hosts: force_get_hosts, preserve_fragment_url_hosts: preserve_fragment_url_hosts)
|
fd = FinalDestination.new(url,
|
||||||
|
ignore_redirects: ignore_redirects,
|
||||||
|
ignore_hostnames: blacklisted_domains,
|
||||||
|
force_get_hosts: force_get_hosts,
|
||||||
|
force_custom_user_agent_hosts: force_custom_user_agent_hosts,
|
||||||
|
preserve_fragment_url_hosts: preserve_fragment_url_hosts)
|
||||||
uri = fd.resolve
|
uri = fd.resolve
|
||||||
return blank_onebox if uri.blank? || blacklisted_domains.map { |hostname| uri.hostname.match?(hostname) }.any?
|
return blank_onebox if uri.blank? || blacklisted_domains.map { |hostname| uri.hostname.match?(hostname) }.any?
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user