Add support for UTF-8 in quoted strings to ImapResponseParser

This commit is contained in:
cketti 2022-09-14 15:24:44 +02:00
parent 00259046c6
commit dd3d103fd3
3 changed files with 18 additions and 4 deletions

View file

@ -17,6 +17,7 @@ dependencies {
implementation "com.jcraft:jzlib:1.0.7"
implementation "com.beetstra.jutf7:jutf7:1.0.0"
implementation "commons-io:commons-io:${versions.commonsIo}"
implementation "com.squareup.okio:okio:${versions.okio}"
testImplementation project(":mail:testing")
testImplementation "junit:junit:${versions.junit}"

View file

@ -10,6 +10,7 @@ import com.fsck.k9.logging.Timber;
import com.fsck.k9.mail.K9MailLib;
import com.fsck.k9.mail.filter.FixedLengthInputStream;
import com.fsck.k9.mail.filter.PeekableInputStream;
import okio.Buffer;
import static com.fsck.k9.mail.K9MailLib.DEBUG_PROTOCOL_IMAP;
@ -396,7 +397,7 @@ class ImapResponseParser {
private String parseQuoted() throws IOException {
expect('"');
StringBuilder sb = new StringBuilder();
Buffer buffer = new Buffer();
int ch;
boolean escape = false;
while ((ch = inputStream.read()) != -1) {
@ -404,12 +405,13 @@ class ImapResponseParser {
// Found the escape character
escape = true;
} else if (!escape && ch == '"') {
return sb.toString();
return buffer.readUtf8();
} else {
sb.append((char) ch);
buffer.writeByte(ch);
escape = false;
}
}
throw new IOException("parseQuoted(): end of stream reached");
}

View file

@ -8,6 +8,7 @@ import java.util.List;
import com.fsck.k9.mail.filter.FixedLengthInputStream;
import com.fsck.k9.mail.filter.PeekableInputStream;
import kotlin.text.Charsets;
import org.junit.Test;
import static java.util.Arrays.asList;
@ -346,6 +347,16 @@ public class ImapResponseParserTest {
assertEquals("qu\"oted", response.getString(0));
}
@Test
public void utf8InQuotedString() throws Exception {
ImapResponseParser parser = createParser("* \"quöted\"\r\n");
ImapResponse response = parser.readResponse();
assertEquals(1, response.size());
assertEquals("quöted", response.getString(0));
}
@Test(expected = IOException.class)
public void testParseQuotedToEndOfStream() throws Exception {
ImapResponseParser parser = createParser("* \"abc");
@ -484,7 +495,7 @@ public class ImapResponseParserTest {
}
private ImapResponseParser createParser(String response) {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(response.getBytes());
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(response.getBytes(Charsets.UTF_8));
peekableInputStream = new PeekableInputStream(byteArrayInputStream);
return new ImapResponseParser(peekableInputStream);
}