Http.php 24 KB

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