2018-10-17 21:52:26 +02:00
|
|
|
<?php
|
|
|
|
declare(strict_types = 1);
|
2018-11-06 03:57:36 +01:00
|
|
|
namespace Psalm\Internal\LanguageServer;
|
2018-10-17 21:52:26 +02:00
|
|
|
|
|
|
|
use AdvancedJsonRpc\Message as MessageBody;
|
2019-07-05 22:24:00 +02:00
|
|
|
use function Amp\asyncCall;
|
2019-02-06 02:00:13 +01:00
|
|
|
use Amp\ByteStream\ResourceInputStream;
|
2018-11-18 00:00:28 +01:00
|
|
|
use Exception;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function explode;
|
|
|
|
use function strlen;
|
2019-07-05 22:24:00 +02:00
|
|
|
use function substr;
|
|
|
|
use function trim;
|
2018-10-17 21:52:26 +02:00
|
|
|
|
2018-11-18 00:00:28 +01:00
|
|
|
/**
|
|
|
|
* Source: https://github.com/felixfbecker/php-language-server/tree/master/src/ProtocolStreamReader.php
|
|
|
|
*/
|
2019-01-19 06:58:08 +01:00
|
|
|
class ProtocolStreamReader implements ProtocolReader
|
2018-10-17 21:52:26 +02:00
|
|
|
{
|
2019-01-19 06:58:08 +01:00
|
|
|
use EmitterTrait;
|
|
|
|
|
2018-10-17 21:52:26 +02:00
|
|
|
const PARSE_HEADERS = 1;
|
|
|
|
const PARSE_BODY = 2;
|
|
|
|
|
2018-11-18 00:00:28 +01:00
|
|
|
/**
|
|
|
|
* This is checked by ProtocolStreamReader so that it will stop reading from streams in the forked process.
|
|
|
|
* There could be buffered bytes in stdin/over TCP, those would be processed by TCP if it were not for this check.
|
2019-07-05 22:24:00 +02:00
|
|
|
*
|
2018-11-18 00:00:28 +01:00
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $is_accepting_new_requests = true;
|
2018-10-17 21:52:26 +02:00
|
|
|
/** @var int */
|
2018-11-18 00:00:28 +01:00
|
|
|
private $parsing_mode = self::PARSE_HEADERS;
|
2018-10-17 21:52:26 +02:00
|
|
|
/** @var string */
|
|
|
|
private $buffer = '';
|
2018-11-18 00:00:28 +01:00
|
|
|
/** @var string[] */
|
2018-10-17 21:52:26 +02:00
|
|
|
private $headers = [];
|
|
|
|
/** @var ?int */
|
2018-11-18 00:00:28 +01:00
|
|
|
private $content_length = null;
|
|
|
|
/** @var bool */
|
|
|
|
private $did_emit_close = false;
|
2018-10-17 21:52:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param resource $input
|
|
|
|
*/
|
|
|
|
public function __construct($input)
|
|
|
|
{
|
2019-02-06 02:00:13 +01:00
|
|
|
$input = new ResourceInputStream($input);
|
2019-02-06 17:40:18 +01:00
|
|
|
asyncCall(
|
2019-02-06 21:52:43 +01:00
|
|
|
/**
|
|
|
|
* @return \Generator<int, string, string, void>
|
|
|
|
*/
|
2019-02-06 19:01:22 +01:00
|
|
|
function () use ($input) : \Generator {
|
|
|
|
while ($this->is_accepting_new_requests && ($chunk = yield $input->read()) !== null) {
|
2019-02-06 17:40:18 +01:00
|
|
|
/** @var string $chunk */
|
2019-02-06 19:01:22 +01:00
|
|
|
if ($this->readMessages($chunk) > 0) {
|
|
|
|
$this->emit('readMessageGroup');
|
|
|
|
}
|
2019-02-06 17:40:18 +01:00
|
|
|
}
|
2019-01-19 06:58:08 +01:00
|
|
|
|
2019-02-06 17:40:18 +01:00
|
|
|
$this->emitClose();
|
|
|
|
}
|
|
|
|
);
|
2019-01-19 06:58:08 +01:00
|
|
|
|
|
|
|
$this->on(
|
|
|
|
'close',
|
|
|
|
/** @return void */
|
2019-02-06 02:00:13 +01:00
|
|
|
static function () use ($input) {
|
|
|
|
$input->close();
|
2019-01-19 06:58:08 +01:00
|
|
|
}
|
|
|
|
);
|
2018-10-17 21:52:26 +02:00
|
|
|
}
|
2018-11-18 00:00:28 +01:00
|
|
|
|
|
|
|
/**
|
2019-02-06 02:00:13 +01:00
|
|
|
* @param string $buffer
|
|
|
|
*
|
2018-11-18 00:00:28 +01:00
|
|
|
* @return int
|
|
|
|
*/
|
2019-02-06 02:00:13 +01:00
|
|
|
private function readMessages(string $buffer) : int
|
2018-11-18 00:00:28 +01:00
|
|
|
{
|
|
|
|
$emitted_messages = 0;
|
2019-02-06 02:00:13 +01:00
|
|
|
$i = 0;
|
|
|
|
while (($buffer[$i] ?? '') !== '') {
|
|
|
|
$this->buffer .= $buffer[$i++];
|
2018-11-18 00:00:28 +01:00
|
|
|
switch ($this->parsing_mode) {
|
|
|
|
case self::PARSE_HEADERS:
|
|
|
|
if ($this->buffer === "\r\n") {
|
|
|
|
$this->parsing_mode = self::PARSE_BODY;
|
2019-10-01 21:44:43 +02:00
|
|
|
$this->content_length = (int) ($this->headers['Content-Length'] ?? 0);
|
2018-11-18 00:00:28 +01:00
|
|
|
$this->buffer = '';
|
|
|
|
} elseif (substr($this->buffer, -2) === "\r\n") {
|
|
|
|
$parts = explode(':', $this->buffer);
|
|
|
|
$this->headers[$parts[0]] = trim($parts[1]);
|
|
|
|
$this->buffer = '';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case self::PARSE_BODY:
|
|
|
|
if (strlen($this->buffer) === $this->content_length) {
|
|
|
|
if (!$this->is_accepting_new_requests) {
|
|
|
|
// If we fork, don't read any bytes in the input buffer from the worker process.
|
|
|
|
$this->emitClose();
|
2019-07-05 22:24:00 +02:00
|
|
|
|
2018-11-18 00:00:28 +01:00
|
|
|
return $emitted_messages;
|
|
|
|
}
|
|
|
|
// MessageBody::parse can throw an Error, maybe log an error?
|
|
|
|
try {
|
|
|
|
$msg = new Message(MessageBody::parse($this->buffer), $this->headers);
|
|
|
|
} catch (Exception $_) {
|
|
|
|
$msg = null;
|
|
|
|
}
|
|
|
|
if ($msg) {
|
2019-07-05 22:24:00 +02:00
|
|
|
++$emitted_messages;
|
2018-11-18 00:00:28 +01:00
|
|
|
$this->emit('message', [$msg]);
|
|
|
|
/** @psalm-suppress DocblockTypeContradiction */
|
|
|
|
if (!$this->is_accepting_new_requests) {
|
|
|
|
// If we fork, don't read any bytes in the input buffer from the worker process.
|
|
|
|
$this->emitClose();
|
2019-07-05 22:24:00 +02:00
|
|
|
|
2018-11-18 00:00:28 +01:00
|
|
|
return $emitted_messages;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->parsing_mode = self::PARSE_HEADERS;
|
|
|
|
$this->headers = [];
|
|
|
|
$this->buffer = '';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-07-05 22:24:00 +02:00
|
|
|
|
2018-11-18 00:00:28 +01:00
|
|
|
return $emitted_messages;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function emitClose()
|
|
|
|
{
|
|
|
|
if ($this->did_emit_close) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->did_emit_close = true;
|
|
|
|
$this->emit('close');
|
|
|
|
}
|
2018-10-17 21:52:26 +02:00
|
|
|
}
|