This repository was archived by the owner on Apr 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsyncHTTPServer.php
114 lines (91 loc) · 2.79 KB
/
AsyncHTTPServer.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
<?php namespace Async\HTTP;
require_once "./AsyncTCPServer.php";
class Request {
private $parseIndex = 0;
private $contentLength = 0;
function entityIsReady() {
return
(isset($this->method) && $this->method === "GET");
}
function isValid() {
$headerBoundaryPos = strpos($this->buffer, "\r\n\r\n");
if($headerBoundaryPos !== FALSE) {
echo "HEADER RECEIVED\n";
$this->parseHeader(substr($this->buffer, 0, $headerBoundaryPos));
$this->buffer = substr($this->buffer, $headerBoundaryPos + 2);
}
if($this->entityIsReady()) {
return TRUE;
}
return FALSE;
}
function parseHeader($headerString) {
$lines = explode("\r\n", $headerString);
$statusLine = $lines[0];
$statusLineParts = explode(" ", $statusLine);
$this->method = $statusLineParts[0];
$this->uri = $statusLineParts[1];
$this->version = $statusLineParts[2];
$this->headers = array();
$lines = array_slice($lines, 1);
$i = 1;
$numLines = count($lines);
$loop = TRUE;
foreach($lines as $line) {
$headerParts = explode(":", $line, 2);
$key = $headerParts[0];
$val = $headerParts[1];
$this->headers[$key] = trim($val);
}
return TRUE;
}
}
class Response {
private $statusSent = FALSE;
private $headersSent = FALSE;
private $connection;
public function __construct($connection) {
$this->connection = $connection;
}
public function status($code) {
$this->connection->write("HTTP/1.1 $code\r\n");
}
public function header($key, $value) {
$this->connection->write("$key: $value\r\n");
}
public function body($data) {
$this->connection->write("\r\n$data");
}
public function close() {
$this->connection->close();
}
}
class Server {
public $tcpServer;
public $requestCallback;
public $eventBase;
private $count;
function __construct($address) {
$this->tcpServer = new \Async\TCP\Server($address);
$self = $this;
$this->tcpServer->onConnect(function($connection) use ($self){
$rsp = new Response($connection);
$req = new Request();
$req->buffer = "";
$connection->onData(function($data) use ($self, $req, $rsp) {
$cb = $self->requestCallback;
if(!$cb) return;
$req->buffer .= $data;
if($req->isValid()) {
$cb($req, $rsp);
}
});
});
}
function onRequest($function) {
$this->requestCallback = $function;
}
function run() {
$this->tcpServer->run();
}
}