apply pre interceptors

Pre interceptors for actual resource are only applied when request is
authorized.

@param   \stubbles\webapp\Request   $request   current request
@param   \stubbles\webapp\Response  $response  response to send
@return  bool
551064
public function applyPreInterceptors(Request $request, Response $response): bool
    {
        if ($this->isAuthorized($request, $response)) {
            return $this->actualResource->applyPreInterceptors($request, $response);
        }

        return true;
    }
875650
public function notFound(Request $request, Response $response)
    {
        $handler = $this->container->get('notFoundHandler');

        return $handler($request, $response);
    }
522552
private function checkCsrf(Request $request, Response $response)
    {
        if (!$this->session->has($request, self::CSRF_KEY)) {
            return $this->errorResponse($request, $response, self::EXCEPTION_MISSING_IN_SESSION);
        }

        $data = $request->getParsedBody();

        if (!isset($data[self::CSRF_KEY])) {
            return $this->errorResponse($request, $response, self::EXCEPTION_MISSING_IN_BODY);
        }

        if ($this->session->get($request, self::CSRF_KEY) !== $data[self::CSRF_KEY]) {
            return $this->errorResponse($request, $response, self::EXCEPTION_IS_NOT_SAME);
        }

        return null;
    }
571186
public function set(&$request, &$response = null)
    {
        $this->request  = $request;
        $this->response = $response;
        return $this;
    }
340188
private function executeInterceptor($type, Request $request, Response $response) {
		foreach ($this->interceptor[$type] as $it) {
			if (isset($it['all'])) {
				if ($it['handler']->{$type}($request, $response) !== null) {
					return true;
				}
			} elseif (isset($it['uri'])) {
				if ($request->uri === $it['uri']) {
					if ($it['handler']->{$type}($request, $response) !== null) {
						return true;
					}
				}
			} else {
				if (preg_match($it['regex'], $request->uri)) {
					if ($it['handler']->{$type}($request, $response) !== null) {
						return true;
					}
				}
			}
		}
		return null;
	}
793614
private function executePostInterceptor($postInterceptor, Request $request, Response $response)
    {
        if (is_callable($postInterceptor)) {
            return $postInterceptor($request, $response);
        }

        if ($postInterceptor instanceof PostInterceptor) {
            return $postInterceptor->postProcess($request, $response);
        }

        $instance = $this->injector->getInstance($postInterceptor);
        if (!($instance instanceof PostInterceptor)) {
            $response->write(
                    $response->internalServerError(
                            'Configured post interceptor ' . $postInterceptor
                            . ' is not an instance of ' . PostInterceptor::class
                    )
            );
            return false;
        }

        return $instance->postProcess($request, $response);
    }
201152
public function dispatch(Request $request, Response $response) {
		$module = ucfirst($request->module);
		$controller = ucfirst($request->controller);
		$action = ucfirst($request->action);
		$response->setTemplate($controller . '/' . $action);
		$response->setTemplatePath(APP_PATH . 'Module/' . $module . '/View/');
		if (!empty($request->extension)) {
			$response->mimeType($request->extension);
		}
		try {
			$code = self::isValid($module, $controller, $action);
			if ($code === self::ROUTE_VALID) {
				$className = EntryUtil::controller($module, $controller);
				$clazz = Container::getInstance()->get($className);
				$actionName = $action . 'Action';
				$result = $clazz->$actionName($request, $response);
				if ($result !== null) {
					$response->result = $result;
				}
			} else {
				// Not found
				$request->status = $code;
			}
		} catch (\Throwable $e) {
			$request->status = $e;
		}
		$this->executeInterceptor('after', $request, $response);
		$request->end();
		$response->end();
		unset($request, $response);
	}
464082
public function resolve(Request $request, Response $response)
    {
        // Ensure to put *route* in before ;) - here.
        $route  = $request->getAttribute('route');
        $target = $route->getPresenter();

        $this
            ->response($response)
            ->route($route)
            ->className($target)
            ->action($route->getAction())
            ->initMvp($target, $this->getRegistry(), $request);

        return $this->run();
    }
1212914
protected function corsHandle($request, $response)
    {
        if (!$this->cors->hasAdded()) {
            $this->cors->setRequest('laravel', $request);
            $this->cors->setResponse('laravel', $response);
            $this->cors->handle();
        }

        return $this->cors->getResponse();
    }
1311869
private boolean preHandle(HttpRequest request, HttpResponse response, RequestHandler handler) throws Exception {
        for (HandlerInterceptor interceptor : mInterceptorList) {
            if (interceptor.onIntercept(request, response, handler)) return true;
        }
        return false;
    }
1007736
public function middleware($request, &$response)
    {
        $that = $this;
        if ($middleware = current($this->middleware)) {
            next($this->middleware);
            $middleware->next($request, $response, function ($request, $response) use ($that) {
                $that->middleware($request, $response);
            });
        }
    }
380373
protected function beforeDispatch(RequestInterface $request, ResponseInterface $response)
    {
        RequestContext::setRequest($request);
        RequestContext::setResponse($response);

        // Trigger 'Before Request' event
        App::trigger(HttpServerEvent::BEFORE_REQUEST);
    }
1151216
public function applyPostInterceptors(Request $request, Response $response): bool
    {
        return $this->interceptors->postProcess($request, $response);
    }
22086
private function enforcePreconditions(Response $response) : void
    {
        $result = $this->preconditionValidator->validate($response);

        if (!$result->isValid()) {
            throw PreconditionNotMetException::createFromValidationResult($result);
        }
    }
722429
public function handleUserInfoRequest(RequestInterface $request, ResponseInterface $response)
    {
        if (!$this->verifyResourceRequest($request, $response, 'openid')) {
            return;
        }

        $token = $this->getToken();
        $claims = $this->userClaimsStorage->getUserClaims($token['user_id'], $token['scope']);
        // The sub Claim MUST always be returned in the UserInfo Response.
        // http://openid.net/specs/openid-connect-core-1_0.html#UserInfoResponse
        $claims += array(
            'sub' => $token['user_id'],
        );
        $response->addParameters($claims);
    }
595934
public function preProcess(Request $request, Response $response): bool
    {
        foreach ($this->preInterceptors as $preInterceptor) {
            if (false === $this->executePreInterceptor($preInterceptor, $request, $response)) {
                return false;
            }
        }

        return true;
    }
49447
protected function _modifyAfterFetch($response, $request) {
        if (!$this->_methodFailed($response)) {
            $rows = $response['result'];
            $methodParts = explode('.', $request['method']);
            $modelClass  = Garp_Content_Api::modelAliasToClass(array_shift($methodParts));

            $params = $request['params'];
            $params = !empty($params) ? $params[0] : array();
            $response['result'] = array(
                'rows' => $rows,
                'total' => $this->_fetchTotal($modelClass, $params)
            );
        }
        return $response;
    }