A platform for community discussion. Free, open, simple.
FEATURE: allow FinalDestination to use custom user agent for specific hosts
Arpit Jalan00c406520e48
diff --git a/lib/final_destination.rb b/lib/final_destination.rbindex f0829d1416f..549f3df2f4b 100644--- a/lib/final_destination.rb+++ b/lib/final_destination.rb@@ -37,6 +37,7 @@ class FinalDestination@opts = opts || {}@force_get_hosts = @opts[:force_get_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[:lookup_ip] ||= lambda { |host| FinalDestination.lookup_ip(host) }@@ -66,6 +67,7 @@ class FinalDestination@timeout = @opts[:timeout] || nil@preserve_fragment_url = @preserve_fragment_url_hosts.any? { |host| hostname_matches?(host) }@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"enddef self.connection_timeout@@ -82,7 +84,7 @@ class FinalDestinationdef request_headersresult = {- "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" => "*/*","Host" => @uri.hostname}diff --git a/lib/oneboxer.rb b/lib/oneboxer.rbindex 54a1a3c9346..96245cf820a 100644--- a/lib/oneboxer.rb+++ b/lib/oneboxer.rb@@ -30,6 +30,10 @@ module Oneboxer@force_get_hosts ||= ['http://us.battle.net']end+ def self.force_custom_user_agent_hosts+ @force_custom_user_agent_hosts ||= ['http://codepen.io']+ end+def self.allowed_post_types@allowed_post_types ||= [Post.types[:regular], Post.types[:moderator_action]]end@@ -270,7 +274,12 @@ module Oneboxerdef self.external_onebox(url)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.resolvereturn blank_onebox if uri.blank? || blacklisted_domains.map { |hostname| uri.hostname.match?(hostname) }.any?