Skip to content

A platform for community discussion. Free, open, simple.

FIX: follow redirects for inline/mini onebox (#13512)

Arpit Jalan0adeddde610a
diff --git a/lib/retrieve_title.rb b/lib/retrieve_title.rb
index fd652b33902..abab83596a6 100644
--- a/lib/retrieve_title.rb
+++ b/lib/retrieve_title.rb
@@ -57,24 +57,26 @@ module RetrieveTitle
encoding = nil
fd.get do |_response, chunk, uri|
+ unless Net::HTTPRedirection === _response
+ if current
+ current << chunk
+ else
+ current = chunk
+ end
- if current
- current << chunk
- else
- current = chunk
- end
- if !encoding && content_type = _response['content-type']&.strip&.downcase
- if content_type =~ /charset="?([a-z0-9_-]+)"?/
- encoding = Regexp.last_match(1)
- if !Encoding.list.map(&:name).map(&:downcase).include?(encoding)
- encoding = nil
+ if !encoding && content_type = _response['content-type']&.strip&.downcase
+ if content_type =~ /charset="?([a-z0-9_-]+)"?/
+ encoding = Regexp.last_match(1)
+ if !Encoding.list.map(&:name).map(&:downcase).include?(encoding)
+ encoding = nil
+ end
end
end
- end
- max_size = max_chunk_size(uri) * 1024
- title = extract_title(current, encoding)
- throw :done if title || max_size < current.length
+ max_size = max_chunk_size(uri) * 1024
+ title = extract_title(current, encoding)
+ throw :done if title || max_size < current.length
+ end
end
title
end
diff --git a/spec/components/retrieve_title_spec.rb b/spec/components/retrieve_title_spec.rb
index 56e80a41301..a519e7eb722 100644
--- a/spec/components/retrieve_title_spec.rb
+++ b/spec/components/retrieve_title_spec.rb
@@ -89,5 +89,16 @@ describe RetrieveTitle do
IPSocket.stubs(:getaddress).returns('100.2.3.4')
expect(RetrieveTitle.crawl("https://brelksdjflaskfj.com/amazing")).to eq("japanese こんにちは website")
end
+
+ it "can follow redirect" do
+ stub_request(:get, "http://foobar.com/amazing").
+ to_return(status: 301, body: "", headers: { "location" => "https://wikipedia.com/amazing" })
+
+ stub_request(:get, "https://wikipedia.com/amazing").
+ to_return(status: 200, body: "<html><title>very amazing</title>", headers: {})
+
+ IPSocket.stubs(:getaddress).returns('100.2.3.4')
+ expect(RetrieveTitle.crawl("http://foobar.com/amazing")).to eq("very amazing")
+ end
end
end