From: Andrey Kutejko Date: Mon, 15 Jul 2013 21:21:07 +0000 (+0300) Subject: refactor http request X-Git-Tag: 0.5~165 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=8f0980edb7503a4e73f71a3d44a4cc0d36e71d5f;p=ipf.git refactor http request --- diff --git a/ipf/context.php b/ipf/context.php index bcc1d1d..1b014ae 100644 --- a/ipf/context.php +++ b/ipf/context.php @@ -17,5 +17,6 @@ function IPF_Context_Upload($request) function IPF_Context_Current($request) { - return array('CURRENT_URL' => IPF_HTTP_URL::getAction()); + return array('CURRENT_URL' => $request->query); } + diff --git a/ipf/http/request.php b/ipf/http/request.php index a7ac0da..9894080 100644 --- a/ipf/http/request.php +++ b/ipf/http/request.php @@ -14,8 +14,9 @@ class IPF_HTTP_Request public $remote_addr = ''; public $http_host = ''; public $SERVER = array(); + public $is_secure = false; - function __construct($query) + public function __construct() { $http = new IPF_HTTP(); $http->removeTheMagic(); @@ -24,28 +25,33 @@ class IPF_HTTP_Request $this->REQUEST =& $_REQUEST; $this->COOKIE =& $_COOKIE; $this->FILES =& $_FILES; - $this->query = $query; $this->method = $_SERVER['REQUEST_METHOD']; $this->uri = $_SERVER['REQUEST_URI']; $this->remote_addr = $_SERVER['REMOTE_ADDR']; $this->http_host = $_SERVER['HTTP_HOST']; + + if (isset($_SERVER['REQUEST_URI'])) { + $uri = $_SERVER['REQUEST_URI']; + $pq = strpos($uri,'?'); + if ($pq !== false) + $uri = substr($uri, 0, $pq); + $this->query = preg_replace('#^(//+)#', '/', '/'.$uri); + } else { + $this->query = '/'; + } + if (isset($_SERVER['PATH_INFO'])) $this->path_info = $_SERVER['PATH_INFO']; else $this->path_info = '/'; $this->SERVER =& $_SERVER; + $this->is_secure = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off'; } - - function isSecure(){ - return false; // # FIXME - } - - function addUrlrotocol($uri){ - if ($this->isSecure()) - $proto = 'https'; - else - $proto = 'http'; - return $proto.'://'.$uri; + + public function absoluteUrl() + { + return ($this->is_secure ? 'https://' : 'http://') . $this->http_host . $this->query; } } + diff --git a/ipf/http/url.php b/ipf/http/url.php index 5f6c843..b4c60ee 100644 --- a/ipf/http/url.php +++ b/ipf/http/url.php @@ -21,18 +21,6 @@ class IPF_HTTP_URL return $url; } - public static function getAction() - { - if (isset($_SERVER['REQUEST_URI'])) { - $uri = $_SERVER['REQUEST_URI']; - $pq = strpos($uri,'?'); - if ($pq!==false) - $uri = substr($uri,0,$pq); - return $uri; - } - return '/'; - } - public static function urlForView($view, $params=array(), $get_params=array(), $encoded=true) { $action = IPF_Project::getInstance()->router->reverse($view, $params); diff --git a/ipf/middleware/common.php b/ipf/middleware/common.php index 389d3cd..7115d3d 100644 --- a/ipf/middleware/common.php +++ b/ipf/middleware/common.php @@ -5,11 +5,9 @@ class IPF_Middleware_Common function processRequest(&$request) { if (IPF::get('append_slash', true)) { - $url = $request->http_host . IPF_HTTP_URL::getAction(); - if (substr($url,-1)!='/') { - $url = $request->addUrlrotocol($url).'/'; - return new IPF_HTTP_Response_Redirect($url); - } + $url = $request->absoluteUrl(); + if (substr($url, -1) !== '/') + return new IPF_HTTP_Response_Redirect($url.'/'); } return false; } diff --git a/ipf/project.php b/ipf/project.php index 3ba1205..7a48770 100644 --- a/ipf/project.php +++ b/ipf/project.php @@ -106,7 +106,8 @@ final class IPF_Project $cli->run(); } else { $this->loadAllModels(); - $this->router->dispatch(IPF_HTTP_URL::getAction()); + $request = new IPF_HTTP_Request; + $this->router->dispatch($request); } return true; diff --git a/ipf/router.php b/ipf/router.php index 6af909e..efc373d 100644 --- a/ipf/router.php +++ b/ipf/router.php @@ -33,11 +33,9 @@ class IPF_Router return new IPF_HTTP_Response_ServerError($e); } - public function dispatch($query='') + public function dispatch($req) { - try{ - $query = preg_replace('#^(/)+#', '/', '/'.$query); - $req = new IPF_HTTP_Request($query); + try { $middleware = array(); foreach (IPF::get('middlewares', array()) as $mw) { $middleware[] = new $mw();