]> cat aescling's git repositories - mastodon.git/commitdiff
Fix #2955 - Send HEAD request ahead of GET when fetching URL previews (#2972)
authorEugen Rochko <eugen@zeonfederated.com>
Wed, 10 May 2017 21:30:07 +0000 (23:30 +0200)
committerGitHub <noreply@github.com>
Wed, 10 May 2017 21:30:07 +0000 (23:30 +0200)
app/services/fetch_link_card_service.rb
spec/services/fetch_link_card_service_spec.rb

index 524b66626ca79200a25ff6c6f6d3c87b47390f39..27d7c2a708329f6780cfc5e1dc52227efd2618f3 100644 (file)
@@ -11,8 +11,12 @@ class FetchLinkCardService < BaseService
 
     return if url.nil?
 
-    url = Addressable::URI.parse(url).normalize.to_s
+    url  = Addressable::URI.parse(url).normalize.to_s
     card = PreviewCard.where(status: status).first_or_initialize(status: status, url: url)
+    res  = http_client.head(url)
+
+    return if res.code != 200 || res.mime_type != 'text/html'
+
     attempt_opengraph(card, url) unless attempt_oembed(card, url)
   end
 
index 46fec295d5d3a3718e6990c92d23704359d55b5f..681c99c11497d45c8f291ef6ccaf99552fea56c4 100644 (file)
@@ -2,6 +2,7 @@ require 'rails_helper'
 
 RSpec.describe FetchLinkCardService do
   before do
+    stub_request(:head, 'http://example.xn--fiqs8s/').to_return(status: 200, headers: { 'Content-Type' => 'text/html' })
     stub_request(:get, 'http://example.xn--fiqs8s/').to_return(request_fixture('idn.txt'))
   end