From 54d4ebbcc9d74a098e9bb982d06d71b4d7d444d7 Mon Sep 17 00:00:00 2001 From: "Richard S. Hall" Date: Mon, 28 Aug 2017 09:35:51 -0400 Subject: [PATCH] When splitting socket data into command lines, zero-length literals were being handling improperly and would not continue to look for additional literals since it wasn't clear that a literal was found. Introduce a flag to indicate that a literal was found, so we know to keep looking for more literals regardless of whether literal has any length or not. --- src/emailjs-imap-client-imap.js | 5 ++++- test/unit/emailjs-imap-client-imap-test.js | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/emailjs-imap-client-imap.js b/src/emailjs-imap-client-imap.js index 0eef95b..b240d45 100644 --- a/src/emailjs-imap-client-imap.js +++ b/src/emailjs-imap-client-imap.js @@ -71,6 +71,7 @@ // As the server sends data in chunks, it needs to be split into separate lines. Helps parsing the input. this._incomingBuffers = []; + this._literalFound = false; this._literalRemaining = 0; // @@ -448,6 +449,7 @@ buf[j+2] === LINE_FEED) { const numBuf = buf.subarray(leftIdx+1, j); this._literalRemaining = Number(mimecodec.fromTypedArray(numBuf)); + this._literalFound = true; i = j + 3; } else { i = j; @@ -458,10 +460,11 @@ } const diff = Math.min(buf.length-i, this._literalRemaining); - if (diff) { + if (diff || this._literalFound) { this._literalRemaining -= diff; i += diff; if (this._literalRemaining === 0) { + this._literalFound = false; continue; // find another literal } } diff --git a/test/unit/emailjs-imap-client-imap-test.js b/test/unit/emailjs-imap-client-imap-test.js index 91d18d1..c8822e8 100644 --- a/test/unit/emailjs-imap-client-imap-test.js +++ b/test/unit/emailjs-imap-client-imap-test.js @@ -132,6 +132,12 @@ }); describe('#_iterateIncomingBuffer', () => { + it('Parse multiple zero-length literals', () => { + appendIncomingBuffer('* 126015 FETCH (UID 585599 BODY[1.2] {0}\r\n BODY[1.1] {0}\r\n)\r\n'); + var iterator = client._iterateIncomingBuffer(); + expect(String.fromCharCode.apply(null, iterator.next().value)).to.equal ('* 126015 FETCH (UID 585599 BODY[1.2] {0}\r\n BODY[1.1] {0}\r\n)'); + }); + it('should iterate chunked input', () => { appendIncomingBuffer('* 1 FETCH (UID 1)\r\n* 2 FETCH (UID 2)\r\n* 3 FETCH (UID 3)\r\n'); var iterator = client._iterateIncomingBuffer();