W

Webserver in PHP

phpinfo Mi., 18. Oktober, 2006 um 23:24 #1

Ich habe mal dieses interressante Skript im Internet gefunden ...

Leider weiss ich nimmer wer es geschrieben hat ...
Falls es wer weiss, so bitte ich denjenigen mir die E-Mailadresse des Prgammierers zusenden

[php:1]<?php

/**
* httPHP - the tiny httpd written PHP
*
* PHP can do more than just run under a webserver
*
*
*/

/****************
* Settings start here
*/

/**
* where to listen - 0.0.0.0 for all interfaces
*/
define('LISTEN_IP', '0.0.0.0');
/**
* port to listen
*/
define('LISTEN_PORT', '7000');
/**
* time a client may wait between sending parts of the request
*/
define('CLIENT_LA_TIMEOUT', 2);
/**
* time a client may take before finishing the request
*/
define('CLIENT_TIMEOUT', 5);

/**
* count of bytes after the request will be truncated
*/
define('MAX_REQUEST_LENGTH', 100*1024);

/**
* maximum concurrent clients
*/
define('MAX_CLIENTS', 100);


/*
* Settings end here
********************/

define('CLOSE_CONNECTION', true);
define('NO_HEADER', false);

set_time_limit(0);

class client {
var $socket = null;
var $data = '';
var $request = '';
var $first_action = 0;
var $last_action = 0;
var $output = '';
var $error = false;
var $remote_host = '';
var $remote_port = '';

function log($message) {
echo "[{$this->remote_host}:{$this->remote_port}] $message\n";
}

function Client($socket) {
$this->socket = $socket;
$this->first_action = time();
$this->last_action = time();
socket_getpeername($socket, $this->remote_host, $this->remote_port);
}



function disconnect() {
@socket_shutdown($this->socket);
socket_close($this->socket);
}

function read($data) {
$this->last_action = time();
$this->data .= $data;

if(strstr($this->data, "\r\n\r\n") || (strlen($this->data) > MAX_REQUEST_LENGTH)) { // request finished
$this->request = strtok($this->data, "\r");
}
}

function write($message, $close = false, $header = true) {
$this->last_action = time();
if($header) {
$message = "HTTP/1.0 200 OK\r\nServer: httPHP\r\n\r\n".$message;
}
socket_write($this->socket, $message, strlen($message));
if($close) {
$this->disconnect();
}
}

function error($errorcode) {
switch($errorcode) {
case 400:
$error_string = 'Bad Request';
break;
case 401:
$error_string = 'Unauthorized';
break;
case 403:
$error_string = 'Fobidden';
break;
case 404:
$error_string = 'Not Found';
break;
case 501:
$error_string = 'Not Implemented';
break;
case 502:
$error_string = 'Bad Gateway';
break;
case 503:
$error_string = 'Service Unavaible';
break;

default:
$errorcode = 500;
$error_string = 'Internal Server Error';
}
$error_string = $errorcode.' '.$error_string;
$message = 'HTTP/1.0 '.$error_string."\r\nServer: httPHP\r\n\r\n";
$message = '<title>'.$error_string.'</title><body>'.$error_string;
$this->write($message, CLOSE_CONNECTION, NO_HEADER);
}

function do_request() {
if(empty($this->request)) {
return false;
}
if(!preg_match('%^GET\s([^\s]*)%', $this->request, $match)) {
$this->error($socket, 501);
} else {
$this->request = $match[1];
$this->request();
if($this->error) {
$this->error($this->error);
} else {
$this->write($this->output, CLOSE_CONNECTION);
}
}

return true;
}

function request() {

// in request.inc:
// $request is the current request
// set $error to the http return code 4xx or 5xx if you need to
// do return when error occured or output is finished
// add your output to $output

include 'request.inc';
}

function timeout() {
if((time() - $this->last_action) > CLIENT_LA_TIMEOUT) {
// should add a write here ...
$this->disconnect();
$this->log('too much time between send data');
return true;
} else if((time() - $this->first_action) > CLIENT_TIMEOUT) {
// should add a write here ...
$this->disconnect();
$this->log('too much time since connection init');
return true;
}

return false;
}
}

class Server {
var $listen;
var $clients = array();
var $sockets = array();
var $peak = 0;


function log($message) {
echo "[server] $message\n";
}

function Server($ip = null, $port = null) {
if($ip && $port) {
$this->init($ip, $port);
}
}

function init($ip = LISTEN_IP, $port = LISTEN_PORT) {
$this->log('starting server ...');

$this->log('loading socket module');
dl('php_sockets.dll');

$this->log('creating socket');

$this->listen = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if(!is_resource($this->listen)) {
die('cannot create socket');
}

if(!socket_setopt($this->listen, SOL_SOCKET, SO_REUSEADDR, 1)) {
socket_close($this->listen);
die('cannot set socket to accepted many connections');
}

$this->log('binding socket');

if(!socket_bind($this->listen, LISTEN_IP, LISTEN_PORT)) {
socket_close($this->listen);
die('cannot bind socket');
}

$this->log('start listening');

if(!socket_listen($this->listen)) {
socket_close($this->listen);
die('cannot listen');
}
}

function run() {
$this->log('running');
while(1) {
$set = array_merge($this->listen, $this->sockets);
if(@socket_select($set, $set_write = NULL, $set_exp = NULL, 1, 0) > 0) {
$this->_check_sockets($set);
}

foreach($this->sockets as $socket) {
if($this->clients[$socket]->do_request()) {
unset($this->clients[$socket]);
unset($this->sockets[$socket]);
} else if($this->clients[$socket]->timeout()){
unset($this->clients[$socket]);
unset($this->sockets[$socket]);
}
}
}
}

function _check_sockets(&$sockets) {
foreach($sockets as $socket) {
if($socket == $this->listen) {
$this->_new_client($socket);
} else {
$this->_read_data($socket);
}
}

}

function _new_client($socket) {
$connection = socket_accept($this->listen);
if($connection < 1) {
$this->log('failed to accept connection');
}
$this->sockets[$connection] = $connection;
$this->clients[$connection] = new Client($connection);
if(count($this->clients) > MAX_CLIENTS) {
$this->clients[$connection]->error(503);
unset($this->sockets[$connection]);
unset($this->clients[$connection]);
$this->log('Too many clients');
}
if(count($this->clients) > $this->peak) {
$this->peak = count($this->clients);
$this->log($this->peak.' concurrent clients is peak');
}
}

function _read_data($socket) {
$data = socket_read($socket, 1024);
if($data === false) { // socket closed
$this->clients[$socket]->disconnect();
unset($this->clients[$socket]);
} else {
$this->clients[$socket]->read($data);
}
}
}

// Let's get started ...
$server = new Server(LISTEN_IP, LISTEN_PORT);
$server->run();

?>[/php:1]



C&M distanziert sich konkret und ausdrücklich vom Inhalt dieses Postings.
Der Ersteller des Postings haftet für seine Äußerungen.
Inhalte, die nicht den Forumsregeln entsprechen sind bitte vom Leser zu melden ...

phpinfo Mi., 18. Oktober, 2006 um 23:25 #2

Hier noch das ZIP-File dazu:



C&M distanziert sich konkret und ausdrücklich vom Inhalt dieses Postings.
Der Ersteller des Postings haftet für seine Äußerungen.
Inhalte, die nicht den Forumsregeln entsprechen sind bitte vom Leser zu melden ...

aleX Do., 19. Oktober, 2006 um 13:16 #3

Habs mal auf "Wichtig" gesetzt ...
Find ich sehr interressant das Skript



Top