首頁 >後端開發 >php教程 >PHP-CLI Web 伺服器

PHP-CLI Web 伺服器

WBOY
WBOY原創
2016-07-25 08:47:371215瀏覽
需要 pthreads 擴充
php 命令列模式運行

寫的比較急,沒時間優化,有時間我會抽空優化一下

來自來源網址:http://www.haowei.me/archives/1009.html
  1. ini_set('display_errors', false);
  2. error_reporting(0);
  3. if(!class_exists( ' thread')) {
  4. messages('PHP 運行環境不支援多執行緒');
  5. exit(0);
  6. }
  7. if(!function_exists('mime_content_type')) {
  8. logs('PHP 運作環境不支援函數mime_content_type()');
  9. exit(0);
  10. }
  11. class pthread extends thread {
  12. proetted sock = null;
  13. protected $arguments = null;
  14. protected $connections = 0;
  15. protected $octet_stream = false;
  16. public function __construct($socket, $arraarments = {
  17. $this->socket = $socket;
  18. $this->arguments = $arguments;
  19. if(!isset($this->arguments['ServerTokens'] ))
  20. $this->arguments['ServerTokens'] = 'off';
  21. }
  22. public function run() {
  23. date_default_timezzz') ;
  24. $clients = 1;
  25. $maxRequests = !isset($this->arguments['MaxRequests'])?
  26. intval($this->arguments ['MaxRequests']):
  27. 100;
  28. $timeout = 5;
  29. $connfd = socket_accept($this->socket);
  30. socket_set_option($connfd, SOL_VTKET, SO_SOCpEO' $timeout, 'usec' =>; 0));
  31. $session = 1;
  32. while($session) {
  33. $buffer = '';
  34. while (( $buffer .= socket_read ($connfd, 1024, PHP_BINARY_READ) ))
  35. if(strpos($buffer, "rnrn") !== false) break;
  36. if($buffer == '') {
  37. socket_close($connfd);
  38. $session = 0;
  39. }else{
  40. $availableRequests = $maxRequests - $clients;
  41. $
  42. ;
  43. $i = 0;
  44. $headers = array();
  45. array_push($headers, 'HTTP/1.1 200 OK');
  46. array_push($headers, '日期:' .gmtdate());
  47. array_push($headers, '伺服器: PHP-CLI/1.0');
  48. array_push($headers, '內容類型: text/html; charset=utf-8');
  49. array_push($headers, '連線:關閉');
  50. if($this->arguments['ServerTokens'] == 'on')
  51. $headers[2] = '伺服器:Tokens'] == 'on')
  52. $headers[2] = '伺服器: PHP-CLI';
  53. $buffer =explode("rn", $buffer);
  54. $http_user_agent = '';
  55. $http_request_method = '';
  56. $http_request_file = '';
  57. $http_protocol = '';
  58. $extension = '';
  59. $mime_types = '';
  60. $this->octet_stream = false;
  61. foreach( $buffer as $line) {
  62. $pattern = '/(GET|POST)s/(.*)s(HTTP/1.[0-1])$/';
  63. if(preg_match($ pattern, $line)) {
  64. $http_request_method = preg_replace($pattern, '\1', $line);
  65. $http_request_file = preg_replace($pattern, '\2', $line);
  66. $http_protocol = preg_replace($模式, '\3', $line);
  67. }
  68. $pattern = '/^用戶代理: (. )$/';
  69. if(preg_match($pattern, $line)) {
  70. $http_user_agent = preg_replace($pattern, '\1', $line);
  71. }
  72. }
  73. $local_request_file = $this->arguments['DocumentRoot['DocumentRoot '].'/'。 $http_request_file;
  74. if(file_exists($local_request_file) && is_file($local_request_file))
  75. $extension = pathinfo($local_request_file, PATHINFO_EXTENSION); $array_key_exists = array_key_exists($extension, $this->arguments['MimeTypes']);
  76. if(is_file($local_request_file)) {
  77. if($array_key; mime_types = $this->arguments['MimeTypes'][$extension];
  78. $headers[3] = sprintf('Content-Type: %s; charset=%s', $mime_types, 'utf- 8' );
  79. }else{
  80. $this->octet_stream = true;
  81. $headers[3] = sprintf('Content-Type: application/octet-stream');
  82. array_push ($headers , '接受範圍: 位元組');
  83. array_push($headers, '接受長度: '.filesize($local_request_file));
  84. array_push($headers, '內容處置: 附件; filename='.namebase ($local_request_file));
  85. }
  86. }
  87. }
  88. $html = '';
  89. $code = '';
  90. $this->HttpStatusCode ($local_request_file, $headers, $html, $code);
  91. if($availableRequests > 0) {
  92. $headers[4] = "連結: 保持活動";
  93. $headers[ 5] = '保持活動: timeout='.$timeout.', max='.$maxRequests;
  94. }
  95. $headers[6] = '內容長度:'。 strlen($html);
  96. $response = array(
  97. 'header'=> implode("rn", $headers) . "rn",
  98. 'html'=> $html) ;
  99. socket_write($connfd, implode("rn", $response));
  100. if($availableRequests socket_close($availableRequests socket_close($connfd); }
  101. $length = strlen($html);
  102. socket_getpeername($connfd, $address, $port);
  103. log(sprintf('%s:%.0f -- " %s %s %s" %s %.0f "-" "%s"',
  104. $位址,
  105. $port,
  106. $http_request_method,
  107. '/'.$http_request_file,
  108. $http_protocol,
  109. $code,
  110. $length,
  111. $http_user_agent));
  112. //logs('times '.intval($clients - 1), false);
  113. }
  114. }
  115. }
  116. public function error_page($statusCode, $ServerTokens) {
  117. $httpStatus = array('403'=> '403'=> '403 禁止' '404'=> '404 找不到');
  118. $string = "
  119. %s
  120. %s


  121. %s
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn