Browse Source

Merge pull request #480 from joanhey/cookie

Separate Set-Cookie from headers
walkor 6 years ago
parent
commit
dfefd61444
1 changed files with 29 additions and 24 deletions
  1. 29 24
      Protocols/Http.php

+ 29 - 24
Protocols/Http.php

@@ -90,6 +90,7 @@ class Http
         $GLOBALS['HTTP_RAW_POST_DATA'] = '';
         // Clear cache.
         HttpCache::$header   = HttpCache::$default;
+        HttpCache::$cookie   = array();
         HttpCache::$instance = new HttpCache();
         // $_SERVER
         $_SERVER = array(
@@ -239,18 +240,18 @@ class Http
         $header = (HttpCache::$status ?: 'HTTP/1.1 200 OK') . "\r\n";
         HttpCache::$status = '';
 
+        // Cookie headers
+        if(HttpCache::$cookie) {
+            $header .= \implode("\r\n", HttpCache::$cookie) . "\r\n";
+        }
+        
         // other headers
-        foreach (HttpCache::$header as $key => $item) {
-            if ('Set-Cookie' === $key && \is_array($item)) {
-                $header .= \implode("\r\n", $item) . "\r\n";
-            } else {
-                $header .= $item . "\r\n";
-            }
+        $header .= \implode("\r\n", HttpCache::$header) . "\r\n";
+
+        if(HttpCache::$gzip && isset($connection->gzip) && $connection->gzip){
+                $header .= "Content-Encoding: gzip\r\n";
+                $content = \gzencode($content,$connection->gzip);
         }
-		if(HttpCache::$gzip && isset($connection->gzip) && $connection->gzip){
-			$header .= "Content-Encoding: gzip\r\n";
-			$content = \gzencode($content,$connection->gzip);
-		}
         // header
         $header .= 'Content-Length: ' . \strlen($content) . "\r\n\r\n";
 
@@ -262,7 +263,8 @@ class Http
     }
 
     /**
-     * 设置http头
+     * Send a raw HTTP header
+     * 
      * @param string $content
      * @param bool   $replace
      * @param int    $http_response_code
@@ -294,7 +296,7 @@ class Http
         }
 
         if ($key === 'Set-Cookie') {
-            HttpCache::$header[$key][] = $content;
+            HttpCache::$cookie[] = $content;
         } else {
             HttpCache::$header[$key] = $content;
         }
@@ -303,7 +305,7 @@ class Http
     }
 
     /**
-     * Remove header.
+     * Remove previously set headers
      *
      * @param string $name
      * @return void
@@ -356,13 +358,15 @@ class Http
         if (PHP_SAPI !== 'cli') {
             return \setcookie($name, $value, $maxage, $path, $domain, $secure, $HTTPOnly);
         }
-        return self::header(
-            'Set-Cookie: ' . $name . '=' . rawurlencode($value)
-            . (empty($domain) ? '' : '; Domain=' . $domain)
-            . (empty($maxage) ? '' : '; Max-Age=' . $maxage)
-            . (empty($path) ? '' : '; Path=' . $path)
-            . (!$secure ? '' : '; Secure')
-            . (!$HTTPOnly ? '' : '; HttpOnly'), false);
+
+        HttpCache::$cookie[] = 'Set-Cookie: ' . $name . '=' . rawurlencode($value)
+                                . (empty($domain) ? '' : '; Domain=' . $domain)
+                                . (empty($maxage) ? '' : '; Max-Age=' . $maxage)
+                                . (empty($path) ? '' : '; Path=' . $path)
+                                . (!$secure ? '' : '; Secure')
+                                . (!$HTTPOnly ? '' : '; HttpOnly');
+        
+        return true;
     }
 
     /**
@@ -377,7 +381,7 @@ class Http
     }
 
     /**
-     * sessionId
+     * Get and/or set the current session id
      *
      * @param string  $id
      *
@@ -395,7 +399,7 @@ class Http
     }
 
     /**
-     * sessionName
+     * Get and/or set the current session name
      *
      * @param string  $name
      *
@@ -414,7 +418,7 @@ class Http
     }
 
     /**
-     * sessionSavePath
+     * Get and/or set the current session save path
      *
      * @param string  $path
      *
@@ -506,7 +510,7 @@ class Http
         if (!empty(HttpCache::$instance->sessionStarted) && !empty($_SESSION)) {
             $session_str = \serialize($_SESSION);
             if ($session_str && HttpCache::$instance->sessionFile) {
-                return \file_put_contents(HttpCache::$instance->sessionFile, $session_str);
+                return (bool) \file_put_contents(HttpCache::$instance->sessionFile, $session_str);
             }
         }
         return empty($_SESSION);
@@ -676,6 +680,7 @@ class HttpCache
     public static $instance             = null;
     public static $status               = '';
     public static $header               = array();
+    public static $cookie               = array();
     public static $gzip                 = false;
     public static $sessionPath          = '';
     public static $sessionName          = '';