[Pkg-owncloud-commits] [owncloud] 30/223: handle http accept headers more gracefully

David Prévot taffit at moszumanska.debian.org
Sun Jun 22 01:54:02 UTC 2014


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository owncloud.

commit 1002281dae6650684ba06d21abeba8ff8ba070d9
Author: Bernhard Posselt <dev at bernhard-posselt.com>
Date:   Wed Jun 11 00:54:25 2014 +0200

    handle http accept headers more gracefully
---
 lib/private/appframework/http/dispatcher.php       | 29 ++++++++--------------
 lib/public/appframework/controller.php             | 24 ++++++++++++++++++
 .../lib/appframework/controller/ControllerTest.php | 23 +++++++++++++++++
 3 files changed, 58 insertions(+), 18 deletions(-)

diff --git a/lib/private/appframework/http/dispatcher.php b/lib/private/appframework/http/dispatcher.php
index 442e33e..c5ec345 100644
--- a/lib/private/appframework/http/dispatcher.php
+++ b/lib/private/appframework/http/dispatcher.php
@@ -75,7 +75,7 @@ class Dispatcher {
 		$out = array(null, array(), null);
 
 		try {
-			// prefill reflector with everything thats needed for the 
+			// prefill reflector with everything thats needed for the
 			// middlewares
 			$this->reflector->reflect($controller, $methodName);
 
@@ -132,14 +132,14 @@ class Dispatcher {
 			// it to the type annotated in the @param annotation
 			$value = $this->request->getParam($param, $default);
 			$type = $this->reflector->getType($param);
-			
-			// if this is submitted using GET or a POST form, 'false' should be 
+
+			// if this is submitted using GET or a POST form, 'false' should be
 			// converted to false
 			if(($type === 'bool' || $type === 'boolean') &&
-				$value === 'false' && 
+				$value === 'false' &&
 				(
 					$this->request->method === 'GET' ||
-					strpos($this->request->getHeader('Content-Type'), 
+					strpos($this->request->getHeader('Content-Type'),
 						'application/x-www-form-urlencoded') !== false
 				)
 			) {
@@ -148,7 +148,7 @@ class Dispatcher {
 			} elseif(in_array($type, $types)) {
 				settype($value, $type);
 			}
-			
+
 			$arguments[] = $value;
 		}
 
@@ -156,22 +156,15 @@ class Dispatcher {
 
 		// format response if not of type response
 		if(!($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) {
-				$header = $this->request->getHeader('Accept');
-				$formats = explode(',', $header);
-
-				if($header !== null && count($formats) > 0) {
-					$accept = strtolower(trim($formats[0]));
-					$format = str_replace('application/', '', $accept);
-				} else {
-					$format = 'json';
-				}
-			}
+                $headers = $this->request->getHeader('Accept');
+                $format = $controller->getResponderByHTTPHeader($headers);
+            }
 
 			$response = $controller->buildResponse($response, $format);
 		}
diff --git a/lib/public/appframework/controller.php b/lib/public/appframework/controller.php
index 7949190..b3bff5e 100644
--- a/lib/public/appframework/controller.php
+++ b/lib/public/appframework/controller.php
@@ -70,6 +70,30 @@ abstract class Controller {
 	}
 
 
+    /**
+     * Parses an HTTP accept header and returns the supported responder type
+     * @param string $acceptHeader
+     * @return string the responder type
+     */
+    public function getResponderByHTTPHeader($acceptHeader) {
+        $headers = explode(',', $acceptHeader);
+
+        // return the first matching responder
+        foreach ($headers as $header) {
+            $header = trim($header);
+
+            $responder = str_replace('application/', '', $header);
+
+            if (array_key_exists($responder, $this->responders)) {
+                return $responder;
+            }
+        }
+
+        // no matching header defaults to json
+        return 'json';
+    }
+
+
 	/**
 	 * Registers a formatter for a type
 	 * @param string $format
diff --git a/tests/lib/appframework/controller/ControllerTest.php b/tests/lib/appframework/controller/ControllerTest.php
index 65144e7..1a09925 100644
--- a/tests/lib/appframework/controller/ControllerTest.php
+++ b/tests/lib/appframework/controller/ControllerTest.php
@@ -30,6 +30,14 @@ use OCP\AppFramework\Http\JSONResponse;
 
 
 class ChildController extends Controller {
+
+	public function __construct($appName, $request) {
+		parent::__construct($appName, $request);
+		$this->registerResponder('tom', function ($respone) {
+			return 'hi';
+		});
+	}
+
 	public function custom($in) {
 		$this->registerResponder('json', function ($response) {
 			return new JSONResponse(array(strlen($response)));
@@ -161,5 +169,20 @@ class ControllerTest extends \PHPUnit_Framework_TestCase {
 	}
 
 
+	public function testDefaultResponderToJSON() {
+		$responder = $this->controller->getResponderByHTTPHeader('*/*');
+
+		$this->assertEquals('json', $responder);
+	}
+
+
+	public function testResponderAcceptHeaderParsed() {
+		$responder = $this->controller->getResponderByHTTPHeader(
+			'*/*, application/tom, application/json'
+		);
+
+		$this->assertEquals('tom', $responder);
+	}
+
 
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud.git



More information about the Pkg-owncloud-commits mailing list