Http.php 22 KB

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