|
1
|
|
|
<?php |
|
2
|
|
|
namespace PHPDaemon\SockJS\Methods; |
|
3
|
|
|
|
|
4
|
|
|
use PHPDaemon\Core\Timer; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Contains some base methods |
|
8
|
|
|
* @package Libraries |
|
9
|
|
|
* @subpackage SockJS |
|
10
|
|
|
* @author Vasily Zorin <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
abstract class Generic extends \PHPDaemon\HTTPRequest\Generic |
|
13
|
|
|
{ |
|
14
|
|
|
protected $stage = 0; |
|
15
|
|
|
protected $sessId; |
|
16
|
|
|
protected $serverId; |
|
17
|
|
|
protected $path; |
|
18
|
|
|
protected $heartbeatTimer; |
|
19
|
|
|
|
|
20
|
|
|
protected $opts; |
|
21
|
|
|
|
|
22
|
|
|
protected $stopped = false; |
|
23
|
|
|
|
|
24
|
|
|
protected $callbackParamEnabled = false; |
|
25
|
|
|
protected $frames = []; |
|
26
|
|
|
|
|
27
|
|
|
protected $allowedMethods = 'GET'; |
|
28
|
|
|
|
|
29
|
|
|
protected $errors = [ |
|
30
|
|
|
1002 => 'Connection interrupted', |
|
31
|
|
|
2010 => 'Another connection still open', |
|
32
|
|
|
3000 => 'Go away!', |
|
33
|
|
|
]; |
|
34
|
|
|
|
|
35
|
|
|
protected $pollMode = ['stream']; |
|
36
|
|
|
|
|
37
|
|
|
protected $gcEnabled = true; |
|
38
|
|
|
|
|
39
|
|
|
protected $cacheable = false; |
|
40
|
|
|
protected $poll = false; |
|
41
|
|
|
|
|
42
|
|
|
protected $bytesSent = 0; |
|
43
|
|
|
|
|
44
|
|
|
protected $heartbeatOnFinish = false; |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Constructor |
|
49
|
|
|
* @return void |
|
50
|
|
|
*/ |
|
51
|
|
|
public function init() |
|
52
|
|
|
{ |
|
53
|
|
|
$this->sessId = $this->attrs->sessId; |
|
54
|
|
|
$this->serverId = $this->attrs->serverId; |
|
55
|
|
|
$this->path = $this->attrs->path; |
|
56
|
|
|
|
|
57
|
|
|
// @TODO: revert timeout after request |
|
58
|
|
|
$this->upstream->setTimeouts($this->appInstance->config->networktimeoutread->value, |
|
59
|
|
|
$this->appInstance->config->networktimeoutwrite->value); |
|
60
|
|
|
|
|
61
|
|
|
$this->opts = $this->appInstance->getRouteOptions($this->attrs->path); |
|
|
|
|
|
|
62
|
|
|
$this->CORS(); |
|
63
|
|
|
if ($this->isFinished()) { |
|
64
|
|
|
return; |
|
65
|
|
|
} |
|
66
|
|
|
if ($this->opts['cookie_needed'] && !$this instanceof Info) { |
|
67
|
|
|
if (isset($_COOKIE['JSESSIONID'])) { |
|
68
|
|
|
$val = $_COOKIE['JSESSIONID']; |
|
69
|
|
|
if (!is_string($val) || $val === '') { |
|
70
|
|
|
$val = 'dummy'; |
|
71
|
|
|
} |
|
72
|
|
|
} else { |
|
73
|
|
|
$val = 'dummy'; |
|
74
|
|
|
} |
|
75
|
|
|
$this->setcookie('JSESSIONID', $val, 0, '/'); |
|
76
|
|
|
} |
|
77
|
|
|
$this->contentType($this->contentType); |
|
|
|
|
|
|
78
|
|
|
if (!$this->cacheable) { |
|
79
|
|
|
$this->noncache(); |
|
80
|
|
|
$this->header('X-Accel-Buffering: no'); |
|
81
|
|
|
} |
|
82
|
|
|
if ($this->callbackParamEnabled) { |
|
83
|
|
|
if (!isset($_GET['c']) || !is_string($_GET['c']) || preg_match('~[^_\.a-zA-Z0-9]~', $_GET['c'])) { |
|
84
|
|
|
$this->header('500 Internal Server Error'); |
|
85
|
|
|
$this->out('"callback" parameter required'); |
|
86
|
|
|
$this->finish(); |
|
87
|
|
|
return; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
if (($f = $this->appInstance->config->heartbeatinterval->value) > 0) { |
|
91
|
|
|
$this->heartbeatTimer = setTimeout(function ($timer) { |
|
|
|
|
|
|
92
|
|
|
if (in_array('one-by-one', $this->pollMode)) { |
|
93
|
|
|
$this->heartbeatOnFinish = true; |
|
94
|
|
|
$this->stop(); |
|
95
|
|
|
return; |
|
96
|
|
|
} |
|
97
|
|
|
$this->sendFrame('h'); |
|
98
|
|
|
if ($this->gcEnabled) { |
|
99
|
|
|
$this->gcCheck(); |
|
100
|
|
|
} |
|
101
|
|
|
}, $f * 1e6); |
|
102
|
|
|
} |
|
103
|
|
|
$this->afterHeaders(); |
|
104
|
|
|
if ($this->poll) { |
|
105
|
|
|
$this->acquire(function () { |
|
106
|
|
|
$this->poll(); |
|
107
|
|
|
}); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* CORS |
|
113
|
|
|
* @return void |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function CORS() |
|
116
|
|
|
{ |
|
117
|
|
|
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) { |
|
118
|
|
|
$this->header('Access-Control-Allow-Headers: ' . $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']); |
|
119
|
|
|
} |
|
120
|
|
|
if (isset($_SERVER['HTTP_ORIGIN']) && $_SERVER['HTTP_ORIGIN'] !== 'null') { |
|
121
|
|
|
$this->header('Access-Control-Allow-Origin:' . $_SERVER['HTTP_ORIGIN']); |
|
122
|
|
|
} else { |
|
123
|
|
|
$this->header('Access-Control-Allow-Origin: *'); |
|
124
|
|
|
} |
|
125
|
|
|
$this->header('Access-Control-Allow-Credentials: true'); |
|
126
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { |
|
127
|
|
|
$this->header('204 No Content'); |
|
128
|
|
|
$this->header('Cache-Control: max-age=31536000, public, pre-check=0, post-check=0'); |
|
129
|
|
|
$this->header('Access-Control-Max-Age: 31536000'); |
|
130
|
|
|
$this->header('Access-Control-Allow-Methods: OPTIONS, ' . $this->allowedMethods); |
|
131
|
|
|
$this->header('Expires: ' . date('r', strtotime('+1 year'))); |
|
132
|
|
|
$this->finish(); |
|
133
|
|
|
} elseif (!in_array($_SERVER['REQUEST_METHOD'], explode(', ', $this->allowedMethods), true)) { |
|
134
|
|
|
$this->header('405 Method Not Allowed'); |
|
135
|
|
|
$this->finish(); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* contentType |
|
141
|
|
|
* @param string $type Content-Type |
|
142
|
|
|
* @return void |
|
143
|
|
|
*/ |
|
144
|
|
|
protected function contentType($type) |
|
145
|
|
|
{ |
|
146
|
|
|
$this->header('Content-Type: ' . $type . '; charset=UTF-8'); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* noncache |
|
151
|
|
|
* @return void |
|
152
|
|
|
*/ |
|
153
|
|
|
protected function noncache() |
|
154
|
|
|
{ |
|
155
|
|
|
$this->header('Pragma: no-cache'); |
|
156
|
|
|
$this->header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0'); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Output some data |
|
161
|
|
|
* @param string $s String to out |
|
162
|
|
|
* @param boolean $flush |
|
163
|
|
|
* @return boolean Success |
|
164
|
|
|
*/ |
|
165
|
|
|
public function out($s, $flush = true) |
|
166
|
|
|
{ |
|
167
|
|
|
if ($this->heartbeatTimer !== null) { |
|
168
|
|
|
Timer::setTimeout($this->heartbeatTimer); |
|
169
|
|
|
} |
|
170
|
|
|
return parent::out($s, $flush);; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Stop |
|
175
|
|
|
* @return void |
|
176
|
|
|
*/ |
|
177
|
|
|
public function stop() |
|
178
|
|
|
{ |
|
179
|
|
|
if ($this->stopped) { |
|
180
|
|
|
return; |
|
181
|
|
|
} |
|
182
|
|
|
$this->stopped = true; |
|
183
|
|
|
if (in_array('one-by-one', $this->pollMode)) { |
|
184
|
|
|
$this->finish(); |
|
185
|
|
|
return; |
|
186
|
|
|
} |
|
187
|
|
|
$this->appInstance->unsubscribeReal('s2c:' . $this->sessId, function ($redis) { |
|
|
|
|
|
|
188
|
|
|
$this->finish(); |
|
189
|
|
|
}); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Send frame |
|
194
|
|
|
* @param string $frame |
|
195
|
|
|
* @return void |
|
196
|
|
|
*/ |
|
197
|
|
|
protected function sendFrame($frame) |
|
198
|
|
|
{ |
|
199
|
|
|
if (substr($frame, 0, 1) === 'c') { |
|
200
|
|
|
$this->finish(); |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* gcCheck |
|
206
|
|
|
* @return void |
|
207
|
|
|
*/ |
|
208
|
|
|
public function gcCheck() |
|
209
|
|
|
{ |
|
210
|
|
|
if ($this->stopped) { |
|
211
|
|
|
return; |
|
212
|
|
|
} |
|
213
|
|
|
$max = $this->appInstance->config->gcmaxresponsesize->value; |
|
214
|
|
|
if ($max > 0 && $this->bytesSent > $max) { |
|
215
|
|
|
$this->stop(); |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* afterHeaders |
|
221
|
|
|
* @return void |
|
222
|
|
|
*/ |
|
223
|
|
|
public function afterHeaders() |
|
224
|
|
|
{ |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* acquire |
|
229
|
|
|
* @param callable $cb |
|
230
|
|
|
* @callback $cb ( ) |
|
231
|
|
|
* @return void |
|
232
|
|
|
*/ |
|
233
|
|
|
protected function acquire($cb) |
|
234
|
|
|
{ |
|
235
|
|
|
$this->appInstance->getkey('error:' . $this->sessId, function ($redis) use ($cb) { |
|
|
|
|
|
|
236
|
|
|
if (!$redis) { |
|
237
|
|
|
$this->internalServerError(); |
|
238
|
|
|
return; |
|
239
|
|
|
} |
|
240
|
|
|
if ($redis->result !== null) { |
|
241
|
|
|
$this->error((int)$redis->result); |
|
242
|
|
|
return; |
|
243
|
|
|
} |
|
244
|
|
|
if ($this->appInstance->getLocalSubscribersCount('w8in:' . $this->sessId) > 0) { |
|
|
|
|
|
|
245
|
|
|
$this->anotherConnectionStillOpen(); |
|
246
|
|
|
return; |
|
247
|
|
|
} |
|
248
|
|
|
$this->appInstance->publish('w8in:' . $this->sessId, '', function ($redis) use ($cb) { |
|
|
|
|
|
|
249
|
|
|
if (!$redis) { |
|
250
|
|
|
$this->internalServerError(); |
|
251
|
|
|
return; |
|
252
|
|
|
} |
|
253
|
|
|
if ($redis->result > 0) { |
|
254
|
|
|
$this->anotherConnectionStillOpen(); |
|
255
|
|
|
return; |
|
256
|
|
|
} |
|
257
|
|
|
$this->appInstance->subscribe('w8in:' . $this->sessId, [$this, 'w8in'], function ($redis) use ($cb) { |
|
|
|
|
|
|
258
|
|
|
if (!$redis) { |
|
259
|
|
|
$this->internalServerError(); |
|
260
|
|
|
return; |
|
261
|
|
|
} |
|
262
|
|
|
if ($this->appInstance->getLocalSubscribersCount('w8in:' . $this->sessId) > 1) { |
|
|
|
|
|
|
263
|
|
|
$this->anotherConnectionStillOpen(); |
|
264
|
|
|
return; |
|
265
|
|
|
} |
|
266
|
|
|
$this->appInstance->publish('w8in:' . $this->sessId, '', function ($redis) use ($cb) { |
|
|
|
|
|
|
267
|
|
|
if (!$redis) { |
|
268
|
|
|
$this->internalServerError(); |
|
269
|
|
|
return; |
|
270
|
|
|
} |
|
271
|
|
|
if ($redis->result > 1) { |
|
272
|
|
|
$this->anotherConnectionStillOpen(); |
|
273
|
|
|
return; |
|
274
|
|
|
} |
|
275
|
|
|
if ($this->appInstance->getLocalSubscribersCount('w8in:' . $this->sessId) > 1) { |
|
|
|
|
|
|
276
|
|
|
$this->anotherConnectionStillOpen(); |
|
277
|
|
|
return; |
|
278
|
|
|
} |
|
279
|
|
|
$cb === null || $cb(); |
|
280
|
|
|
}); |
|
281
|
|
|
}); |
|
282
|
|
|
}); |
|
283
|
|
|
}); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* internalServerError |
|
288
|
|
|
* @return void |
|
289
|
|
|
*/ |
|
290
|
|
|
public function internalServerError() |
|
291
|
|
|
{ |
|
292
|
|
|
$this->header('500 Internal Server Error'); |
|
293
|
|
|
$this->out('"callback" parameter required'); |
|
294
|
|
|
$this->finish(); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* error |
|
299
|
|
|
* @param integer $code |
|
300
|
|
|
* @return void |
|
301
|
|
|
*/ |
|
302
|
|
|
protected function error($code) |
|
303
|
|
|
{ |
|
304
|
|
|
$this->sendFrame('c' . json_encode([$code, isset($this->errors[$code]) ? $this->errors[$code] : null])); |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
/** |
|
308
|
|
|
* anotherConnectionStillOpen |
|
309
|
|
|
* @return void |
|
310
|
|
|
*/ |
|
311
|
|
|
protected function anotherConnectionStillOpen() |
|
312
|
|
|
{ |
|
313
|
|
|
$this->appInstance->setkey('error:' . $this->sessId, 1002, function () { |
|
|
|
|
|
|
314
|
|
|
$this->appInstance->expire('error:' . $this->sessId, $this->appInstance->config->deadsessiontimeout->value, |
|
|
|
|
|
|
315
|
|
|
function () { |
|
316
|
|
|
$this->error(2010); |
|
317
|
|
|
}); |
|
318
|
|
|
}); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* Poll |
|
323
|
|
|
* @param callable $cb |
|
|
|
|
|
|
324
|
|
|
* @callback $cb ( ) |
|
325
|
|
|
* @return void |
|
326
|
|
|
*/ |
|
327
|
|
|
protected function poll($cb = null) |
|
328
|
|
|
{ |
|
329
|
|
|
$this->appInstance->subscribe('s2c:' . $this->sessId, [$this, 's2c'], function ($redis) use ($cb) { |
|
|
|
|
|
|
330
|
|
|
$this->appInstance->publish('poll:' . $this->sessId, json_encode($this->pollMode), |
|
|
|
|
|
|
331
|
|
|
function ($redis) use ($cb) { |
|
332
|
|
|
if (!$redis) { |
|
333
|
|
|
$cb === null || $cb(); |
|
334
|
|
|
return; |
|
335
|
|
|
} |
|
336
|
|
|
if ($redis->result > 0) { |
|
337
|
|
|
$cb === null || $cb(); |
|
338
|
|
|
return; |
|
339
|
|
|
} |
|
340
|
|
|
$this->appInstance->setnx('sess:' . $this->sessId, $this->attrs->server['REQUEST_URI'], |
|
|
|
|
|
|
341
|
|
|
function ($redis) use ($cb) { |
|
342
|
|
View Code Duplication |
if (!$redis || $redis->result === 0) { |
|
|
|
|
|
|
343
|
|
|
$this->error(3000); |
|
344
|
|
|
$cb === null || $cb(); |
|
345
|
|
|
return; |
|
346
|
|
|
} |
|
347
|
|
|
$this->appInstance->expire('sess:' . $this->sessId, |
|
|
|
|
|
|
348
|
|
|
$this->appInstance->config->deadsessiontimeout->value, function ($redis) use ($cb) { |
|
349
|
|
View Code Duplication |
if (!$redis || $redis->result === 0) { |
|
|
|
|
|
|
350
|
|
|
$this->error(3000); |
|
351
|
|
|
$cb === null || $cb(); |
|
352
|
|
|
return; |
|
353
|
|
|
} |
|
354
|
|
|
$this->appInstance->subscribe('state:' . $this->sessId, |
|
|
|
|
|
|
355
|
|
|
function ($redis) use ($cb) { |
|
356
|
|
|
if (!$redis) { |
|
357
|
|
|
return; |
|
358
|
|
|
} |
|
359
|
|
|
list(, $chan, $state) = $redis->result; |
|
|
|
|
|
|
360
|
|
|
if ($state === 'started') { |
|
361
|
|
|
$this->sendFrame('o'); |
|
362
|
|
|
if (!in_array('stream', $this->pollMode)) { |
|
363
|
|
|
$this->finish(); |
|
364
|
|
|
return; |
|
365
|
|
|
} |
|
366
|
|
|
} |
|
367
|
|
|
$this->appInstance->publish('poll:' . $this->sessId, |
|
|
|
|
|
|
368
|
|
|
json_encode($this->pollMode), function ($redis) use ($cb) { |
|
369
|
|
View Code Duplication |
if (!$redis || $redis->result === 0) { |
|
|
|
|
|
|
370
|
|
|
$this->error(3000); |
|
371
|
|
|
$cb === null || $cb(); |
|
372
|
|
|
return; |
|
373
|
|
|
} |
|
374
|
|
|
$cb === null || $cb(); |
|
375
|
|
|
}); |
|
376
|
|
|
}, function ($redis) use ($cb) { |
|
|
|
|
|
|
377
|
|
|
if (!$this->appInstance->beginSession($this->path, $this->sessId, |
|
|
|
|
|
|
378
|
|
|
$this->attrs->server) |
|
379
|
|
|
) { |
|
380
|
|
|
$this->header('404 Not Found'); |
|
381
|
|
|
$this->finish(); |
|
382
|
|
|
$this->unsubscribeReal('state:' . $this->sessId); |
|
|
|
|
|
|
383
|
|
|
} |
|
384
|
|
|
$cb === null || $cb(); |
|
385
|
|
|
}); |
|
386
|
|
|
}); |
|
387
|
|
|
}); |
|
388
|
|
|
}); |
|
389
|
|
|
}); |
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
|
|
/** |
|
393
|
|
|
* Output some data |
|
394
|
|
|
* @param string $s String to out |
|
395
|
|
|
* @param boolean $flush |
|
396
|
|
|
* @return boolean Success |
|
397
|
|
|
*/ |
|
398
|
|
|
public function outputFrame($s, $flush = true) |
|
399
|
|
|
{ |
|
400
|
|
|
$this->bytesSent += mb_orig_strlen($s); |
|
401
|
|
|
return parent::out($s, $flush); |
|
|
|
|
|
|
402
|
|
|
} |
|
403
|
|
|
|
|
404
|
|
|
/** |
|
405
|
|
|
* Called when request iterated |
|
406
|
|
|
* @return void |
|
407
|
|
|
*/ |
|
408
|
|
|
public function run() |
|
409
|
|
|
{ |
|
410
|
|
|
$this->sleep(30); |
|
411
|
|
|
} |
|
412
|
|
|
|
|
413
|
|
|
/** |
|
414
|
|
|
* w8in |
|
415
|
|
|
* @return void |
|
416
|
|
|
*/ |
|
417
|
|
|
public function w8in() |
|
418
|
|
|
{ |
|
419
|
|
|
} |
|
420
|
|
|
|
|
421
|
|
|
/** |
|
422
|
|
|
* s2c |
|
423
|
|
|
* @param object $redis |
|
424
|
|
|
* @return void |
|
425
|
|
|
*/ |
|
426
|
|
|
public function s2c($redis) |
|
427
|
|
|
{ |
|
428
|
|
|
if (!$redis) { |
|
429
|
|
|
return; |
|
430
|
|
|
} |
|
431
|
|
|
list(, $chan, $msg) = $redis->result; |
|
|
|
|
|
|
432
|
|
|
$frames = json_decode($msg, true); |
|
433
|
|
|
if (!is_array($frames) || !sizeof($frames)) { |
|
434
|
|
|
return; |
|
435
|
|
|
} |
|
436
|
|
|
foreach ($frames as $frame) { |
|
437
|
|
|
$this->sendFrame($frame); |
|
438
|
|
|
} |
|
439
|
|
|
if (!in_array('stream', $this->pollMode)) { |
|
440
|
|
|
$this->heartbeatOnFinish = false; |
|
441
|
|
|
$this->stop(); |
|
442
|
|
|
return; |
|
443
|
|
|
} |
|
444
|
|
|
if ($this->gcEnabled) { |
|
445
|
|
|
$this->gcCheck(); |
|
446
|
|
|
} |
|
447
|
|
|
} |
|
448
|
|
|
|
|
449
|
|
|
/** |
|
450
|
|
|
* On finish |
|
451
|
|
|
* @return void |
|
452
|
|
|
*/ |
|
453
|
|
|
public function onFinish() |
|
454
|
|
|
{ |
|
455
|
|
|
$this->appInstance->unsubscribe('s2c:' . $this->sessId, [$this, 's2c']); |
|
|
|
|
|
|
456
|
|
|
$this->appInstance->unsubscribe('w8in:' . $this->sessId, [$this, 'w8in']); |
|
|
|
|
|
|
457
|
|
|
Timer::remove($this->heartbeatTimer); |
|
458
|
|
|
if ($this->heartbeatOnFinish) { |
|
459
|
|
|
$this->sendFrame('h'); |
|
460
|
|
|
} |
|
461
|
|
|
parent::onFinish(); |
|
462
|
|
|
} |
|
463
|
|
|
} |
|
464
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: