mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-18 18:44:16 +00:00
This commit is contained in:
@@ -187,6 +187,7 @@ class Kernel extends HttpKernel
|
|||||||
],
|
],
|
||||||
// do only bindings, no auth
|
// do only bindings, no auth
|
||||||
'api_basic' => [
|
'api_basic' => [
|
||||||
|
AcceptHeaders::class,
|
||||||
'bindings',
|
'bindings',
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
@@ -44,14 +44,23 @@ class AcceptHeaders
|
|||||||
*/
|
*/
|
||||||
public function handle($request, $next): mixed
|
public function handle($request, $next): mixed
|
||||||
{
|
{
|
||||||
$method = $request->getMethod();
|
$method = $request->getMethod();
|
||||||
|
$accepts = ['application/x-www-form-urlencoded', 'application/json', 'application/vnd.api+json', '*/*'];
|
||||||
|
$contentTypes = ['application/x-www-form-urlencoded', 'application/json', 'application/vnd.api+json'];
|
||||||
|
$submitted = (string)$request->header('Content-Type');
|
||||||
|
|
||||||
if ('GET' === $method && !$request->accepts(['application/json', 'application/vnd.api+json'])) {
|
|
||||||
throw new BadHttpHeaderException('Your request must accept either application/json or application/vnd.api+json');
|
// if bad Accept header, send error.
|
||||||
|
if (!$request->accepts($accepts)) {
|
||||||
|
throw new BadHttpHeaderException(sprintf('Accept header "%s" is not something this server can provide.', $request->header('Accept')));
|
||||||
}
|
}
|
||||||
$allowed = ['application/x-www-form-urlencoded', 'application/json',''];
|
// if bad 'Content-Type' header, refuse service.
|
||||||
$submitted = (string)$request->header('Content-Type');
|
if (('POST' === $method || 'PUT' === $method) && !$request->hasHeader('Content-Type')) {
|
||||||
if (('POST' === $method || 'PUT' === $method) && !in_array($submitted, $allowed, true)) {
|
$error = new BadHttpHeaderException('Content-Type header cannot be empty');
|
||||||
|
$error->statusCode = 415;
|
||||||
|
throw $error;
|
||||||
|
}
|
||||||
|
if (('POST' === $method || 'PUT' === $method) && !in_array($submitted, $contentTypes, true)) {
|
||||||
$error = new BadHttpHeaderException(sprintf('Content-Type cannot be "%s"', $submitted));
|
$error = new BadHttpHeaderException(sprintf('Content-Type cannot be "%s"', $submitted));
|
||||||
$error->statusCode = 415;
|
$error->statusCode = 415;
|
||||||
throw $error;
|
throw $error;
|
||||||
|
Reference in New Issue
Block a user