]> git.andy128k.dev Git - ipf.git/commitdiff
refactor http request
authorAndrey Kutejko <andy128k@gmail.com>
Mon, 15 Jul 2013 21:21:07 +0000 (00:21 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Mon, 15 Jul 2013 21:21:07 +0000 (00:21 +0300)
ipf/context.php
ipf/http/request.php
ipf/http/url.php
ipf/middleware/common.php
ipf/project.php
ipf/router.php

index bcc1d1d62713936b9fee1071562e0ea885fe1087..1b014ae8104994df65ade18b236a39b24c50a68e 100644 (file)
@@ -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);
 }
+
index a7ac0dabd44a1ed103a47ec269d76be9a76da2ad..9894080965efde4b8451293c8ab95fa50d98d422 100644 (file)
@@ -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;
     }
 }
+
index 5f6c843df9b5bf7350e3fdf800aa8d97094344e5..b4c60ee666d09228291b70dfa03b729623e94a68 100644 (file)
@@ -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);
index 389d3cd52a41ad2965886bfb9f0b3ae11a0aada7..7115d3d5d2154a9bbe47f6f03347c9afbbcbfc02 100644 (file)
@@ -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;
     }
index 3ba120526ad1d80b24c2717012ec4c538afbbad0..7a48770745e95abc0ba1095342a6eea9b7aab5d4 100644 (file)
@@ -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;
index 6af909e4563c8afc95e28aaa5e24cc363f9e3771..efc373d87f3ffcedc04b2e399e239eb9c496a94c 100644 (file)
@@ -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();