1
0
Fork 0

content-limit become configurable

pull/744/head
Robert Lu 2019-05-21 23:52:49 +08:00
parent b210c37d57
commit 89763896d3
4 changed files with 19 additions and 17 deletions

View File

@ -1,10 +1,10 @@
<?php <?php
use LanguageServer\{LanguageServer, ProtocolStreamReader, ProtocolStreamWriter, StderrLogger}; use LanguageServer\{LanguageServer, ProtocolStreamReader, ProtocolStreamWriter, StderrLogger, ContentTooLargeException};
use Sabre\Event\Loop; use Sabre\Event\Loop;
use Composer\XdebugHandler\XdebugHandler; use Composer\XdebugHandler\XdebugHandler;
$options = getopt('', ['tcp::', 'tcp-server::', 'memory-limit::']); $options = getopt('', ['tcp::', 'tcp-server::', 'memory-limit::', "content-limit::"]);
ini_set('memory_limit', $options['memory-limit'] ?? '4G'); ini_set('memory_limit', $options['memory-limit'] ?? '4G');
@ -39,6 +39,10 @@ $xdebugHandler->setLogger($logger);
$xdebugHandler->check(); $xdebugHandler->check();
unset($xdebugHandler); unset($xdebugHandler);
if (is_integer($options['content-limit'] ?? null)) {
ContentTooLargeException::$limit = (int)$options['content-limit'];
}
if (!empty($options['tcp'])) { if (!empty($options['tcp'])) {
// Connect to a TCP server // Connect to a TCP server
$address = $options['tcp']; $address = $options['tcp'];

View File

@ -20,12 +20,10 @@ class FileSystemContentRetriever implements ContentRetriever
*/ */
public function retrieve(string $uri): Promise public function retrieve(string $uri): Promise
{ {
$limit = 150000;
$path = uriToPath($uri); $path = uriToPath($uri);
$size = filesize($path); $size = filesize($path);
if ($size > $limit) { if ($size > ContentTooLargeException::$limit) {
return Promise\reject(new ContentTooLargeException($uri, $size, $limit)); return Promise\reject(new ContentTooLargeException($uri, $size));
} }
return Promise\resolve(file_get_contents($path)); return Promise\resolve(file_get_contents($path));
} }

View File

@ -8,6 +8,13 @@ namespace LanguageServer;
*/ */
class ContentTooLargeException extends \Exception class ContentTooLargeException extends \Exception
{ {
/**
* The limit of content
*
* @var int
*/
public static $limit = 1000000;
/** /**
* The URI of the file that exceeded the limit * The URI of the file that exceeded the limit
* *
@ -22,24 +29,16 @@ class ContentTooLargeException extends \Exception
*/ */
public $size; public $size;
/**
* The limit that was exceeded in bytes
*
* @var int
*/
public $limit;
/** /**
* @param string $uri The URI of the file that exceeded the limit * @param string $uri The URI of the file that exceeded the limit
* @param int $size The size of the file in bytes * @param int $size The size of the file in bytes
* @param int $limit The limit that was exceeded in bytes
* @param \Throwable $previous The previous exception used for the exception chaining. * @param \Throwable $previous The previous exception used for the exception chaining.
*/ */
public function __construct(string $uri, int $size, int $limit, \Throwable $previous = null) public function __construct(string $uri, int $size, \Throwable $previous = null)
{ {
$this->uri = $uri; $this->uri = $uri;
$this->size = $size; $this->size = $size;
$this->limit = $limit; $limit = self::$limit;
parent::__construct("$uri exceeds size limit of $limit bytes ($size)", 0, $previous); parent::__construct("$uri exceeds size limit of $limit bytes ($size)", 0, $previous);
} }
} }

View File

@ -218,9 +218,10 @@ class Indexer
$this->client->textDocument->publishDiagnostics($uri, $document->getDiagnostics()); $this->client->textDocument->publishDiagnostics($uri, $document->getDiagnostics());
} }
} catch (ContentTooLargeException $e) { } catch (ContentTooLargeException $e) {
$limit = ContentTooLargeException::$limit;
$this->client->window->logMessage( $this->client->window->logMessage(
MessageType::INFO, MessageType::INFO,
"Ignoring file {$uri} because it exceeds size limit of {$e->limit} bytes ({$e->size})" "Ignoring file {$uri} because it exceeds size limit of {$limit} bytes ({$e->size})"
); );
} catch (\Exception $e) { } catch (\Exception $e) {
$this->client->window->logMessage(MessageType::ERROR, "Error parsing $uri: " . (string)$e); $this->client->window->logMessage(MessageType::ERROR, "Error parsing $uri: " . (string)$e);