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