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