Pthreads extension required Php command line mode
Writing is urgent and there is no time to optimize it. I will take time to optimize it.
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, 1024, PHP_BINARY_READ )))
- If (strpos ($ buffer, "\ 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 200 OK ');
- Array_push ($ headers, 'date: '. gmtdate ());
- Array_push ($ headers, 'server: PHP-CLI/100 ');
- 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 %. 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 ('20140901' => '20160901', '20160901' => '20160901' 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/123456': 'php-CLI ');
- }
- Public function HttpStatusCode ($ file, & $ headers, & $ html, & $ code ){
- $ Code = '000000 ';
- If (! File_exists ($ file )){
- $ Headers [0] = 'http/1.1 404 Not Found ';
- $ Html = $ this-> error_page ('20140901', $ this-> arguments ['servertokens']);
- $ Code = '000000 ';
- Return 0;
- }
- If (is_dir ($ file )){
- $ Find = false;
- $ DirectoryIndex = $ this-> arguments ['directoryindex'];
- If (empty ($ directoryIndex )){
- $ Headers [0] = 'http/1.1 403 Forbidden ';
- $ Code = '000000 ';
- } 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 ('20140901', $ 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 (
- 'Metypes '=> $ mime_types,
- 'Servertokens' => 'on ',
- 'Maxrequests' => 100,
- 'Timeout' => 15,
- 'Listen' = & gt; 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 ();
- }
|