|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
// run composer install in top directory |
|
4
|
|
|
require_once __DIR__.'/../vendor/autoload.php'; |
|
5
|
|
|
|
|
6
|
|
|
use Navarr\Socket\Server; |
|
7
|
|
|
use Navarr\Socket\Socket; |
|
8
|
|
|
|
|
9
|
|
|
class WebServer extends Server |
|
10
|
|
|
{ |
|
11
|
|
|
const DEFAULT_PORT = 80; |
|
12
|
|
|
|
|
13
|
|
|
/** @var WebClient[] */ |
|
14
|
|
|
protected $clientMap; |
|
15
|
|
|
protected $readType = PHP_BINARY_READ; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct($address = null, $port = self::DEFAULT_PORT) |
|
18
|
|
|
{ |
|
19
|
|
|
parent::__construct($address, $port); |
|
20
|
|
|
$this->addHook(Server::HOOK_CONNECT, [$this, 'onConnect']); |
|
21
|
|
|
$this->addHook(Server::HOOK_INPUT, [$this, 'onInput']); |
|
22
|
|
|
$this->addHook(Server::HOOK_DISCONNECT, [$this, 'onDisconnect']); |
|
23
|
|
|
$this->run(); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function onConnect(Server $server, Socket $client, $message) |
|
27
|
|
|
{ |
|
28
|
|
|
echo "Connection\n"; |
|
29
|
|
|
$this->clientMap[(string) $client] = new WebClient($server, $client); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function onInput(Server $server, Socket $client, $message) |
|
33
|
|
|
{ |
|
34
|
|
|
$messages = explode("\n", $message); |
|
35
|
|
|
foreach ($messages as $message) { |
|
36
|
|
|
$message .= "\n"; |
|
37
|
|
|
$this->clientMap[(string) $client]->dispatch($message); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function onDisconnect(Server $server, Socket $client, $message) |
|
42
|
|
|
{ |
|
43
|
|
|
echo "Disconnect\n"; |
|
44
|
|
|
unset($this->clientMap[(string) $client]); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
class WebClient |
|
49
|
|
|
{ |
|
50
|
|
|
protected $server = null; |
|
51
|
|
|
protected $socket = null; |
|
52
|
|
|
protected $firstLine = null; |
|
53
|
|
|
protected $verb = null; |
|
54
|
|
|
protected $resource = null; |
|
55
|
|
|
protected $lastLine = null; |
|
56
|
|
|
protected $protocol = null; |
|
57
|
|
|
protected $headers = []; |
|
58
|
|
|
|
|
59
|
|
|
public function __construct(Server $server, Socket $client) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->server = $server; |
|
62
|
|
|
$this->socket = $client; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $message |
|
67
|
|
|
*/ |
|
68
|
|
|
public function dispatch($message) |
|
69
|
|
|
{ |
|
70
|
|
|
echo trim($message), "\n"; |
|
71
|
|
|
$message = trim($message); |
|
72
|
|
|
if ($this->firstLine === null) { |
|
73
|
|
|
$tokens = explode(' ', $message, 3); |
|
74
|
|
|
$this->verb = $tokens[0]; |
|
75
|
|
|
$this->resource = $tokens[1]; |
|
76
|
|
|
$this->protocol = $tokens[2]; |
|
77
|
|
|
|
|
78
|
|
|
$this->firstLine = $message; |
|
79
|
|
|
$this->lastLine = $message; |
|
80
|
|
|
|
|
81
|
|
|
return; |
|
82
|
|
|
} |
|
83
|
|
|
if ($message !== '') { |
|
84
|
|
|
$tokens = explode(': ', $message, 2); |
|
85
|
|
|
$this->headers[$tokens[0]] = $tokens[1]; |
|
86
|
|
|
} |
|
87
|
|
|
if ($this->lastLine === '' && $message === '') { |
|
88
|
|
|
$this->writeLine('HTTP/1.1 200 OK'); |
|
89
|
|
|
$this->writeLine('Content-Type: text/plain'); |
|
90
|
|
|
$this->writeLine(); |
|
91
|
|
|
|
|
92
|
|
|
$url = $this->headers['Host'].$this->resource; |
|
93
|
|
|
$this->writeLine( |
|
94
|
|
|
"You requested {$url} using verb {$this->verb} over {$this->protocol}" |
|
95
|
|
|
); |
|
96
|
|
|
|
|
97
|
|
|
$this->disconnect(); |
|
98
|
|
|
} |
|
99
|
|
|
$this->lastLine = $message; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
protected function disconnect() |
|
103
|
|
|
{ |
|
104
|
|
|
$this->server->disconnect($this->socket); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
protected function writeLine($message = '') |
|
108
|
|
|
{ |
|
109
|
|
|
$message .= "\r\n"; |
|
110
|
|
|
$this->socket->write($message, strlen($message)); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$server = new WebServer('0.0.0.0', 8001); |
|
115
|
|
|
|