Skip to content

Commit

Permalink
Extract redirect location handling and clarify behaviour.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Aug 13, 2024
1 parent 498b912 commit 7d170a4
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions lib/async/http/relative_location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ module HTTP
class TooManyRedirects < StandardError
end

# A client wrapper which transparently handles both relative and absolute redirects to a given maximum number of hops.
# A client wrapper which transparently handles redirects to a given maximum number of hops.
#
# The default implementation will only follow relative locations (i.e. those without a scheme) and will only switch to GET if the original request was not a GET.
#
# The best reference for these semantics is defined by the [Fetch specification](https://fetch.spec.whatwg.org/#http-redirect-fetch).
#
Expand Down Expand Up @@ -58,6 +60,16 @@ def redirect_with_get?(request, response)
end
end

def handle_redirect(request, location)
uri = URI.parse(location)

if uri.absolute?
return false
end

request.path = Reference[request.path] + location
end

def call(request)
# We don't want to follow redirects for HEAD requests:
return super if request.head?
Expand All @@ -83,12 +95,8 @@ def call(request)

response.finish

uri = URI.parse(location)

if uri.absolute?
unless handle_redirect(request, location)
return response
else
request.path = Reference[request.path] + location
end

if request.method == GET or response.preserve_method?
Expand Down

0 comments on commit 7d170a4

Please sign in to comment.