Merge pull request 'Use standard-compliant Accept header when fetching' (#740) from Oneric/akkoma:fetch_std-accept-hdr into develop

Reviewed-on: AkkomaGang/akkoma#740
This commit is contained in:
floatingghost 2024-04-12 18:28:26 +00:00
commit e2e4f53585
2 changed files with 38 additions and 32 deletions

View file

@ -324,7 +324,11 @@ def get_object(id) do
date = Pleroma.Signature.signed_date() date = Pleroma.Signature.signed_date()
headers = headers =
[{"accept", "application/activity+json"}] [
# The first is required by spec, the second provided as a fallback for buggy implementations
{"accept", "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""},
{"accept", "application/activity+json"}
]
|> maybe_date_fetch(date) |> maybe_date_fetch(date)
|> sign_fetch(id, date) |> sign_fetch(id, date)

View file

@ -5,7 +5,16 @@
defmodule HttpRequestMock do defmodule HttpRequestMock do
require Logger require Logger
def activitypub_object_headers, do: [{"content-type", "application/activity+json"}] def activitypub_object_headers,
do: [
{"content-type", "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""}
]
# The Accept headers we genrate to be exact; AP spec only requires the first somewhere
@activitypub_accept_headers [
{"accept", "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""},
{"accept", "application/activity+json"}
]
def request( def request(
%Tesla.Env{ %Tesla.Env{
@ -97,7 +106,7 @@ def get("https://mastodon.sdf.org/users/rinpatch/collections/featured", _, _, _)
File.read!("test/fixtures/users_mock/masto_featured.json") File.read!("test/fixtures/users_mock/masto_featured.json")
|> String.replace("{{domain}}", "mastodon.sdf.org") |> String.replace("{{domain}}", "mastodon.sdf.org")
|> String.replace("{{nickname}}", "rinpatch"), |> String.replace("{{nickname}}", "rinpatch"),
headers: [{"content-type", "application/activity+json"}] headers: activitypub_object_headers()
}} }}
end end
@ -208,7 +217,7 @@ def get(
"https://mst3k.interlinked.me/users/luciferMysticus", "https://mst3k.interlinked.me/users/luciferMysticus",
_, _,
_, _,
[{"accept", "application/activity+json"}] @activitypub_accept_headers
) do ) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
@ -231,7 +240,7 @@ def get(
"https://hubzilla.example.org/channel/kaniini", "https://hubzilla.example.org/channel/kaniini",
_, _,
_, _,
[{"accept", "application/activity+json"}] @activitypub_accept_headers
) do ) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
@ -241,7 +250,7 @@ def get(
}} }}
end end
def get("https://niu.moe/users/rye", _, _, [{"accept", "application/activity+json"}]) do def get("https://niu.moe/users/rye", _, _, @activitypub_accept_headers) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
status: 200, status: 200,
@ -250,7 +259,7 @@ def get("https://niu.moe/users/rye", _, _, [{"accept", "application/activity+jso
}} }}
end end
def get("https://n1u.moe/users/rye", _, _, [{"accept", "application/activity+json"}]) do def get("https://n1u.moe/users/rye", _, _, @activitypub_accept_headers) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
status: 200, status: 200,
@ -270,7 +279,7 @@ def get("http://mastodon.example.org/users/admin/statuses/100787282858396771", _
}} }}
end end
def get("https://puckipedia.com/", _, _, [{"accept", "application/activity+json"}]) do def get("https://puckipedia.com/", _, _, @activitypub_accept_headers) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
status: 200, status: 200,
@ -342,9 +351,12 @@ def get("https://peertube.social/videos/watch/278d2b7c-0f38-4aaa-afe6-9ecc0c4a34
}} }}
end end
def get("https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39", _, _, [ def get(
{"accept", "application/activity+json"} "https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39",
]) do _,
_,
@activitypub_accept_headers
) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
status: 200, status: 200,
@ -353,7 +365,7 @@ def get("https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39", _,
}} }}
end end
def get("https://mobilizon.org/@tcit", _, _, [{"accept", "application/activity+json"}]) do def get("https://mobilizon.org/@tcit", _, _, @activitypub_accept_headers) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
status: 200, status: 200,
@ -416,9 +428,7 @@ def get(
{:ok, %Tesla.Env{status: 404, body: ""}} {:ok, %Tesla.Env{status: 404, body: ""}}
end end
def get("http://mastodon.example.org/users/relay", _, _, [ def get("http://mastodon.example.org/users/relay", _, _, @activitypub_accept_headers) do
{"accept", "application/activity+json"}
]) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
status: 200, status: 200,
@ -427,9 +437,7 @@ def get("http://mastodon.example.org/users/relay", _, _, [
}} }}
end end
def get("http://mastodon.example.org/users/gargron", _, _, [ def get("http://mastodon.example.org/users/gargron", _, _, @activitypub_accept_headers) do
{"accept", "application/activity+json"}
]) do
{:error, :nxdomain} {:error, :nxdomain}
end end
@ -620,7 +628,7 @@ def get("https://shitposter.club/notice/7369654", _, _, _) do
}} }}
end end
def get("https://mstdn.io/users/mayuutann", _, _, [{"accept", "application/activity+json"}]) do def get("https://mstdn.io/users/mayuutann", _, _, @activitypub_accept_headers) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
status: 200, status: 200,
@ -633,7 +641,7 @@ def get(
"https://mstdn.io/users/mayuutann/statuses/99568293732299394", "https://mstdn.io/users/mayuutann/statuses/99568293732299394",
_, _,
_, _,
[{"accept", "application/activity+json"}] @activitypub_accept_headers
) do ) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
@ -779,7 +787,7 @@ def get(
"http://gs.example.org:4040/index.php/user/1", "http://gs.example.org:4040/index.php/user/1",
_, _,
_, _,
[{"accept", "application/activity+json"}] @activitypub_accept_headers
) do ) do
{:ok, %Tesla.Env{status: 406, body: ""}} {:ok, %Tesla.Env{status: 406, body: ""}}
end end
@ -966,7 +974,7 @@ def get("https://apfed.club/channel/indio", _, _, _) do
}} }}
end end
def get("https://social.heldscal.la/user/23211", _, _, [{"accept", "application/activity+json"}]) do def get("https://social.heldscal.la/user/23211", _, _, @activitypub_accept_headers) do
{:ok, Tesla.Mock.json(%{"id" => "https://social.heldscal.la/user/23211"}, status: 200)} {:ok, Tesla.Mock.json(%{"id" => "https://social.heldscal.la/user/23211"}, status: 200)}
end end
@ -1207,13 +1215,11 @@ def get("https://lm.kazv.moe/users/mewmew/collections/featured", _, _, _) do
File.read!("test/fixtures/users_mock/masto_featured.json") File.read!("test/fixtures/users_mock/masto_featured.json")
|> String.replace("{{domain}}", "lm.kazv.moe") |> String.replace("{{domain}}", "lm.kazv.moe")
|> String.replace("{{nickname}}", "mewmew"), |> String.replace("{{nickname}}", "mewmew"),
headers: [{"content-type", "application/activity+json"}] headers: activitypub_object_headers()
}} }}
end end
def get("https://info.pleroma.site/activity.json", _, _, [ def get("https://info.pleroma.site/activity.json", _, _, @activitypub_accept_headers) do
{"accept", "application/activity+json"}
]) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
status: 200, status: 200,
@ -1226,9 +1232,7 @@ def get("https://info.pleroma.site/activity.json", _, _, _) do
{:ok, %Tesla.Env{status: 404, body: ""}} {:ok, %Tesla.Env{status: 404, body: ""}}
end end
def get("https://info.pleroma.site/activity2.json", _, _, [ def get("https://info.pleroma.site/activity2.json", _, _, @activitypub_accept_headers) do
{"accept", "application/activity+json"}
]) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
status: 200, status: 200,
@ -1241,9 +1245,7 @@ def get("https://info.pleroma.site/activity2.json", _, _, _) do
{:ok, %Tesla.Env{status: 404, body: ""}} {:ok, %Tesla.Env{status: 404, body: ""}}
end end
def get("https://info.pleroma.site/activity3.json", _, _, [ def get("https://info.pleroma.site/activity3.json", _, _, @activitypub_accept_headers) do
{"accept", "application/activity+json"}
]) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
status: 200, status: 200,