-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Handshake.php
258 lines (214 loc) · 5.96 KB
/
Handshake.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
namespace Amp\Websocket\Client;
use Amp\Http\Client\Request;
use Amp\Http\Message;
use Amp\Websocket\Options;
use League\Uri;
use Psr\Http\Message\UriInterface as PsrUri;
final class Handshake extends Message
{
/** @var PsrUri */
private $uri;
/** @var Options */
private $options;
/** @var int */
private $tcpConnectTimeout = 10000;
/** @var int */
private $tlsHandshakeTimeout = 10000;
/** @var int */
private $headerSizeLimit = Request::DEFAULT_HEADER_SIZE_LIMIT;
/**
* @param string|PsrUri $uri target address of websocket (e.g. ws://foo.bar/bar or a
* wss://crypto.example/?secureConnection) or a PsrUri instance.
* @param Options|null $options
* @param string[]|string[][] $headers
*
* @throws \TypeError If $uri is not a string or an instance of PsrUri.
*/
public function __construct($uri, ?Options $options = null, array $headers = [])
{
$this->uri = $this->makeUri($uri);
$this->options = $this->checkOptions($options);
$this->setHeaders($headers);
}
/**
* @return PsrUri Websocket URI (scheme will be either ws or wss).
*/
public function getUri(): PsrUri
{
return $this->uri;
}
/**
* @param string|PsrUri $uri
*
* @return self Cloned object
*/
public function withUri($uri): self
{
$clone = clone $this;
$clone->uri = $clone->makeUri($uri);
return $clone;
}
/**
* @return Options
*/
public function getOptions(): Options
{
return $this->options;
}
/**
* @param Options $options
*
* @return self Cloned object.
*/
public function withOptions(Options $options): self
{
$clone = clone $this;
$clone->options = $clone->checkOptions($options);
return $clone;
}
/**
* @return int Timeout in milliseconds for the TCP connection.
*/
public function getTcpConnectTimeout(): int
{
return $this->tcpConnectTimeout;
}
public function withTcpConnectTimeout(int $tcpConnectTimeout): self
{
$clone = clone $this;
$clone->tcpConnectTimeout = $tcpConnectTimeout;
return $clone;
}
/**
* @return int Timeout in milliseconds for the TLS handshake.
*/
public function getTlsHandshakeTimeout(): int
{
return $this->tlsHandshakeTimeout;
}
public function withTlsHandshakeTimeout(int $tlsHandshakeTimeout): self
{
$clone = clone $this;
$clone->tlsHandshakeTimeout = $tlsHandshakeTimeout;
return $clone;
}
public function getHeaderSizeLimit(): int
{
return $this->headerSizeLimit;
}
public function withHeaderSizeLimit(int $headerSizeLimit): self
{
$clone = clone $this;
$clone->headerSizeLimit = $headerSizeLimit;
return $clone;
}
/**
* Replaces all headers in the returned instance.
*
* @param string[]|string[][] $headers
*
* @return self Cloned object.
*/
public function withHeaders(array $headers): self
{
$clone = clone $this;
foreach ($clone->getRawHeaders() as [$field]) {
$clone->removeHeader($field);
}
$clone->setHeaders($headers);
return $clone;
}
/**
* Replaces the given header in the returned instance.
*
* @param string $name
* @param string|string[] $value
*
* @return self Cloned object.
*/
public function withHeader(string $name, $value): self
{
$clone = clone $this;
$clone->setHeader($name, $value);
return $clone;
}
/**
* Adds the given header in the returned instance.
*
* @param string $name
* @param string|string[] $value
*
* @return self Cloned object.
*/
public function withAddedHeader(string $name, $value): self
{
$clone = clone $this;
$clone->addHeader($name, $value);
return $clone;
}
/**
* Removes the given header in the returned instance.
*
* @param string $name
*
* @return self Cloned object.
*/
public function withoutHeader(string $name): self
{
$clone = clone $this;
$clone->removeHeader($name);
return $clone;
}
protected function setHeader(string $name, $value): void
{
if (($name[0] ?? ':') === ':') {
throw new \Error("Header name cannot be empty or start with a colon (:)");
}
parent::setHeader($name, $value);
}
protected function addHeader(string $name, $value): void
{
if (($name[0] ?? ':') === ':') {
throw new \Error("Header name cannot be empty or start with a colon (:)");
}
parent::addHeader($name, $value);
}
/**
* @param string|PsrUri $uri
*
* @return PsrUri
*/
private function makeUri($uri): PsrUri
{
if (\is_string($uri)) {
try {
$uri = Uri\Http::createFromString($uri);
} catch (\Exception $exception) {
throw new \Error('Invalid Websocket URI provided', 0, $exception);
}
}
/** @psalm-suppress DocblockTypeContradiction */
if (!$uri instanceof PsrUri) {
throw new \TypeError(\sprintf(
'Must provide an instance of %s or a websocket URI as a string',
PsrUri::class
));
}
switch ($uri->getScheme()) {
case 'ws':
case 'wss':
break;
default:
throw new \Error('The URI scheme must be ws or wss: \'' . $uri->getScheme() . '\'');
}
return $uri;
}
private function checkOptions(?Options $options): Options
{
if ($options === null) {
return Options::createClientDefault();
}
return $options;
}
}