diff --git a/lib/req/steps.ex b/lib/req/steps.ex index 712a8a5..222c28c 100644 --- a/lib/req/steps.ex +++ b/lib/req/steps.ex @@ -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 @@ -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