Esempio n. 1
0
 /**
  * The main method that handles the thread in a separate context.
  *
  * @return void
  */
 public function run()
 {
     try {
         // register the default autoloader
         require SERVER_AUTOLOADER;
         // register shutdown handler
         register_shutdown_function(array(&$this, "shutdown"));
         // synchronize the application instance and register the class loaders
         $application = $this->application;
         $application->registerClassLoaders();
         // synchronize the valves, servlet request/response
         $valves = $this->valves;
         $servletRequest = $this->servletRequest;
         // initialize servlet session, request + response
         $servletResponse = new Response();
         $servletResponse->init();
         // we initialize this with a 500 to handle 'Fatal Error' case
         $this->statusCode = 500;
         // initialize arrays for header and cookies
         $this->state = $servletResponse->getState();
         $this->version = $servletResponse->getVersion();
         $this->headers = $servletResponse->getHeaders();
         $this->cookies = $servletResponse->getCookies();
         // inject the sapplication and servlet response
         $servletRequest->injectResponse($servletResponse);
         $servletRequest->injectContext($application);
         // prepare the request instance
         $servletRequest->prepare();
         // initialize static request and application context
         RequestHandler::$requestContext = $servletRequest;
         RequestHandler::$applicationContext = $application;
         // process the valves
         foreach ($valves as $valve) {
             $valve->invoke($servletRequest, $servletResponse);
             if ($servletRequest->isDispatched() === true) {
                 break;
             }
         }
         // profile the request if the profile logger is available
         if ($profileLogger = $application->getInitialContext()->getLogger(LoggerUtils::PROFILE)) {
             $profileLogger->appendThreadContext('request-handler');
             $profileLogger->debug($servletRequest->getUri());
         }
     } catch (\Exception $e) {
         // log the exception
         $application->getInitialContext()->getSystemLogger()->error($e->__toString());
         // ATTENTION: We MUST wrap the exception, because it's possible that
         //            the exception contains not serializable data that will
         //            lead to a white page!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
         $this->exception = new ServletException($e, $e->getCode());
     }
     // copy the the response values
     $this->statusCode = $servletResponse->getStatusCode();
     $this->statusReasonPhrase = $servletResponse->getStatusReasonPhrase();
     $this->version = $servletResponse->getVersion();
     $this->state = $servletResponse->getState();
     // copy the content of the body stream
     $this->bodyStream = $servletResponse->getBodyStream();
     // copy headers and cookies
     $this->headers = $servletResponse->getHeaders();
     $this->cookies = $servletResponse->getCookies();
 }
Esempio n. 2
0
 /**
  * The main method that handles the thread in a separate context.
  *
  * @return void
  */
 public function run()
 {
     try {
         // register the default autoloader
         require SERVER_AUTOLOADER;
         // register shutdown handler
         set_error_handler(array(&$this, 'errorHandler'));
         register_shutdown_function(array(&$this, 'shutdown'));
         // initialize the array for the errors
         $this->errors = array();
         // synchronize the application instance and register the class loaders
         $application = $this->application;
         $application->registerClassLoaders();
         // synchronize the valves, servlet request/response
         $valves = $this->valves;
         $servletRequest = $this->servletRequest;
         $servletResponse = $this->servletResponse;
         // load the session and the authentication manager
         $sessionManager = $application->search(SessionManagerInterface::IDENTIFIER);
         $authenticationManager = $application->search(AuthenticationManagerInterface::IDENTIFIER);
         // inject the sapplication and servlet response
         $servletRequest->injectContext($application);
         $servletRequest->injectResponse($servletResponse);
         $servletRequest->injectSessionManager($sessionManager);
         $servletRequest->injectAuthenticationManager($authenticationManager);
         // prepare the request instance
         $servletRequest->prepare();
         // initialize static request and application context
         RequestHandler::$requestContext = $servletRequest;
         RequestHandler::$applicationContext = $application;
         // process the valves
         foreach ($valves as $valve) {
             $valve->invoke($servletRequest, $servletResponse);
             if ($servletRequest->isDispatched() === true) {
                 break;
             }
         }
         // profile the request if the profile logger is available
         if ($profileLogger = $application->getInitialContext()->getLogger(LoggerUtils::PROFILE)) {
             $profileLogger->appendThreadContext('request-handler');
             $profileLogger->debug($servletRequest->getUri());
         }
     } catch (\Exception $e) {
         $this->addError(ErrorUtil::singleton()->fromException($e));
     }
     // re-attach request and response instances
     $this->servletRequest = $servletRequest;
     $this->servletResponse = $servletResponse;
 }
 /**
  * Process servlet request.
  *
  * @param \AppserverIo\Psr\HttpMessage\RequestInterface          $request        A request object
  * @param \AppserverIo\Psr\HttpMessage\ResponseInterface         $response       A response object
  * @param \AppserverIo\Server\Interfaces\RequestContextInterface $requestContext A requests context instance
  * @param integer                                                $hook           The current hook to process logic for
  *
  * @return boolean
  *
  * @throws \AppserverIo\Server\Exceptions\ModuleException
  */
 public function process(RequestInterface $request, ResponseInterface $response, RequestContextInterface $requestContext, $hook)
 {
     // if false hook is coming do nothing
     if (ModuleHooks::REQUEST_POST !== $hook) {
         return;
     }
     // check if we are the handler that has to process this request
     if ($requestContext->getServerVar(ServerVars::SERVER_HANDLER) !== $this->getModuleName()) {
         return;
     }
     // load the application associated with this request
     $application = $this->findRequestedApplication($requestContext);
     $application->registerClassLoaders();
     // check if the application has already been connected
     if ($application->isConnected() === false) {
         throw new \Exception(sprintf('Application %s has not connected yet', $application->getName()), 503);
     }
     // create a copy of the valve instances
     $valves = $this->valves;
     $handlers = $this->handlers;
     // create a new request instance from the HTTP request
     $servletRequest = new Request();
     $servletRequest->injectHandlers($handlers);
     $servletRequest->injectHttpRequest($request);
     $servletRequest->injectServerVars($requestContext->getServerVars());
     $servletRequest->init();
     // initialize servlet response
     $servletResponse = new Response();
     $servletResponse->init();
     // load the session and the authentication manager
     $sessionManager = $application->search(SessionManagerInterface::IDENTIFIER);
     $authenticationManager = $application->search(AuthenticationManagerInterface::IDENTIFIER);
     // inject the sapplication and servlet response
     $servletRequest->injectContext($application);
     $servletRequest->injectResponse($servletResponse);
     $servletRequest->injectSessionManager($sessionManager);
     $servletRequest->injectAuthenticationManager($authenticationManager);
     // prepare the request instance
     $servletRequest->prepare();
     // initialize static request and application context
     RequestHandler::$requestContext = $servletRequest;
     RequestHandler::$applicationContext = $application;
     // process the valves
     foreach ($valves as $valve) {
         $valve->invoke($servletRequest, $servletResponse);
         if ($servletRequest->isDispatched() === true) {
             break;
         }
     }
     // copy response values to the HTTP response
     $response->setState($servletResponse->getState());
     $response->setVersion($servletResponse->getVersion());
     $response->setStatusCode($servletResponse->getStatusCode());
     $response->setStatusReasonPhrase($servletResponse->getStatusReasonPhrase());
     // copy the body content to the HTTP response
     $response->appendBodyStream($servletResponse->getBodyStream());
     // copy headers to the HTTP response
     foreach ($servletResponse->getHeaders() as $headerName => $headerValue) {
         $response->addHeader($headerName, $headerValue);
     }
     // copy cookies to the HTTP response
     $response->setCookies($servletResponse->getCookies());
     // append the servlet engine's signature
     $response->addHeader(Protocol::HEADER_X_POWERED_BY, get_class($this), true);
     // set response state to be dispatched after this without calling other modules process
     $response->setState(HttpResponseStates::DISPATCH);
 }