Http.php 24 KB

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