-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.php
117 lines (105 loc) · 3.78 KB
/
server.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
use Esockets\Client;
ini_set('log_errors', false);
ini_set('display_errors', true);
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
require_once __DIR__ . '/../../vendor/autoload.php';
\Esockets\Debug\Log::setEnv('HTTP_SERVER');
$configurator = new \Esockets\Base\Configurator(require 'config.php');
$httpServer = $configurator->makeServer();
$httpServer->connect(new \Esockets\Socket\Ipv4Address('0.0.0.0', '8181'));
$fork = pcntl_fork();
cli_set_process_title('PHP Fork ' . $fork);
$fork = pcntl_fork();
cli_set_process_title('PHP Fork ' . $fork);
$fileLoader = new class
{
private $existsCache = [];
private $cache;
/**
* @inheritDoc
*/
public function __construct()
{
$this->cache = [];
}
public function existFile(string $path): bool
{
if (isset($this->existsCache[$path])) {
$exists = $this->existsCache[$path];
if ($exists) {
return true;
}
}
return $this->existsCache[$path] = file_exists($path);
}
public function loadFile(string $path): string
{
if (isset($this->cache[$path])) {
return $this->cache[$path];
}
return $this->cache[$path] = file_get_contents($path);
}
};
$httpServer->onFound(function (Client $client) use ($fileLoader) {
$client->onReceive(function ($request) use ($client, $fileLoader) {
if ($request instanceof HttpRequest) {
$baseDir = __DIR__ . '/www/'; // базовая директория сервера
// декодируем url закодированную строку URI, обрезаем пробельные символы, и обрезаем слеш в начале
// а также убираем все небезопасные символы из строки
$parsed = parse_url(ltrim(trim(rawurldecode($request->getRequestUri())), '/'));
$uri = $parsed['path'];
if (empty($uri)) {
$uri = 'index.html';
}
$mime = 'text/html';
$path = $baseDir . $uri;
if (!$fileLoader->existFile($path)) {
$response = new HttpResponse(404, 'Not found', '<h1>Not found</h1><p>' . $uri . '</p>');
} else {
$time = microtime(true);
$extension = pathinfo($path, PATHINFO_EXTENSION);
if ($extension === 'php') {
ob_start();
try {
include $path;
} catch (\Throwable $e) {
echo '<p>Error: ' . $e->getMessage() . '</p>';
} finally {
$body = ob_get_contents();
ob_end_clean();
}
} else {
switch ($extension) {
case 'css':
$mime = 'text/css';
break;
default:
// $mime = mime_content_type($path);
}
$body = $fileLoader->loadFile($path);
}
$body .= '<div>exec ' . (microtime(true) - $time) . '</div>';
$response = new HttpResponse(
200,
'OK',
$body,
['Content-Type: ' . $mime . '; charset=utf-8']
);
}
} else {
$response = new HttpResponse(400, 'Bad request', '<h1>Bad request</h1>');
}
$client->send($response);
$client->disconnect();
});
});
$httpServer->unblock();
for (; ;) {
try {
$httpServer->find();
} catch (Throwable $e) {
fwrite(STDERR, $e->getMessage() . PHP_EOL);
}
}
return 0;