Skip to content

Commit

Permalink
redirect: Normalize URLs without scheme (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
bfolkens authored Mar 25, 2024
1 parent ad6e489 commit 95a6b14
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/req/steps.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1964,7 +1964,10 @@ defmodule Req.Steps do
request.options[:redirect_trusted]
end

location_url = URI.merge(request.url, URI.parse(location))
location_url =
request.url
|> URI.merge(URI.parse(location))
|> normalize_redirect_uri()

request
# assume put_params step already run so remove :params option so it's not applied again
Expand All @@ -1980,6 +1983,10 @@ defmodule Req.Steps do
Logger.log(level, ["redirecting to ", location])
end

defp normalize_redirect_uri(%URI{scheme: "http", port: nil} = uri), do: %URI{uri | port: 80}
defp normalize_redirect_uri(%URI{scheme: "https", port: nil} = uri), do: %URI{uri | port: 443}
defp normalize_redirect_uri(%URI{} = uri), do: uri

# https://www.rfc-editor.org/rfc/rfc9110#name-301-moved-permanently and 302:
#
# > Note: For historical reasons, a user agent MAY change the request method from
Expand Down

0 comments on commit 95a6b14

Please sign in to comment.