Http.php 22 KB

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