class Async::HTTP::Middleware::LocationRedirector

def call(request)

def call(request)
	# We don't want to follow redirects for HEAD requests:
	return super if request.head?
	
	body = ::Protocol::HTTP::Body::Rewindable.wrap(request)
	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
			
			unless handle_redirect(request, location)
				return response
			end
			
			# Ensure the request (body) is finished and set to nil before we manipulate the request:
			request.finish
			
			if request.method == GET or response.preserve_method?
				# We (might) need to rewind the body so that it can be submitted again:
				body&.rewind
				request.body = body
			else
				# We are changing the method to GET:
				request.method = GET
				
				# We will no longer be submitting the body:
				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