Http.php 23 KB

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