From 210362e66578dae32cbf169884d3c2905fe32cb7 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Mon, 26 Sep 2016 17:04:05 +0200 Subject: [PATCH] Improve redirect handling in FetchAtomService --- app/services/fetch_atom_service.rb | 8 ++++---- app/services/fetch_remote_account_service.rb | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/app/services/fetch_atom_service.rb b/app/services/fetch_atom_service.rb index 57f789ada..ada36b9f8 100644 --- a/app/services/fetch_atom_service.rb +++ b/app/services/fetch_atom_service.rb @@ -8,7 +8,7 @@ class FetchAtomService < BaseService if response.mime_type == 'application/atom+xml' return [url, fetch(url)] elsif !response['Link'].blank? - return process_headers(response) + return process_headers(url, response) else return process_html(fetch(url)) end @@ -26,13 +26,13 @@ class FetchAtomService < BaseService return [alternate_link['href'], fetch(alternate_link['href'])] end - def process_headers(response) + def process_headers(url, response) Rails.logger.debug "Processing link header" link_header = LinkHeader.parse(response['Link']) alternate_link = link_header.find_link(['rel', 'alternate'], ['type', 'application/atom+xml']) - return nil if alternate_link.nil? + return process_html(fetch(url)) if alternate_link.nil? return [alternate_link.href, fetch(alternate_link.href)] end @@ -41,6 +41,6 @@ class FetchAtomService < BaseService end def http_client - HTTP.timeout(:per_operation, write: 20, connect: 20, read: 50) + HTTP.timeout(:per_operation, write: 20, connect: 20, read: 50).follow end end diff --git a/app/services/fetch_remote_account_service.rb b/app/services/fetch_remote_account_service.rb index d24ac2a37..072eb2cd3 100644 --- a/app/services/fetch_remote_account_service.rb +++ b/app/services/fetch_remote_account_service.rb @@ -9,6 +9,7 @@ class FetchRemoteAccountService < BaseService private def process_atom(url, body) + xml = Nokogiri::XML(body) url_parts = Addressable::URI.parse(url) username = xml.at_xpath('//xmlns:author/xmlns:name').try(:content) domain = url_parts.host