0
0
Fork 0

Allow setting a custom HTTP method in CacheBuster (#26528)

Co-authored-by: Jorijn Schrijvershof <jorijn@jorijn.com>
This commit is contained in:
Renaud Chaput 2023-08-18 08:18:40 +02:00 committed by GitHub
parent b5acf13886
commit b95867ad1f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 81 additions and 6 deletions

View file

@ -2,8 +2,14 @@
class CacheBuster
def initialize(options = {})
@secret_header = options[:secret_header] || 'Secret-Header'
@secret = options[:secret] || 'True'
ActiveSupport::Deprecation.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])
@secret_header = options[:secret_header] ||
(options[:http_method] ? nil : 'Secret-Header')
@secret = options[:secret] ||
(options[:http_method] ? nil : 'True')
@http_method = options[:http_method] || 'GET'
end
def bust(url)
@ -21,8 +27,9 @@ class CacheBuster
end
def build_request(url, http_client)
Request.new(:get, url, http_client: http_client).tap do |request|
request.add_headers(@secret_header => @secret)
end
request = Request.new(@http_method.downcase.to_sym, url, http_client: http_client)
request.add_headers(@secret_header => @secret) if @secret_header.present? && @secret && !@secret.empty?
request
end
end

View file

@ -117,7 +117,7 @@ class Request
def perform
begin
response = http_client.public_send(@verb, @url.to_s, @options.merge(headers: headers))
response = http_client.request(@verb, @url.to_s, @options.merge(headers: headers))
rescue => e
raise e.class, "#{e.message} on #{@url}", e.backtrace[0]
end