Http.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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\Worker;
  17. /**
  18. * http protocol
  19. */
  20. class Http
  21. {
  22. /**
  23. * The supported HTTP methods
  24. * @var array
  25. */
  26. public static $methods = array('GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS');
  27. /**
  28. * Check the integrity of the package.
  29. *
  30. * @param string $recv_buffer
  31. * @param TcpConnection $connection
  32. * @return int
  33. */
  34. public static function input($recv_buffer, TcpConnection $connection)
  35. {
  36. if (!\strpos($recv_buffer, "\r\n\r\n")) {
  37. // Judge whether the package length exceeds the limit.
  38. if (\strlen($recv_buffer) >= $connection->maxPackageSize) {
  39. $connection->close();
  40. return 0;
  41. }
  42. return 0;
  43. }
  44. list($header,) = \explode("\r\n\r\n", $recv_buffer, 2);
  45. $method = \substr($header, 0, \strpos($header, ' '));
  46. if(\in_array($method, static::$methods)) {
  47. return static::getRequestSize($header, $method);
  48. }else{
  49. $connection->send("HTTP/1.1 400 Bad Request\r\n\r\n", true);
  50. return 0;
  51. }
  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 \Workerman\Protocols\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. // Default http-code.
  222. if (!isset(HttpCache::$header['Http-Code'])) {
  223. $header = "HTTP/1.1 200 OK\r\n";
  224. } else {
  225. $header = HttpCache::$header['Http-Code'] . "\r\n";
  226. unset(HttpCache::$header['Http-Code']);
  227. }
  228. // Content-Type
  229. if (!isset(HttpCache::$header['Content-Type'])) {
  230. $header .= "Content-Type: text/html;charset=utf-8\r\n";
  231. }
  232. // other headers
  233. foreach (HttpCache::$header as $key => $item) {
  234. if ('Set-Cookie' === $key && \is_array($item)) {
  235. foreach ($item as $it) {
  236. $header .= $it . "\r\n";
  237. }
  238. } else {
  239. $header .= $item . "\r\n";
  240. }
  241. }
  242. if(HttpCache::$gzip && isset($connection->gzip) && $connection->gzip){
  243. $header .= "Content-Encoding: gzip\r\n";
  244. $content = \gzencode($content,$connection->gzip);
  245. }
  246. // header
  247. $header .= "Server: workerman/" . Worker::VERSION . "\r\nContent-Length: " . \strlen($content) . "\r\n\r\n";
  248. // save session
  249. self::sessionWriteClose();
  250. // the whole http package
  251. return $header . $content;
  252. }
  253. /**
  254. * 设置http头
  255. *
  256. * @return bool|void
  257. */
  258. public static function header($content, $replace = true, $http_response_code = 0)
  259. {
  260. if (PHP_SAPI !== 'cli') {
  261. return $http_response_code ? \header($content, $replace, $http_response_code) : \header($content, $replace);
  262. }
  263. if (\strpos($content, 'HTTP') === 0) {
  264. $key = 'Http-Code';
  265. } else {
  266. $key = \strstr($content, ":", true);
  267. if (empty($key)) {
  268. return false;
  269. }
  270. }
  271. if ('location' === \strtolower($key) && !$http_response_code) {
  272. return self::header($content, true, 302);
  273. }
  274. if (isset(HttpCache::$codes[$http_response_code])) {
  275. HttpCache::$header['Http-Code'] = "HTTP/1.1 $http_response_code " . HttpCache::$codes[$http_response_code];
  276. if ($key === 'Http-Code') {
  277. return true;
  278. }
  279. }
  280. if ($key === 'Set-Cookie') {
  281. HttpCache::$header[$key][] = $content;
  282. } else {
  283. HttpCache::$header[$key] = $content;
  284. }
  285. return true;
  286. }
  287. /**
  288. * Remove header.
  289. *
  290. * @param string $name
  291. * @return void
  292. */
  293. public static function headerRemove($name)
  294. {
  295. if (PHP_SAPI !== 'cli') {
  296. \header_remove($name);
  297. return;
  298. }
  299. unset(HttpCache::$header[$name]);
  300. }
  301. /**
  302. * Set cookie.
  303. *
  304. * @param string $name
  305. * @param string $value
  306. * @param integer $maxage
  307. * @param string $path
  308. * @param string $domain
  309. * @param bool $secure
  310. * @param bool $HTTPOnly
  311. * @return bool|void
  312. */
  313. public static function setcookie(
  314. $name,
  315. $value = '',
  316. $maxage = 0,
  317. $path = '',
  318. $domain = '',
  319. $secure = false,
  320. $HTTPOnly = false
  321. ) {
  322. if (PHP_SAPI !== 'cli') {
  323. return \setcookie($name, $value, $maxage, $path, $domain, $secure, $HTTPOnly);
  324. }
  325. return self::header(
  326. 'Set-Cookie: ' . $name . '=' . rawurlencode($value)
  327. . (empty($domain) ? '' : '; Domain=' . $domain)
  328. . (empty($maxage) ? '' : '; Max-Age=' . $maxage)
  329. . (empty($path) ? '' : '; Path=' . $path)
  330. . (!$secure ? '' : '; Secure')
  331. . (!$HTTPOnly ? '' : '; HttpOnly'), false);
  332. }
  333. /**
  334. * sessionCreateId
  335. *
  336. * @return string
  337. */
  338. public static function sessionCreateId()
  339. {
  340. \mt_srand();
  341. return bin2hex(\pack('d', \microtime(true)) . \pack('N',\mt_rand(0, 2147483647)));
  342. }
  343. /**
  344. * sessionId
  345. *
  346. * @param string $id
  347. *
  348. * @return string|null
  349. */
  350. public static function sessionId($id = null)
  351. {
  352. if (PHP_SAPI !== 'cli') {
  353. return $id ? \session_id($id) : \session_id();
  354. }
  355. if (static::sessionStarted() && HttpCache::$instance->sessionFile) {
  356. return \str_replace('ses_', '', \basename(HttpCache::$instance->sessionFile));
  357. }
  358. return '';
  359. }
  360. /**
  361. * sessionName
  362. *
  363. * @param string $name
  364. *
  365. * @return string
  366. */
  367. public static function sessionName($name = null)
  368. {
  369. if (PHP_SAPI !== 'cli') {
  370. return $name ? \session_name($name) : \session_name();
  371. }
  372. $session_name = HttpCache::$sessionName;
  373. if ($name && ! static::sessionStarted()) {
  374. HttpCache::$sessionName = $name;
  375. }
  376. return $session_name;
  377. }
  378. /**
  379. * sessionSavePath
  380. *
  381. * @param string $path
  382. *
  383. * @return void
  384. */
  385. public static function sessionSavePath($path = null)
  386. {
  387. if (PHP_SAPI !== 'cli') {
  388. return $path ? \session_save_path($path) : \session_save_path();
  389. }
  390. if ($path && \is_dir($path) && \is_writable($path) && !static::sessionStarted()) {
  391. HttpCache::$sessionPath = $path;
  392. }
  393. return HttpCache::$sessionPath;
  394. }
  395. /**
  396. * sessionStarted
  397. *
  398. * @return bool
  399. */
  400. public static function sessionStarted()
  401. {
  402. if (!HttpCache::$instance) return false;
  403. return HttpCache::$instance->sessionStarted;
  404. }
  405. /**
  406. * sessionStart
  407. *
  408. * @return bool
  409. */
  410. public static function sessionStart()
  411. {
  412. if (PHP_SAPI !== 'cli') {
  413. return \session_start();
  414. }
  415. self::tryGcSessions();
  416. if (HttpCache::$instance->sessionStarted) {
  417. Worker::safeEcho("already sessionStarted\n");
  418. return true;
  419. }
  420. HttpCache::$instance->sessionStarted = true;
  421. // Generate a SID.
  422. if (!isset($_COOKIE[HttpCache::$sessionName]) || !\is_file(HttpCache::$sessionPath . '/ses_' . $_COOKIE[HttpCache::$sessionName])) {
  423. // Create a unique session_id and the associated file name.
  424. while (true) {
  425. $session_id = static::sessionCreateId();
  426. if (!\is_file($file_name = HttpCache::$sessionPath . '/ses_' . $session_id)) break;
  427. }
  428. HttpCache::$instance->sessionFile = $file_name;
  429. return self::setcookie(
  430. HttpCache::$sessionName
  431. , $session_id
  432. , \ini_get('session.cookie_lifetime')
  433. , \ini_get('session.cookie_path')
  434. , \ini_get('session.cookie_domain')
  435. , \ini_get('session.cookie_secure')
  436. , \ini_get('session.cookie_httponly')
  437. );
  438. }
  439. if (!HttpCache::$instance->sessionFile) {
  440. HttpCache::$instance->sessionFile = HttpCache::$sessionPath . '/ses_' . $_COOKIE[HttpCache::$sessionName];
  441. }
  442. // Read session from session file.
  443. if (HttpCache::$instance->sessionFile) {
  444. $raw = \file_get_contents(HttpCache::$instance->sessionFile);
  445. if ($raw) {
  446. $_SESSION = \unserialize($raw);
  447. }
  448. }
  449. return true;
  450. }
  451. /**
  452. * Save session.
  453. *
  454. * @return bool
  455. */
  456. public static function sessionWriteClose()
  457. {
  458. if (PHP_SAPI !== 'cli') {
  459. \session_write_close();
  460. return true;
  461. }
  462. if (!empty(HttpCache::$instance->sessionStarted) && !empty($_SESSION)) {
  463. $session_str = \serialize($_SESSION);
  464. if ($session_str && HttpCache::$instance->sessionFile) {
  465. return \file_put_contents(HttpCache::$instance->sessionFile, $session_str);
  466. }
  467. }
  468. return empty($_SESSION);
  469. }
  470. /**
  471. * End, like call exit in php-fpm.
  472. *
  473. * @param string $msg
  474. * @throws \Exception
  475. */
  476. public static function end($msg = '')
  477. {
  478. if (PHP_SAPI !== 'cli') {
  479. exit($msg);
  480. }
  481. if ($msg) {
  482. echo $msg;
  483. }
  484. throw new \Exception('jump_exit');
  485. }
  486. /**
  487. * Get mime types.
  488. *
  489. * @return string
  490. */
  491. public static function getMimeTypesFile()
  492. {
  493. return __DIR__ . '/Http/mime.types';
  494. }
  495. /**
  496. * Parse $_FILES.
  497. *
  498. * @param string $http_body
  499. * @param string $http_post_boundary
  500. * @return void
  501. */
  502. protected static function parseUploadFiles($http_body, $http_post_boundary)
  503. {
  504. $http_body = \substr($http_body, 0, \strlen($http_body) - (\strlen($http_post_boundary) + 4));
  505. $boundary_data_array = \explode($http_post_boundary . "\r\n", $http_body);
  506. if ($boundary_data_array[0] === '') {
  507. unset($boundary_data_array[0]);
  508. }
  509. $key = -1;
  510. foreach ($boundary_data_array as $boundary_data_buffer) {
  511. list($boundary_header_buffer, $boundary_value) = \explode("\r\n\r\n", $boundary_data_buffer, 2);
  512. // Remove \r\n from the end of buffer.
  513. $boundary_value = \substr($boundary_value, 0, -2);
  514. $key ++;
  515. foreach (\explode("\r\n", $boundary_header_buffer) as $item) {
  516. list($header_key, $header_value) = \explode(": ", $item);
  517. $header_key = \strtolower($header_key);
  518. switch ($header_key) {
  519. case "content-disposition":
  520. // Is file data.
  521. if (\preg_match('/name="(.*?)"; filename="(.*?)"$/', $header_value, $match)) {
  522. // Parse $_FILES.
  523. $_FILES[$key] = array(
  524. 'name' => $match[1],
  525. 'file_name' => $match[2],
  526. 'file_data' => $boundary_value,
  527. 'file_size' => \strlen($boundary_value),
  528. );
  529. break;
  530. } // Is post field.
  531. else {
  532. // Parse $_POST.
  533. if (\preg_match('/name="(.*?)"$/', $header_value, $match)) {
  534. $_POST[$match[1]] = $boundary_value;
  535. }
  536. }
  537. break;
  538. case "content-type":
  539. // add file_type
  540. $_FILES[$key]['file_type'] = \trim($header_value);
  541. break;
  542. }
  543. }
  544. }
  545. }
  546. /**
  547. * Try GC sessions.
  548. *
  549. * @return void
  550. */
  551. public static function tryGcSessions()
  552. {
  553. if (HttpCache::$sessionGcProbability <= 0 ||
  554. HttpCache::$sessionGcDivisor <= 0 ||
  555. \rand(1, HttpCache::$sessionGcDivisor) > HttpCache::$sessionGcProbability) {
  556. return;
  557. }
  558. $time_now = \time();
  559. foreach(glob(HttpCache::$sessionPath.'/ses*') as $file) {
  560. if(\is_file($file) && $time_now - \filemtime($file) > HttpCache::$sessionGcMaxLifeTime) {
  561. \unlink($file);
  562. }
  563. }
  564. }
  565. }
  566. /**
  567. * Http cache for the current http response.
  568. */
  569. class HttpCache
  570. {
  571. public static $codes = array(
  572. 100 => 'Continue',
  573. 101 => 'Switching Protocols',
  574. 200 => 'OK',
  575. 201 => 'Created',
  576. 202 => 'Accepted',
  577. 203 => 'Non-Authoritative Information',
  578. 204 => 'No Content',
  579. 205 => 'Reset Content',
  580. 206 => 'Partial Content',
  581. 300 => 'Multiple Choices',
  582. 301 => 'Moved Permanently',
  583. 302 => 'Found',
  584. 303 => 'See Other',
  585. 304 => 'Not Modified',
  586. 305 => 'Use Proxy',
  587. 306 => '(Unused)',
  588. 307 => 'Temporary Redirect',
  589. 400 => 'Bad Request',
  590. 401 => 'Unauthorized',
  591. 402 => 'Payment Required',
  592. 403 => 'Forbidden',
  593. 404 => 'Not Found',
  594. 405 => 'Method Not Allowed',
  595. 406 => 'Not Acceptable',
  596. 407 => 'Proxy Authentication Required',
  597. 408 => 'Request Timeout',
  598. 409 => 'Conflict',
  599. 410 => 'Gone',
  600. 411 => 'Length Required',
  601. 412 => 'Precondition Failed',
  602. 413 => 'Request Entity Too Large',
  603. 414 => 'Request-URI Too Long',
  604. 415 => 'Unsupported Media Type',
  605. 416 => 'Requested Range Not Satisfiable',
  606. 417 => 'Expectation Failed',
  607. 422 => 'Unprocessable Entity',
  608. 423 => 'Locked',
  609. 500 => 'Internal Server Error',
  610. 501 => 'Not Implemented',
  611. 502 => 'Bad Gateway',
  612. 503 => 'Service Unavailable',
  613. 504 => 'Gateway Timeout',
  614. 505 => 'HTTP Version Not Supported',
  615. );
  616. /**
  617. * @var HttpCache
  618. */
  619. public static $instance = null;
  620. public static $header = array();
  621. public static $gzip = false;
  622. public static $sessionPath = '';
  623. public static $sessionName = '';
  624. public static $sessionGcProbability = 1;
  625. public static $sessionGcDivisor = 1000;
  626. public static $sessionGcMaxLifeTime = 1440;
  627. public $sessionStarted = false;
  628. public $sessionFile = '';
  629. public static function init()
  630. {
  631. if (!self::$sessionName) {
  632. self::$sessionName = \ini_get('session.name');
  633. }
  634. if (!self::$sessionPath) {
  635. self::$sessionPath = @\session_save_path();
  636. }
  637. if (!self::$sessionPath || \strpos(self::$sessionPath, 'tcp://') === 0) {
  638. self::$sessionPath = \sys_get_temp_dir();
  639. }
  640. if ($gc_probability = \ini_get('session.gc_probability')) {
  641. self::$sessionGcProbability = $gc_probability;
  642. }
  643. if ($gc_divisor = \ini_get('session.gc_divisor')) {
  644. self::$sessionGcDivisor = $gc_divisor;
  645. }
  646. if ($gc_max_life_time = \ini_get('session.gc_maxlifetime')) {
  647. self::$sessionGcMaxLifeTime = $gc_max_life_time;
  648. }
  649. }
  650. }
  651. HttpCache::init();