Http.php 23 KB

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