From ee7719d54003cc48940567fd9f04f3d9f42cece8 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Wed, 10 May 2017 23:30:07 +0200 Subject: [PATCH] Fix #2955 - Send HEAD request ahead of GET when fetching URL previews (#2972) --- app/services/fetch_link_card_service.rb | 6 +++++- spec/services/fetch_link_card_service_spec.rb | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/app/services/fetch_link_card_service.rb b/app/services/fetch_link_card_service.rb index 524b66626c..27d7c2a708 100644 --- a/app/services/fetch_link_card_service.rb +++ b/app/services/fetch_link_card_service.rb @@ -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 diff --git a/spec/services/fetch_link_card_service_spec.rb b/spec/services/fetch_link_card_service_spec.rb index 46fec295d5..681c99c114 100644 --- a/spec/services/fetch_link_card_service_spec.rb +++ b/spec/services/fetch_link_card_service_spec.rb @@ -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