Skip to content

Commit

Permalink
avoid java.net.URI encoding/decoding for query args
Browse files Browse the repository at this point in the history
This is only needed when query contain user input and may contain
reserved chars.
  • Loading branch information
toppk committed Jan 30, 2021
1 parent 1b0319a commit cd4d22d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static String create(ServerSettings server) {
} else {
path = "/1|";
}
return new URI(scheme, userInfo, server.host, server.port, path, query, null).toString();
return new URI(scheme, userInfo, server.host, server.port, path, null, null).toString() + "?" + query;
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Can't create ImapStore URI", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public static ImapStoreSettings decode(String uri) {
}
}
}
String query = imapUri.getQuery();
String query = imapUri.getRawQuery();
Map<String,String> queryParams = splitQuery(query);
if (queryParams.containsKey("prefix")) {
if (queryParams.get("prefix").toLowerCase().equals("auto")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public void testDecodeStoreUriImapAutodetectAndPrefix() {

@Test
public void testDecodeStoreUriImapHybridPrefix() {
String uri = "imap://PLAIN:user:pass@server:143/0%7CcustomPathPrefix?prefix=/customPathPrefix&auth-type=plain";
String uri = "imap://PLAIN:user:pass@server:143/0%7CcustomPathPrefix?prefix=%2FcustomPathPrefix&auth-type=plain";

ServerSettings settings = ImapStoreUriDecoder.decode(uri);

Expand Down Expand Up @@ -240,6 +240,19 @@ public void testDecodeStoreUriImapCleanishPureExternal() {
assertNull(settings.getExtra().get("pathPrefix"));
}

@Test
public void testDecodeStoreUriImapFunkyPrefixl() {
String uri = "imap+ssl+://PLAIN:user:pass@server:993/0%7CThis%22Is%3F=Not&%23Healthy?prefix=%2FThis%22Is%3F%3DNot%26+%23Healthy&tls-cert=emailCert&auth-type=plain";

ServerSettings settings = ImapStoreUriDecoder.decode(uri);

assertEquals("This\"Is?=Not& #Healthy", settings.getExtra().get("pathPrefix"));


}



@Test
public void testCreateStoreUriImapPrefix() {
Map<String, String> extra = new HashMap<>();
Expand All @@ -250,7 +263,7 @@ public void testCreateStoreUriImapPrefix() {

String uri = ImapStoreUriCreator.create(settings);

assertEquals("imap://PLAIN:user:pass@server:143/0%7CcustomPathPrefix?prefix=/customPathPrefix&auth-type=plain", uri);
assertEquals("imap://PLAIN:user:pass@server:143/0%7CcustomPathPrefix?prefix=%2FcustomPathPrefix&auth-type=plain", uri);
}

@Test
Expand All @@ -263,7 +276,21 @@ public void testCreateStoreUriImapExternal() {

String uri = ImapStoreUriCreator.create(settings);

assertEquals("imap+ssl+://EXTERNAL:user:emailCert@server:993/0%7C?prefix=/&tls-cert=emailCert&auth-type=external", uri);
assertEquals("imap+ssl+://EXTERNAL:user:emailCert@server:993/0%7C?prefix=%2F&tls-cert=emailCert&auth-type=external", uri);
}


@Test
public void testCreateStoreUriImapFunkyPrefixl() {
Map<String, String> extra = new HashMap<>();
extra.put("autoDetectNamespace", "false");
extra.put("pathPrefix", "This\"Is?=Not& #Healthy");
ServerSettings settings = new ServerSettings("imap", "server", 993,
ConnectionSecurity.SSL_TLS_REQUIRED, AuthType.PLAIN, "user", "pass", "emailCert", extra);

String uri = ImapStoreUriCreator.create(settings);

assertEquals("imap+ssl+://PLAIN:user:pass@server:993/0%7CThis%22Is%3F=Not&%20%23Healthy?prefix=%2FThis%22Is%3F%3DNot%26+%23Healthy&tls-cert=emailCert&auth-type=plain", uri);
}

@Test
Expand All @@ -276,7 +303,7 @@ public void testCreateStoreUriImapEmptyPrefix() {

String uri = ImapStoreUriCreator.create(settings);

assertEquals("imap://PLAIN:user:pass@server:143/0%7C?prefix=/&auth-type=plain", uri);
assertEquals("imap://PLAIN:user:pass@server:143/0%7C?prefix=%2F&auth-type=plain", uri);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static Map<String, String> splitQuery(String query) {
String[] params = query.split("&");
for(String param: params) {
String[] parts = param.split("=");
queryParams.put(parts[0], parts[1]);
queryParams.put(parts[0], decodeUtf8(parts[1]));
}
return queryParams;
}
Expand All @@ -29,7 +29,8 @@ public static String buildQuery(Map<String, String> params) {
for (String param: params.keySet()) {
query.append(param);
query.append('=');
query.append(params.get(param));
//query.append(params.get(param));
query.append(encodeUtf8(params.get(param)));
query.append('&');
}
query.deleteCharAt(query.length()-1);
Expand Down

0 comments on commit cd4d22d

Please sign in to comment.