forked from dhotson/httpparser-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebserver.php
More file actions
67 lines (59 loc) · 1.77 KB
/
webserver.php
File metadata and controls
67 lines (59 loc) · 1.77 KB
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
<?php
class HttpServer
{
protected $buffers = array();
protected $nparsed = array();
function clearBuffer($fd)
{
$this->buffers[$fd] = "";
$this->nparsed[$fd] = 0;
}
function onClose($serv, $fd, $from_id)
{
$this->clearBuffer($fd);
}
function onReceive($serv, $fd, $from_id, $data)
{
$parser = new HttpParser;
if(empty($this->buffers[$fd]))
{
$this->clearBuffer($fd);
}
$this->buffers[$fd] .= $data;
$buffer = &$this->buffers[$fd];
$nparsed = &$this->nparsed[$fd];
$nparsed = $parser->execute($buffer, $nparsed);
if ($parser->hasError())
{
$serv->close($fd, $from_id);
}
else if ($parser->isFinished())
{
$this->clearBuffer($fd);
//$env = $parser->getEnvironment();
// $result = '<form action="" method="post"><input type="submit" name="testvar" value="Testing!" /></form><pre>';
// foreach ($env as $k => $v)
// $result .= sprintf("%s -> '%s'\n", $k, $v);
$result = "hello world";
$response = join(
"\r\n",
array(
'HTTP/1.1 200 OK',
'Content-Type: text/html',
'Content-Length: '.strlen($result),
'',
$result));
$serv->send($fd, $response);
$serv->close($fd);
}
}
}
$server = new swoole_server('127.0.0.1', 9506);
$php_http_server = new HttpServer;
$server->on('Receive', array($php_http_server, 'onReceive'));
$server->on('Close', array($php_http_server, 'onClose'));
$server->set(array(
'worker_num' => 4,
'daemonize' => 1,
));
$server->start();