cache_buster.rb 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. # frozen_string_literal: true
  2. class CacheBuster
  3. def initialize(options = {})
  4. Rails.application.deprecators[:mastodon].warn('Default values for the cache buster secret header name and values will be removed in Mastodon 4.3. Please set them explicitely if you rely on those.') unless options[:http_method] || (options[:secret] && options[:secret_header])
  5. @secret_header = options[:secret_header] ||
  6. (options[:http_method] ? nil : 'Secret-Header')
  7. @secret = options[:secret] ||
  8. (options[:http_method] ? nil : 'True')
  9. @http_method = options[:http_method] || 'GET'
  10. end
  11. def bust(url)
  12. site = Addressable::URI.parse(url).normalized_site
  13. request_pool.with(site) do |http_client|
  14. build_request(url, http_client).perform
  15. end
  16. end
  17. private
  18. def request_pool
  19. RequestPool.current
  20. end
  21. def build_request(url, http_client)
  22. request = Request.new(@http_method.downcase.to_sym, url, http_client: http_client)
  23. request.add_headers(@secret_header => @secret) if @secret_header.present? && @secret && !@secret.empty?
  24. request
  25. end
  26. end