class Async::HTTP::RelativeLocation
def call(request)
def call(request) # We don't want to follow redirects for HEAD requests: return super if request.head? if body = request.body # We need to cache the body as it might be submitted multiple times if we get a response status of 307 or 308: body = ::Protocol::HTTP::Body::Rewindable.new(body) request.body = body end hops = 0 while hops <= @maximum_hops response = super(request) if response.redirection? hops += 1 # Get the redirect location: unless location = response.headers['location'] return response end response.finish uri = URI.parse(location) if uri.absolute? return response else request.path = Reference[request.path] + location end if request.method == GET or response.preserve_method? # We (might) need to rewind the body so that it can be submitted again: body&.rewind else # We are changing the method to GET: request.method = GET # Clear the request body: request.finish body = nil # Remove any headers which are not allowed in a GET request: PROHIBITED_GET_HEADERS.each do |header| request.headers.delete(header) end end else return response end end raise TooManyRedirects, "Redirected #{hops} times, exceeded maximum!" end