Skip to content

Commit

Permalink
Do not turn HEAD requests to GET by default through redirects (#967)
Browse files Browse the repository at this point in the history
  • Loading branch information
giordano authored Nov 24, 2022
1 parent 41df951 commit 9a95100
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/clientlayers/RedirectRequest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ function newmethod(request_method, response_status, redirect_method)
return request_method
elseif redirect_method !== nothing && String(redirect_method) in ("GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS", "PATCH")
return redirect_method
elseif request_method == "HEAD"
# Unless otherwise specified (e.g. with `redirect_method`), be conservative and keep the
# same method, see:
#
# * <https://httpwg.org/specs/rfc9110.html#status.301>
# * <https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirections#permanent_redirections>
#
# Turning a HEAD request through a redirect may be undesired:
# <https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD>.
return request_method
end
return "GET"
end
Expand Down
9 changes: 9 additions & 0 deletions test/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,15 @@ end

# canonicalizeheaders
@test isok(HTTP.get("https://$httpbin/ip"; canonicalizeheaders=false, socket_type_tls=tls))

# Ensure HEAD requests stay the same through redirects by default
r = HTTP.head("https://$httpbin/redirect/1")
@test r.request.method == "HEAD"
@test iszero(length(r.body))
# But if explicitly requested, GET can be used instead
r = HTTP.head("https://$httpbin/redirect/1"; redirect_method="GET")
@test r.request.method == "GET"
@test length(r.body) > 0
end
end

Expand Down

0 comments on commit 9a95100

Please sign in to comment.