fetch_resource_service.rb 3.0 KB

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