Http.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. <?php
  2. /**
  3. * This file is part of workerman.
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the MIT-LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @author walkor<walkor@workerman.net>
  10. * @copyright walkor<walkor@workerman.net>
  11. * @link http://www.workerman.net/
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Workerman\Protocols;
  15. use Workerman\Connection\TcpConnection;
  16. use Workerman\Protocols\Websocket;
  17. use Workerman\Worker;
  18. /**
  19. * http protocol
  20. */
  21. class Http
  22. {
  23. /**
  24. * The supported HTTP methods
  25. * @var array
  26. */
  27. public static $methods = array('GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS');
  28. /**
  29. * Check the integrity of the package.
  30. *
  31. * @param string $recv_buffer
  32. * @param TcpConnection $connection
  33. * @return int
  34. */
  35. public static function input($recv_buffer, TcpConnection $connection)
  36. {
  37. if (!\strpos($recv_buffer, "\r\n\r\n")) {
  38. // Judge whether the package length exceeds the limit.
  39. if (\strlen($recv_buffer) >= $connection->maxPackageSize) {
  40. $connection->close();
  41. return 0;
  42. }
  43. return 0;
  44. }
  45. list($header,) = \explode("\r\n\r\n", $recv_buffer, 2);
  46. $method = \substr($header, 0, \strpos($header, ' '));
  47. if(\in_array($method, static::$methods)) {
  48. return static::getRequestSize($header, $method);
  49. }
  50. $connection->send("HTTP/1.1 400 Bad Request\r\n\r\n", true);
  51. return 0;
  52. }
  53. /**
  54. * Get whole size of the request
  55. * includes the request headers and request body.
  56. * @param string $header The request headers
  57. * @param string $method The request method
  58. * @return integer
  59. */
  60. protected static function getRequestSize($header, $method)
  61. {
  62. if($method === 'GET' || $method === 'OPTIONS' || $method === 'HEAD') {
  63. return \strlen($header) + 4;
  64. }
  65. $match = array();
  66. if (\preg_match("/\r\nContent-Length: ?(\d+)/i", $header, $match)) {
  67. $content_length = isset($match[1]) ? $match[1] : 0;
  68. return $content_length + \strlen($header) + 4;
  69. }
  70. return $method === 'DELETE' ? \strlen($header) + 4 : 0;
  71. }
  72. /**
  73. * Parse $_POST、$_GET、$_COOKIE.
  74. *
  75. * @param string $recv_buffer
  76. * @param TcpConnection $connection
  77. * @return array
  78. */
  79. public static function decode($recv_buffer, TcpConnection $connection)
  80. {
  81. // Init.
  82. $_POST = $_GET = $_COOKIE = $_REQUEST = $_SESSION = $_FILES = array();
  83. $GLOBALS['HTTP_RAW_POST_DATA'] = '';
  84. // Clear cache.
  85. HttpCache::$header = array('Connection' => 'Connection: keep-alive');
  86. HttpCache::$instance = new HttpCache();
  87. // $_SERVER
  88. $_SERVER = array(
  89. 'QUERY_STRING' => '',
  90. 'REQUEST_METHOD' => '',
  91. 'REQUEST_URI' => '',
  92. 'SERVER_PROTOCOL' => '',
  93. 'SERVER_SOFTWARE' => 'workerman/'.Worker::VERSION,
  94. 'SERVER_NAME' => '',
  95. 'HTTP_HOST' => '',
  96. 'HTTP_USER_AGENT' => '',
  97. 'HTTP_ACCEPT' => '',
  98. 'HTTP_ACCEPT_LANGUAGE' => '',
  99. 'HTTP_ACCEPT_ENCODING' => '',
  100. 'HTTP_COOKIE' => '',
  101. 'HTTP_CONNECTION' => '',
  102. 'CONTENT_TYPE' => '',
  103. 'REMOTE_ADDR' => '',
  104. 'REMOTE_PORT' => '0',
  105. 'REQUEST_TIME' => \time(),
  106. 'REQUEST_TIME_FLOAT' => \microtime(true) //compatible php5.4
  107. );
  108. // Parse headers.
  109. list($http_header, $http_body) = \explode("\r\n\r\n", $recv_buffer, 2);
  110. $header_data = \explode("\r\n", $http_header);
  111. list($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'], $_SERVER['SERVER_PROTOCOL']) = \explode(' ',
  112. $header_data[0]);
  113. $http_post_boundary = '';
  114. unset($header_data[0]);
  115. foreach ($header_data as $content) {
  116. // \r\n\r\n
  117. if (empty($content)) {
  118. continue;
  119. }
  120. list($key, $value) = \explode(':', $content, 2);
  121. $key = \str_replace('-', '_', strtoupper($key));
  122. $value = \trim($value);
  123. $_SERVER['HTTP_' . $key] = $value;
  124. switch ($key) {
  125. // HTTP_HOST
  126. case 'HOST':
  127. $tmp = \explode(':', $value);
  128. $_SERVER['SERVER_NAME'] = $tmp[0];
  129. if (isset($tmp[1])) {
  130. $_SERVER['SERVER_PORT'] = $tmp[1];
  131. }
  132. break;
  133. // cookie
  134. case 'COOKIE':
  135. \parse_str(\str_replace('; ', '&', $_SERVER['HTTP_COOKIE']), $_COOKIE);
  136. break;
  137. // content-type
  138. case 'CONTENT_TYPE':
  139. if (!\preg_match('/boundary="?(\S+)"?/', $value, $match)) {
  140. if ($pos = \strpos($value, ';')) {
  141. $_SERVER['CONTENT_TYPE'] = \substr($value, 0, $pos);
  142. } else {
  143. $_SERVER['CONTENT_TYPE'] = $value;
  144. }
  145. } else {
  146. $_SERVER['CONTENT_TYPE'] = 'multipart/form-data';
  147. $http_post_boundary = '--' . $match[1];
  148. }
  149. break;
  150. case 'CONTENT_LENGTH':
  151. $_SERVER['CONTENT_LENGTH'] = $value;
  152. break;
  153. case 'UPGRADE':
  154. if($value === 'websocket'){
  155. $connection->protocol = '\Workerman\Protocols\Websocket';
  156. return Websocket::input($recv_buffer,$connection);
  157. }
  158. break;
  159. }
  160. }
  161. if(isset($_SERVER['HTTP_ACCEPT_ENCODING']) && \strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false){
  162. HttpCache::$gzip = true;
  163. }
  164. // Parse $_POST.
  165. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  166. if (isset($_SERVER['CONTENT_TYPE'])) {
  167. switch ($_SERVER['CONTENT_TYPE']) {
  168. case 'multipart/form-data':
  169. self::parseUploadFiles($http_body, $http_post_boundary);
  170. break;
  171. case 'application/json':
  172. $_POST = \json_decode($http_body, true);
  173. break;
  174. case 'application/x-www-form-urlencoded':
  175. \parse_str($http_body, $_POST);
  176. break;
  177. }
  178. }
  179. }
  180. // Parse other HTTP action parameters
  181. if ($_SERVER['REQUEST_METHOD'] !== 'GET' && $_SERVER['REQUEST_METHOD'] !== "POST") {
  182. $data = array();
  183. if ($_SERVER['CONTENT_TYPE'] === "application/x-www-form-urlencoded") {
  184. \parse_str($http_body, $data);
  185. } elseif ($_SERVER['CONTENT_TYPE'] === "application/json") {
  186. $data = \json_decode($http_body, true);
  187. }
  188. $_REQUEST = \array_merge($_REQUEST, $data);
  189. }
  190. // HTTP_RAW_REQUEST_DATA HTTP_RAW_POST_DATA
  191. $GLOBALS['HTTP_RAW_REQUEST_DATA'] = $GLOBALS['HTTP_RAW_POST_DATA'] = $http_body;
  192. // QUERY_STRING
  193. $_SERVER['QUERY_STRING'] = \parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
  194. if ($_SERVER['QUERY_STRING']) {
  195. // $GET
  196. \parse_str($_SERVER['QUERY_STRING'], $_GET);
  197. } else {
  198. $_SERVER['QUERY_STRING'] = '';
  199. }
  200. if (\is_array($_POST)) {
  201. // REQUEST
  202. $_REQUEST = \array_merge($_GET, $_POST, $_REQUEST);
  203. } else {
  204. // REQUEST
  205. $_REQUEST = \array_merge($_GET, $_REQUEST);
  206. }
  207. // REMOTE_ADDR REMOTE_PORT
  208. $_SERVER['REMOTE_ADDR'] = $connection->getRemoteIp();
  209. $_SERVER['REMOTE_PORT'] = $connection->getRemotePort();
  210. return array('get' => $_GET, 'post' => $_POST, 'cookie' => $_COOKIE, 'server' => $_SERVER, 'files' => $_FILES);
  211. }
  212. /**
  213. * Http encode.
  214. *
  215. * @param string $content
  216. * @param TcpConnection $connection
  217. * @return string
  218. */
  219. public static function encode($content, TcpConnection $connection)
  220. {
  221. // http-code status line.
  222. $header = HttpCache::$status . "\r\n";
  223. // other headers
  224. foreach (HttpCache::$header as $key => $item) {
  225. if ('Set-Cookie' === $key && \is_array($item)) {
  226. foreach ($item as $it) {
  227. $header .= $it . "\r\n";
  228. }
  229. } else {
  230. $header .= $item . "\r\n";
  231. }
  232. }
  233. if(HttpCache::$gzip && isset($connection->gzip) && $connection->gzip){
  234. $header .= "Content-Encoding: gzip\r\n";
  235. $content = \gzencode($content,$connection->gzip);
  236. }
  237. // header
  238. $header .= "Server: workerman/" . Worker::VERSION . "\r\nContent-Length: " . \strlen($content) . "\r\n\r\n";
  239. // save session
  240. self::sessionWriteClose();
  241. // the whole http package
  242. return $header . $content;
  243. }
  244. /**
  245. * 设置http头
  246. * @param string $content
  247. * @param bool $replace
  248. * @param int $http_response_code
  249. *
  250. * @return bool|void
  251. */
  252. public static function header($content, $replace = true, $http_response_code = null)
  253. {
  254. if (PHP_SAPI !== 'cli') {
  255. \header($content, $replace, $http_response_code);
  256. return;
  257. }
  258. if (\strpos($content, 'HTTP') === 0) {
  259. HttpCache::$status = $content;
  260. return true;
  261. }
  262. $key = \strstr($content, ":", true);
  263. if (empty($key)) {
  264. return false;
  265. }
  266. if ('location' === \strtolower($key)) {
  267. if (!$http_response_code) {
  268. $http_response_code = 302;
  269. }
  270. if (isset(HttpCache::$codes[$http_response_code])) {
  271. HttpCache::$status = "HTTP/1.1 $http_response_code " . HttpCache::$codes[$http_response_code];
  272. }
  273. }
  274. if ($key === 'Set-Cookie') {
  275. HttpCache::$header[$key][] = $content;
  276. } else {
  277. HttpCache::$header[$key] = $content;
  278. }
  279. return true;
  280. }
  281. /**
  282. * Remove header.
  283. *
  284. * @param string $name
  285. * @return void
  286. */
  287. public static function headerRemove($name)
  288. {
  289. if (PHP_SAPI !== 'cli') {
  290. \header_remove($name);
  291. return;
  292. }
  293. unset(HttpCache::$header[$name]);
  294. }
  295. /**
  296. * Set cookie.
  297. *
  298. * @param string $name
  299. * @param string $value
  300. * @param integer $maxage
  301. * @param string $path
  302. * @param string $domain
  303. * @param bool $secure
  304. * @param bool $HTTPOnly
  305. * @return bool|void
  306. */
  307. public static function setcookie(
  308. $name,
  309. $value = '',
  310. $maxage = 0,
  311. $path = '',
  312. $domain = '',
  313. $secure = false,
  314. $HTTPOnly = false
  315. ) {
  316. if (PHP_SAPI !== 'cli') {
  317. return \setcookie($name, $value, $maxage, $path, $domain, $secure, $HTTPOnly);
  318. }
  319. return self::header(
  320. 'Set-Cookie: ' . $name . '=' . rawurlencode($value)
  321. . (empty($domain) ? '' : '; Domain=' . $domain)
  322. . (empty($maxage) ? '' : '; Max-Age=' . $maxage)
  323. . (empty($path) ? '' : '; Path=' . $path)
  324. . (!$secure ? '' : '; Secure')
  325. . (!$HTTPOnly ? '' : '; HttpOnly'), false);
  326. }
  327. /**
  328. * sessionCreateId
  329. *
  330. * @return string
  331. */
  332. public static function sessionCreateId()
  333. {
  334. \mt_srand();
  335. return bin2hex(\pack('d', \microtime(true)) . \pack('N',\mt_rand(0, 2147483647)));
  336. }
  337. /**
  338. * sessionId
  339. *
  340. * @param string $id
  341. *
  342. * @return string|null
  343. */
  344. public static function sessionId($id = null)
  345. {
  346. if (PHP_SAPI !== 'cli') {
  347. return $id ? \session_id($id) : \session_id();
  348. }
  349. if (static::sessionStarted() && HttpCache::$instance->sessionFile) {
  350. return \str_replace('ses_', '', \basename(HttpCache::$instance->sessionFile));
  351. }
  352. return '';
  353. }
  354. /**
  355. * sessionName
  356. *
  357. * @param string $name
  358. *
  359. * @return string
  360. */
  361. public static function sessionName($name = null)
  362. {
  363. if (PHP_SAPI !== 'cli') {
  364. return $name ? \session_name($name) : \session_name();
  365. }
  366. $session_name = HttpCache::$sessionName;
  367. if ($name && ! static::sessionStarted()) {
  368. HttpCache::$sessionName = $name;
  369. }
  370. return $session_name;
  371. }
  372. /**
  373. * sessionSavePath
  374. *
  375. * @param string $path
  376. *
  377. * @return void
  378. */
  379. public static function sessionSavePath($path = null)
  380. {
  381. if (PHP_SAPI !== 'cli') {
  382. return $path ? \session_save_path($path) : \session_save_path();
  383. }
  384. if ($path && \is_dir($path) && \is_writable($path) && !static::sessionStarted()) {
  385. HttpCache::$sessionPath = $path;
  386. }
  387. return HttpCache::$sessionPath;
  388. }
  389. /**
  390. * sessionStarted
  391. *
  392. * @return bool
  393. */
  394. public static function sessionStarted()
  395. {
  396. if (!HttpCache::$instance) return false;
  397. return HttpCache::$instance->sessionStarted;
  398. }
  399. /**
  400. * sessionStart
  401. *
  402. * @return bool
  403. */
  404. public static function sessionStart()
  405. {
  406. if (PHP_SAPI !== 'cli') {
  407. return \session_start();
  408. }
  409. self::tryGcSessions();
  410. if (HttpCache::$instance->sessionStarted) {
  411. Worker::safeEcho("already sessionStarted\n");
  412. return true;
  413. }
  414. HttpCache::$instance->sessionStarted = true;
  415. // Generate a SID.
  416. if (!isset($_COOKIE[HttpCache::$sessionName]) || !\is_file(HttpCache::$sessionPath . '/ses_' . $_COOKIE[HttpCache::$sessionName])) {
  417. // Create a unique session_id and the associated file name.
  418. while (true) {
  419. $session_id = static::sessionCreateId();
  420. if (!\is_file($file_name = HttpCache::$sessionPath . '/ses_' . $session_id)) break;
  421. }
  422. HttpCache::$instance->sessionFile = $file_name;
  423. return self::setcookie(
  424. HttpCache::$sessionName
  425. , $session_id
  426. , \ini_get('session.cookie_lifetime')
  427. , \ini_get('session.cookie_path')
  428. , \ini_get('session.cookie_domain')
  429. , \ini_get('session.cookie_secure')
  430. , \ini_get('session.cookie_httponly')
  431. );
  432. }
  433. if (!HttpCache::$instance->sessionFile) {
  434. HttpCache::$instance->sessionFile = HttpCache::$sessionPath . '/ses_' . $_COOKIE[HttpCache::$sessionName];
  435. }
  436. // Read session from session file.
  437. if (HttpCache::$instance->sessionFile) {
  438. $raw = \file_get_contents(HttpCache::$instance->sessionFile);
  439. if ($raw) {
  440. $_SESSION = \unserialize($raw);
  441. }
  442. }
  443. return true;
  444. }
  445. /**
  446. * Save session.
  447. *
  448. * @return bool
  449. */
  450. public static function sessionWriteClose()
  451. {
  452. if (PHP_SAPI !== 'cli') {
  453. \session_write_close();
  454. return true;
  455. }
  456. if (!empty(HttpCache::$instance->sessionStarted) && !empty($_SESSION)) {
  457. $session_str = \serialize($_SESSION);
  458. if ($session_str && HttpCache::$instance->sessionFile) {
  459. return \file_put_contents(HttpCache::$instance->sessionFile, $session_str);
  460. }
  461. }
  462. return empty($_SESSION);
  463. }
  464. /**
  465. * End, like call exit in php-fpm.
  466. *
  467. * @param string $msg
  468. * @throws \Exception
  469. */
  470. public static function end($msg = '')
  471. {
  472. if (PHP_SAPI !== 'cli') {
  473. exit($msg);
  474. }
  475. if ($msg) {
  476. echo $msg;
  477. }
  478. throw new \Exception('jump_exit');
  479. }
  480. /**
  481. * Get mime types.
  482. *
  483. * @return string
  484. */
  485. public static function getMimeTypesFile()
  486. {
  487. return __DIR__ . '/Http/mime.types';
  488. }
  489. /**
  490. * Parse $_FILES.
  491. *
  492. * @param string $http_body
  493. * @param string $http_post_boundary
  494. * @return void
  495. */
  496. protected static function parseUploadFiles($http_body, $http_post_boundary)
  497. {
  498. $http_body = \substr($http_body, 0, \strlen($http_body) - (\strlen($http_post_boundary) + 4));
  499. $boundary_data_array = \explode($http_post_boundary . "\r\n", $http_body);
  500. if ($boundary_data_array[0] === '') {
  501. unset($boundary_data_array[0]);
  502. }
  503. $key = -1;
  504. foreach ($boundary_data_array as $boundary_data_buffer) {
  505. list($boundary_header_buffer, $boundary_value) = \explode("\r\n\r\n", $boundary_data_buffer, 2);
  506. // Remove \r\n from the end of buffer.
  507. $boundary_value = \substr($boundary_value, 0, -2);
  508. $key ++;
  509. foreach (\explode("\r\n", $boundary_header_buffer) as $item) {
  510. list($header_key, $header_value) = \explode(": ", $item);
  511. $header_key = \strtolower($header_key);
  512. switch ($header_key) {
  513. case "content-disposition":
  514. // Is file data.
  515. if (\preg_match('/name="(.*?)"; filename="(.*?)"$/', $header_value, $match)) {
  516. // Parse $_FILES.
  517. $_FILES[$key] = array(
  518. 'name' => $match[1],
  519. 'file_name' => $match[2],
  520. 'file_data' => $boundary_value,
  521. 'file_size' => \strlen($boundary_value),
  522. );
  523. break;
  524. } // Is post field.
  525. else {
  526. // Parse $_POST.
  527. if (\preg_match('/name="(.*?)"$/', $header_value, $match)) {
  528. $_POST[$match[1]] = $boundary_value;
  529. }
  530. }
  531. break;
  532. case "content-type":
  533. // add file_type
  534. $_FILES[$key]['file_type'] = \trim($header_value);
  535. break;
  536. }
  537. }
  538. }
  539. }
  540. /**
  541. * Try GC sessions.
  542. *
  543. * @return void
  544. */
  545. public static function tryGcSessions()
  546. {
  547. if (HttpCache::$sessionGcProbability <= 0 ||
  548. HttpCache::$sessionGcDivisor <= 0 ||
  549. \rand(1, HttpCache::$sessionGcDivisor) > HttpCache::$sessionGcProbability) {
  550. return;
  551. }
  552. $time_now = \time();
  553. foreach(glob(HttpCache::$sessionPath.'/ses*') as $file) {
  554. if(\is_file($file) && $time_now - \filemtime($file) > HttpCache::$sessionGcMaxLifeTime) {
  555. \unlink($file);
  556. }
  557. }
  558. }
  559. }
  560. /**
  561. * Http cache for the current http response.
  562. */
  563. class HttpCache
  564. {
  565. public static $codes = array(
  566. 100 => 'Continue',
  567. 101 => 'Switching Protocols',
  568. 200 => 'OK',
  569. 201 => 'Created',
  570. 202 => 'Accepted',
  571. 203 => 'Non-Authoritative Information',
  572. 204 => 'No Content',
  573. 205 => 'Reset Content',
  574. 206 => 'Partial Content',
  575. 300 => 'Multiple Choices',
  576. 301 => 'Moved Permanently',
  577. 302 => 'Found',
  578. 303 => 'See Other',
  579. 304 => 'Not Modified',
  580. 305 => 'Use Proxy',
  581. 306 => '(Unused)',
  582. 307 => 'Temporary Redirect',
  583. 400 => 'Bad Request',
  584. 401 => 'Unauthorized',
  585. 402 => 'Payment Required',
  586. 403 => 'Forbidden',
  587. 404 => 'Not Found',
  588. 405 => 'Method Not Allowed',
  589. 406 => 'Not Acceptable',
  590. 407 => 'Proxy Authentication Required',
  591. 408 => 'Request Timeout',
  592. 409 => 'Conflict',
  593. 410 => 'Gone',
  594. 411 => 'Length Required',
  595. 412 => 'Precondition Failed',
  596. 413 => 'Request Entity Too Large',
  597. 414 => 'Request-URI Too Long',
  598. 415 => 'Unsupported Media Type',
  599. 416 => 'Requested Range Not Satisfiable',
  600. 417 => 'Expectation Failed',
  601. 422 => 'Unprocessable Entity',
  602. 423 => 'Locked',
  603. 500 => 'Internal Server Error',
  604. 501 => 'Not Implemented',
  605. 502 => 'Bad Gateway',
  606. 503 => 'Service Unavailable',
  607. 504 => 'Gateway Timeout',
  608. 505 => 'HTTP Version Not Supported',
  609. );
  610. /**
  611. * @var HttpCache
  612. */
  613. public static $instance = null;
  614. public static $status = 'HTTP/1.1 200 OK'; // default status code
  615. public static $header = array(
  616. 'Content-Type' => 'text/html;charset=utf-8' // default Content-Type
  617. );
  618. public static $gzip = false;
  619. public static $sessionPath = '';
  620. public static $sessionName = '';
  621. public static $sessionGcProbability = 1;
  622. public static $sessionGcDivisor = 1000;
  623. public static $sessionGcMaxLifeTime = 1440;
  624. public $sessionStarted = false;
  625. public $sessionFile = '';
  626. public static function init()
  627. {
  628. if (!self::$sessionName) {
  629. self::$sessionName = \ini_get('session.name');
  630. }
  631. if (!self::$sessionPath) {
  632. self::$sessionPath = @\session_save_path();
  633. }
  634. if (!self::$sessionPath || \strpos(self::$sessionPath, 'tcp://') === 0) {
  635. self::$sessionPath = \sys_get_temp_dir();
  636. }
  637. if ($gc_probability = \ini_get('session.gc_probability')) {
  638. self::$sessionGcProbability = $gc_probability;
  639. }
  640. if ($gc_divisor = \ini_get('session.gc_divisor')) {
  641. self::$sessionGcDivisor = $gc_divisor;
  642. }
  643. if ($gc_max_life_time = \ini_get('session.gc_maxlifetime')) {
  644. self::$sessionGcMaxLifeTime = $gc_max_life_time;
  645. }
  646. }
  647. }
  648. HttpCache::init();