From 92ae5477f985a96831a8dfdbbcc0244322411952 Mon Sep 17 00:00:00 2001 From: Virgil-Adrian Teaca Date: Mon, 2 Apr 2018 17:56:37 +0300 Subject: [PATCH 1/7] Improve the HTTP Session management --- Protocols/Http.php | 119 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 96 insertions(+), 23 deletions(-) diff --git a/Protocols/Http.php b/Protocols/Http.php index c99d0d83d..2c7083f73 100644 --- a/Protocols/Http.php +++ b/Protocols/Http.php @@ -174,23 +174,9 @@ public static function decode($recv_buffer, TcpConnection $connection) case 'application/x-www-form-urlencoded': parse_str($http_body, $_POST); break; - case 'application/json': - $_POST = json_decode($http_body, true); - break; } } } - - // 解析其他HTTP动作参数 - if ($_SERVER['REQUEST_METHOD'] != 'GET' && $_SERVER['REQUEST_METHOD'] != "POST") { - $data = array(); - if ($_SERVER['HTTP_CONTENT_TYPE'] === "application/x-www-form-urlencoded") { - parse_str($http_body, $data); - } elseif ($_SERVER['HTTP_CONTENT_TYPE'] === "application/json") { - $data = json_decode($http_body, true); - } - $_REQUEST = array_merge($_REQUEST, $data); - } // HTTP_RAW_REQUEST_DATA HTTP_RAW_POST_DATA $GLOBALS['HTTP_RAW_REQUEST_DATA'] = $GLOBALS['HTTP_RAW_POST_DATA'] = $http_body; @@ -205,7 +191,7 @@ public static function decode($recv_buffer, TcpConnection $connection) } // REQUEST - $_REQUEST = array_merge($_GET, $_POST, $_REQUEST); + $_REQUEST = array_merge($_GET, $_POST); // REMOTE_ADDR REMOTE_PORT $_SERVER['REMOTE_ADDR'] = $connection->getRemoteIp(); @@ -344,6 +330,89 @@ public static function setcookie( . (!$HTTPOnly ? '' : '; HttpOnly'), false); } + /** + * sessionId + * + * @param string $id + * + * @return string|null + */ + public static function sessionId($id = null) + { + if (PHP_SAPI != 'cli') { + return $id ? session_id($id) : session_id(); + } + if (static::sessionStarted()) { + return str_replace('sess_', '', basename(HttpCache::$instance->sessionFile)); + } + return ''; + } + + /** + * sessionName + * + * @param string $name + * + * @return string + */ + public static function sessionName($name = null) + { + if (PHP_SAPI != 'cli') { + return $name ? session_name($name) : session_name(); + } + $session_name = HttpCache::$sessionName; + if ($name && ! static::sessionStarted()) { + HttpCache::$sessionName = $name; + } + return $session_name; + } + + /** + * sessionSavePath + * + * @param string $path + * + * @return void + */ + public static function sessionSavePath($path = null) + { + if (PHP_SAPI != 'cli') { + return $path ? session_save_path($path) : session_save_path(); + } + if ($path && is_dir($path) && is_writable($path) && !static::sessionStarted()) { + HttpCache::$sessionPath = $path; + } + return HttpCache::$sessionPath; + } + + /** + * sessionStatus + * + * @return int + */ + public static function sessionStatus() + { + if (PHP_SAPI != 'cli') { + return session_status(); + } + if (!extension_loaded('session')) { + return PHP_SESSION_DISABLED; + } + return static::sessionStarted() ? PHP_SESSION_ACTIVE : PHP_SESSION_NONE; + } + + /** + * sessionStarted + * + * @return bool + */ + public static function sessionStarted() + { + if (!HttpCache::$instance) return false; + + return HttpCache::$instance->sessionStarted; + } + /** * sessionStart * @@ -363,13 +432,13 @@ public static function sessionStart() } HttpCache::$instance->sessionStarted = true; // Generate a SID. - if (!isset($_COOKIE[HttpCache::$sessionName]) || !is_file(HttpCache::$sessionPath . '/ses' . $_COOKIE[HttpCache::$sessionName])) { - $file_name = tempnam(HttpCache::$sessionPath, 'ses'); - if (!$file_name) { - return false; + if (!isset($_COOKIE[HttpCache::$sessionName]) || !is_file(HttpCache::$sessionPath . '/sess_' . $_COOKIE[HttpCache::$sessionName])) { + // Create a new unique session_id and its associated file name. + while (true) { + $session_id = session_create_id(); + if (!is_file($file_name = HttpCache::$sessionPath . '/sess_' . $session_id)) break; } HttpCache::$instance->sessionFile = $file_name; - $session_id = substr(basename($file_name), strlen('ses')); return self::setcookie( HttpCache::$sessionName , $session_id @@ -381,7 +450,7 @@ public static function sessionStart() ); } if (!HttpCache::$instance->sessionFile) { - HttpCache::$instance->sessionFile = HttpCache::$sessionPath . '/ses' . $_COOKIE[HttpCache::$sessionName]; + HttpCache::$instance->sessionFile = HttpCache::$sessionPath . '/sess_' . $_COOKIE[HttpCache::$sessionName]; } // Read session from session file. if (HttpCache::$instance->sessionFile) { @@ -579,8 +648,12 @@ class HttpCache public static function init() { - self::$sessionName = ini_get('session.name'); - self::$sessionPath = @session_save_path(); + if (!self::$sessionName) { + self::$sessionName = ini_get('session.name'); + } + if (!self::$sessionPath) { + self::$sessionPath = @session_save_path(); + } if (!self::$sessionPath || strpos(self::$sessionPath, 'tcp://') === 0) { self::$sessionPath = sys_get_temp_dir(); } From 7f171a2643138ace75850d3a7264452bb3b01258 Mon Sep 17 00:00:00 2001 From: Virgil-Adrian Teaca Date: Mon, 2 Apr 2018 18:03:18 +0300 Subject: [PATCH 2/7] Improve the Workerman\Protocols\Http --- Protocols/Http.php | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/Protocols/Http.php b/Protocols/Http.php index 2c7083f73..378fee827 100644 --- a/Protocols/Http.php +++ b/Protocols/Http.php @@ -174,10 +174,24 @@ public static function decode($recv_buffer, TcpConnection $connection) case 'application/x-www-form-urlencoded': parse_str($http_body, $_POST); break; + case 'application/json': + $_POST = json_decode($http_body, true); + break; } } } + // 解析其他HTTP动作参数 + if ($_SERVER['REQUEST_METHOD'] != 'GET' && $_SERVER['REQUEST_METHOD'] != "POST") { + $data = array(); + if ($_SERVER['HTTP_CONTENT_TYPE'] === "application/x-www-form-urlencoded") { + parse_str($http_body, $data); + } elseif ($_SERVER['HTTP_CONTENT_TYPE'] === "application/json") { + $data = json_decode($http_body, true); + } + $_REQUEST = array_merge($_REQUEST, $data); + } + // HTTP_RAW_REQUEST_DATA HTTP_RAW_POST_DATA $GLOBALS['HTTP_RAW_REQUEST_DATA'] = $GLOBALS['HTTP_RAW_POST_DATA'] = $http_body; @@ -191,7 +205,7 @@ public static function decode($recv_buffer, TcpConnection $connection) } // REQUEST - $_REQUEST = array_merge($_GET, $_POST); + $_REQUEST = array_merge($_GET, $_POST, $_REQUEST); // REMOTE_ADDR REMOTE_PORT $_SERVER['REMOTE_ADDR'] = $connection->getRemoteIp(); @@ -432,7 +446,7 @@ public static function sessionStart() } HttpCache::$instance->sessionStarted = true; // Generate a SID. - if (!isset($_COOKIE[HttpCache::$sessionName]) || !is_file(HttpCache::$sessionPath . '/sess_' . $_COOKIE[HttpCache::$sessionName])) { + if (!isset($_COOKIE[HttpCache::$sessionName]) || !is_file(HttpCache::$sessionPath . '/ses' . $_COOKIE[HttpCache::$sessionName])) { // Create a new unique session_id and its associated file name. while (true) { $session_id = session_create_id(); @@ -450,7 +464,7 @@ public static function sessionStart() ); } if (!HttpCache::$instance->sessionFile) { - HttpCache::$instance->sessionFile = HttpCache::$sessionPath . '/sess_' . $_COOKIE[HttpCache::$sessionName]; + HttpCache::$instance->sessionFile = HttpCache::$sessionPath . '/ses' . $_COOKIE[HttpCache::$sessionName]; } // Read session from session file. if (HttpCache::$instance->sessionFile) { From fc835dcaaa91f842b17c8fedddff1d6e3ae173e2 Mon Sep 17 00:00:00 2001 From: Virgil-Adrian Teaca Date: Mon, 2 Apr 2018 18:12:51 +0300 Subject: [PATCH 3/7] Improve the Workerman\Protocols\Http --- Protocols/Http.php | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/Protocols/Http.php b/Protocols/Http.php index 378fee827..46722e96e 100644 --- a/Protocols/Http.php +++ b/Protocols/Http.php @@ -344,6 +344,18 @@ public static function setcookie( . (!$HTTPOnly ? '' : '; HttpOnly'), false); } + /** + * sessionCreateId + * + * @param string|prefix $prefix + * + * @return string + */ + public static function sessionCreateId($prefix = null) + { + return session_create_id($prefix); + } + /** * sessionId * @@ -399,22 +411,6 @@ public static function sessionSavePath($path = null) return HttpCache::$sessionPath; } - /** - * sessionStatus - * - * @return int - */ - public static function sessionStatus() - { - if (PHP_SAPI != 'cli') { - return session_status(); - } - if (!extension_loaded('session')) { - return PHP_SESSION_DISABLED; - } - return static::sessionStarted() ? PHP_SESSION_ACTIVE : PHP_SESSION_NONE; - } - /** * sessionStarted * @@ -449,7 +445,7 @@ public static function sessionStart() if (!isset($_COOKIE[HttpCache::$sessionName]) || !is_file(HttpCache::$sessionPath . '/ses' . $_COOKIE[HttpCache::$sessionName])) { // Create a new unique session_id and its associated file name. while (true) { - $session_id = session_create_id(); + $session_id = static::sessionCreateId(); if (!is_file($file_name = HttpCache::$sessionPath . '/sess_' . $session_id)) break; } HttpCache::$instance->sessionFile = $file_name; From 8cd7a62bf5e92bc5e7e368ef2cf9f438e14953a5 Mon Sep 17 00:00:00 2001 From: Virgil-Adrian Teaca Date: Mon, 2 Apr 2018 18:15:56 +0300 Subject: [PATCH 4/7] Comments change --- Protocols/Http.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Protocols/Http.php b/Protocols/Http.php index 46722e96e..1edb0938c 100644 --- a/Protocols/Http.php +++ b/Protocols/Http.php @@ -443,7 +443,7 @@ public static function sessionStart() HttpCache::$instance->sessionStarted = true; // Generate a SID. if (!isset($_COOKIE[HttpCache::$sessionName]) || !is_file(HttpCache::$sessionPath . '/ses' . $_COOKIE[HttpCache::$sessionName])) { - // Create a new unique session_id and its associated file name. + // Create a unique session_id and the associated file name. while (true) { $session_id = static::sessionCreateId(); if (!is_file($file_name = HttpCache::$sessionPath . '/sess_' . $session_id)) break; From ca1d022f8d8b1f37edb617b356ab37123e4efbce Mon Sep 17 00:00:00 2001 From: Virgil-Adrian Teaca Date: Mon, 2 Apr 2018 18:18:26 +0300 Subject: [PATCH 5/7] Improve the Workerman\Protocols\Http --- Protocols/Http.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Protocols/Http.php b/Protocols/Http.php index 1edb0938c..89c2bbff6 100644 --- a/Protocols/Http.php +++ b/Protocols/Http.php @@ -368,7 +368,7 @@ public static function sessionId($id = null) if (PHP_SAPI != 'cli') { return $id ? session_id($id) : session_id(); } - if (static::sessionStarted()) { + if (static::sessionStarted() && HttpCache::$instance->sessionFile) { return str_replace('sess_', '', basename(HttpCache::$instance->sessionFile)); } return ''; From 44070b844e263b3d12ff2bea1d6ee093c171ede8 Mon Sep 17 00:00:00 2001 From: Virgil-Adrian Teaca Date: Mon, 2 Apr 2018 19:15:40 +0300 Subject: [PATCH 6/7] Small changes --- Protocols/Http.php | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/Protocols/Http.php b/Protocols/Http.php index 89c2bbff6..cb8b819f6 100644 --- a/Protocols/Http.php +++ b/Protocols/Http.php @@ -175,21 +175,21 @@ public static function decode($recv_buffer, TcpConnection $connection) parse_str($http_body, $_POST); break; case 'application/json': - $_POST = json_decode($http_body, true); - break; + $_POST = json_decode($http_body, true); + break; } } } - // 解析其他HTTP动作参数 + // Parse other HTTP action parameters if ($_SERVER['REQUEST_METHOD'] != 'GET' && $_SERVER['REQUEST_METHOD'] != "POST") { - $data = array(); - if ($_SERVER['HTTP_CONTENT_TYPE'] === "application/x-www-form-urlencoded") { - parse_str($http_body, $data); - } elseif ($_SERVER['HTTP_CONTENT_TYPE'] === "application/json") { - $data = json_decode($http_body, true); - } - $_REQUEST = array_merge($_REQUEST, $data); + $data = array(); + if ($_SERVER['HTTP_CONTENT_TYPE'] === "application/x-www-form-urlencoded") { + parse_str($http_body, $data); + } elseif ($_SERVER['HTTP_CONTENT_TYPE'] === "application/json") { + $data = json_decode($http_body, true); + } + $_REQUEST = array_merge($_REQUEST, $data); } // HTTP_RAW_REQUEST_DATA HTTP_RAW_POST_DATA @@ -661,9 +661,11 @@ public static function init() if (!self::$sessionName) { self::$sessionName = ini_get('session.name'); } + if (!self::$sessionPath) { self::$sessionPath = @session_save_path(); } + if (!self::$sessionPath || strpos(self::$sessionPath, 'tcp://') === 0) { self::$sessionPath = sys_get_temp_dir(); } From 881d74e14d6f6f0307f8676f2255e045bf598ed0 Mon Sep 17 00:00:00 2001 From: Virgil-Adrian Teaca Date: Mon, 2 Apr 2018 21:34:26 +0300 Subject: [PATCH 7/7] Improve the Workerman\Protocols\Http --- Protocols/Http.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Protocols/Http.php b/Protocols/Http.php index cb8b819f6..da6bdf2fd 100644 --- a/Protocols/Http.php +++ b/Protocols/Http.php @@ -442,7 +442,7 @@ public static function sessionStart() } HttpCache::$instance->sessionStarted = true; // Generate a SID. - if (!isset($_COOKIE[HttpCache::$sessionName]) || !is_file(HttpCache::$sessionPath . '/ses' . $_COOKIE[HttpCache::$sessionName])) { + if (!isset($_COOKIE[HttpCache::$sessionName]) || !is_file(HttpCache::$sessionPath . '/sess_' . $_COOKIE[HttpCache::$sessionName])) { // Create a unique session_id and the associated file name. while (true) { $session_id = static::sessionCreateId(); @@ -460,7 +460,7 @@ public static function sessionStart() ); } if (!HttpCache::$instance->sessionFile) { - HttpCache::$instance->sessionFile = HttpCache::$sessionPath . '/ses' . $_COOKIE[HttpCache::$sessionName]; + HttpCache::$instance->sessionFile = HttpCache::$sessionPath . '/sess_' . $_COOKIE[HttpCache::$sessionName]; } // Read session from session file. if (HttpCache::$instance->sessionFile) {