Dispatcher.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  6. * @author Georg Ehrke <georg@owncloud.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. * @author Thomas Tanghus <thomas@tanghus.net>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\AppFramework\Http;
  28. use \OC\AppFramework\Middleware\MiddlewareDispatcher;
  29. use \OC\AppFramework\Http;
  30. use \OC\AppFramework\Utility\ControllerMethodReflector;
  31. use OCP\AppFramework\Controller;
  32. use OCP\AppFramework\Http\Response;
  33. use OCP\AppFramework\Http\DataResponse;
  34. use OCP\IRequest;
  35. /**
  36. * Class to dispatch the request to the middleware dispatcher
  37. */
  38. class Dispatcher {
  39. private $middlewareDispatcher;
  40. private $protocol;
  41. private $reflector;
  42. private $request;
  43. /**
  44. * @param Http $protocol the http protocol with contains all status headers
  45. * @param MiddlewareDispatcher $middlewareDispatcher the dispatcher which
  46. * runs the middleware
  47. * @param ControllerMethodReflector $reflector the reflector that is used to inject
  48. * the arguments for the controller
  49. * @param IRequest $request the incoming request
  50. */
  51. public function __construct(Http $protocol,
  52. MiddlewareDispatcher $middlewareDispatcher,
  53. ControllerMethodReflector $reflector,
  54. IRequest $request) {
  55. $this->protocol = $protocol;
  56. $this->middlewareDispatcher = $middlewareDispatcher;
  57. $this->reflector = $reflector;
  58. $this->request = $request;
  59. }
  60. /**
  61. * Handles a request and calls the dispatcher on the controller
  62. * @param Controller $controller the controller which will be called
  63. * @param string $methodName the method name which will be called on
  64. * the controller
  65. * @return array $array[0] contains a string with the http main header,
  66. * $array[1] contains headers in the form: $key => value, $array[2] contains
  67. * the response output
  68. * @throws \Exception
  69. */
  70. public function dispatch(Controller $controller, $methodName) {
  71. $out = array(null, array(), null);
  72. try {
  73. // prefill reflector with everything thats needed for the
  74. // middlewares
  75. $this->reflector->reflect($controller, $methodName);
  76. $this->middlewareDispatcher->beforeController($controller,
  77. $methodName);
  78. $response = $this->executeController($controller, $methodName);
  79. // if an exception appears, the middleware checks if it can handle the
  80. // exception and creates a response. If no response is created, it is
  81. // assumed that theres no middleware who can handle it and the error is
  82. // thrown again
  83. } catch(\Exception $exception){
  84. $response = $this->middlewareDispatcher->afterException(
  85. $controller, $methodName, $exception);
  86. if (is_null($response)) {
  87. throw $exception;
  88. }
  89. }
  90. $response = $this->middlewareDispatcher->afterController(
  91. $controller, $methodName, $response);
  92. // depending on the cache object the headers need to be changed
  93. $out[0] = $this->protocol->getStatusHeader($response->getStatus(),
  94. $response->getLastModified(), $response->getETag());
  95. $out[1] = array_merge($response->getHeaders());
  96. $out[2] = $response->getCookies();
  97. $out[3] = $this->middlewareDispatcher->beforeOutput(
  98. $controller, $methodName, $response->render()
  99. );
  100. $out[4] = $response;
  101. return $out;
  102. }
  103. /**
  104. * Uses the reflected parameters, types and request parameters to execute
  105. * the controller
  106. * @param Controller $controller the controller to be executed
  107. * @param string $methodName the method on the controller that should be executed
  108. * @return Response
  109. */
  110. private function executeController($controller, $methodName) {
  111. $arguments = array();
  112. // valid types that will be casted
  113. $types = array('int', 'integer', 'bool', 'boolean', 'float');
  114. foreach($this->reflector->getParameters() as $param => $default) {
  115. // try to get the parameter from the request object and cast
  116. // it to the type annotated in the @param annotation
  117. $value = $this->request->getParam($param, $default);
  118. $type = $this->reflector->getType($param);
  119. // if this is submitted using GET or a POST form, 'false' should be
  120. // converted to false
  121. if(($type === 'bool' || $type === 'boolean') &&
  122. $value === 'false' &&
  123. (
  124. $this->request->method === 'GET' ||
  125. strpos($this->request->getHeader('Content-Type'),
  126. 'application/x-www-form-urlencoded') !== false
  127. )
  128. ) {
  129. $value = false;
  130. } elseif($value !== null && in_array($type, $types)) {
  131. settype($value, $type);
  132. }
  133. $arguments[] = $value;
  134. }
  135. $response = call_user_func_array(array($controller, $methodName), $arguments);
  136. // format response
  137. if($response instanceof DataResponse || !($response instanceof Response)) {
  138. // get format from the url format or request format parameter
  139. $format = $this->request->getParam('format');
  140. // if none is given try the first Accept header
  141. if($format === null) {
  142. $headers = $this->request->getHeader('Accept');
  143. $format = $controller->getResponderByHTTPHeader($headers, null);
  144. }
  145. if ($format !== null) {
  146. $response = $controller->buildResponse($response, $format);
  147. } else {
  148. $response = $controller->buildResponse($response);
  149. }
  150. }
  151. return $response;
  152. }
  153. }