Пример #1
0
 /**
  * Expands request context on given request constellation (uri) based on file handler configuration
  *
  * @param \AppserverIo\Server\Interfaces\RequestContextInterface $requestContext The request context instance
  *
  * @return void
  */
 public function populateRequestContext(RequestContextInterface $requestContext)
 {
     // get local refs
     $serverContext = $this->getServerContext();
     // get document root
     $documentRoot = $requestContext->getServerVar(ServerVars::DOCUMENT_ROOT);
     // load the default handlers
     $handlers = $serverContext->getServerConfig()->getHandlers();
     // check if there are some volatile location definitions so use them and merge with global locations
     if ($requestContext->hasModuleVar(ModuleVars::VOLATILE_HANDLERS)) {
         $handlers = array_merge($handlers, $requestContext->getModuleVar(ModuleVars::VOLATILE_HANDLERS));
     }
     // get uri without querystring
     // Just make sure that you check for the existence of the query string first, as it might not be set
     $uriWithoutQueryString = parse_url($requestContext->getServerVar(ServerVars::X_REQUEST_URI), PHP_URL_PATH);
     // check if uri without query string is just "/"
     if ($uriWithoutQueryString === '/' && $requestContext->hasServerVar(ServerVars::SERVER_WELCOME_PAGE_TEMPLATE_PATH)) {
         // in this case we will set welcome page template to be errors template
         if ($welcomePageTemplate = $requestContext->getServerVar(ServerVars::SERVER_WELCOME_PAGE_TEMPLATE_PATH)) {
             $requestContext->setServerVar(ServerVars::SERVER_ERRORS_PAGE_TEMPLATE_PATH, $welcomePageTemplate);
         }
     }
     // split all path parts got from uri without query string
     $pathParts = explode('/', $uriWithoutQueryString);
     // init vars for path parsing
     $possibleValidPathExtension = '';
     $possibleValidPath = '';
     $pathInfo = '';
     $validDir = null;
     $scriptName = null;
     $scriptFilename = null;
     // note: only if file extension hits a filehandle info it will be possible to set path info etc...
     // iterate through all dirs beginning at 1 because 0 is always empty in this case
     for ($i = 1; $i < count($pathParts); ++$i) {
         // check if no script name was found yet
         if (!$scriptName) {
             // append valid path
             $possibleValidPath .= DIRECTORY_SEPARATOR . $pathParts[$i];
             // get possible extension
             $possibleValidPathExtension = pathinfo($possibleValidPath, PATHINFO_EXTENSION);
             // check if dir does not exists
             if (!is_dir($documentRoot . $possibleValidPath)) {
                 // check if its not a existing file
                 if (!is_file($documentRoot . $possibleValidPath)) {
                     // check if file handler is defined for that virtual file
                     if (isset($handlers['.' . $possibleValidPathExtension])) {
                         // set script name for further processing as script aspect
                         $scriptName = $possibleValidPath;
                     }
                 } else {
                     // set script name
                     $scriptName = $possibleValidPath;
                     // set script filename
                     $scriptFilename = $documentRoot . $scriptName;
                 }
             } else {
                 // save valid dir for indexed surfing later on
                 $validDir = $possibleValidPath;
             }
         } else {
             // else build up path info
             $pathInfo .= DIRECTORY_SEPARATOR . $pathParts[$i];
         }
     }
     // set special server var for requested file
     $requestContext->setServerVar(ServerVars::REQUEST_FILENAME, $documentRoot . $possibleValidPath);
     // set specific script name server var if exists
     if ($scriptName) {
         $requestContext->setServerVar(ServerVars::SCRIPT_NAME, $scriptName);
     }
     // check if requested file is on filesystem and set it to be valid script filename
     if ($scriptFilename) {
         $requestContext->setServerVar(ServerVars::SCRIPT_FILENAME, $scriptFilename);
     }
     // if path info is set put it into server vars
     if (strlen($pathInfo) > 0) {
         // set path info vars
         $requestContext->setServerVar(ServerVars::PATH_INFO, $pathInfo);
         $requestContext->setServerVar(ServerVars::PATH_TRANSLATED, $documentRoot . $pathInfo);
     }
     // first check if wildcard file handler was registered
     if (isset($handlers['.*'])) {
         // set wildcard filehandler which will overload all specific filehandlers at this point
         $possibleValidPathExtension = '*';
     }
     // check if file handler is defined for that script and expand request context
     if (isset($handlers['.' . $possibleValidPathExtension])) {
         // set the file handler to use for modules being able to react on this setting
         $requestContext->setServerVar(ServerVars::SERVER_HANDLER, $handlers['.' . $possibleValidPathExtension]['name']);
         // if file handler params are given, set them as module var
         if (isset($handlers['.' . $possibleValidPathExtension]['params'])) {
             $requestContext->setModuleVar(ModuleVars::VOLATILE_FILE_HANDLER_VARIABLES, $handlers['.' . $possibleValidPathExtension]['params']);
         }
     }
 }
Пример #2
0
 /**
  * Implements module logic for given hook
  *
  * @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 int                                                    $hook           The current hook to process logic for
  *
  * @return bool
  * @throws \AppserverIo\Server\Exceptions\ModuleException
  */
 public function process(RequestInterface $request, ResponseInterface $response, RequestContextInterface $requestContext, $hook)
 {
     /**
      * @var $request \AppserverIo\Psr\HttpMessage\RequestInterface
      */
     /**
      * @var $response \AppserverIo\Psr\HttpMessage\ResponseInterface
      */
     // if false hook is comming do nothing
     if (ModuleHooks::REQUEST_POST !== $hook) {
         return;
     }
     // load the locations
     $locations = $this->locations;
     // check if there are some volatile location definitions so use them and override global locations
     if ($requestContext->hasModuleVar(ModuleVars::VOLATILE_LOCATIONS)) {
         $locations = $requestContext->getModuleVar(ModuleVars::VOLATILE_LOCATIONS);
     }
     // query whether we've locations configured or not
     if (sizeof($locations) === 0) {
         return;
     }
     // initialize the array for the handlers
     $handlers = array();
     // initialize the array for the headers
     $headers = array();
     // load the actual request URI without query string
     $uriWithoutQueryString = $requestContext->getServerVar(ServerVars::X_REQUEST_URI);
     // process the all locations found for this request
     foreach ($locations as $location) {
         // query whether the location matches the acutal request URI
         if (preg_match('/' . $location['condition'] . '/', $uriWithoutQueryString)) {
             // query whether the location has file handlers configured for the actual URI
             if (isset($location['params'])) {
                 // iterate over all params and try to set as server var via mapping
                 foreach ($location['params'] as $paramName => $paramValue) {
                     // check if server var mapping exists
                     if (isset($this->paramServerVarsMap[$paramName])) {
                         // check if documentRoot is changed
                         if ($this->paramServerVarsMap[$paramName] === ServerVars::DOCUMENT_ROOT) {
                             // check if relative path is given and make is absolute by using cwd as prefix
                             if (substr($paramValue, 0, 1) !== "/") {
                                 $paramValue = getcwd() . DIRECTORY_SEPARATOR . $paramValue;
                             }
                         }
                         // set server var
                         $requestContext->setServerVar($this->paramServerVarsMap[$paramName], $paramValue);
                     }
                 }
             }
             // query whether the location has file handlers configured for the actual URI
             if (isset($location['handlers'])) {
                 $handlers = array_merge($handlers, $location['handlers']);
             }
             // merge headers information to volatile headers if exists
             if (isset($location['headers']) && is_array($location['headers'])) {
                 $volatileHeaders = array();
                 if ($requestContext->hasModuleVar(ModuleVars::VOLATILE_HEADERS)) {
                     $volatileHeaders = $requestContext->getModuleVar(ModuleVars::VOLATILE_HEADERS);
                 }
                 $headers = array_merge_recursive($volatileHeaders, $location['headers']);
             }
         }
     }
     // add the handlers we have (if any)
     if (sizeof($handlers) !== 0) {
         $requestContext->setModuleVar(ModuleVars::VOLATILE_HANDLERS, $handlers);
     }
     // add the headers we have (if any)
     if (sizeof($headers) !== 0) {
         $requestContext->setModuleVar(ModuleVars::VOLATILE_HEADERS, $headers);
     }
 }
Пример #3
0
 /**
  * Implements module logic for given hook
  *
  * @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 int                                                    $hook           The current hook to process logic for
  *
  * @return bool
  * @throws \AppserverIo\Server\Exceptions\ModuleException
  */
 public function process(RequestInterface $request, ResponseInterface $response, RequestContextInterface $requestContext, $hook)
 {
     // if false hook is comming do nothing
     if (ModuleHooks::REQUEST_POST !== $hook) {
         return;
     }
     // set req and res object internally
     $this->request = $request;
     $this->response = $response;
     $virtualHosts = $this->getServerContext()->getServerConfig()->getVirtualHosts();
     $serverName = $requestContext->getServerVar(ServerVars::SERVER_NAME);
     // check if current host matches any virtual host configuration
     if (isset($virtualHosts[$serverName])) {
         // read out params
         $params = $virtualHosts[$serverName]['params'];
         // iterate over all params and try to set as server var via mapping
         foreach ($params as $paramName => $paramValue) {
             // check if server var mapping exists
             if (isset($this->paramServerVarsMap[$paramName])) {
                 // check if documentRoot is changed
                 if ($this->paramServerVarsMap[$paramName] === ServerVars::DOCUMENT_ROOT) {
                     // check if relative path is given and make is absolute by using cwd as prefix
                     if (substr($paramValue, 0, 1) !== "/") {
                         $paramValue = getcwd() . DIRECTORY_SEPARATOR . $paramValue;
                     }
                 }
                 // set server var
                 $requestContext->setServerVar($this->paramServerVarsMap[$paramName], $paramValue);
             }
         }
         // Add the headers we have (if any) to the configuration's headers pool
         if (!empty($virtualHosts[$serverName]['headers'])) {
             // Set the rewrites we encountered as a temporary module var
             $requestContext->setModuleVar(ModuleVars::VOLATILE_HEADERS, $virtualHosts[$serverName]['headers']);
         }
         // Add the rewrites we have (if any) to the configuration's rewrite pool
         if (!empty($virtualHosts[$serverName]['rewrites'])) {
             // Set the rewrites we encountered as a temporary module var
             $requestContext->setModuleVar(ModuleVars::VOLATILE_REWRITES, $virtualHosts[$serverName]['rewrites']);
         }
         // Add the environment vars we have (if any) to the configuration's environment variable pool
         if (!empty($virtualHosts[$serverName]['environmentVariables'])) {
             // Set the environment variables we encountered as a temporary module var
             $requestContext->setModuleVar(ModuleVars::VOLATILE_ENVIRONMENT_VARIABLES, $virtualHosts[$serverName]['environmentVariables']);
         }
         // Add the accesses (if any) to the configuration's access pool
         if (!empty($virtualHosts[$serverName]['accesses'])) {
             // Set the environment variables we encountered as a temporary module var
             $requestContext->setModuleVar(ModuleVars::VOLATILE_ACCESSES, $virtualHosts[$serverName]['accesses']);
         }
         // add the analytics (if any) to the configuration's analytics pool
         if (!empty($virtualHosts[$serverName]['analytics'])) {
             // set the analytics we encountered as a temporary module var
             $requestContext->setModuleVar(ModuleVars::VOLATILE_ANALYTICS, $virtualHosts[$serverName]['analytics']);
         }
         // Add the locations we have (if any) to the configuration's location pool
         if (!empty($virtualHosts[$serverName]['locations'])) {
             // Set the locations we encountered as a temporary module var
             $requestContext->setModuleVar(ModuleVars::VOLATILE_LOCATIONS, $virtualHosts[$serverName]['locations']);
         }
         // Add the rewriteMaps we have (if any) to the configuration's rewriteMaps pool
         if (!empty($virtualHosts[$serverName]['rewriteMaps'])) {
             // Set the rewriteMaps we encountered as a temporary module var
             $requestContext->setModuleVar(ModuleVars::VOLATILE_REWRITE_MAPS, $virtualHosts[$serverName]['rewriteMaps']);
         }
         // Add the authentications we have (if any) to the configuration's authentications pool
         if (!empty($virtualHosts[$serverName]['authentications'])) {
             // Set the authentications we encountered as a temporary module var
             $requestContext->setModuleVar(ModuleVars::VOLATILE_AUTHENTICATIONS, $virtualHosts[$serverName]['authentications']);
         }
     }
 }