Requires pthreads extension PHP command line mode run
Write more urgent, no time to optimize, there is time I will take a moment to optimize
From Source Address: http://www.haowei.me/archives/1009.html
-
- Ini_set (' display_errors ', false);
- error_reporting (0);
- if (!class_exists (' thread ')) {
- Logs (' PHP Runtime Environment not support multi thread ');
- Exit (0);
- }
- if (!function_exists (' Mime_content_type ')) {
- Logs (' PHP runtime Environment not support function Mime_content_type () ');
- Exit (0);
- }
- Class Pthread extends Thread {
- 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 = socket_accept ($this->socket);
- Socket_set_option ($CONNFD, Sol_socket, So_rcvtimeo, Array (' sec ' = $timeout, ' usec ' = 0));
- $session = 1;
- while ($session) {
- $buffer = ";
- while ($buffer. = Socket_read ($CONNFD, 1024x768, Php_binary_read)))
- if (Strpos ($buffer, "\r\n\r\n")!== false) break;
- if ($buffer = = ") {
- Socket_close ($CONNFD);
- $session = 0;
- }else{
- $availableRequests = $maxRequests-$clients;
- $clients + +;
- $i = 0;
- $headers = Array ();
- Array_push ($headers, ' http/1.1 OK ');
- Array_push ($headers, ' Date: '. gmtdate ());
- Array_push ($headers, ' server:php-cli/1.0 ');
- Array_push ($headers, ' content-type:text/html; Charset=utf-8 ');
- Array_push ($headers, ' connection:close ');
- if ($this->arguments[' servertokens '] = = ' on ')
- $headers [2] = ' server:php-cli ';
- $buffer = Explode ("\ r \ n", $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-ranges: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] = "connection:keep-alive";
- $headers [5] = ' keep-alive:timeout= '. $timeout. ', max= '. $maxRequests;
- }
- $headers [6] = ' content-length: '. Strlen ($html);
- $response = Array (
- ' Header ' = Implode ("\ r \ n", $headers). "\ r \ n",
- ' html ' = $html);
- Socket_write ($CONNFD, implode ("\ r \ n", $response));
- if ($availableRequests <= 0) {
- Socket_close ($CONNFD);
- $session = 0;
- }
- $length = strlen ($html);
- Socket_getpeername ($CONNFD, $address, $port);
- Logs (sprintf ('%s:%.0f--"%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 Forbidden ', ' 404 ' = ' 404 Not Found ');
- $string = "
- %s
-
%s
-
- %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 Not Found ';
- $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 Forbidden ';
- $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]%s\n", date (' D-m-y h:i:s '), $string):
- sprintf ("\0\0[%s]\n", $string);
- Ob_end_flush ();
- }
- $mime _types = Array (
- ' htm ' = ' text/html ',
- ' html ' = ' text/html ',
- ' jpg ' = ' image/jpeg ',
- ' jpeg ' = ' image/jpeg ',
- ' png ' = ' image/png ',
- ' js ' = ' text/javascript ',
- ' CSS ' = ' text/css ',
- ' xml ' = ' text/xml ');
- $conf = Array (
- ' Mimetypes ' = $mime _types,
- ' Servertokens ' = ' on ',
- ' maxrequests ' = 100,
- ' Timeout ' = 15,
- ' Listen ' = 8080,
- ' DocumentRoot ' = '/home/www ',
- ' DirectoryIndex ' = ' index.htm index.html ');
- Error_reporting (E_all);
- Logs (' Initializing the Operating Environment ');
- Sleep (1);
- Set_time_limit (0);
- Logs (' Initializing php-cli execution timeout ');
- Sleep (1);
- $socket = Socket_create (Af_inet, Sock_stream, sol_tcp);
- Logs (' Initializing the socket ');
- Sleep (1);
- Logs (' initialization bind local address any IP address ');
- Sleep (1);
- $int = Socket_bind ($socket, ' 0.0.0.0 ', $conf [' Listen ']);
- Logs (' Initialization bind local port '. $conf [' Listen ']);
- if (! $int) {
- Logs ($conf [' Listen ']. ' Port is occupied by other services '. " \ n ");
- Exit (0);
- }
- Sleep (1);
- Socket_listen ($socket, 1024);
- Logs (' Opening a socket listening ');
- Sleep (1);
- Logs (' Waiting for clients to access ');
- echo "\ n";
- $i = 0;
- while (1) {
- $pthread [$i] = new Pthread ($socket, $conf);
- $pthread [$i]->start ();
- $pthread [$i]->join ();
- }
Copy Code |