From 00c406520e48a76ae51f072b8d11af7ca1c3aca6 Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Thu, 7 Nov 2019 14:44:43 +0530 Subject: [PATCH] FEATURE: allow FinalDestination to use custom user agent for specific hosts --- lib/final_destination.rb | 4 +++- lib/oneboxer.rb | 11 ++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/final_destination.rb b/lib/final_destination.rb index 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" end def self.connection_timeout @@ -82,7 +84,7 @@ class FinalDestination def request_headers 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" => "*/*", "Host" => @uri.hostname } diff --git a/lib/oneboxer.rb b/lib/oneboxer.rb index 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 Oneboxer def 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.resolve return blank_onebox if uri.blank? || blacklisted_domains.map { |hostname| uri.hostname.match?(hostname) }.any?