@@ -26,227 +26,227 @@ |
||
| 26 | 26 | * Class to dispatch the request to the middleware dispatcher |
| 27 | 27 | */ |
| 28 | 28 | class Dispatcher { |
| 29 | - /** @var MiddlewareDispatcher */ |
|
| 30 | - private $middlewareDispatcher; |
|
| 31 | - |
|
| 32 | - /** @var Http */ |
|
| 33 | - private $protocol; |
|
| 34 | - |
|
| 35 | - /** @var ControllerMethodReflector */ |
|
| 36 | - private $reflector; |
|
| 37 | - |
|
| 38 | - /** @var IRequest */ |
|
| 39 | - private $request; |
|
| 40 | - |
|
| 41 | - /** @var IConfig */ |
|
| 42 | - private $config; |
|
| 43 | - |
|
| 44 | - /** @var ConnectionAdapter */ |
|
| 45 | - private $connection; |
|
| 46 | - |
|
| 47 | - /** @var LoggerInterface */ |
|
| 48 | - private $logger; |
|
| 49 | - |
|
| 50 | - /** @var IEventLogger */ |
|
| 51 | - private $eventLogger; |
|
| 52 | - |
|
| 53 | - private ContainerInterface $appContainer; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * @param Http $protocol the http protocol with contains all status headers |
|
| 57 | - * @param MiddlewareDispatcher $middlewareDispatcher the dispatcher which |
|
| 58 | - * runs the middleware |
|
| 59 | - * @param ControllerMethodReflector $reflector the reflector that is used to inject |
|
| 60 | - * the arguments for the controller |
|
| 61 | - * @param IRequest $request the incoming request |
|
| 62 | - * @param IConfig $config |
|
| 63 | - * @param ConnectionAdapter $connection |
|
| 64 | - * @param LoggerInterface $logger |
|
| 65 | - * @param IEventLogger $eventLogger |
|
| 66 | - */ |
|
| 67 | - public function __construct( |
|
| 68 | - Http $protocol, |
|
| 69 | - MiddlewareDispatcher $middlewareDispatcher, |
|
| 70 | - ControllerMethodReflector $reflector, |
|
| 71 | - IRequest $request, |
|
| 72 | - IConfig $config, |
|
| 73 | - ConnectionAdapter $connection, |
|
| 74 | - LoggerInterface $logger, |
|
| 75 | - IEventLogger $eventLogger, |
|
| 76 | - ContainerInterface $appContainer, |
|
| 77 | - ) { |
|
| 78 | - $this->protocol = $protocol; |
|
| 79 | - $this->middlewareDispatcher = $middlewareDispatcher; |
|
| 80 | - $this->reflector = $reflector; |
|
| 81 | - $this->request = $request; |
|
| 82 | - $this->config = $config; |
|
| 83 | - $this->connection = $connection; |
|
| 84 | - $this->logger = $logger; |
|
| 85 | - $this->eventLogger = $eventLogger; |
|
| 86 | - $this->appContainer = $appContainer; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Handles a request and calls the dispatcher on the controller |
|
| 92 | - * @param Controller $controller the controller which will be called |
|
| 93 | - * @param string $methodName the method name which will be called on |
|
| 94 | - * the controller |
|
| 95 | - * @return array $array[0] contains the http status header as a string, |
|
| 96 | - * $array[1] contains response headers as an array, |
|
| 97 | - * $array[2] contains response cookies as an array, |
|
| 98 | - * $array[3] contains the response output as a string, |
|
| 99 | - * $array[4] contains the response object |
|
| 100 | - * @throws \Exception |
|
| 101 | - */ |
|
| 102 | - public function dispatch(Controller $controller, string $methodName): array { |
|
| 103 | - $out = [null, [], null]; |
|
| 104 | - |
|
| 105 | - try { |
|
| 106 | - // prefill reflector with everything that's needed for the |
|
| 107 | - // middlewares |
|
| 108 | - $this->reflector->reflect($controller, $methodName); |
|
| 109 | - |
|
| 110 | - $this->middlewareDispatcher->beforeController($controller, |
|
| 111 | - $methodName); |
|
| 112 | - |
|
| 113 | - $databaseStatsBefore = []; |
|
| 114 | - if ($this->config->getSystemValueBool('debug', false)) { |
|
| 115 | - $databaseStatsBefore = $this->connection->getInner()->getStats(); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - $response = $this->executeController($controller, $methodName); |
|
| 119 | - |
|
| 120 | - if (!empty($databaseStatsBefore)) { |
|
| 121 | - $databaseStatsAfter = $this->connection->getInner()->getStats(); |
|
| 122 | - $numBuilt = $databaseStatsAfter['built'] - $databaseStatsBefore['built']; |
|
| 123 | - $numExecuted = $databaseStatsAfter['executed'] - $databaseStatsBefore['executed']; |
|
| 124 | - |
|
| 125 | - if ($numBuilt > 50) { |
|
| 126 | - $this->logger->debug('Controller {class}::{method} created {count} QueryBuilder objects, please check if they are created inside a loop by accident.', [ |
|
| 127 | - 'class' => get_class($controller), |
|
| 128 | - 'method' => $methodName, |
|
| 129 | - 'count' => $numBuilt, |
|
| 130 | - ]); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - if ($numExecuted > 100) { |
|
| 134 | - $this->logger->warning('Controller {class}::{method} executed {count} queries.', [ |
|
| 135 | - 'class' => get_class($controller), |
|
| 136 | - 'method' => $methodName, |
|
| 137 | - 'count' => $numExecuted, |
|
| 138 | - ]); |
|
| 139 | - } |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - // if an exception appears, the middleware checks if it can handle the |
|
| 143 | - // exception and creates a response. If no response is created, it is |
|
| 144 | - // assumed that there's no middleware who can handle it and the error is |
|
| 145 | - // thrown again |
|
| 146 | - } catch (\Exception $exception) { |
|
| 147 | - $response = $this->middlewareDispatcher->afterException( |
|
| 148 | - $controller, $methodName, $exception); |
|
| 149 | - } catch (\Throwable $throwable) { |
|
| 150 | - $exception = new \Exception($throwable->getMessage() . ' in file \'' . $throwable->getFile() . '\' line ' . $throwable->getLine(), $throwable->getCode(), $throwable); |
|
| 151 | - $response = $this->middlewareDispatcher->afterException( |
|
| 152 | - $controller, $methodName, $exception); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - $response = $this->middlewareDispatcher->afterController( |
|
| 156 | - $controller, $methodName, $response); |
|
| 157 | - |
|
| 158 | - // depending on the cache object the headers need to be changed |
|
| 159 | - $out[0] = $this->protocol->getStatusHeader($response->getStatus()); |
|
| 160 | - $out[1] = array_merge($response->getHeaders()); |
|
| 161 | - $out[2] = $response->getCookies(); |
|
| 162 | - $out[3] = $this->middlewareDispatcher->beforeOutput( |
|
| 163 | - $controller, $methodName, $response->render() |
|
| 164 | - ); |
|
| 165 | - $out[4] = $response; |
|
| 166 | - |
|
| 167 | - return $out; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Uses the reflected parameters, types and request parameters to execute |
|
| 173 | - * the controller |
|
| 174 | - * @param Controller $controller the controller to be executed |
|
| 175 | - * @param string $methodName the method on the controller that should be executed |
|
| 176 | - * @return Response |
|
| 177 | - */ |
|
| 178 | - private function executeController(Controller $controller, string $methodName): Response { |
|
| 179 | - $arguments = []; |
|
| 180 | - |
|
| 181 | - // valid types that will be cast |
|
| 182 | - $types = ['int', 'integer', 'bool', 'boolean', 'float', 'double']; |
|
| 183 | - |
|
| 184 | - foreach ($this->reflector->getParameters() as $param => $default) { |
|
| 185 | - // try to get the parameter from the request object and cast |
|
| 186 | - // it to the type annotated in the @param annotation |
|
| 187 | - $value = $this->request->getParam($param, $default); |
|
| 188 | - $type = $this->reflector->getType($param); |
|
| 189 | - |
|
| 190 | - // Converted the string `'false'` to false when the controller wants a boolean |
|
| 191 | - if ($value === 'false' && ($type === 'bool' || $type === 'boolean')) { |
|
| 192 | - $value = false; |
|
| 193 | - } elseif ($value !== null && \in_array($type, $types, true)) { |
|
| 194 | - settype($value, $type); |
|
| 195 | - $this->ensureParameterValueSatisfiesRange($param, $value); |
|
| 196 | - } elseif ($value === null && $type !== null && $this->appContainer->has($type)) { |
|
| 197 | - $value = $this->appContainer->get($type); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - $arguments[] = $value; |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - $this->eventLogger->start('controller:' . get_class($controller) . '::' . $methodName, 'App framework controller execution'); |
|
| 204 | - try { |
|
| 205 | - $response = \call_user_func_array([$controller, $methodName], $arguments); |
|
| 206 | - } catch (\TypeError $e) { |
|
| 207 | - // Only intercept TypeErrors occurring on the first line, meaning that the invocation of the controller method failed. |
|
| 208 | - // Any other TypeError happens inside the controller method logic and should be logged as normal. |
|
| 209 | - if ($e->getFile() === $this->reflector->getFile() && $e->getLine() === $this->reflector->getStartLine()) { |
|
| 210 | - $this->logger->debug('Failed to call controller method: ' . $e->getMessage(), ['exception' => $e]); |
|
| 211 | - return new Response(Http::STATUS_BAD_REQUEST); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - throw $e; |
|
| 215 | - } |
|
| 216 | - $this->eventLogger->end('controller:' . get_class($controller) . '::' . $methodName); |
|
| 217 | - |
|
| 218 | - if (!($response instanceof Response)) { |
|
| 219 | - $this->logger->debug($controller::class . '::' . $methodName . ' returned raw data. Please wrap it in a Response or one of it\'s inheritors.'); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - // format response |
|
| 223 | - if ($response instanceof DataResponse || !($response instanceof Response)) { |
|
| 224 | - $format = $this->request->getFormat(); |
|
| 225 | - if ($format !== null && $controller->isResponderRegistered($format)) { |
|
| 226 | - $response = $controller->buildResponse($response, $format); |
|
| 227 | - } else { |
|
| 228 | - $response = $controller->buildResponse($response); |
|
| 229 | - } |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - return $response; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * @psalm-param mixed $value |
|
| 237 | - * @throws ParameterOutOfRangeException |
|
| 238 | - */ |
|
| 239 | - private function ensureParameterValueSatisfiesRange(string $param, $value): void { |
|
| 240 | - $rangeInfo = $this->reflector->getRange($param); |
|
| 241 | - if ($rangeInfo) { |
|
| 242 | - if ($value < $rangeInfo['min'] || $value > $rangeInfo['max']) { |
|
| 243 | - throw new ParameterOutOfRangeException( |
|
| 244 | - $param, |
|
| 245 | - $value, |
|
| 246 | - $rangeInfo['min'], |
|
| 247 | - $rangeInfo['max'], |
|
| 248 | - ); |
|
| 249 | - } |
|
| 250 | - } |
|
| 251 | - } |
|
| 29 | + /** @var MiddlewareDispatcher */ |
|
| 30 | + private $middlewareDispatcher; |
|
| 31 | + |
|
| 32 | + /** @var Http */ |
|
| 33 | + private $protocol; |
|
| 34 | + |
|
| 35 | + /** @var ControllerMethodReflector */ |
|
| 36 | + private $reflector; |
|
| 37 | + |
|
| 38 | + /** @var IRequest */ |
|
| 39 | + private $request; |
|
| 40 | + |
|
| 41 | + /** @var IConfig */ |
|
| 42 | + private $config; |
|
| 43 | + |
|
| 44 | + /** @var ConnectionAdapter */ |
|
| 45 | + private $connection; |
|
| 46 | + |
|
| 47 | + /** @var LoggerInterface */ |
|
| 48 | + private $logger; |
|
| 49 | + |
|
| 50 | + /** @var IEventLogger */ |
|
| 51 | + private $eventLogger; |
|
| 52 | + |
|
| 53 | + private ContainerInterface $appContainer; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * @param Http $protocol the http protocol with contains all status headers |
|
| 57 | + * @param MiddlewareDispatcher $middlewareDispatcher the dispatcher which |
|
| 58 | + * runs the middleware |
|
| 59 | + * @param ControllerMethodReflector $reflector the reflector that is used to inject |
|
| 60 | + * the arguments for the controller |
|
| 61 | + * @param IRequest $request the incoming request |
|
| 62 | + * @param IConfig $config |
|
| 63 | + * @param ConnectionAdapter $connection |
|
| 64 | + * @param LoggerInterface $logger |
|
| 65 | + * @param IEventLogger $eventLogger |
|
| 66 | + */ |
|
| 67 | + public function __construct( |
|
| 68 | + Http $protocol, |
|
| 69 | + MiddlewareDispatcher $middlewareDispatcher, |
|
| 70 | + ControllerMethodReflector $reflector, |
|
| 71 | + IRequest $request, |
|
| 72 | + IConfig $config, |
|
| 73 | + ConnectionAdapter $connection, |
|
| 74 | + LoggerInterface $logger, |
|
| 75 | + IEventLogger $eventLogger, |
|
| 76 | + ContainerInterface $appContainer, |
|
| 77 | + ) { |
|
| 78 | + $this->protocol = $protocol; |
|
| 79 | + $this->middlewareDispatcher = $middlewareDispatcher; |
|
| 80 | + $this->reflector = $reflector; |
|
| 81 | + $this->request = $request; |
|
| 82 | + $this->config = $config; |
|
| 83 | + $this->connection = $connection; |
|
| 84 | + $this->logger = $logger; |
|
| 85 | + $this->eventLogger = $eventLogger; |
|
| 86 | + $this->appContainer = $appContainer; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Handles a request and calls the dispatcher on the controller |
|
| 92 | + * @param Controller $controller the controller which will be called |
|
| 93 | + * @param string $methodName the method name which will be called on |
|
| 94 | + * the controller |
|
| 95 | + * @return array $array[0] contains the http status header as a string, |
|
| 96 | + * $array[1] contains response headers as an array, |
|
| 97 | + * $array[2] contains response cookies as an array, |
|
| 98 | + * $array[3] contains the response output as a string, |
|
| 99 | + * $array[4] contains the response object |
|
| 100 | + * @throws \Exception |
|
| 101 | + */ |
|
| 102 | + public function dispatch(Controller $controller, string $methodName): array { |
|
| 103 | + $out = [null, [], null]; |
|
| 104 | + |
|
| 105 | + try { |
|
| 106 | + // prefill reflector with everything that's needed for the |
|
| 107 | + // middlewares |
|
| 108 | + $this->reflector->reflect($controller, $methodName); |
|
| 109 | + |
|
| 110 | + $this->middlewareDispatcher->beforeController($controller, |
|
| 111 | + $methodName); |
|
| 112 | + |
|
| 113 | + $databaseStatsBefore = []; |
|
| 114 | + if ($this->config->getSystemValueBool('debug', false)) { |
|
| 115 | + $databaseStatsBefore = $this->connection->getInner()->getStats(); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + $response = $this->executeController($controller, $methodName); |
|
| 119 | + |
|
| 120 | + if (!empty($databaseStatsBefore)) { |
|
| 121 | + $databaseStatsAfter = $this->connection->getInner()->getStats(); |
|
| 122 | + $numBuilt = $databaseStatsAfter['built'] - $databaseStatsBefore['built']; |
|
| 123 | + $numExecuted = $databaseStatsAfter['executed'] - $databaseStatsBefore['executed']; |
|
| 124 | + |
|
| 125 | + if ($numBuilt > 50) { |
|
| 126 | + $this->logger->debug('Controller {class}::{method} created {count} QueryBuilder objects, please check if they are created inside a loop by accident.', [ |
|
| 127 | + 'class' => get_class($controller), |
|
| 128 | + 'method' => $methodName, |
|
| 129 | + 'count' => $numBuilt, |
|
| 130 | + ]); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + if ($numExecuted > 100) { |
|
| 134 | + $this->logger->warning('Controller {class}::{method} executed {count} queries.', [ |
|
| 135 | + 'class' => get_class($controller), |
|
| 136 | + 'method' => $methodName, |
|
| 137 | + 'count' => $numExecuted, |
|
| 138 | + ]); |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + // if an exception appears, the middleware checks if it can handle the |
|
| 143 | + // exception and creates a response. If no response is created, it is |
|
| 144 | + // assumed that there's no middleware who can handle it and the error is |
|
| 145 | + // thrown again |
|
| 146 | + } catch (\Exception $exception) { |
|
| 147 | + $response = $this->middlewareDispatcher->afterException( |
|
| 148 | + $controller, $methodName, $exception); |
|
| 149 | + } catch (\Throwable $throwable) { |
|
| 150 | + $exception = new \Exception($throwable->getMessage() . ' in file \'' . $throwable->getFile() . '\' line ' . $throwable->getLine(), $throwable->getCode(), $throwable); |
|
| 151 | + $response = $this->middlewareDispatcher->afterException( |
|
| 152 | + $controller, $methodName, $exception); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + $response = $this->middlewareDispatcher->afterController( |
|
| 156 | + $controller, $methodName, $response); |
|
| 157 | + |
|
| 158 | + // depending on the cache object the headers need to be changed |
|
| 159 | + $out[0] = $this->protocol->getStatusHeader($response->getStatus()); |
|
| 160 | + $out[1] = array_merge($response->getHeaders()); |
|
| 161 | + $out[2] = $response->getCookies(); |
|
| 162 | + $out[3] = $this->middlewareDispatcher->beforeOutput( |
|
| 163 | + $controller, $methodName, $response->render() |
|
| 164 | + ); |
|
| 165 | + $out[4] = $response; |
|
| 166 | + |
|
| 167 | + return $out; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Uses the reflected parameters, types and request parameters to execute |
|
| 173 | + * the controller |
|
| 174 | + * @param Controller $controller the controller to be executed |
|
| 175 | + * @param string $methodName the method on the controller that should be executed |
|
| 176 | + * @return Response |
|
| 177 | + */ |
|
| 178 | + private function executeController(Controller $controller, string $methodName): Response { |
|
| 179 | + $arguments = []; |
|
| 180 | + |
|
| 181 | + // valid types that will be cast |
|
| 182 | + $types = ['int', 'integer', 'bool', 'boolean', 'float', 'double']; |
|
| 183 | + |
|
| 184 | + foreach ($this->reflector->getParameters() as $param => $default) { |
|
| 185 | + // try to get the parameter from the request object and cast |
|
| 186 | + // it to the type annotated in the @param annotation |
|
| 187 | + $value = $this->request->getParam($param, $default); |
|
| 188 | + $type = $this->reflector->getType($param); |
|
| 189 | + |
|
| 190 | + // Converted the string `'false'` to false when the controller wants a boolean |
|
| 191 | + if ($value === 'false' && ($type === 'bool' || $type === 'boolean')) { |
|
| 192 | + $value = false; |
|
| 193 | + } elseif ($value !== null && \in_array($type, $types, true)) { |
|
| 194 | + settype($value, $type); |
|
| 195 | + $this->ensureParameterValueSatisfiesRange($param, $value); |
|
| 196 | + } elseif ($value === null && $type !== null && $this->appContainer->has($type)) { |
|
| 197 | + $value = $this->appContainer->get($type); |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + $arguments[] = $value; |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + $this->eventLogger->start('controller:' . get_class($controller) . '::' . $methodName, 'App framework controller execution'); |
|
| 204 | + try { |
|
| 205 | + $response = \call_user_func_array([$controller, $methodName], $arguments); |
|
| 206 | + } catch (\TypeError $e) { |
|
| 207 | + // Only intercept TypeErrors occurring on the first line, meaning that the invocation of the controller method failed. |
|
| 208 | + // Any other TypeError happens inside the controller method logic and should be logged as normal. |
|
| 209 | + if ($e->getFile() === $this->reflector->getFile() && $e->getLine() === $this->reflector->getStartLine()) { |
|
| 210 | + $this->logger->debug('Failed to call controller method: ' . $e->getMessage(), ['exception' => $e]); |
|
| 211 | + return new Response(Http::STATUS_BAD_REQUEST); |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + throw $e; |
|
| 215 | + } |
|
| 216 | + $this->eventLogger->end('controller:' . get_class($controller) . '::' . $methodName); |
|
| 217 | + |
|
| 218 | + if (!($response instanceof Response)) { |
|
| 219 | + $this->logger->debug($controller::class . '::' . $methodName . ' returned raw data. Please wrap it in a Response or one of it\'s inheritors.'); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + // format response |
|
| 223 | + if ($response instanceof DataResponse || !($response instanceof Response)) { |
|
| 224 | + $format = $this->request->getFormat(); |
|
| 225 | + if ($format !== null && $controller->isResponderRegistered($format)) { |
|
| 226 | + $response = $controller->buildResponse($response, $format); |
|
| 227 | + } else { |
|
| 228 | + $response = $controller->buildResponse($response); |
|
| 229 | + } |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + return $response; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * @psalm-param mixed $value |
|
| 237 | + * @throws ParameterOutOfRangeException |
|
| 238 | + */ |
|
| 239 | + private function ensureParameterValueSatisfiesRange(string $param, $value): void { |
|
| 240 | + $rangeInfo = $this->reflector->getRange($param); |
|
| 241 | + if ($rangeInfo) { |
|
| 242 | + if ($value < $rangeInfo['min'] || $value > $rangeInfo['max']) { |
|
| 243 | + throw new ParameterOutOfRangeException( |
|
| 244 | + $param, |
|
| 245 | + $value, |
|
| 246 | + $rangeInfo['min'], |
|
| 247 | + $rangeInfo['max'], |
|
| 248 | + ); |
|
| 249 | + } |
|
| 250 | + } |
|
| 251 | + } |
|
| 252 | 252 | } |
@@ -20,109 +20,109 @@ |
||
| 20 | 20 | use OCP\IRequest; |
| 21 | 21 | |
| 22 | 22 | class OCSMiddleware extends Middleware { |
| 23 | - /** @var IRequest */ |
|
| 24 | - private $request; |
|
| 25 | - |
|
| 26 | - /** @var int */ |
|
| 27 | - private $ocsVersion; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @param IRequest $request |
|
| 31 | - */ |
|
| 32 | - public function __construct(IRequest $request) { |
|
| 33 | - $this->request = $request; |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @param Controller $controller |
|
| 38 | - * @param string $methodName |
|
| 39 | - */ |
|
| 40 | - public function beforeController($controller, $methodName) { |
|
| 41 | - if ($controller instanceof OCSController) { |
|
| 42 | - if (substr_compare($this->request->getScriptName(), '/ocs/v2.php', -strlen('/ocs/v2.php')) === 0) { |
|
| 43 | - $this->ocsVersion = 2; |
|
| 44 | - } else { |
|
| 45 | - $this->ocsVersion = 1; |
|
| 46 | - } |
|
| 47 | - $controller->setOCSVersion($this->ocsVersion); |
|
| 48 | - } |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @param Controller $controller |
|
| 53 | - * @param string $methodName |
|
| 54 | - * @param \Exception $exception |
|
| 55 | - * @throws \Exception |
|
| 56 | - * @return BaseResponse |
|
| 57 | - */ |
|
| 58 | - public function afterException($controller, $methodName, \Exception $exception) { |
|
| 59 | - if ($controller instanceof OCSController && $exception instanceof OCSException) { |
|
| 60 | - $code = $exception->getCode(); |
|
| 61 | - if ($code === 0) { |
|
| 62 | - $code = \OCP\AppFramework\OCSController::RESPOND_UNKNOWN_ERROR; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - return $this->buildNewResponse($controller, $code, $exception->getMessage()); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - throw $exception; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * @param Controller $controller |
|
| 73 | - * @param string $methodName |
|
| 74 | - * @param Response $response |
|
| 75 | - * @return \OCP\AppFramework\Http\Response |
|
| 76 | - */ |
|
| 77 | - public function afterController($controller, $methodName, Response $response) { |
|
| 78 | - /* |
|
| 23 | + /** @var IRequest */ |
|
| 24 | + private $request; |
|
| 25 | + |
|
| 26 | + /** @var int */ |
|
| 27 | + private $ocsVersion; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @param IRequest $request |
|
| 31 | + */ |
|
| 32 | + public function __construct(IRequest $request) { |
|
| 33 | + $this->request = $request; |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @param Controller $controller |
|
| 38 | + * @param string $methodName |
|
| 39 | + */ |
|
| 40 | + public function beforeController($controller, $methodName) { |
|
| 41 | + if ($controller instanceof OCSController) { |
|
| 42 | + if (substr_compare($this->request->getScriptName(), '/ocs/v2.php', -strlen('/ocs/v2.php')) === 0) { |
|
| 43 | + $this->ocsVersion = 2; |
|
| 44 | + } else { |
|
| 45 | + $this->ocsVersion = 1; |
|
| 46 | + } |
|
| 47 | + $controller->setOCSVersion($this->ocsVersion); |
|
| 48 | + } |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @param Controller $controller |
|
| 53 | + * @param string $methodName |
|
| 54 | + * @param \Exception $exception |
|
| 55 | + * @throws \Exception |
|
| 56 | + * @return BaseResponse |
|
| 57 | + */ |
|
| 58 | + public function afterException($controller, $methodName, \Exception $exception) { |
|
| 59 | + if ($controller instanceof OCSController && $exception instanceof OCSException) { |
|
| 60 | + $code = $exception->getCode(); |
|
| 61 | + if ($code === 0) { |
|
| 62 | + $code = \OCP\AppFramework\OCSController::RESPOND_UNKNOWN_ERROR; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + return $this->buildNewResponse($controller, $code, $exception->getMessage()); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + throw $exception; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * @param Controller $controller |
|
| 73 | + * @param string $methodName |
|
| 74 | + * @param Response $response |
|
| 75 | + * @return \OCP\AppFramework\Http\Response |
|
| 76 | + */ |
|
| 77 | + public function afterController($controller, $methodName, Response $response) { |
|
| 78 | + /* |
|
| 79 | 79 | * If a different middleware has detected that a request unauthorized or forbidden |
| 80 | 80 | * we need to catch the response and convert it to a proper OCS response. |
| 81 | 81 | */ |
| 82 | - if ($controller instanceof OCSController && !($response instanceof BaseResponse)) { |
|
| 83 | - if ($response->getStatus() === Http::STATUS_UNAUTHORIZED) { |
|
| 84 | - $message = ''; |
|
| 85 | - if ($response instanceof JSONResponse) { |
|
| 86 | - /** @var DataResponse $response */ |
|
| 87 | - $message = $response->getData()['message']; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - return $this->buildNewResponse($controller, OCSController::RESPOND_UNAUTHORISED, $message); |
|
| 91 | - } |
|
| 92 | - if ($response->getStatus() === Http::STATUS_FORBIDDEN) { |
|
| 93 | - $message = ''; |
|
| 94 | - if ($response instanceof JSONResponse) { |
|
| 95 | - /** @var DataResponse $response */ |
|
| 96 | - $message = $response->getData()['message']; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - return $this->buildNewResponse($controller, Http::STATUS_FORBIDDEN, $message); |
|
| 100 | - } |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - return $response; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * @param Controller $controller |
|
| 108 | - * @param int $code |
|
| 109 | - * @param string $message |
|
| 110 | - * @return V1Response|V2Response |
|
| 111 | - */ |
|
| 112 | - private function buildNewResponse(Controller $controller, $code, $message) { |
|
| 113 | - $format = $this->request->getFormat(); |
|
| 114 | - if ($format === null || !$controller->isResponderRegistered($format)) { |
|
| 115 | - $format = 'xml'; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - $data = new DataResponse(); |
|
| 119 | - $data->setStatus($code); |
|
| 120 | - if ($this->ocsVersion === 1) { |
|
| 121 | - $response = new V1Response($data, $format, $message); |
|
| 122 | - } else { |
|
| 123 | - $response = new V2Response($data, $format, $message); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - return $response; |
|
| 127 | - } |
|
| 82 | + if ($controller instanceof OCSController && !($response instanceof BaseResponse)) { |
|
| 83 | + if ($response->getStatus() === Http::STATUS_UNAUTHORIZED) { |
|
| 84 | + $message = ''; |
|
| 85 | + if ($response instanceof JSONResponse) { |
|
| 86 | + /** @var DataResponse $response */ |
|
| 87 | + $message = $response->getData()['message']; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + return $this->buildNewResponse($controller, OCSController::RESPOND_UNAUTHORISED, $message); |
|
| 91 | + } |
|
| 92 | + if ($response->getStatus() === Http::STATUS_FORBIDDEN) { |
|
| 93 | + $message = ''; |
|
| 94 | + if ($response instanceof JSONResponse) { |
|
| 95 | + /** @var DataResponse $response */ |
|
| 96 | + $message = $response->getData()['message']; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + return $this->buildNewResponse($controller, Http::STATUS_FORBIDDEN, $message); |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + return $response; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * @param Controller $controller |
|
| 108 | + * @param int $code |
|
| 109 | + * @param string $message |
|
| 110 | + * @return V1Response|V2Response |
|
| 111 | + */ |
|
| 112 | + private function buildNewResponse(Controller $controller, $code, $message) { |
|
| 113 | + $format = $this->request->getFormat(); |
|
| 114 | + if ($format === null || !$controller->isResponderRegistered($format)) { |
|
| 115 | + $format = 'xml'; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + $data = new DataResponse(); |
|
| 119 | + $data->setStatus($code); |
|
| 120 | + if ($this->ocsVersion === 1) { |
|
| 121 | + $response = new V1Response($data, $format, $message); |
|
| 122 | + } else { |
|
| 123 | + $response = new V2Response($data, $format, $message); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + return $response; |
|
| 127 | + } |
|
| 128 | 128 | } |
@@ -18,130 +18,130 @@ |
||
| 18 | 18 | * @since 6.0.0 |
| 19 | 19 | */ |
| 20 | 20 | abstract class Controller { |
| 21 | - /** |
|
| 22 | - * app name |
|
| 23 | - * @var string |
|
| 24 | - * @since 7.0.0 |
|
| 25 | - */ |
|
| 26 | - protected $appName; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * current request |
|
| 30 | - * @var \OCP\IRequest |
|
| 31 | - * @since 6.0.0 |
|
| 32 | - */ |
|
| 33 | - protected $request; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @var array<string, Closure> |
|
| 37 | - * @since 7.0.0 |
|
| 38 | - */ |
|
| 39 | - private $responders; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * constructor of the controller |
|
| 43 | - * @param string $appName the name of the app |
|
| 44 | - * @param IRequest $request an instance of the request |
|
| 45 | - * @since 6.0.0 - parameter $appName was added in 7.0.0 - parameter $app was removed in 7.0.0 |
|
| 46 | - */ |
|
| 47 | - public function __construct($appName, |
|
| 48 | - IRequest $request) { |
|
| 49 | - $this->appName = $appName; |
|
| 50 | - $this->request = $request; |
|
| 51 | - |
|
| 52 | - // default responders |
|
| 53 | - $this->responders = [ |
|
| 54 | - 'json' => function ($data) { |
|
| 55 | - if ($data instanceof DataResponse) { |
|
| 56 | - $response = new JSONResponse( |
|
| 57 | - $data->getData(), |
|
| 58 | - $data->getStatus() |
|
| 59 | - ); |
|
| 60 | - $dataHeaders = $data->getHeaders(); |
|
| 61 | - $headers = $response->getHeaders(); |
|
| 62 | - // do not overwrite Content-Type if it already exists |
|
| 63 | - if (isset($dataHeaders['Content-Type'])) { |
|
| 64 | - unset($headers['Content-Type']); |
|
| 65 | - } |
|
| 66 | - $response->setHeaders(array_merge($dataHeaders, $headers)); |
|
| 67 | - |
|
| 68 | - if ($data->getETag() !== null) { |
|
| 69 | - $response->setETag($data->getETag()); |
|
| 70 | - } |
|
| 71 | - if ($data->getLastModified() !== null) { |
|
| 72 | - $response->setLastModified($data->getLastModified()); |
|
| 73 | - } |
|
| 74 | - if ($data->isThrottled()) { |
|
| 75 | - $response->throttle($data->getThrottleMetadata()); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - return $response; |
|
| 79 | - } |
|
| 80 | - return new JSONResponse($data); |
|
| 81 | - } |
|
| 82 | - ]; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Parses an HTTP accept header and returns the supported responder type |
|
| 88 | - * @param string $acceptHeader |
|
| 89 | - * @param string $default |
|
| 90 | - * @return string the responder type |
|
| 91 | - * @since 7.0.0 |
|
| 92 | - * @since 9.1.0 Added default parameter |
|
| 93 | - * @deprecated 33.0.0 Use {@see \OCP\IRequest::getFormat} instead |
|
| 94 | - */ |
|
| 95 | - public function getResponderByHTTPHeader($acceptHeader, $default = 'json') { |
|
| 96 | - $headers = explode(',', $acceptHeader); |
|
| 97 | - |
|
| 98 | - // return the first matching responder |
|
| 99 | - foreach ($headers as $header) { |
|
| 100 | - $header = strtolower(trim($header)); |
|
| 101 | - |
|
| 102 | - $responder = str_replace('application/', '', $header); |
|
| 103 | - |
|
| 104 | - if (array_key_exists($responder, $this->responders)) { |
|
| 105 | - return $responder; |
|
| 106 | - } |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - // no matching header return default |
|
| 110 | - return $default; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Registers a formatter for a type |
|
| 116 | - * @param string $format |
|
| 117 | - * @param Closure $responder |
|
| 118 | - * @since 7.0.0 |
|
| 119 | - */ |
|
| 120 | - protected function registerResponder($format, Closure $responder) { |
|
| 121 | - $this->responders[$format] = $responder; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Serializes and formats a response |
|
| 127 | - * @param mixed $response the value that was returned from a controller and |
|
| 128 | - * is not a Response instance |
|
| 129 | - * @param string $format the format for which a formatter has been registered |
|
| 130 | - * @throws \DomainException if format does not match a registered formatter |
|
| 131 | - * @return Response |
|
| 132 | - * @since 7.0.0 |
|
| 133 | - */ |
|
| 134 | - public function buildResponse($response, $format = 'json') { |
|
| 135 | - if (array_key_exists($format, $this->responders)) { |
|
| 136 | - $responder = $this->responders[$format]; |
|
| 137 | - |
|
| 138 | - return $responder($response); |
|
| 139 | - } |
|
| 140 | - throw new \DomainException('No responder registered for format ' |
|
| 141 | - . $format . '!'); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - public function isResponderRegistered(string $responder): bool { |
|
| 145 | - return isset($this->responders[$responder]); |
|
| 146 | - } |
|
| 21 | + /** |
|
| 22 | + * app name |
|
| 23 | + * @var string |
|
| 24 | + * @since 7.0.0 |
|
| 25 | + */ |
|
| 26 | + protected $appName; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * current request |
|
| 30 | + * @var \OCP\IRequest |
|
| 31 | + * @since 6.0.0 |
|
| 32 | + */ |
|
| 33 | + protected $request; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @var array<string, Closure> |
|
| 37 | + * @since 7.0.0 |
|
| 38 | + */ |
|
| 39 | + private $responders; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * constructor of the controller |
|
| 43 | + * @param string $appName the name of the app |
|
| 44 | + * @param IRequest $request an instance of the request |
|
| 45 | + * @since 6.0.0 - parameter $appName was added in 7.0.0 - parameter $app was removed in 7.0.0 |
|
| 46 | + */ |
|
| 47 | + public function __construct($appName, |
|
| 48 | + IRequest $request) { |
|
| 49 | + $this->appName = $appName; |
|
| 50 | + $this->request = $request; |
|
| 51 | + |
|
| 52 | + // default responders |
|
| 53 | + $this->responders = [ |
|
| 54 | + 'json' => function ($data) { |
|
| 55 | + if ($data instanceof DataResponse) { |
|
| 56 | + $response = new JSONResponse( |
|
| 57 | + $data->getData(), |
|
| 58 | + $data->getStatus() |
|
| 59 | + ); |
|
| 60 | + $dataHeaders = $data->getHeaders(); |
|
| 61 | + $headers = $response->getHeaders(); |
|
| 62 | + // do not overwrite Content-Type if it already exists |
|
| 63 | + if (isset($dataHeaders['Content-Type'])) { |
|
| 64 | + unset($headers['Content-Type']); |
|
| 65 | + } |
|
| 66 | + $response->setHeaders(array_merge($dataHeaders, $headers)); |
|
| 67 | + |
|
| 68 | + if ($data->getETag() !== null) { |
|
| 69 | + $response->setETag($data->getETag()); |
|
| 70 | + } |
|
| 71 | + if ($data->getLastModified() !== null) { |
|
| 72 | + $response->setLastModified($data->getLastModified()); |
|
| 73 | + } |
|
| 74 | + if ($data->isThrottled()) { |
|
| 75 | + $response->throttle($data->getThrottleMetadata()); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + return $response; |
|
| 79 | + } |
|
| 80 | + return new JSONResponse($data); |
|
| 81 | + } |
|
| 82 | + ]; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Parses an HTTP accept header and returns the supported responder type |
|
| 88 | + * @param string $acceptHeader |
|
| 89 | + * @param string $default |
|
| 90 | + * @return string the responder type |
|
| 91 | + * @since 7.0.0 |
|
| 92 | + * @since 9.1.0 Added default parameter |
|
| 93 | + * @deprecated 33.0.0 Use {@see \OCP\IRequest::getFormat} instead |
|
| 94 | + */ |
|
| 95 | + public function getResponderByHTTPHeader($acceptHeader, $default = 'json') { |
|
| 96 | + $headers = explode(',', $acceptHeader); |
|
| 97 | + |
|
| 98 | + // return the first matching responder |
|
| 99 | + foreach ($headers as $header) { |
|
| 100 | + $header = strtolower(trim($header)); |
|
| 101 | + |
|
| 102 | + $responder = str_replace('application/', '', $header); |
|
| 103 | + |
|
| 104 | + if (array_key_exists($responder, $this->responders)) { |
|
| 105 | + return $responder; |
|
| 106 | + } |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + // no matching header return default |
|
| 110 | + return $default; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Registers a formatter for a type |
|
| 116 | + * @param string $format |
|
| 117 | + * @param Closure $responder |
|
| 118 | + * @since 7.0.0 |
|
| 119 | + */ |
|
| 120 | + protected function registerResponder($format, Closure $responder) { |
|
| 121 | + $this->responders[$format] = $responder; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Serializes and formats a response |
|
| 127 | + * @param mixed $response the value that was returned from a controller and |
|
| 128 | + * is not a Response instance |
|
| 129 | + * @param string $format the format for which a formatter has been registered |
|
| 130 | + * @throws \DomainException if format does not match a registered formatter |
|
| 131 | + * @return Response |
|
| 132 | + * @since 7.0.0 |
|
| 133 | + */ |
|
| 134 | + public function buildResponse($response, $format = 'json') { |
|
| 135 | + if (array_key_exists($format, $this->responders)) { |
|
| 136 | + $responder = $this->responders[$format]; |
|
| 137 | + |
|
| 138 | + return $responder($response); |
|
| 139 | + } |
|
| 140 | + throw new \DomainException('No responder registered for format ' |
|
| 141 | + . $format . '!'); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + public function isResponderRegistered(string $responder): bool { |
|
| 145 | + return isset($this->responders[$responder]); |
|
| 146 | + } |
|
| 147 | 147 | } |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | |
| 52 | 52 | // default responders |
| 53 | 53 | $this->responders = [ |
| 54 | - 'json' => function ($data) { |
|
| 54 | + 'json' => function($data) { |
|
| 55 | 55 | if ($data instanceof DataResponse) { |
| 56 | 56 | $response = new JSONResponse( |
| 57 | 57 | $data->getData(), |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | return $responder($response); |
| 139 | 139 | } |
| 140 | 140 | throw new \DomainException('No responder registered for format ' |
| 141 | - . $format . '!'); |
|
| 141 | + . $format.'!'); |
|
| 142 | 142 | } |
| 143 | 143 | |
| 144 | 144 | public function isResponderRegistered(string $responder): bool { |