Skip to content

Commit 7d170a4

Browse files
committed
Extract redirect location handling and clarify behaviour.
1 parent 498b912 commit 7d170a4

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

lib/async/http/relative_location.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ module HTTP
1616
class TooManyRedirects < StandardError
1717
end
1818

19-
# A client wrapper which transparently handles both relative and absolute redirects to a given maximum number of hops.
19+
# A client wrapper which transparently handles redirects to a given maximum number of hops.
20+
#
21+
# 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.
2022
#
2123
# The best reference for these semantics is defined by the [Fetch specification](https://fetch.spec.whatwg.org/#http-redirect-fetch).
2224
#
@@ -58,6 +60,16 @@ def redirect_with_get?(request, response)
5860
end
5961
end
6062

63+
def handle_redirect(request, location)
64+
uri = URI.parse(location)
65+
66+
if uri.absolute?
67+
return false
68+
end
69+
70+
request.path = Reference[request.path] + location
71+
end
72+
6173
def call(request)
6274
# We don't want to follow redirects for HEAD requests:
6375
return super if request.head?
@@ -83,12 +95,8 @@ def call(request)
8395

8496
response.finish
8597

86-
uri = URI.parse(location)
87-
88-
if uri.absolute?
98+
unless handle_redirect(request, location)
8999
return response
90-
else
91-
request.path = Reference[request.path] + location
92100
end
93101

94102
if request.method == GET or response.preserve_method?

0 commit comments

Comments
 (0)