Http.php 23 KB

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