diff --git a/build/psalm-baseline.xml b/build/psalm-baseline.xml index fd9c1b31a310d..7be6b8dedaa6a 100644 --- a/build/psalm-baseline.xml +++ b/build/psalm-baseline.xml @@ -3330,11 +3330,6 @@ - - - - - diff --git a/lib/private/AppFramework/Http/Dispatcher.php b/lib/private/AppFramework/Http/Dispatcher.php index 8d91ddf7502ca..2c5f12764a687 100644 --- a/lib/private/AppFramework/Http/Dispatcher.php +++ b/lib/private/AppFramework/Http/Dispatcher.php @@ -210,14 +210,7 @@ private function executeController(Controller $controller, string $methodName): // format response if ($response instanceof DataResponse || !($response instanceof Response)) { - // get format from the url format or request format parameter - $format = $this->request->getParam('format'); - - // if none is given try the first Accept header - if ($format === null) { - $headers = $this->request->getHeader('Accept'); - $format = $controller->getResponderByHTTPHeader($headers, null); - } + $format = $this->request->getFormat(); if ($format !== null) { $response = $controller->buildResponse($response, $format); diff --git a/lib/private/AppFramework/Http/Request.php b/lib/private/AppFramework/Http/Request.php index 7cc7467675ca8..f7d45630a81f2 100644 --- a/lib/private/AppFramework/Http/Request.php +++ b/lib/private/AppFramework/Http/Request.php @@ -877,4 +877,23 @@ private function fromTrustedProxy(): bool { return \is_array($trustedProxies) && $this->isTrustedProxy($trustedProxies, $remoteAddress); } + + public function getFormat(): ?string { + $format = $this->getParam('format'); + if ($format !== null) { + return $format; + } + + $prefix = 'application/'; + $headers = explode(',', $this->getHeader('Accept')); + foreach ($headers as $header) { + $header = strtolower(trim($header)); + + if (str_starts_with($header, $prefix)) { + return substr($header, strlen($prefix)); + } + } + + return null; + } } diff --git a/lib/private/AppFramework/Middleware/OCSMiddleware.php b/lib/private/AppFramework/Middleware/OCSMiddleware.php index 64f4b0054de98..3b16b1aef503c 100644 --- a/lib/private/AppFramework/Middleware/OCSMiddleware.php +++ b/lib/private/AppFramework/Middleware/OCSMiddleware.php @@ -110,7 +110,7 @@ public function afterController($controller, $methodName, Response $response) { * @return V1Response|V2Response */ private function buildNewResponse(Controller $controller, $code, $message) { - $format = $this->getFormat($controller); + $format = $this->request->getFormat() ?? 'xml'; $data = new DataResponse(); $data->setStatus($code); @@ -122,21 +122,4 @@ private function buildNewResponse(Controller $controller, $code, $message) { return $response; } - - /** - * @param Controller $controller - * @return string - */ - private function getFormat(Controller $controller) { - // get format from the url format or request format parameter - $format = $this->request->getParam('format'); - - // if none is given try the first Accept header - if ($format === null) { - $headers = $this->request->getHeader('Accept'); - $format = $controller->getResponderByHTTPHeader($headers, 'xml'); - } - - return $format; - } } diff --git a/lib/private/OCS/ApiHelper.php b/lib/private/OCS/ApiHelper.php index f69b540eafa01..e8f219da11243 100644 --- a/lib/private/OCS/ApiHelper.php +++ b/lib/private/OCS/ApiHelper.php @@ -25,7 +25,8 @@ class ApiHelper { */ public static function respond(int $statusCode, string $statusMessage, array $headers = [], ?int $overrideHttpStatusCode = null): void { $request = Server::get(IRequest::class); - $format = $request->getParam('format', 'xml'); + $format = $request->getFormat() ?? 'xml'; + if (self::isV2($request)) { $response = new V2Response(new DataResponse([], $statusCode, $headers), $format, $statusMessage); } else { @@ -58,7 +59,7 @@ public static function respond(int $statusCode, string $statusMessage, array $he * Based on the requested format the response content type is set */ public static function setContentType(?string $format = null): void { - $format ??= Server::get(IRequest::class)->getParam('format', 'xml'); + $format ??= Server::get(IRequest::class)->getFormat() ?? 'xml'; if ($format === 'xml') { header('Content-type: text/xml; charset=UTF-8'); return; diff --git a/lib/public/AppFramework/Controller.php b/lib/public/AppFramework/Controller.php index cdeaac99366e8..50b6d18f044da 100644 --- a/lib/public/AppFramework/Controller.php +++ b/lib/public/AppFramework/Controller.php @@ -89,6 +89,7 @@ public function __construct($appName, * @return string the responder type * @since 7.0.0 * @since 9.1.0 Added default parameter + * @deprecated 33.0.0 Use {@see \OCP\IRequest::getFormat} instead */ public function getResponderByHTTPHeader($acceptHeader, $default = 'json') { $headers = explode(',', $acceptHeader); diff --git a/lib/public/IRequest.php b/lib/public/IRequest.php index cbac143d77575..913ecbdd4cd1d 100644 --- a/lib/public/IRequest.php +++ b/lib/public/IRequest.php @@ -315,4 +315,14 @@ public function getServerHost(): string; * @since 32.0.0 */ public function throwDecodingExceptionIfAny(): void; + + /** + * Returns the format of the response to this request. + * + * The `Accept` header and the `format` query parameter control the format. + * + * @return string|null + * @since 33.0.0 + */ + public function getFormat(): ?string; }