Http.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. <?php
  2. /**
  3. * This file is part of workerman.
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the MIT-LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @author walkor<walkor@workerman.net>
  10. * @copyright walkor<walkor@workerman.net>
  11. * @link http://www.workerman.net/
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Workerman\Protocols;
  15. use Workerman\Connection\TcpConnection;
  16. use Workerman\Worker;
  17. /**
  18. * http protocol
  19. */
  20. class Http
  21. {
  22. /**
  23. * The supported HTTP methods
  24. * @var array
  25. */
  26. public static $methods = array('GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS');
  27. /**
  28. * Check the integrity of the package.
  29. *
  30. * @param string $recv_buffer
  31. * @param TcpConnection $connection
  32. * @return int
  33. */
  34. public static function input($recv_buffer, TcpConnection $connection)
  35. {
  36. if (!\strpos($recv_buffer, "\r\n\r\n")) {
  37. // Judge whether the package length exceeds the limit.
  38. if (\strlen($recv_buffer) >= $connection->maxPackageSize) {
  39. $connection->close();
  40. return 0;
  41. }
  42. return 0;
  43. }
  44. list($header,) = \explode("\r\n\r\n", $recv_buffer, 2);
  45. $method = \substr($header, 0, \strpos($header, ' '));
  46. if(\in_array($method, static::$methods)) {
  47. return static::getRequestSize($header, $method);
  48. }else{
  49. $connection->send("HTTP/1.1 400 Bad Request\r\n\r\n", true);
  50. return 0;
  51. }
  52. }
  53. /**
  54. * Get whole size of the request
  55. * includes the request headers and request body.
  56. * @param string $header The request headers
  57. * @param string $method The request method
  58. * @return integer
  59. */
  60. protected static function getRequestSize($header, $method)
  61. {
  62. if($method === 'GET' || $method === 'OPTIONS' || $method === 'HEAD') {
  63. return \strlen($header) + 4;
  64. }
  65. $match = array();
  66. if (\preg_match("/\r\nContent-Length: ?(\d+)/i", $header, $match)) {
  67. $content_length = isset($match[1]) ? $match[1] : 0;
  68. return $content_length + \strlen($header) + 4;
  69. }
  70. return $method === 'DELETE' ? \strlen($header) + 4 : 0;
  71. }
  72. /**
  73. * Parse $_POST、$_GET、$_COOKIE.
  74. *
  75. * @param string $recv_buffer
  76. * @param TcpConnection $connection
  77. * @return array
  78. */
  79. public static function decode($recv_buffer, TcpConnection $connection)
  80. {
  81. // Init.
  82. $_POST = $_GET = $_COOKIE = $_REQUEST = $_SESSION = $_FILES = array();
  83. $GLOBALS['HTTP_RAW_POST_DATA'] = '';
  84. // Clear cache.
  85. HttpCache::$header = array('Connection' => 'Connection: keep-alive');
  86. HttpCache::$instance = new HttpCache();
  87. // $_SERVER
  88. $_SERVER = array(
  89. 'QUERY_STRING' => '',
  90. 'REQUEST_METHOD' => '',
  91. 'REQUEST_URI' => '',
  92. 'SERVER_PROTOCOL' => '',
  93. 'SERVER_SOFTWARE' => 'workerman/'.Worker::VERSION,
  94. 'SERVER_NAME' => '',
  95. 'HTTP_HOST' => '',
  96. 'HTTP_USER_AGENT' => '',
  97. 'HTTP_ACCEPT' => '',
  98. 'HTTP_ACCEPT_LANGUAGE' => '',
  99. 'HTTP_ACCEPT_ENCODING' => '',
  100. 'HTTP_COOKIE' => '',
  101. 'HTTP_CONNECTION' => '',
  102. 'CONTENT_TYPE' => '',
  103. 'REMOTE_ADDR' => '',
  104. 'REMOTE_PORT' => '0',
  105. 'REQUEST_TIME' => \time(),
  106. '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 \Workerman\Protocols\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 = 0)
  262. {
  263. if (PHP_SAPI !== 'cli') {
  264. return $http_response_code ? \header($content, $replace, $http_response_code) : \header($content, $replace);
  265. }
  266. if (\strpos($content, 'HTTP') === 0) {
  267. $key = 'Http-Code';
  268. } else {
  269. $key = \strstr($content, ":", true);
  270. if (empty($key)) {
  271. return false;
  272. }
  273. }
  274. if ('location' === \strtolower($key) && !$http_response_code) {
  275. return self::header($content, true, 302);
  276. }
  277. if (isset(HttpCache::$codes[$http_response_code])) {
  278. HttpCache::$header['Http-Code'] = "HTTP/1.1 $http_response_code " . HttpCache::$codes[$http_response_code];
  279. if ($key === 'Http-Code') {
  280. return true;
  281. }
  282. }
  283. if ($key === 'Set-Cookie') {
  284. HttpCache::$header[$key][] = $content;
  285. } else {
  286. HttpCache::$header[$key] = $content;
  287. }
  288. return true;
  289. }
  290. /**
  291. * Remove header.
  292. *
  293. * @param string $name
  294. * @return void
  295. */
  296. public static function headerRemove($name)
  297. {
  298. if (PHP_SAPI !== 'cli') {
  299. \header_remove($name);
  300. return;
  301. }
  302. unset(HttpCache::$header[$name]);
  303. }
  304. /**
  305. * Set cookie.
  306. *
  307. * @param string $name
  308. * @param string $value
  309. * @param integer $maxage
  310. * @param string $path
  311. * @param string $domain
  312. * @param bool $secure
  313. * @param bool $HTTPOnly
  314. * @return bool|void
  315. */
  316. public static function setcookie(
  317. $name,
  318. $value = '',
  319. $maxage = 0,
  320. $path = '',
  321. $domain = '',
  322. $secure = false,
  323. $HTTPOnly = false
  324. ) {
  325. if (PHP_SAPI !== 'cli') {
  326. return \setcookie($name, $value, $maxage, $path, $domain, $secure, $HTTPOnly);
  327. }
  328. return self::header(
  329. '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'), false);
  335. }
  336. /**
  337. * sessionCreateId
  338. *
  339. * @return string
  340. */
  341. public static function sessionCreateId()
  342. {
  343. \mt_srand();
  344. return bin2hex(\pack('d', \microtime(true)) . \pack('N',\mt_rand(0, 2147483647)));
  345. }
  346. /**
  347. * sessionId
  348. *
  349. * @param string $id
  350. *
  351. * @return string|null
  352. */
  353. public static function sessionId($id = null)
  354. {
  355. if (PHP_SAPI !== 'cli') {
  356. return $id ? \session_id($id) : \session_id();
  357. }
  358. if (static::sessionStarted() && HttpCache::$instance->sessionFile) {
  359. return \str_replace('ses_', '', \basename(HttpCache::$instance->sessionFile));
  360. }
  361. return '';
  362. }
  363. /**
  364. * sessionName
  365. *
  366. * @param string $name
  367. *
  368. * @return string
  369. */
  370. public static function sessionName($name = null)
  371. {
  372. if (PHP_SAPI !== 'cli') {
  373. return $name ? \session_name($name) : \session_name();
  374. }
  375. $session_name = HttpCache::$sessionName;
  376. if ($name && ! static::sessionStarted()) {
  377. HttpCache::$sessionName = $name;
  378. }
  379. return $session_name;
  380. }
  381. /**
  382. * sessionSavePath
  383. *
  384. * @param string $path
  385. *
  386. * @return void
  387. */
  388. public static function sessionSavePath($path = null)
  389. {
  390. if (PHP_SAPI !== 'cli') {
  391. return $path ? \session_save_path($path) : \session_save_path();
  392. }
  393. if ($path && \is_dir($path) && \is_writable($path) && !static::sessionStarted()) {
  394. HttpCache::$sessionPath = $path;
  395. }
  396. return HttpCache::$sessionPath;
  397. }
  398. /**
  399. * sessionStarted
  400. *
  401. * @return bool
  402. */
  403. public static function sessionStarted()
  404. {
  405. if (!HttpCache::$instance) return false;
  406. return HttpCache::$instance->sessionStarted;
  407. }
  408. /**
  409. * sessionStart
  410. *
  411. * @return bool
  412. */
  413. public static function sessionStart()
  414. {
  415. if (PHP_SAPI !== 'cli') {
  416. return \session_start();
  417. }
  418. self::tryGcSessions();
  419. if (HttpCache::$instance->sessionStarted) {
  420. Worker::safeEcho("already sessionStarted\n");
  421. return true;
  422. }
  423. HttpCache::$instance->sessionStarted = true;
  424. // Generate a SID.
  425. if (!isset($_COOKIE[HttpCache::$sessionName]) || !\is_file(HttpCache::$sessionPath . '/ses_' . $_COOKIE[HttpCache::$sessionName])) {
  426. // Create a unique session_id and the associated file name.
  427. while (true) {
  428. $session_id = static::sessionCreateId();
  429. if (!\is_file($file_name = HttpCache::$sessionPath . '/ses_' . $session_id)) break;
  430. }
  431. HttpCache::$instance->sessionFile = $file_name;
  432. return self::setcookie(
  433. HttpCache::$sessionName
  434. , $session_id
  435. , \ini_get('session.cookie_lifetime')
  436. , \ini_get('session.cookie_path')
  437. , \ini_get('session.cookie_domain')
  438. , \ini_get('session.cookie_secure')
  439. , \ini_get('session.cookie_httponly')
  440. );
  441. }
  442. if (!HttpCache::$instance->sessionFile) {
  443. HttpCache::$instance->sessionFile = HttpCache::$sessionPath . '/ses_' . $_COOKIE[HttpCache::$sessionName];
  444. }
  445. // Read session from session file.
  446. if (HttpCache::$instance->sessionFile) {
  447. $raw = \file_get_contents(HttpCache::$instance->sessionFile);
  448. if ($raw) {
  449. $_SESSION = \unserialize($raw);
  450. }
  451. }
  452. return true;
  453. }
  454. /**
  455. * Save session.
  456. *
  457. * @return bool
  458. */
  459. public static function sessionWriteClose()
  460. {
  461. if (PHP_SAPI !== 'cli') {
  462. \session_write_close();
  463. return true;
  464. }
  465. if (!empty(HttpCache::$instance->sessionStarted) && !empty($_SESSION)) {
  466. $session_str = \serialize($_SESSION);
  467. if ($session_str && HttpCache::$instance->sessionFile) {
  468. return \file_put_contents(HttpCache::$instance->sessionFile, $session_str);
  469. }
  470. }
  471. return empty($_SESSION);
  472. }
  473. /**
  474. * End, like call exit in php-fpm.
  475. *
  476. * @param string $msg
  477. * @throws \Exception
  478. */
  479. public static function end($msg = '')
  480. {
  481. if (PHP_SAPI !== 'cli') {
  482. exit($msg);
  483. }
  484. if ($msg) {
  485. echo $msg;
  486. }
  487. throw new \Exception('jump_exit');
  488. }
  489. /**
  490. * Get mime types.
  491. *
  492. * @return string
  493. */
  494. public static function getMimeTypesFile()
  495. {
  496. return __DIR__ . '/Http/mime.types';
  497. }
  498. /**
  499. * Parse $_FILES.
  500. *
  501. * @param string $http_body
  502. * @param string $http_post_boundary
  503. * @return void
  504. */
  505. protected static function parseUploadFiles($http_body, $http_post_boundary)
  506. {
  507. $http_body = \substr($http_body, 0, \strlen($http_body) - (\strlen($http_post_boundary) + 4));
  508. $boundary_data_array = \explode($http_post_boundary . "\r\n", $http_body);
  509. if ($boundary_data_array[0] === '') {
  510. unset($boundary_data_array[0]);
  511. }
  512. $key = -1;
  513. foreach ($boundary_data_array as $boundary_data_buffer) {
  514. list($boundary_header_buffer, $boundary_value) = \explode("\r\n\r\n", $boundary_data_buffer, 2);
  515. // Remove \r\n from the end of buffer.
  516. $boundary_value = \substr($boundary_value, 0, -2);
  517. $key ++;
  518. foreach (\explode("\r\n", $boundary_header_buffer) as $item) {
  519. list($header_key, $header_value) = \explode(": ", $item);
  520. $header_key = \strtolower($header_key);
  521. switch ($header_key) {
  522. case "content-disposition":
  523. // Is file data.
  524. if (\preg_match('/name="(.*?)"; filename="(.*?)"$/', $header_value, $match)) {
  525. // Parse $_FILES.
  526. $_FILES[$key] = array(
  527. 'name' => $match[1],
  528. 'file_name' => $match[2],
  529. 'file_data' => $boundary_value,
  530. 'file_size' => \strlen($boundary_value),
  531. );
  532. break;
  533. } // Is post field.
  534. else {
  535. // Parse $_POST.
  536. if (\preg_match('/name="(.*?)"$/', $header_value, $match)) {
  537. $_POST[$match[1]] = $boundary_value;
  538. }
  539. }
  540. break;
  541. case "content-type":
  542. // add file_type
  543. $_FILES[$key]['file_type'] = \trim($header_value);
  544. break;
  545. }
  546. }
  547. }
  548. }
  549. /**
  550. * Try GC sessions.
  551. *
  552. * @return void
  553. */
  554. public static function tryGcSessions()
  555. {
  556. if (HttpCache::$sessionGcProbability <= 0 ||
  557. HttpCache::$sessionGcDivisor <= 0 ||
  558. \rand(1, HttpCache::$sessionGcDivisor) > HttpCache::$sessionGcProbability) {
  559. return;
  560. }
  561. $time_now = \time();
  562. foreach(glob(HttpCache::$sessionPath.'/ses*') as $file) {
  563. if(\is_file($file) && $time_now - \filemtime($file) > HttpCache::$sessionGcMaxLifeTime) {
  564. \unlink($file);
  565. }
  566. }
  567. }
  568. }
  569. /**
  570. * Http cache for the current http response.
  571. */
  572. class HttpCache
  573. {
  574. public static $codes = array(
  575. 100 => 'Continue',
  576. 101 => 'Switching Protocols',
  577. 200 => 'OK',
  578. 201 => 'Created',
  579. 202 => 'Accepted',
  580. 203 => 'Non-Authoritative Information',
  581. 204 => 'No Content',
  582. 205 => 'Reset Content',
  583. 206 => 'Partial Content',
  584. 300 => 'Multiple Choices',
  585. 301 => 'Moved Permanently',
  586. 302 => 'Found',
  587. 303 => 'See Other',
  588. 304 => 'Not Modified',
  589. 305 => 'Use Proxy',
  590. 306 => '(Unused)',
  591. 307 => 'Temporary Redirect',
  592. 400 => 'Bad Request',
  593. 401 => 'Unauthorized',
  594. 402 => 'Payment Required',
  595. 403 => 'Forbidden',
  596. 404 => 'Not Found',
  597. 405 => 'Method Not Allowed',
  598. 406 => 'Not Acceptable',
  599. 407 => 'Proxy Authentication Required',
  600. 408 => 'Request Timeout',
  601. 409 => 'Conflict',
  602. 410 => 'Gone',
  603. 411 => 'Length Required',
  604. 412 => 'Precondition Failed',
  605. 413 => 'Request Entity Too Large',
  606. 414 => 'Request-URI Too Long',
  607. 415 => 'Unsupported Media Type',
  608. 416 => 'Requested Range Not Satisfiable',
  609. 417 => 'Expectation Failed',
  610. 422 => 'Unprocessable Entity',
  611. 423 => 'Locked',
  612. 500 => 'Internal Server Error',
  613. 501 => 'Not Implemented',
  614. 502 => 'Bad Gateway',
  615. 503 => 'Service Unavailable',
  616. 504 => 'Gateway Timeout',
  617. 505 => 'HTTP Version Not Supported',
  618. );
  619. /**
  620. * @var HttpCache
  621. */
  622. public static $instance = null;
  623. public static $header = array();
  624. public static $gzip = false;
  625. public static $sessionPath = '';
  626. public static $sessionName = '';
  627. public static $sessionGcProbability = 1;
  628. public static $sessionGcDivisor = 1000;
  629. public static $sessionGcMaxLifeTime = 1440;
  630. public $sessionStarted = false;
  631. public $sessionFile = '';
  632. public static function init()
  633. {
  634. if (!self::$sessionName) {
  635. self::$sessionName = \ini_get('session.name');
  636. }
  637. if (!self::$sessionPath) {
  638. self::$sessionPath = @\session_save_path();
  639. }
  640. if (!self::$sessionPath || \strpos(self::$sessionPath, 'tcp://') === 0) {
  641. self::$sessionPath = \sys_get_temp_dir();
  642. }
  643. if ($gc_probability = \ini_get('session.gc_probability')) {
  644. self::$sessionGcProbability = $gc_probability;
  645. }
  646. if ($gc_divisor = \ini_get('session.gc_divisor')) {
  647. self::$sessionGcDivisor = $gc_divisor;
  648. }
  649. if ($gc_max_life_time = \ini_get('session.gc_maxlifetime')) {
  650. self::$sessionGcMaxLifeTime = $gc_max_life_time;
  651. }
  652. }
  653. }
  654. HttpCache::init();