Http.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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 (NO_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 (NO_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 (NO_CLI) {
  297. return \http_response_code($code);
  298. }
  299. if (isset(HttpCache::$codes[$code])) {
  300. HttpCache::$status = "HTTP/1.1 $code " . HttpCache::$codes[$code];
  301. return $code;
  302. }
  303. return false;
  304. }
  305. /**
  306. * Set cookie.
  307. *
  308. * @param string $name
  309. * @param string $value
  310. * @param integer $maxage
  311. * @param string $path
  312. * @param string $domain
  313. * @param bool $secure
  314. * @param bool $HTTPOnly
  315. * @return bool|void
  316. */
  317. public static function setcookie(
  318. $name,
  319. $value = '',
  320. $maxage = 0,
  321. $path = '',
  322. $domain = '',
  323. $secure = false,
  324. $HTTPOnly = false
  325. ) {
  326. if (NO_CLI) {
  327. return \setcookie($name, $value, $maxage, $path, $domain, $secure, $HTTPOnly);
  328. }
  329. HttpCache::$cookie[] = 'Set-Cookie: ' . $name . '=' . rawurlencode($value)
  330. . (empty($domain) ? '' : '; Domain=' . $domain)
  331. . (empty($maxage) ? '' : '; Max-Age=' . $maxage)
  332. . (empty($path) ? '' : '; Path=' . $path)
  333. . (!$secure ? '' : '; Secure')
  334. . (!$HTTPOnly ? '' : '; HttpOnly');
  335. return true;
  336. }
  337. /**
  338. * sessionCreateId
  339. *
  340. * @return string
  341. */
  342. public static function sessionCreateId()
  343. {
  344. \mt_srand();
  345. return bin2hex(\pack('d', \microtime(true)) . \pack('N',\mt_rand(0, 2147483647)));
  346. }
  347. /**
  348. * Get and/or set the current session id
  349. *
  350. * @param string $id
  351. *
  352. * @return string|null
  353. */
  354. public static function sessionId($id = null)
  355. {
  356. if (NO_CLI) {
  357. return $id ? \session_id($id) : \session_id();
  358. }
  359. if (static::sessionStarted() && HttpCache::$instance->sessionFile) {
  360. return \str_replace('ses_', '', \basename(HttpCache::$instance->sessionFile));
  361. }
  362. return '';
  363. }
  364. /**
  365. * Get and/or set the current session name
  366. *
  367. * @param string $name
  368. *
  369. * @return string
  370. */
  371. public static function sessionName($name = null)
  372. {
  373. if (NO_CLI) {
  374. return $name ? \session_name($name) : \session_name();
  375. }
  376. $session_name = HttpCache::$sessionName;
  377. if ($name && ! static::sessionStarted()) {
  378. HttpCache::$sessionName = $name;
  379. }
  380. return $session_name;
  381. }
  382. /**
  383. * Get and/or set the current session save path
  384. *
  385. * @param string $path
  386. *
  387. * @return void
  388. */
  389. public static function sessionSavePath($path = null)
  390. {
  391. if (NO_CLI) {
  392. return $path ? \session_save_path($path) : \session_save_path();
  393. }
  394. if ($path && \is_dir($path) && \is_writable($path) && !static::sessionStarted()) {
  395. HttpCache::$sessionPath = $path;
  396. }
  397. return HttpCache::$sessionPath;
  398. }
  399. /**
  400. * sessionStarted
  401. *
  402. * @return bool
  403. */
  404. public static function sessionStarted()
  405. {
  406. if (!HttpCache::$instance) return false;
  407. return HttpCache::$instance->sessionStarted;
  408. }
  409. /**
  410. * sessionStart
  411. *
  412. * @return bool
  413. */
  414. public static function sessionStart()
  415. {
  416. if (NO_CLI) {
  417. return \session_start();
  418. }
  419. self::tryGcSessions();
  420. if (HttpCache::$instance->sessionStarted) {
  421. Worker::safeEcho("already sessionStarted\n");
  422. return true;
  423. }
  424. HttpCache::$instance->sessionStarted = true;
  425. // Generate a SID.
  426. if (!isset($_COOKIE[HttpCache::$sessionName]) || !\is_file(HttpCache::$sessionPath . '/ses_' . $_COOKIE[HttpCache::$sessionName])) {
  427. // Create a unique session_id and the associated file name.
  428. while (true) {
  429. $session_id = static::sessionCreateId();
  430. if (!\is_file($file_name = HttpCache::$sessionPath . '/ses_' . $session_id)) break;
  431. }
  432. HttpCache::$instance->sessionFile = $file_name;
  433. return self::setcookie(
  434. HttpCache::$sessionName
  435. , $session_id
  436. , \ini_get('session.cookie_lifetime')
  437. , \ini_get('session.cookie_path')
  438. , \ini_get('session.cookie_domain')
  439. , \ini_get('session.cookie_secure')
  440. , \ini_get('session.cookie_httponly')
  441. );
  442. }
  443. if (!HttpCache::$instance->sessionFile) {
  444. HttpCache::$instance->sessionFile = HttpCache::$sessionPath . '/ses_' . $_COOKIE[HttpCache::$sessionName];
  445. }
  446. // Read session from session file.
  447. if (HttpCache::$instance->sessionFile) {
  448. $raw = \file_get_contents(HttpCache::$instance->sessionFile);
  449. if ($raw) {
  450. $_SESSION = \unserialize($raw);
  451. }
  452. }
  453. return true;
  454. }
  455. /**
  456. * Save session.
  457. *
  458. * @return bool
  459. */
  460. public static function sessionWriteClose()
  461. {
  462. if (NO_CLI) {
  463. \session_write_close();
  464. return true;
  465. }
  466. if (!empty(HttpCache::$instance->sessionStarted) && !empty($_SESSION)) {
  467. $session_str = \serialize($_SESSION);
  468. if ($session_str && HttpCache::$instance->sessionFile) {
  469. return (bool) \file_put_contents(HttpCache::$instance->sessionFile, $session_str);
  470. }
  471. }
  472. return empty($_SESSION);
  473. }
  474. /**
  475. * End, like call exit in php-fpm.
  476. *
  477. * @param string $msg
  478. * @throws \Exception
  479. */
  480. public static function end($msg = '')
  481. {
  482. if (NO_CLI) {
  483. exit($msg);
  484. }
  485. if ($msg) {
  486. echo $msg;
  487. }
  488. throw new \Exception('jump_exit');
  489. }
  490. /**
  491. * Get mime types.
  492. *
  493. * @return string
  494. */
  495. public static function getMimeTypesFile()
  496. {
  497. return __DIR__ . '/Http/mime.types';
  498. }
  499. /**
  500. * Parse $_FILES.
  501. *
  502. * @param string $http_body
  503. * @param string $http_post_boundary
  504. * @return void
  505. */
  506. protected static function parseUploadFiles($http_body, $http_post_boundary)
  507. {
  508. $http_body = \substr($http_body, 0, \strlen($http_body) - (\strlen($http_post_boundary) + 4));
  509. $boundary_data_array = \explode($http_post_boundary . "\r\n", $http_body);
  510. if ($boundary_data_array[0] === '') {
  511. unset($boundary_data_array[0]);
  512. }
  513. $key = -1;
  514. foreach ($boundary_data_array as $boundary_data_buffer) {
  515. list($boundary_header_buffer, $boundary_value) = \explode("\r\n\r\n", $boundary_data_buffer, 2);
  516. // Remove \r\n from the end of buffer.
  517. $boundary_value = \substr($boundary_value, 0, -2);
  518. $key ++;
  519. foreach (\explode("\r\n", $boundary_header_buffer) as $item) {
  520. list($header_key, $header_value) = \explode(": ", $item);
  521. $header_key = \strtolower($header_key);
  522. switch ($header_key) {
  523. case "content-disposition":
  524. // Is file data.
  525. if (\preg_match('/name="(.*?)"; filename="(.*?)"$/', $header_value, $match)) {
  526. // Parse $_FILES.
  527. $_FILES[$key] = array(
  528. 'name' => $match[1],
  529. 'file_name' => $match[2],
  530. 'file_data' => $boundary_value,
  531. 'file_size' => \strlen($boundary_value),
  532. );
  533. break;
  534. } // Is post field.
  535. else {
  536. // Parse $_POST.
  537. if (\preg_match('/name="(.*?)"$/', $header_value, $match)) {
  538. $_POST[$match[1]] = $boundary_value;
  539. }
  540. }
  541. break;
  542. case "content-type":
  543. // add file_type
  544. $_FILES[$key]['file_type'] = \trim($header_value);
  545. break;
  546. }
  547. }
  548. }
  549. }
  550. /**
  551. * Try GC sessions.
  552. *
  553. * @return void
  554. */
  555. public static function tryGcSessions()
  556. {
  557. if (HttpCache::$sessionGcProbability <= 0 ||
  558. HttpCache::$sessionGcDivisor <= 0 ||
  559. \rand(1, HttpCache::$sessionGcDivisor) > HttpCache::$sessionGcProbability) {
  560. return;
  561. }
  562. $time_now = \time();
  563. foreach(glob(HttpCache::$sessionPath.'/ses*') as $file) {
  564. if(\is_file($file) && $time_now - \filemtime($file) > HttpCache::$sessionGcMaxLifeTime) {
  565. \unlink($file);
  566. }
  567. }
  568. }
  569. }
  570. /**
  571. * Http cache for the current http response.
  572. */
  573. class HttpCache
  574. {
  575. public static $codes = array(
  576. 100 => 'Continue',
  577. 101 => 'Switching Protocols',
  578. 200 => 'OK',
  579. 201 => 'Created',
  580. 202 => 'Accepted',
  581. 203 => 'Non-Authoritative Information',
  582. 204 => 'No Content',
  583. 205 => 'Reset Content',
  584. 206 => 'Partial Content',
  585. 300 => 'Multiple Choices',
  586. 301 => 'Moved Permanently',
  587. 302 => 'Found',
  588. 303 => 'See Other',
  589. 304 => 'Not Modified',
  590. 305 => 'Use Proxy',
  591. 306 => '(Unused)',
  592. 307 => 'Temporary Redirect',
  593. 400 => 'Bad Request',
  594. 401 => 'Unauthorized',
  595. 402 => 'Payment Required',
  596. 403 => 'Forbidden',
  597. 404 => 'Not Found',
  598. 405 => 'Method Not Allowed',
  599. 406 => 'Not Acceptable',
  600. 407 => 'Proxy Authentication Required',
  601. 408 => 'Request Timeout',
  602. 409 => 'Conflict',
  603. 410 => 'Gone',
  604. 411 => 'Length Required',
  605. 412 => 'Precondition Failed',
  606. 413 => 'Request Entity Too Large',
  607. 414 => 'Request-URI Too Long',
  608. 415 => 'Unsupported Media Type',
  609. 416 => 'Requested Range Not Satisfiable',
  610. 417 => 'Expectation Failed',
  611. 422 => 'Unprocessable Entity',
  612. 423 => 'Locked',
  613. 500 => 'Internal Server Error',
  614. 501 => 'Not Implemented',
  615. 502 => 'Bad Gateway',
  616. 503 => 'Service Unavailable',
  617. 504 => 'Gateway Timeout',
  618. 505 => 'HTTP Version Not Supported',
  619. );
  620. public static $default = array(
  621. 'Content-Type' => 'Content-Type: text/html;charset=utf-8',
  622. 'Connection' => 'Connection: keep-alive',
  623. 'Server' => 'Server: workerman'
  624. );
  625. /**
  626. * @var HttpCache
  627. */
  628. public static $instance = null;
  629. public static $status = '';
  630. public static $header = array();
  631. public static $cookie = array();
  632. public static $gzip = false;
  633. public static $sessionPath = '';
  634. public static $sessionName = '';
  635. public static $sessionGcProbability = 1;
  636. public static $sessionGcDivisor = 1000;
  637. public static $sessionGcMaxLifeTime = 1440;
  638. public $sessionStarted = false;
  639. public $sessionFile = '';
  640. public static function reset()
  641. {
  642. self::$status = 'HTTP/1.1 200 OK';
  643. self::$header = self::$default;
  644. self::$cookie = array();
  645. self::$instance = new HttpCache();
  646. }
  647. public static function init()
  648. {
  649. if (!self::$sessionName) {
  650. self::$sessionName = \ini_get('session.name');
  651. }
  652. if (!self::$sessionPath) {
  653. self::$sessionPath = @\session_save_path();
  654. }
  655. if (!self::$sessionPath || \strpos(self::$sessionPath, 'tcp://') === 0) {
  656. self::$sessionPath = \sys_get_temp_dir();
  657. }
  658. if ($gc_probability = \ini_get('session.gc_probability')) {
  659. self::$sessionGcProbability = $gc_probability;
  660. }
  661. if ($gc_divisor = \ini_get('session.gc_divisor')) {
  662. self::$sessionGcDivisor = $gc_divisor;
  663. }
  664. if ($gc_max_life_time = \ini_get('session.gc_maxlifetime')) {
  665. self::$sessionGcMaxLifeTime = $gc_max_life_time;
  666. }
  667. }
  668. }
  669. HttpCache::init();
  670. define('NO_CLI', \PHP_SAPI !== 'cli');