コード例 #1
0
ファイル: HTRouter.php プロジェクト: newairhost/HTRouter
 /**
  * Initialize the request with standard values taken from the $_SERVER.
  *
  * @param HTRouter\Request $request The request to be filled
  */
 protected function _populateInitialRequest(\HTRouter\Request $request)
 {
     /**
      * A lot of stuff is already filtered by either apache or the built-in webserver. We just have to
      * populate our request so we have a generic state which we can work with. From this point on, it
      * should never matter on what kind of webserver we are actually working on (in fact: this can be
      * the base of writing your own webserver like nanoweb)
      */
     $routerConfig = $this->_getRouterConfig();
     // By default, we don't have any authentication
     //$request->setAuthType(null);
     $request->setUser("");
     // Query arguments
     if (isset($_SERVER['QUERY_STRING'])) {
         parse_str($_SERVER['QUERY_STRING'], $args);
     } else {
         $args = array();
     }
     $request->setArgs($args);
     $request->setContentEncoding("");
     $request->setContentLanguage("");
     $request->setContentType("text/plain");
     // @TODO: Find requesting file?
     // NOTE: Must be set before checking findUriOnDisk!
     if (isset($routerConfig['global']['documentroot'])) {
         $request->setDocumentRoot($routerConfig['global']['documentroot']);
     } else {
         $request->setDocumentRoot($_SERVER['DOCUMENT_ROOT']);
     }
     // Set INPUT headers
     foreach ($_SERVER as $key => $item) {
         if (!is_string($key)) {
             continue;
         }
         if (substr($key, 0, 5) != "HTTP_") {
             continue;
         }
         $key = substr($key, 5);
         $key = strtolower($key);
         $key = str_replace("_", "-", $key);
         $key = preg_replace_callback("/^(.)|-(.)/", function ($matches) {
             return strtoupper($matches[0]);
         }, $key);
         $request->appendInHeaders($key, $item);
     }
     /*
      * Apache does not send us the Authorization variable. So this piece of code checks if apache_request_headers
      * function is present (we are running the router from apache(compatible) browser), and add the authorization
      * header.
      */
     if (function_exists("apache_request_headers")) {
         $tmp = apache_request_headers();
         if (isset($tmp['Authorization'])) {
             $request->appendInHeaders('Authorization', $tmp['Authorization']);
         }
     }
     // We don't have the actual host-header, but we can use the http_host variable for this
     $tmp = parse_url($_SERVER['HTTP_HOST']);
     $request->setHostname(isset($tmp['host']) ? $tmp['host'] : $tmp['path']);
     $request->setMethod($_SERVER['REQUEST_METHOD']);
     $request->setProtocol($_SERVER['SERVER_PROTOCOL']);
     $request->setStatus(\HTRouter::STATUS_HTTP_OK);
     if (!isset($_SERVER['PATH_INFO'])) {
         $_SERVER['PATH_INFO'] = "";
     }
     // These are again, depending on the type of server. Strip the router.php if needed
     $request->setUnparsedUri($_SERVER['REQUEST_URI']);
     $request->setUri($_SERVER['SCRIPT_NAME'] . $_SERVER['PATH_INFO']);
     // Check if we need to remove our router info
     if (isset($routerConfig['global']['apacherouterprefix'])) {
         $routerName = $routerConfig['global']['apacherouterprefix'];
         if (strpos($_SERVER['REQUEST_URI'], $routerName) === 0) {
             $uri = substr($_SERVER['REQUEST_URI'], strlen($routerName));
             if ($uri === false) {
                 $uri = "/";
             }
             $request->setUnparsedUri($uri);
         }
         if (strpos($_SERVER['SCRIPT_NAME'] . $_SERVER['PATH_INFO'], $routerName) === 0) {
             $uri = substr($_SERVER['SCRIPT_NAME'] . $_SERVER['PATH_INFO'], strlen($routerName));
             if ($uri === false) {
                 $uri = "/";
             }
             $request->setUri($uri);
         }
     }
     // Let SetEnvIf etc do their thing
     $this->runHook(self::HOOK_POST_READ_REQUEST, self::RUNHOOK_ALL, $this->_container);
     $this->_getLogger()->log(\HTRouter\Logger::ERRORLEVEL_DEBUG, "Populating new request done");
 }