需要 pthreads 擴充 php 命令列模式運行
寫的比較急,沒時間優化,有時間我會抽空優化一下
來自來源網址:http://www.haowei.me/archives/1009.html
- ini_set('display_errors', false);
- error_reporting(0);
-
- if(!class_exists( ' thread')) {
- messages('PHP 運行環境不支援多執行緒');
- exit(0);
- }
-
- if(!function_exists('mime_content_type')) {
- logs('PHP 運作環境不支援函數mime_content_type()');
- exit(0);
- }
-
- class pthread extends thread {
-
- proetted sock = null;
- protected $arguments = null;
- protected $connections = 0;
- protected $octet_stream = false;
-
- public function __construct($socket, $arraarments = {
-
- $this->socket = $socket;
- $this->arguments = $arguments;
- if(!isset($this->arguments['ServerTokens'] ))
- $this->arguments['ServerTokens'] = 'off';
-
-
- }
-
- public function run() {
-
- date_default_timezzz') ;
-
- $clients = 1;
- $maxRequests = !isset($this->arguments['MaxRequests'])?
- intval($this->arguments ['MaxRequests']):
- 100;
- $timeout = 5;
-
- $connfd = socket_accept($this->socket);
- socket_set_option($connfd, SOL_VTKET, SO_SOCpEO' $timeout, 'usec' =>; 0));
-
- $session = 1;
-
- while($session) {
-
- $buffer = '';
- while (( $buffer .= socket_read ($connfd, 1024, PHP_BINARY_READ) ))
- if(strpos($buffer, "rnrn") !== false) break;
-
- if($buffer == '') {
- socket_close($connfd);
- $session = 0;
- }else{
-
- $availableRequests = $maxRequests - $clients;
- $
- ;
-
- $i = 0;
- $headers = array();
- array_push($headers, 'HTTP/1.1 200 OK');
- array_push($headers, '日期:' .gmtdate());
- array_push($headers, '伺服器: PHP-CLI/1.0');
- array_push($headers, '內容類型: text/html; charset=utf-8');
- array_push($headers, '連線:關閉');
-
- if($this->arguments['ServerTokens'] == 'on')
- $headers[2] = '伺服器:Tokens'] == 'on')
- $headers[2] = '伺服器: PHP-CLI';
-
- $buffer =explode("rn", $buffer);
-
- $http_user_agent = '';
- $http_request_method = '';
- $http_request_file = '';
- $http_protocol = '';
- $extension = '';
- $mime_types = '';
- $this->octet_stream = false;
-
- foreach( $buffer as $line) {
- $pattern = '/(GET|POST)s/(.*)s(HTTP/1.[0-1])$/';
- if(preg_match($ pattern, $line)) {
- $http_request_method = preg_replace($pattern, '\1', $line);
- $http_request_file = preg_replace($pattern, '\2', $line);
- $http_protocol = preg_replace($模式, '\3', $line);
- }
- $pattern = '/^用戶代理: (. )$/';
- if(preg_match($pattern, $line)) {
- $http_user_agent = preg_replace($pattern, '\1', $line);
- }
- }
-
- $local_request_file = $this->arguments['DocumentRoot['DocumentRoot '].'/'。 $http_request_file;
- if(file_exists($local_request_file) && is_file($local_request_file))
- $extension = pathinfo($local_request_file, PATHINFO_EXTENSION); $array_key_exists = array_key_exists($extension, $this->arguments['MimeTypes']);
-
- if(is_file($local_request_file)) {
- if($array_key; mime_types = $this->arguments['MimeTypes'][$extension];
- $headers[3] = sprintf('Content-Type: %s; charset=%s', $mime_types, 'utf- 8' );
- }else{
- $this->octet_stream = true;
- $headers[3] = sprintf('Content-Type: application/octet-stream');
- array_push ($headers , '接受範圍: 位元組');
- array_push($headers, '接受長度: '.filesize($local_request_file));
- array_push($headers, '內容處置: 附件; filename='.namebase ($local_request_file));
- }
- }
-
- }
-
- $html = '';
- $code = '';
- $this->HttpStatusCode ($local_request_file, $headers, $html, $code);
-
- if($availableRequests > 0) {
- $headers[4] = "連結: 保持活動";
- $headers[ 5] = '保持活動: timeout='.$timeout.', max='.$maxRequests;
- }
-
-
- $headers[6] = '內容長度:'。 strlen($html);
-
- $response = array(
- 'header'=> implode("rn", $headers) . "rn",
- 'html'=> $html) ;
-
- socket_write($connfd, implode("rn", $response));
-
- if($availableRequests socket_close($availableRequests socket_close($connfd); }
-
- $length = strlen($html);
-
- socket_getpeername($connfd, $address, $port);
- log(sprintf('%s:%.0f -- " %s %s %s" %s %.0f "-" "%s"',
- $位址,
- $port,
- $http_request_method,
- '/'.$http_request_file,
- $http_protocol,
- $code,
- $length,
- $http_user_agent));
- //logs('times '.intval($clients - 1), false);
- }
- }
-
- }
-
- public function error_page($statusCode, $ServerTokens) {
-
- $httpStatus = array('403'=> '403'=> '403 禁止' '404'=> '404 找不到');
- $string = "
-
- %s
-
-
-
%s
-
- %s
-
-
|