Drupal investigation

HttpKernel.php 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel;
  11. use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
  12. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  13. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  14. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  15. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  16. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  17. use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
  18. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  19. use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
  20. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  21. use Symfony\Component\HttpKernel\Event\PostResponseEvent;
  22. use Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\HttpFoundation\RequestStack;
  25. use Symfony\Component\HttpFoundation\Response;
  26. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  27. /**
  28. * HttpKernel notifies events to convert a Request object to a Response one.
  29. *
  30. * @author Fabien Potencier <fabien@symfony.com>
  31. */
  32. class HttpKernel implements HttpKernelInterface, TerminableInterface
  33. {
  34. protected $dispatcher;
  35. protected $resolver;
  36. protected $requestStack;
  37. /**
  38. * Constructor.
  39. *
  40. * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
  41. * @param ControllerResolverInterface $resolver A ControllerResolverInterface instance
  42. * @param RequestStack $requestStack A stack for master/sub requests
  43. */
  44. public function __construct(EventDispatcherInterface $dispatcher, ControllerResolverInterface $resolver, RequestStack $requestStack = null)
  45. {
  46. $this->dispatcher = $dispatcher;
  47. $this->resolver = $resolver;
  48. $this->requestStack = $requestStack ?: new RequestStack();
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
  54. {
  55. $request->headers->set('X-Php-Ob-Level', ob_get_level());
  56. try {
  57. return $this->handleRaw($request, $type);
  58. } catch (\Exception $e) {
  59. if ($e instanceof ConflictingHeadersException) {
  60. $e = new BadRequestHttpException('The request headers contain conflicting information regarding the origin of this request.', $e);
  61. }
  62. if (false === $catch) {
  63. $this->finishRequest($request, $type);
  64. throw $e;
  65. }
  66. return $this->handleException($e, $request, $type);
  67. }
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function terminate(Request $request, Response $response)
  73. {
  74. $this->dispatcher->dispatch(KernelEvents::TERMINATE, new PostResponseEvent($this, $request, $response));
  75. }
  76. /**
  77. * @throws \LogicException If the request stack is empty
  78. *
  79. * @internal
  80. */
  81. public function terminateWithException(\Exception $exception)
  82. {
  83. if (!$request = $this->requestStack->getMasterRequest()) {
  84. throw new \LogicException('Request stack is empty', 0, $exception);
  85. }
  86. $response = $this->handleException($exception, $request, self::MASTER_REQUEST);
  87. $response->sendHeaders();
  88. $response->sendContent();
  89. $this->terminate($request, $response);
  90. }
  91. /**
  92. * Handles a request to convert it to a response.
  93. *
  94. * Exceptions are not caught.
  95. *
  96. * @param Request $request A Request instance
  97. * @param int $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
  98. *
  99. * @return Response A Response instance
  100. *
  101. * @throws \LogicException If one of the listener does not behave as expected
  102. * @throws NotFoundHttpException When controller cannot be found
  103. */
  104. private function handleRaw(Request $request, $type = self::MASTER_REQUEST)
  105. {
  106. $this->requestStack->push($request);
  107. // request
  108. $event = new GetResponseEvent($this, $request, $type);
  109. $this->dispatcher->dispatch(KernelEvents::REQUEST, $event);
  110. if ($event->hasResponse()) {
  111. return $this->filterResponse($event->getResponse(), $request, $type);
  112. }
  113. // load controller
  114. if (false === $controller = $this->resolver->getController($request)) {
  115. throw new NotFoundHttpException(sprintf('Unable to find the controller for path "%s". The route is wrongly configured.', $request->getPathInfo()));
  116. }
  117. $event = new FilterControllerEvent($this, $controller, $request, $type);
  118. $this->dispatcher->dispatch(KernelEvents::CONTROLLER, $event);
  119. $controller = $event->getController();
  120. // controller arguments
  121. $arguments = $this->resolver->getArguments($request, $controller);
  122. // call controller
  123. $response = call_user_func_array($controller, $arguments);
  124. // view
  125. if (!$response instanceof Response) {
  126. $event = new GetResponseForControllerResultEvent($this, $request, $type, $response);
  127. $this->dispatcher->dispatch(KernelEvents::VIEW, $event);
  128. if ($event->hasResponse()) {
  129. $response = $event->getResponse();
  130. }
  131. if (!$response instanceof Response) {
  132. $msg = sprintf('The controller must return a response (%s given).', $this->varToString($response));
  133. // the user may have forgotten to return something
  134. if (null === $response) {
  135. $msg .= ' Did you forget to add a return statement somewhere in your controller?';
  136. }
  137. throw new \LogicException($msg);
  138. }
  139. }
  140. return $this->filterResponse($response, $request, $type);
  141. }
  142. /**
  143. * Filters a response object.
  144. *
  145. * @param Response $response A Response instance
  146. * @param Request $request An error message in case the response is not a Response object
  147. * @param int $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
  148. *
  149. * @return Response The filtered Response instance
  150. *
  151. * @throws \RuntimeException if the passed object is not a Response instance
  152. */
  153. private function filterResponse(Response $response, Request $request, $type)
  154. {
  155. $event = new FilterResponseEvent($this, $request, $type, $response);
  156. $this->dispatcher->dispatch(KernelEvents::RESPONSE, $event);
  157. $this->finishRequest($request, $type);
  158. return $event->getResponse();
  159. }
  160. /**
  161. * Publishes the finish request event, then pop the request from the stack.
  162. *
  163. * Note that the order of the operations is important here, otherwise
  164. * operations such as {@link RequestStack::getParentRequest()} can lead to
  165. * weird results.
  166. *
  167. * @param Request $request
  168. * @param int $type
  169. */
  170. private function finishRequest(Request $request, $type)
  171. {
  172. $this->dispatcher->dispatch(KernelEvents::FINISH_REQUEST, new FinishRequestEvent($this, $request, $type));
  173. $this->requestStack->pop();
  174. }
  175. /**
  176. * Handles an exception by trying to convert it to a Response.
  177. *
  178. * @param \Exception $e An \Exception instance
  179. * @param Request $request A Request instance
  180. * @param int $type The type of the request
  181. *
  182. * @return Response A Response instance
  183. *
  184. * @throws \Exception
  185. */
  186. private function handleException(\Exception $e, $request, $type)
  187. {
  188. $event = new GetResponseForExceptionEvent($this, $request, $type, $e);
  189. $this->dispatcher->dispatch(KernelEvents::EXCEPTION, $event);
  190. // a listener might have replaced the exception
  191. $e = $event->getException();
  192. if (!$event->hasResponse()) {
  193. $this->finishRequest($request, $type);
  194. throw $e;
  195. }
  196. $response = $event->getResponse();
  197. // the developer asked for a specific status code
  198. if ($response->headers->has('X-Status-Code')) {
  199. $response->setStatusCode($response->headers->get('X-Status-Code'));
  200. $response->headers->remove('X-Status-Code');
  201. } elseif (!$response->isClientError() && !$response->isServerError() && !$response->isRedirect()) {
  202. // ensure that we actually have an error response
  203. if ($e instanceof HttpExceptionInterface) {
  204. // keep the HTTP status code and headers
  205. $response->setStatusCode($e->getStatusCode());
  206. $response->headers->add($e->getHeaders());
  207. } else {
  208. $response->setStatusCode(500);
  209. }
  210. }
  211. try {
  212. return $this->filterResponse($response, $request, $type);
  213. } catch (\Exception $e) {
  214. return $response;
  215. }
  216. }
  217. private function varToString($var)
  218. {
  219. if (is_object($var)) {
  220. return sprintf('Object(%s)', get_class($var));
  221. }
  222. if (is_array($var)) {
  223. $a = array();
  224. foreach ($var as $k => $v) {
  225. $a[] = sprintf('%s => %s', $k, $this->varToString($v));
  226. }
  227. return sprintf('Array(%s)', implode(', ', $a));
  228. }
  229. if (is_resource($var)) {
  230. return sprintf('Resource(%s)', get_resource_type($var));
  231. }
  232. if (null === $var) {
  233. return 'null';
  234. }
  235. if (false === $var) {
  236. return 'false';
  237. }
  238. if (true === $var) {
  239. return 'true';
  240. }
  241. return (string) $var;
  242. }
  243. }