実行するには pthreads 拡張機能 php コマンドラインモードが必要です
急いで書いたので最適化する時間がありません。時間があるときに時間をかけて最適化します
。来源地址: http://www.haowei.me/archives/1009.html
- ini_set('display_errors', false);
- error_reporting(0);
- if(!class_exists('thread')) {
- logs('PHP ランタイム環境はマルチスレッドをサポートしていません' );
- exit(0);
- }
- if(!function_exists('mime_content_type')) {
- logs('PHP ランタイム環境は function mime_content_type() をサポートしていません');
- exit(0);
- }
- class pthread はスレッドを拡張します {
-
- protected $socket = null;
- protected $arguments = null;
- protected $connections = 0;
- protected $octet_stream = false;
-
- public function __construct($socket, $arguments = array()) {
-
- $this->socket = $socket;
- $this->arguments = $arguments;
- if(!isset($this->arguments['ServerTokens']))
- $this->arguments[ 'ServerTokens'] = 'off';
-
- }
-
- public function run() {
-
- date_default_timezone_set('UTC');
-
- $clients = 1;
- $maxRequests = !isset($this->arguments ['MaxRequests'])?
- intval($this->arguments['MaxRequests']):
- 100;
- $timeout = 5;
-
- $connfd =ソケット_accept($this->socket);
- socket_set_option( $connfd、SOL_SOCKET、SO_RCVTIMEO、array('sec' => $timeout, 'usec' => 0));
-
- $session = 1;
-
- while($session) {
-
- $buffer = '';
- while (( $buffer .=ソケット_read($connfd, 1024, PHP_BINARY_READ) ))
- if(strpos ($buffer, "rnrn") !== false) Break;
-
- if($buffer == '') {
- ソケット_クローズ($connfd);
- $session = 0;
- }else{
-
- $availableRequests = $ maxRequests - $clients;
- $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, 'Content-Type: text/html; charset=utf-8');
- array_push($headers , '接続: 閉じる');
-
- if($this->arguments['ServerTokens'] == 'on')
- $headers[2] = 'サーバー: PHP-CLI';
-
- $buffer = 爆発("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($pattern, '\3', $line);
- }
- $pattern = '/^User-Agent: (.+)$/';
- if(preg_match($) pattern, $line)) {
- $http_user_agent = preg_replace($pattern, '\1', $line);
- }
- }
-
- $local_request_file = $this->arguments['DocumentRoot'].'/'。 $http_request_file;
- if(file_exists($local_request_file) && is_file($local_request_file))
- $extension = pathinfo($local_request_file, PATHINFO_EXTENSION);
-
- if(file_exists($local_request_file)) {
- $array_key_exists = array_key_exists($extension, $this->arguments['MimeTypes']);
-
- if(is_file($local_request_file)) {
- if($array_key_exists) {
- $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, 'Accept-Range: bytes');
- array_push($headers, 'Accept-Length: '.filesize( $local_request_file));
- array_push($headers, 'Content-Disposition:attachment; filename='.basename($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 ソケットクローズ($connfd);
- $session = 0;
- }
-
- $length = strlen($html);
-
- socket_getpeername($connfd, $address, $port);
- logs(sprintf('%s:%.0f -- "%s %s %s" %s % .0f "-" "%s"',
- $address,
- $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 禁止', '404'=> '404 が見つかりません');
- $string = "
-
- %s
-
-
%sgt; -
- %s
-
- ";
-
- if(!in_array($ServerTokens, array('on', 'off')))
- $ServerTokens = 'off';
-
- return (string) sprintf($string,
- $httpStatus[$statusCode],
- $httpStatus[$statusCode],
- $ServerTokens == 'off' ? 'PHP-CLI/1.0' : 'PHP-CLI');
-
- }
-
- public function HttpStatusCode($file) , &$headers, &$html, &$code) {
-
- $code = '200';
- if(!file_exists($file)) {
- $headers[0] = 'HTTP/1.1 404 が見つかりません';
- $html = $this->error_page('404', $this->arguments['ServerTokens']);
- $code = '404';
- return 0;
- }
-
- if(is_dir($file) )){
- $find = false;
- $directoryIndex = $this->arguments['DirectoryIndex'];
- if(empty($directoryIndex)) {
- $headers[0] = 'HTTP/1.1 403 禁止';
- $code = '403';
- }else{
- $list =explode(' ', $directoryIndex);
- foreach($list as $index) {
- if(file_exists($file .'/'. $index)) {
- $file .= '/'。 $index;
- if(file_exists($file) && is_file($file))
- $extension = pathinfo($file, PATHINFO_EXTENSION);
- $array_key_exists = array_key_exists($extension, $this->arguments['MimeTypes'] );
- if($array_key_exists) {
- $mime_types = $this->arguments['MimeTypes'][$extension];
- }else{
- $this->otect_stream = true;
- $headers[3] = sprintf('Content-Type: application/octet-stream');
- array_push($headers, 'Accept-Ranges: bytes');
- array_push($headers, 'Accept-Length: '.filesize($local_request_file));
- array_push($headers, 'Content-Disposition:attachment; filename='.basename($local_request_file));
- }
- $find = true;
- Break;
- }
- }
- }
-
- if(!$find) {
- $html = $this->error_page('403', $this->arguments['ServerTokens']);
- }else{
- if(!$this->octet_stream)
- $headers[3] = sprintf('Content-Type: %s; charset=%s', $mime_types, 'utf-8');
- $html = $this->get_local_handle_buffer($file);
- }
-
- return -1;
-
- }else{
-
- $html = $this->get_local_handle_buffer($file);
-
- }
-
- return 1;
- }
-
- public function get_local_handle_buffer($file) {
- $handle = fopen($file, ' rb');
- return $this->get_buffer($handle);
- }
-
- public function get_buffer($handle) {
- $buffer = '';
- if(!is_resource($handle)) return null;
- while(!feof($handle))
- $buffer .= fgets($handle, 1024);
- fclose($handle);
- return $buffer;
- }
-
- }
-
- function gmtdate() {
- return (string) ) date('D, d M Y H:i:s')。 ' GMT';
- }
-
- function logs($string, $perfix = true) {
- ob_start();
- echo $perfix ?
- sprintf("[ %s ] %sn", date('d-M-Y H:i:s'), $string) :
- sprintf(" [ %s ]n", $string);
-
- ob_end_flush();
-
- }
-
- $mime_types = array(
- 'htm'=> 'text/html',
- 'html'=> 'text/html',
- 'jpg'=> 'image/jpeg',
- 'jpeg' => '画像/jpeg',
- 'png'=> '画像/png',
- 'css'=> xml'=> 'text/xml');
-
- $conf = array(
- 'MimeTypes'=> $mime_types,
- 'ServerTokens'=> 'on',
- 'MaxRequests'=> 100,
- 'タイムアウト'=> 15,
- 'Listen'=> '/home/www',
- 'DirectoryIndex'=> error_reporting(E_ALL);
- logs('動作環境の初期化');
- sleep(1);
-
- set_time_limit(0);
- logs('PHP-CLI実行タイムアウトの初期化');
- sleep(1);
-
- $socket =ソケット_create(AF_INET, SOCK_STREAM, SOL_TCP);
- logs('ソケットの初期化');
- sleep(1);
-
- logs('初期化バインドローカルアドレス任意のIPアドレス');
- sleep(1);
-
- $int =ソケット_バインド($socket, '0.0.0.0', $conf['Listen']);
- logs('初期化バインドローカルポート'.$conf['Listen']);
- if(!$int) {
- logs($conf['Listen'].' ポートは他のサービスによって占有されています'."n");
- exit(0);
- }
- sleep(1);
-
- socket_listen($socket, 1024);
- logs('ソケットをリッスンしている');
- sleep(1);
-
- logs('クライアントのアクセスを待機中');
- echo "n";
- $i = 0;
-
- while(1) {
- $pthread[$i] = new pthread($socket, $conf);
- $pthread[$i]->start();
- $pthread[$i]->join();
- }
-
-
-
- 复制幣
-
-
|