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