slightly better large literal handling

This commit is contained in:
Andris Reinman 2018-12-27 17:04:05 +02:00
parent c3f2b8f3d3
commit 73eb27acb0
2 changed files with 13 additions and 3 deletions

View file

@ -382,11 +382,14 @@ class TokenParser {
if (chr === '\u0000') {
throw new Error('Unexpected \\x00 at position ' + (this.pos + i));
}
this.currentNode.value += chr;
if (this.currentNode.value.length >= this.currentNode.literalLength) {
this.currentNode.chBuffer[this.currentNode.chPos++] = chr.charCodeAt(0);
if (this.currentNode.chPos >= this.currentNode.literalLength) {
this.currentNode.endPos = this.pos + i;
this.currentNode.closed = true;
this.currentNode.value = this.currentNode.chBuffer.toString('binary');
this.currentNode.chBuffer = Buffer.alloc(0);
this.currentNode = this.currentNode.parentNode;
this.state = 'NORMAL';
checkSP();
@ -410,6 +413,7 @@ class TokenParser {
} else {
throw new Error('Unexpected char at position ' + (this.pos + i));
}
this.currentNode.literalLength = Number(this.currentNode.literalLength);
this.currentNode.started = true;
@ -421,6 +425,11 @@ class TokenParser {
this.currentNode = this.currentNode.parentNode;
this.state = 'NORMAL';
checkSP();
} else {
// Allocate expected size buffer. Max size check is already performed
// Maybe should use allocUnsafe instead?
this.currentNode.chBuffer = Buffer.alloc(this.currentNode.literalLength);
this.currentNode.chPos = 0;
}
break;
}

View file

@ -137,7 +137,8 @@ class IMAPCommand {
if (command.expecting > maxAllowed) {
// APPENDLIMIT response for too large messages
this.connection.send(this.tag + ' BAD [TOOBIG] Literal too large');
// TOOBIG: https://tools.ietf.org/html/rfc4469#section-4.2
this.connection.send(this.tag + ' NO [TOOBIG] Literal too large');
} else {
this.connection.send(this.tag + ' NO Literal too large');
}