fetch_resource_service.rb 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # frozen_string_literal: true
  2. class FetchResourceService < BaseService
  3. include JsonLdHelper
  4. ACCEPT_HEADER = 'application/activity+json, application/ld+json; profile="https://www.w3.org/ns/activitystreams", text/html;q=0.1'
  5. ACTIVITY_STREAM_LINK_TYPES = ['application/activity+json', 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'].freeze
  6. attr_reader :response_code
  7. def call(url)
  8. return if url.blank?
  9. process(url)
  10. rescue HTTP::Error, OpenSSL::SSL::SSLError, Addressable::URI::InvalidURIError, Mastodon::HostValidationError, Mastodon::LengthValidationError => e
  11. Rails.logger.debug { "Error fetching resource #{@url}: #{e}" }
  12. nil
  13. end
  14. private
  15. def process(url, terminal: false)
  16. @url = url
  17. perform_request { |response| process_response(response, terminal) }
  18. end
  19. def perform_request(&block)
  20. Request.new(:get, @url).tap do |request|
  21. request.add_headers('Accept' => ACCEPT_HEADER)
  22. # In a real setting we want to sign all outgoing requests,
  23. # in case the remote server has secure mode enabled and requires
  24. # authentication on all resources. However, during development,
  25. # sending request signatures with an inaccessible host is useless
  26. # and prevents even public resources from being fetched, so
  27. # don't do it
  28. request.on_behalf_of(Account.representative) unless Rails.env.development?
  29. end.perform(&block)
  30. end
  31. def process_response(response, terminal = false)
  32. @response_code = response.code
  33. return nil if response.code != 200
  34. if ['application/activity+json', 'application/ld+json'].include?(response.mime_type)
  35. body = response.body_with_limit
  36. json = body_to_json(body)
  37. [json['id'], { prefetched_body: body, id: true }] if supported_context?(json) && (equals_or_includes_any?(json['type'], ActivityPub::FetchRemoteActorService::SUPPORTED_TYPES) || expected_type?(json))
  38. elsif !terminal
  39. link_header = response['Link'] && parse_link_header(response)
  40. if link_header&.find_link(%w(rel alternate))
  41. process_link_headers(link_header)
  42. elsif response.mime_type == 'text/html'
  43. process_html(response)
  44. end
  45. end
  46. end
  47. def expected_type?(json)
  48. equals_or_includes_any?(json['type'], ActivityPub::Activity::Create::SUPPORTED_TYPES + ActivityPub::Activity::Create::CONVERTED_TYPES)
  49. end
  50. def process_html(response)
  51. page = Nokogiri::HTML(response.body_with_limit)
  52. json_link = page.xpath('//link[@rel="alternate"]').find { |link| ACTIVITY_STREAM_LINK_TYPES.include?(link['type']) }
  53. process(json_link['href'], terminal: true) unless json_link.nil?
  54. end
  55. def process_link_headers(link_header)
  56. json_link = link_header.find_link(%w(rel alternate), %w(type application/activity+json)) || link_header.find_link(%w(rel alternate), ['type', 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'])
  57. process(json_link.href, terminal: true) unless json_link.nil?
  58. end
  59. def parse_link_header(response)
  60. LinkHeader.parse(response['Link'].is_a?(Array) ? response['Link'].first : response['Link'])
  61. end
  62. end