Add Vary header in case of content negotiation

Sometimes we return different content depending on whether JSON, XML or
HTML was requested in the Accept request header. The Vary response
header should list that header in these cases, to allow caching
frameworks to determine what to cache.
This commit is contained in:
Gidi Kroon 2023-06-25 03:22:41 +02:00
parent ff1409e626
commit 6a5266c6b8
5 changed files with 13 additions and 1 deletions

View file

@ -130,6 +130,8 @@ class Objects extends BaseModule
// Relaxed CORS header for public items
header('Access-Control-Allow-Origin: *');
header('Vary: Accept', false);
System::jsonExit($data, 'application/activity+json');
}
}

View file

@ -90,6 +90,8 @@ class Friendica extends BaseModule
$blocked = null;
}
header('Vary: Accept', false);
$hooked = '';
Hook::callAll('about_hook', $hooked);
@ -125,6 +127,7 @@ class Friendica extends BaseModule
$data = ActivityPub\Transmitter::getProfile(0);
header('Access-Control-Allow-Origin: *');
header('Cache-Control: max-age=23200, stale-while-revalidate=23200');
header('Vary: Accept', false);
System::jsonExit($data, 'application/activity+json');
} catch (HTTPException\NotFoundException $e) {
System::jsonError(404, ['error' => 'Record not found']);

View file

@ -162,6 +162,8 @@ class Display extends BaseModule
$output .= $this->getDisplayData($item);
header('Vary: Accept', false);
return $output;
}

View file

@ -87,6 +87,7 @@ class Profile extends BaseProfile
$data = ActivityPub\Transmitter::getProfile($user['uid']);
header('Access-Control-Allow-Origin: *');
header('Cache-Control: max-age=23200, stale-while-revalidate=23200');
header('Vary: Accept', false);
System::jsonExit($data, 'application/activity+json');
} catch (HTTPException\NotFoundException $e) {
System::jsonError(404, ['error' => 'Record not found']);
@ -103,6 +104,8 @@ class Profile extends BaseProfile
System::jsonError(404, []);
}
}
header('Vary: Accept', false);
}
protected function content(array $request = []): string

View file

@ -152,6 +152,7 @@ class Xrd extends BaseModule
]
];
header('Access-Control-Allow-Origin: *');
header('Vary: Accept', false);
System::jsonExit($json, 'application/jrd+json; charset=utf-8');
}
@ -229,6 +230,7 @@ class Xrd extends BaseModule
];
header('Access-Control-Allow-Origin: *');
header('Vary: Accept', false);
System::jsonExit($json, 'application/jrd+json; charset=utf-8');
}
@ -326,7 +328,7 @@ class Xrd extends BaseModule
]);
header('Access-Control-Allow-Origin: *');
header('Vary: Accept', false);
System::httpExit($xmlString, Response::TYPE_XML, 'application/xrd+xml');
}
}