Skip to content

Commit

Permalink
Fix aeweb case sensitivity for asset names (#1580)
Browse files Browse the repository at this point in the history
  • Loading branch information
wassimans authored Oct 22, 2024
1 parent bcd6e95 commit f2cbc55
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/archethic_web/aeweb/controllers/web_hosting_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ defmodule ArchethicWeb.AEWeb.WebHostingController do
defp do_web_hosting(conn, params) do
cache_headers = get_cache_headers(conn)

# Normalise/downcase url_paths
params =
Map.update!(
params,
"url_path",
fn url ->
Enum.map(url, &String.downcase/1)
end
)

case get_website(params, cache_headers) do
{:ok, file_content, encoding, mime_type, cached?, etag} ->
send_response(conn, file_content, encoding, mime_type, cached?, etag)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ defmodule ArchethicWeb.AEWeb.WebHostingController.Resources do
defp get_file(metadata, url_path) do
resource_path = Enum.join(url_path, "/")

metadata = normalise_downcase_key(metadata)

case Map.get(metadata, resource_path) do
nil ->
if is_a_directory?(metadata, resource_path) do
Expand Down Expand Up @@ -141,7 +143,8 @@ defmodule ArchethicWeb.AEWeb.WebHostingController.Resources do

{:ok, decoded_content} = Jason.decode(tx_content)

{:ok, res_content} = access(decoded_content, resource_path)
{:ok, res_content} =
decoded_content |> normalise_downcase_key() |> access(resource_path)

acc <> res_content
end)
Expand Down Expand Up @@ -209,4 +212,12 @@ defmodule ArchethicWeb.AEWeb.WebHostingController.Resources do
String.starts_with?(key, file_path)
end)
end

# Normalise/downcase map keys
defp normalise_downcase_key(map) do
Enum.map(map, fn {key, value} ->
{String.downcase(key), value}
end)
|> Map.new()
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,11 @@ defmodule ArchethicWeb.AEWeb.WebHostingControllerTest do
"000071fbc2205f3eba39d310baf15bd89a019b0929be76b7864852cb68c9cd6502de"
]
},
"IMAGE2.png":{
"addresses":[
"000071fbc2205f3eba39d310baf15bd89a019b0929be76b7864852cb68c9cd6502de"
]
},
"ungzip.png":{
"encoding":"gzip",
"addresses":[
Expand All @@ -286,6 +291,7 @@ defmodule ArchethicWeb.AEWeb.WebHostingControllerTest do
"raw.html":"PGgxPkhlbGxvIHdvcmxkICE8L2gxPg",
"no_content.html":"unsupported",
"image.png":"PGgxPkhlbGxvIHdvcmxkICE8L2gxPg",
"IMAGE2.png":"PGgxPkhlbGxvIHdvcmxkICE8L2gxPg",
"ungzip.png":"H4sIAAAAAAAAA7PJMLTzSM3JyVcozy_KSVFQtNEHigAA4YcXnxYAAAA"
}
"""
Expand Down Expand Up @@ -383,6 +389,32 @@ defmodule ArchethicWeb.AEWeb.WebHostingControllerTest do
assert "<h1>Hello world !</h1>" = response(conn, 200)
end

test "should downcase url_path before processing", %{conn: conn} do
conn1 =
get(
conn,
"/aeweb/0000225496a380d5005cb68374e9b8b45d7e0f505a42f8cd61cbd43c3684c5cbacba/IMage.png"
)

assert "<h1>Hello world !</h1>" = response(conn1, 200)

conn2 =
get(
conn,
"/aeweb/0000225496a380d5005cb68374e9b8b45d7e0f505a42f8cd61cbd43c3684c5cbacba/image2.png"
)

assert "<h1>Hello world !</h1>" = response(conn2, 200)

conn3 =
get(
conn,
"/aeweb/0000225496a380d5005cb68374e9b8b45d7e0f505a42f8cd61cbd43c3684c5cbacba/image.jpeg"
)

assert "Cannot find file content" = response(conn3, 404)
end

test "should return raw file content", %{conn: conn} do
conn1 =
get(
Expand Down

0 comments on commit f2cbc55

Please sign in to comment.