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