Ejemplo n.º 1
0
 static function startServer()
 {
     $server = new WebServer(function (WebRequest $request) {
         self::registerAll();
         if ($request->servletPath == '/bench') {
             self::showResult(self::startBench());
         } else {
             $t = microtime(1);
             foreach (self::$benchmarks as $benchmark) {
                 $benchmark->run();
             }
             echo "----- total: " . (microtime(1) - $t);
         }
     });
     $server->hotReload = false;
     $server->isolated = false;
     $server->run();
 }
Ejemplo n.º 2
0
 public function __construct($tokenEnabled = true, $token = "", $whitelistEnabled = true, $whitelist = "")
 {
     $this->server = new WebServer(function (WebRequest $req, WebResponse $res) {
         require 'res://class/WebServerRouter.php';
         require 'res://class/WebServerRoute.php';
         $current = WebServer::current();
         $drop = false;
         if ($current->tokenEnabled) {
             $drop = true;
             foreach ($req->cookies as $cookie) {
                 if (strtolower($cookie['name']) == "token" && in_array($cookie['value'], $current->token)) {
                     $drop = false;
                 }
             }
         }
         if ($current->whitelistEnabled && !in_array($req->ip, $current->whitelist)) {
             $drop = true;
         }
         if (!$drop) {
             $router = new WebServerRouter($req->servletPath, $req->method);
             $router->get('/', function () {
                 echo "Root";
             });
             $router->get('/root/:name', function ($name) {
                 echo $name;
             });
             $router->run();
             /* Some debug data */
             /* var_dump($req->method);
                var_dump($req->scheme);
                var_dump($req->pathInfo);
                var_dump($req->servletPath);
                var_dump($req->queryString);
                var_dump($req->authType);
                var_dump($req->url);
                var_dump($req->port);
                var_dump($req->ip);
                var_dump($req->cookies); */
         } else {
             $res->status = 403;
             echo "Access denied";
         }
     });
     $this->server->tokenEnabled = $this->tokenEnabled = $tokenEnabled;
     $this->server->token = $this->token = explode(";", $token);
     $this->server->whitelistEnabled = $this->whitelistEnabled = $whitelistEnabled;
     $this->server->whitelist = $this->whitelist = explode(";", $whitelist);
     $this->server->isolated = true;
     $this->server->hotReload = true;
 }
Ejemplo n.º 3
0
--TEST--
Test response
--FILE--
<?php 
use php\net\ServerSocket;
use php\net\URL;
use php\webserver\WebRequest;
use php\webserver\WebResponse;
use php\webserver\WebServer;
$server = new WebServer(function (WebRequest $request, WebResponse $response) {
    $response->writeToBody('foobar');
    $response->status = 404;
});
$server->port = ServerSocket::findAvailableLocalPort();
$server->run();
$conn = null;
$conn = (new URL("http://localhost:{$server->port}/"))->openConnection();
$conn->requestMethod = 'GET';
$conn->setRequestProperty('Content-Type', 'text/html');
$conn->setRequestProperty('Accept', 'text/plain');
$conn->connect();
var_dump($conn->responseCode);
var_dump($conn->getErrorStream()->readFully());
?>
--EXPECT--
int(404)
string(6) "foobar"