diff --git a/src/main/java/org/apache/commons/net/imap/IMAP.java b/src/main/java/org/apache/commons/net/imap/IMAP.java index 395be9c9d..bd9ac115a 100644 --- a/src/main/java/org/apache/commons/net/imap/IMAP.java +++ b/src/main/java/org/apache/commons/net/imap/IMAP.java @@ -280,7 +280,12 @@ private void getReply(final boolean wantTag) throws IOException { throw new EOFException("Connection closed without indication."); } replyLines.add(line); - literalCount -= line.length() + 2; // Allow for CRLF + if (literalCount >= line.length() + 2) { + literalCount -= line.length() + 2; // Allow for CRLF + } else { + // The literal ends on this line, and the rest of the line may introduce another literal. + literalCount = literalCount < line.length() ? IMAPReply.literalCount(line.substring(literalCount)) : -1; + } } if (isMultiLine) { final IMAPChunkListener il = chunkListener; diff --git a/src/test/java/org/apache/commons/net/imap/IMAPTest.java b/src/test/java/org/apache/commons/net/imap/IMAPTest.java index 1193ec0a1..52c43b549 100644 --- a/src/test/java/org/apache/commons/net/imap/IMAPTest.java +++ b/src/test/java/org/apache/commons/net/imap/IMAPTest.java @@ -22,8 +22,13 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; import java.util.stream.Stream; +import org.apache.commons.net.io.CRLFLineReader; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; @@ -84,6 +89,25 @@ void testConstructDefaultIMAP() { assertEquals(0, imap.getReplyStrings().length); } + @Test + void testGetReplyReadsEveryLiteral() throws IOException { + final String header = "Subject: hi\r\n\r\n"; + // Message text is not protocol data, even when it looks like a tagged reply and a FETCH response. + final String text = "x OK\r\n* 2 FETCH (BODY[] {6}\r\nfake\r\n)\r\ny OK FETCH completed\r\n"; + final String message2 = "Subject: two\r\n"; + final IMAP imap = new IMAP(); + imap._reader = new CRLFLineReader(new StringReader("* 1 FETCH (BODY[HEADER] {" + header.length() + "}\r\n" + header + " BODY[1] {3}\r\nabc BODY[TEXT] {" + + text.length() + "}\r\n" + text + ")\r\nA001 OK FETCH completed\r\n* 2 FETCH (BODY[] {" + message2.length() + "}\r\n" + message2 + + ")\r\nA002 OK FETCH completed\r\n")); + imap.__writer = new BufferedWriter(new StringWriter()); + assertEquals(IMAPReply.OK, imap.sendCommand(IMAPCommand.FETCH, "1 (BODY.PEEK[HEADER] BODY.PEEK[1] BODY.PEEK[TEXT])")); + final String[] reply = imap.getReplyStrings(); + assertEquals(12, reply.length); + assertEquals("A001 OK FETCH completed", reply[reply.length - 1]); + assertEquals(IMAPReply.OK, imap.sendCommand(IMAPCommand.FETCH, "2 BODY[]")); + assertEquals("Subject: two", imap.getReplyStrings()[1]); + } + @Test void testQuoteMailboxNameNullInput() { assertNull(IMAP.quoteMailboxName(null));