> Forums > PHP

W

Webserver in PHP

Erstellt
Okt. '06
letzte Antwort Antworten
2
Aufrufe
5.1T
25
„Gefällt mir“
Abos
Noch keine
Mi., 18. Oktober, 2006 um 23:24
#1

Ich habe mal dieses interessante 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 :wink:

PHP:
<?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 " $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(*)%', $this->request, $match)) {
$this->error($socket, 501);
} else {
$this->request = $match;
$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 " $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->do_request()) {
unset($this->clients);
unset($this->sockets);
} else if($this->clients->timeout()){
unset($this->clients);
unset($this->sockets);
}
}
}
}

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;
$this->clients = new Client($connection);
if(count($this->clients) > MAX_CLIENTS) {
$this->clients->error(503);
unset($this->sockets);
unset($this->clients);
$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->disconnect();
unset($this->clients);
} else {
$this->clients->read($data);
}
}
}

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

?>




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 ...
 
1
 
0

echo "Php Rules!";
http://www.php.net

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

Hier noch das ZIP-File dazu:


Download webserver.zip (3.25 kB, 1 mal)
MD5: 301bbc5808cafb6d0a0f3abdb2b6ff92
SHA1: 3373357729f46f179783b818a4711f96be039f18
CRC32: 6125d8ce




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 ...
 
1
 
0

echo "Php Rules!";
http://www.php.net

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

Hab es mal auf "Wichtig" gesetzt ...
Find ich sehr interessant das Skript
 
1
 
0

Der Mensch ist ein naiver Tourist mit einem abgelaufenem Visum für den Planeten Erde ..

> Forums > PHP

Du hast bereits für diesen Post abgestimmt...

;-)



Logo https://t.ress.at/r4WCe/


Ähnliche Themen: