From 5240e8416050363f6f3974cba0c7de90e57526ca Mon Sep 17 00:00:00 2001 From: bohung Date: Tue, 10 May 2022 10:00:50 +0800 Subject: [PATCH] Fix request bug. --- app/models/site_feed.rb | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/app/models/site_feed.rb b/app/models/site_feed.rb index 846a528..d1d1c82 100644 --- a/app/models/site_feed.rb +++ b/app/models/site_feed.rb @@ -86,6 +86,21 @@ class SiteFeed def channel_title_for_cache !self[:channel_title].to_s.empty? ? self[:channel_title] : I18n.t("feed.source") end + def http_request(http, request) + response = http.request(request) + if response.code.to_i == 301 || response.code.to_i == 302 + location = response["location"] + new_uri = URI(location) + http = Net::HTTP.new(new_uri.host, new_uri.port) + if location.include?('https') + http.use_ssl = true + http.verify_mode = OpenSSL::SSL::VERIFY_NONE + end + request.instance_variable_set(:@path, new_uri.path) + response = http_request(http, request) + end + response + end def add_notify unless self.enable_notify root_url = Site.first.root_url rescue "" @@ -98,7 +113,7 @@ class SiteFeed http_req.open_timeout = 10 request = Net::HTTP::Post.new("/xhr/#{self.channel_key.pluralize}/feed_add_remote/#{self.feed_uid}", 'Content-Type' => 'application/json') request.body = {"url"=>root_url}.to_json - response = http_req.request(request) + response = http_request( http_req , request ) if response.code.to_i == 200 self.update(:enable_notify=>true) end @@ -117,7 +132,7 @@ class SiteFeed http_req.open_timeout = 10 request = Net::HTTP::Post.new("/xhr/#{self.channel_key.pluralize}/feed_remove_remote/#{self.feed_uid}", 'Content-Type' => 'application/json') request.body = {"url"=>root_url}.to_json - response = http_req.request(request) + response = http_request( http_req , request ) if response.code.to_i == 200 self.update(:enable_notify=>false) end