Commit 9b0d4c1ee55ed5ef8501af98934f3cd1c121cf84

Michael Crumm 2021-07-30T14:23:24

CI (#2) Handle redirect to S3 asset manually

diff --git a/lib/dart_sass.ex b/lib/dart_sass.ex
index 4c4d5b3..6e4d158 100644
--- a/lib/dart_sass.ex
+++ b/lib/dart_sass.ex
@@ -257,6 +257,7 @@ defmodule DartSass do
     cacertfile = CAStore.file_path() |> String.to_charlist()
 
     http_options = [
+      autoredirect: false,
       ssl: [
         verify: :verify_peer,
         cacertfile: cacertfile,
@@ -267,11 +268,18 @@ defmodule DartSass do
       ]
     ]
 
-    options = [body_format: :binary]
+    case :httpc.request(:get, {url, []}, http_options, []) do
+      {:ok, {{_, 302, _}, headers, _}} ->
+        {'location', download} = List.keyfind(headers, 'location', 0)
+        options = [body_format: :binary]
 
-    case :httpc.request(:get, {url, []}, http_options, options) do
-      {:ok, {{_, 200, _}, _headers, body}} ->
-        body
+        case :httpc.request(:get, {download, []}, http_options, options) do
+          {:ok, {{_, 200, _}, _, body}} ->
+            body
+
+          other ->
+            raise "couldn't fetch #{download}: #{inspect(other)}"
+        end
 
       other ->
         raise "couldn't fetch #{url}: #{inspect(other)}"