1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dazzle\Http\Http; |
4
|
|
|
|
5
|
|
|
use Dazzle\Http\NetworkComponentAwareInterface; |
6
|
|
|
use Dazzle\Http\Http\Driver\HttpDriver; |
7
|
|
|
use Dazzle\Http\Http\Driver\HttpDriverInterface; |
8
|
|
|
use Dazzle\Http\Null\NullServer; |
9
|
|
|
use Dazzle\Http\NetworkMessageInterface; |
10
|
|
|
use Dazzle\Http\NetworkComponentInterface; |
11
|
|
|
use Dazzle\Http\NetworkConnectionInterface; |
12
|
|
|
use Dazzle\Util\Buffer\Buffer; |
13
|
|
|
use Error; |
14
|
|
|
use Exception; |
15
|
|
|
|
16
|
|
|
class HttpServer implements HttpServerInterface, NetworkComponentAwareInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var NetworkComponentInterface |
20
|
|
|
*/ |
21
|
|
|
protected $httpServer; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var HttpDriverInterface |
25
|
|
|
*/ |
26
|
|
|
protected $httpDriver; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param NetworkComponentAwareInterface|null $aware |
30
|
|
|
* @param NetworkComponentInterface|null $component |
31
|
|
|
*/ |
32
|
65 |
|
public function __construct(NetworkComponentAwareInterface $aware = null, NetworkComponentInterface $component = null) |
33
|
|
|
{ |
34
|
65 |
|
$this->httpServer = $component; |
35
|
65 |
|
$this->httpDriver = new HttpDriver(); |
36
|
|
|
|
37
|
65 |
|
if ($aware !== null) |
38
|
|
|
{ |
39
|
65 |
|
$aware->setComponent($this); |
40
|
|
|
} |
41
|
65 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* |
45
|
|
|
*/ |
46
|
|
|
public function __destruct() |
47
|
|
|
{ |
48
|
|
|
unset($this->httpServer); |
49
|
|
|
unset($this->httpDriver); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @override |
54
|
|
|
* @inheritDoc |
55
|
|
|
*/ |
56
|
1 |
|
public function getDriver() |
57
|
|
|
{ |
58
|
1 |
|
return $this->httpDriver; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @override |
63
|
|
|
* @inheritDoc |
64
|
|
|
*/ |
65
|
23 |
|
public function setComponent(NetworkComponentInterface $component = null) |
66
|
|
|
{ |
67
|
23 |
|
$this->httpServer = $component === null ? new NullServer() : $component; |
68
|
23 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @override |
72
|
|
|
* @inheritDoc |
73
|
|
|
*/ |
74
|
3 |
|
public function getComponent() |
75
|
|
|
{ |
76
|
3 |
|
return $this->httpServer; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @override |
81
|
|
|
* @inheritDoc |
82
|
|
|
*/ |
83
|
4 |
|
public function handleConnect(NetworkConnectionInterface $conn) |
84
|
|
|
{ |
85
|
4 |
|
$conn->httpBuffer = new Buffer(); |
|
|
|
|
86
|
4 |
|
$conn->httpHeadersReceived = false; |
|
|
|
|
87
|
4 |
|
$conn->httpRequest = null; |
|
|
|
|
88
|
4 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @override |
92
|
|
|
* @inheritDoc |
93
|
|
|
*/ |
94
|
5 |
|
public function handleDisconnect(NetworkConnectionInterface $conn) |
95
|
|
|
{ |
96
|
5 |
|
if ($conn->httpHeadersReceived) |
|
|
|
|
97
|
|
|
{ |
98
|
4 |
|
$this->httpServer->handleDisconnect($conn); |
99
|
|
|
} |
100
|
5 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @override |
104
|
|
|
* @inheritDoc |
105
|
|
|
*/ |
106
|
6 |
|
public function handleMessage(NetworkConnectionInterface $conn, NetworkMessageInterface $message) |
107
|
|
|
{ |
108
|
6 |
|
if ($conn->httpHeadersReceived !== true) |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
try |
111
|
|
|
{ |
112
|
5 |
|
if (($request = $this->httpDriver->readRequest($conn->httpBuffer, $message->read())) === null) |
|
|
|
|
113
|
|
|
{ |
114
|
4 |
|
return; |
115
|
|
|
} |
116
|
|
|
} |
117
|
1 |
|
catch (Error $ex) |
118
|
|
|
{ |
119
|
|
|
return $this->close($conn, 413); |
120
|
|
|
} |
121
|
1 |
|
catch (Exception $ex) |
122
|
|
|
{ |
123
|
1 |
|
return $this->close($conn, 413); |
124
|
|
|
} |
125
|
|
|
|
126
|
4 |
|
$conn->httpHeadersReceived = true; |
|
|
|
|
127
|
4 |
|
$conn->httpRequest = $request; |
|
|
|
|
128
|
|
|
|
129
|
4 |
|
$this->httpServer->handleConnect($conn); |
130
|
4 |
|
$this->httpServer->handleMessage($conn, $request); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
else |
133
|
|
|
{ |
134
|
1 |
|
$this->httpServer->handleMessage($conn, $message); |
135
|
|
|
} |
136
|
5 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @override |
140
|
|
|
* @inheritDoc |
141
|
|
|
*/ |
142
|
2 |
|
public function handleError(NetworkConnectionInterface $conn, $ex) |
143
|
|
|
{ |
144
|
2 |
|
if ($conn->httpHeadersReceived) |
|
|
|
|
145
|
|
|
{ |
146
|
1 |
|
$this->httpServer->handleError($conn, $ex); |
147
|
|
|
} |
148
|
|
|
else |
149
|
|
|
{ |
150
|
1 |
|
$this->close($conn, 500); |
151
|
|
|
} |
152
|
2 |
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Close a connection with an HTTP response. |
156
|
|
|
* |
157
|
|
|
* @param NetworkConnectionInterface $conn |
158
|
|
|
* @param int $code |
159
|
|
|
* @return null |
160
|
|
|
*/ |
161
|
1 |
|
protected function close(NetworkConnectionInterface $conn, $code = 400) |
162
|
|
|
{ |
163
|
1 |
|
$response = new HttpResponse($code); |
164
|
|
|
|
165
|
1 |
|
$conn->send($response); |
166
|
1 |
|
$conn->close(); |
167
|
1 |
|
} |
168
|
|
|
} |
169
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: