Http.php 23 KB

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