class Async::HTTP::RelativeLocation

A client wrapper which transparently handles both relative and absolute redirects to a given maximum number of hops.

Constants

DEFAULT_METHOD

Attributes

maximum_hops[R]

The maximum number of hops which will limit the number of redirects until an error is thrown.

Public Class Methods

new(app, maximum_hops = 3) click to toggle source

maximum_hops is the max number of redirects. Set to 0 to allow 1 request with no redirects.

Calls superclass method
# File lib/async/http/relative_location.rb, line 37
def initialize(app, maximum_hops = 3)
        super(app)
        
        @maximum_hops = maximum_hops
end

Public Instance Methods

call(request) click to toggle source
Calls superclass method
# File lib/async/http/relative_location.rb, line 46
def call(request)
        hops = 0
        
        # We need to cache the body as it might be submitted multiple times.
        request.finish
        
        while hops <= @maximum_hops
                response = super(request)

                if response.redirection?
                        hops += 1
                        response.finish
                        
                        location = response.headers['location']
                        uri = URI.parse(location)
                        
                        if uri.absolute?
                                return response
                        else
                                request.path = Reference[request.path] + location
                        end
                        
                        unless response.preserve_method?
                                request.method = DEFAULT_METHOD
                        end
                else
                        return response
                end
        end
        
        raise TooManyRedirects, "Redirected #{hops} times, exceeded maximum!"
end