From 14b9eba4285c2af551af665577b1a286b3245f37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Wed, 12 Mar 2014 00:35:19 +0100 Subject: [PATCH 1/2] set content-type on ocs exceptions --- lib/private/api.php | 33 ++++++++++++++++++++++++++++++--- ocs/v1.php | 3 +++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/lib/private/api.php b/lib/private/api.php index 857bc73b1a1..00f80b57468 100644 --- a/lib/private/api.php +++ b/lib/private/api.php @@ -116,9 +116,7 @@ class OC_API { ); } $response = self::mergeResponses($responses); - $formats = array('json', 'xml'); - - $format = !empty($_GET['format']) && in_array($_GET['format'], $formats) ? $_GET['format'] : 'xml'; + $format = self::requestedFormat(); if (self::$logoutRequired) { OC_User::logout(); } @@ -347,4 +345,33 @@ class OC_API { } } + /** + * @return string + */ + public static function requestedFormat() { + $formats = array('json', 'xml'); + + $format = !empty($_GET['format']) && in_array($_GET['format'], $formats) ? $_GET['format'] : 'xml'; + return $format; + } + + /** + * Based on the requested format the response content type is set + */ + public static function setOcsContentType() { + $format = \OC_API::requestedFormat(); + if ($format === 'xml') { + header('Content-type: text/xml; charset=UTF-8'); + return; + } + + if ($format === 'json') { + header('Content-Type: application/json; charset=utf-8'); + return; + } + + header('Content-Type: application/octet-stream; charset=utf-8'); + } + + } diff --git a/ocs/v1.php b/ocs/v1.php index 1c7d1c89768..9d84f482ef7 100644 --- a/ocs/v1.php +++ b/ocs/v1.php @@ -28,8 +28,11 @@ use Symfony\Component\Routing\Exception\MethodNotAllowedException; try { OC::getRouter()->match('/ocs'.OC_Request::getRawPathInfo()); } catch (ResourceNotFoundException $e) { + OC_API::setContentType(); OC_OCS::notFound(); } catch (MethodNotAllowedException $e) { + setOcsContentType(); + OC_API::setContentType(); OC_Response::setStatus(405); } From 46657ad8a6a345daf8404f2e4f5d49cad2030e56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Wed, 12 Mar 2014 10:00:22 +0100 Subject: [PATCH 2/2] fixing method names --- lib/private/api.php | 2 +- ocs/v1.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/private/api.php b/lib/private/api.php index 00f80b57468..0d155b9116c 100644 --- a/lib/private/api.php +++ b/lib/private/api.php @@ -358,7 +358,7 @@ class OC_API { /** * Based on the requested format the response content type is set */ - public static function setOcsContentType() { + public static function setContentType() { $format = \OC_API::requestedFormat(); if ($format === 'xml') { header('Content-type: text/xml; charset=UTF-8'); diff --git a/ocs/v1.php b/ocs/v1.php index 9d84f482ef7..4cbc857bbcf 100644 --- a/ocs/v1.php +++ b/ocs/v1.php @@ -31,7 +31,6 @@ try { OC_API::setContentType(); OC_OCS::notFound(); } catch (MethodNotAllowedException $e) { - setOcsContentType(); OC_API::setContentType(); OC_Response::setStatus(405); }