Skip to content
This repository was archived by the owner on Feb 20, 2020. It is now read-only.

Commit dac2583

Browse files
committed
search with Unicode char throws BadCommandException with UTF8=ALLOW - fix #333
1 parent 9480110 commit dac2583

File tree

3 files changed

+75
-6
lines changed

3 files changed

+75
-6
lines changed

doc/release/CHANGES.txt

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ GH 323 Support loading protocol providers using ServiceLoader
3333
GH 326 Apply relaxed Content-Disposition parsing to Content-Disposition params
3434
GH 330 Attachment filename is ignored
3535
GH 332 http proxy support should support authenticating to the proxy server
36+
GH 333 search with Unicode char throws BadCommandException with UTF8=ALLOW
3637
GH 334 gimap set labels error with some non english characters
3738

3839

mail/src/main/java/com/sun/mail/imap/protocol/IMAPProtocol.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -2493,17 +2493,20 @@ public int[] search(SearchTerm term)
24932493
*/
24942494
private int[] search(String msgSequence, SearchTerm term)
24952495
throws ProtocolException, SearchException {
2496-
// Check if the search "text" terms contain only ASCII chars
2497-
if (SearchSequence.isAscii(term)) {
2496+
// Check if the search "text" terms contain only ASCII chars,
2497+
// or if utf8 support has been enabled (in which case CHARSET
2498+
// is not allowed; see RFC 6855, section 3, last paragraph)
2499+
if (supportsUtf8() || SearchSequence.isAscii(term)) {
24982500
try {
24992501
return issueSearch(msgSequence, term, null);
25002502
} catch (IOException ioex) { /* will not happen */ }
25012503
}
25022504

25032505
/*
2504-
* The search "text" terms do contain non-ASCII chars. We need to
2505-
* use SEARCH CHARSET <charset> ...
2506-
* The charsets we try to use are UTF-8 and the locale's
2506+
* The search "text" terms do contain non-ASCII chars and utf8
2507+
* support has not been enabled. We need to use:
2508+
* "SEARCH CHARSET <charset> ..."
2509+
* The charsets we try to use are UTF-8 and the locale's
25072510
* default charset. If the server supports UTF-8, great,
25082511
* always use it. Else we try to use the default charset.
25092512
*/

mail/src/test/java/com/sun/mail/imap/IMAPSearchTest.java

+66-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2009-2017 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2009-2018 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -113,4 +113,69 @@ public void search(String line) throws IOException {
113113
}
114114
}
115115
}
116+
117+
/**
118+
* Test that when the server supports UTF8 and the client enables it,
119+
* the client doesn't issue a SEARCH CHARSET command even if the search
120+
* term includes a non-ASCII character.
121+
* (see RFC 6855, section 3, last paragraph)
122+
*/
123+
@Test
124+
public void testUtf8Search() {
125+
TestServer server = null;
126+
try {
127+
server = new TestServer(new IMAPUtf8Handler() {
128+
@Override
129+
public void search(String line) throws IOException {
130+
if (line.contains("CHARSET"))
131+
bad("CHARSET not supported");
132+
else
133+
ok();
134+
}
135+
});
136+
server.start();
137+
138+
final Properties properties = new Properties();
139+
properties.setProperty("mail.imap.host", "localhost");
140+
properties.setProperty("mail.imap.port", "" + server.getPort());
141+
final Session session = Session.getInstance(properties);
142+
//session.setDebug(true);
143+
144+
final Store store = session.getStore("imap");
145+
Folder folder = null;
146+
try {
147+
store.connect("test", "test");
148+
folder = store.getFolder("INBOX");
149+
folder.open(Folder.READ_ONLY);
150+
Message[] msgs = folder.search(new SubjectTerm("\u2019"));
151+
} catch (Exception ex) {
152+
System.out.println(ex);
153+
//ex.printStackTrace();
154+
fail(ex.toString());
155+
} finally {
156+
if (folder != null)
157+
folder.close(false);
158+
store.close();
159+
}
160+
} catch (final Exception e) {
161+
e.printStackTrace();
162+
fail(e.getMessage());
163+
} finally {
164+
if (server != null) {
165+
server.quit();
166+
}
167+
}
168+
}
169+
170+
/**
171+
* An IMAPHandler that enables UTF-8 support.
172+
*/
173+
private static class IMAPUtf8Handler extends IMAPHandler {
174+
{{ capabilities += " ENABLE UTF8=ACCEPT"; }}
175+
176+
@Override
177+
public void enable(String line) throws IOException {
178+
ok();
179+
}
180+
}
116181
}

0 commit comments

Comments
 (0)