Skip to content

Commit 322bca8

Browse files
author
Stephane Landelle
committed
Force encoding when using String.toLower/UpperCase, close AsyncHttpClient#361
1 parent cdfbea6 commit 322bca8

File tree

9 files changed

+37
-26
lines changed

9 files changed

+37
-26
lines changed

api/src/main/java/org/asynchttpclient/FluentCaseInsensitiveStringsMap.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.LinkedHashMap;
2727
import java.util.LinkedHashSet;
2828
import java.util.List;
29+
import java.util.Locale;
2930
import java.util.Map;
3031
import java.util.Set;
3132

@@ -105,7 +106,7 @@ public FluentCaseInsensitiveStringsMap add(String key, Collection<String> values
105106
List<String> nonNullValues = fetchValues(values);
106107

107108
if (nonNullValues != null) {
108-
String lcKey = key.toLowerCase();
109+
String lcKey = key.toLowerCase(Locale.ENGLISH);
109110
String realKey = keyLookup.get(lcKey);
110111
List<String> curValues = null;
111112

@@ -177,7 +178,7 @@ public FluentCaseInsensitiveStringsMap replace(final String key, final String...
177178
public FluentCaseInsensitiveStringsMap replace(final String key, final Collection<String> values) {
178179
if (key != null) {
179180
List<String> nonNullValues = fetchValues(values);
180-
String lcKkey = key.toLowerCase();
181+
String lcKkey = key.toLowerCase(Locale.ENGLISH);
181182
String realKey = keyLookup.get(lcKkey);
182183

183184
if (nonNullValues == null) {
@@ -259,7 +260,7 @@ public void putAll(Map<? extends String, ? extends List<String>> values) {
259260
*/
260261
public FluentCaseInsensitiveStringsMap delete(String key) {
261262
if (key != null) {
262-
String lcKey = key.toLowerCase();
263+
String lcKey = key.toLowerCase(Locale.ENGLISH);
263264
String realKey = keyLookup.remove(lcKey);
264265

265266
if (realKey != null) {
@@ -368,7 +369,7 @@ public boolean isEmpty() {
368369
*/
369370
/* @Override */
370371
public boolean containsKey(Object key) {
371-
return key == null ? false : keyLookup.containsKey(key.toString().toLowerCase());
372+
return key == null ? false : keyLookup.containsKey(key.toString().toLowerCase(Locale.ENGLISH));
372373
}
373374

374375
/**
@@ -433,7 +434,7 @@ public List<String> get(Object key) {
433434
return null;
434435
}
435436

436-
String lcKey = key.toString().toLowerCase();
437+
String lcKey = key.toString().toLowerCase(Locale.ENGLISH);
437438
String realKey = keyLookup.get(lcKey);
438439

439440
if (realKey == null) {

api/src/main/java/org/asynchttpclient/util/ProxyUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public static boolean avoidProxy(final ProxyServer proxyServer, final Request re
113113
*/
114114
public static boolean avoidProxy(final ProxyServer proxyServer, final String target) {
115115
if (proxyServer != null) {
116-
final String targetHost = target.toLowerCase();
116+
final String targetHost = target.toLowerCase(Locale.ENGLISH);
117117

118118
List<String> nonProxyHosts = proxyServer.getNonProxyHosts();
119119

api/src/test/java/org/asynchttpclient/async/AsyncStreamHandlerTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.Arrays;
3030
import java.util.Collection;
3131
import java.util.HashMap;
32+
import java.util.Locale;
3233
import java.util.Map;
3334
import java.util.concurrent.CountDownLatch;
3435
import java.util.concurrent.Future;
@@ -52,7 +53,7 @@ public STATE onHeadersReceived(HttpResponseHeaders content) throws Exception {
5253
try {
5354
FluentCaseInsensitiveStringsMap h = content.getHeaders();
5455
Assert.assertNotNull(h);
55-
Assert.assertEquals(h.getJoinedValue("content-type", ", ").toLowerCase(), UTF8);
56+
Assert.assertEquals(h.getJoinedValue("content-type", ", ").toLowerCase(Locale.ENGLISH), UTF8);
5657
return STATE.ABORT;
5758
} finally {
5859
l.countDown();
@@ -94,7 +95,7 @@ public void asyncStreamPOSTTest() throws Throwable {
9495
public STATE onHeadersReceived(HttpResponseHeaders content) throws Exception {
9596
FluentCaseInsensitiveStringsMap h = content.getHeaders();
9697
Assert.assertNotNull(h);
97-
Assert.assertEquals(h.getJoinedValue("content-type", ", ").toLowerCase(), UTF8);
98+
Assert.assertEquals(h.getJoinedValue("content-type", ", ").toLowerCase(Locale.ENGLISH), UTF8);
9899
return STATE.CONTINUE;
99100
}
100101

@@ -142,7 +143,7 @@ public void asyncStreamInterruptTest() throws Throwable {
142143
public STATE onHeadersReceived(HttpResponseHeaders content) throws Exception {
143144
FluentCaseInsensitiveStringsMap h = content.getHeaders();
144145
Assert.assertNotNull(h);
145-
Assert.assertEquals(h.getJoinedValue("content-type", ", ").toLowerCase(), UTF8);
146+
Assert.assertEquals(h.getJoinedValue("content-type", ", ").toLowerCase(Locale.ENGLISH), UTF8);
146147
return STATE.ABORT;
147148
}
148149

@@ -183,7 +184,7 @@ public void asyncStreamFutureTest() throws Throwable {
183184
public STATE onHeadersReceived(HttpResponseHeaders content) throws Exception {
184185
FluentCaseInsensitiveStringsMap h = content.getHeaders();
185186
Assert.assertNotNull(h);
186-
Assert.assertEquals(h.getJoinedValue("content-type", ", ").toLowerCase(), UTF8);
187+
Assert.assertEquals(h.getJoinedValue("content-type", ", ").toLowerCase(Locale.ENGLISH), UTF8);
187188
return STATE.CONTINUE;
188189
}
189190

@@ -268,7 +269,7 @@ public void asyncStreamReusePOSTTest() throws Throwable {
268269
public STATE onHeadersReceived(HttpResponseHeaders content) throws Exception {
269270
FluentCaseInsensitiveStringsMap h = content.getHeaders();
270271
Assert.assertNotNull(h);
271-
Assert.assertEquals(h.getJoinedValue("content-type", ", ").toLowerCase(), UTF8);
272+
Assert.assertEquals(h.getJoinedValue("content-type", ", ").toLowerCase(Locale.ENGLISH), UTF8);
272273
return STATE.CONTINUE;
273274
}
274275

@@ -303,7 +304,7 @@ public String onCompleted() throws Exception {
303304
public STATE onHeadersReceived(HttpResponseHeaders content) throws Exception {
304305
FluentCaseInsensitiveStringsMap h = content.getHeaders();
305306
Assert.assertNotNull(h);
306-
Assert.assertEquals(h.getJoinedValue("content-type", ", ").toLowerCase(), UTF8);
307+
Assert.assertEquals(h.getJoinedValue("content-type", ", ").toLowerCase(Locale.ENGLISH), UTF8);
307308
return STATE.CONTINUE;
308309
}
309310

@@ -345,7 +346,7 @@ public void asyncStream301WithBody() throws Throwable {
345346
public STATE onHeadersReceived(HttpResponseHeaders content) throws Exception {
346347
FluentCaseInsensitiveStringsMap h = content.getHeaders();
347348
Assert.assertNotNull(h);
348-
Assert.assertEquals(h.getJoinedValue("content-type", ", ").toLowerCase(), "text/html; charset=utf-8");
349+
Assert.assertEquals(h.getJoinedValue("content-type", ", ").toLowerCase(Locale.ENGLISH), "text/html; charset=utf-8");
349350
return STATE.CONTINUE;
350351
}
351352

providers/grizzly/src/main/java/org/asynchttpclient/providers/grizzly/ConnectionManager.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@
3232

3333
import javax.net.ssl.HostnameVerifier;
3434
import javax.net.ssl.SSLSession;
35+
3536
import java.io.IOException;
3637
import java.net.ConnectException;
3738
import java.net.InetAddress;
3839
import java.net.InetSocketAddress;
3940
import java.net.SocketAddress;
4041
import java.net.URI;
4142
import java.util.HashMap;
43+
import java.util.Locale;
4244
import java.util.Map;
4345
import java.util.concurrent.CancellationException;
4446
import java.util.concurrent.ExecutionException;
@@ -250,7 +252,7 @@ private SocketAddress getRemoteAddress(final Request request,
250252
private static int getPort(final URI uri, final int p) {
251253
int port = p;
252254
if (port == -1) {
253-
final String protocol = uri.getScheme().toLowerCase();
255+
final String protocol = uri.getScheme().toLowerCase(Locale.ENGLISH);
254256
if ("http".equals(protocol) || "ws".equals(protocol)) {
255257
port = 80;
256258
} else if ("https".equals(protocol) || "wss".equals(protocol)) {

providers/grizzly/src/main/java/org/asynchttpclient/providers/grizzly/EventHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import java.net.URI;
4949
import java.util.HashMap;
5050
import java.util.List;
51+
import java.util.Locale;
5152
import java.util.Map;
5253
import java.util.concurrent.TimeUnit;
5354

@@ -424,7 +425,7 @@ private static void processKeepAlive(final Connection c,
424425
if (connectionHeader == null) {
425426
state.setKeepAlive(header.getProtocol() == Protocol.HTTP_1_1);
426427
} else {
427-
if ("close".equals(connectionHeader.toLowerCase())) {
428+
if ("close".equals(connectionHeader.toLowerCase(Locale.ENGLISH))) {
428429
ConnectionManager.markConnectionAsDoNotCache(c);
429430
state.setKeepAlive(false);
430431
} else {

providers/grizzly/src/main/java/org/asynchttpclient/providers/grizzly/statushandler/AuthorizationHandler.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import java.io.UnsupportedEncodingException;
2828
import java.security.NoSuchAlgorithmException;
29+
import java.util.Locale;
2930

3031
import static org.asynchttpclient.providers.grizzly.statushandler.StatusHandler.InvocationStatus.STOP;
3132

@@ -70,15 +71,16 @@ public boolean handleStatus(final HttpResponsePacket responsePacket,
7071
.setUsePreemptiveAuth(true)
7172
.parseWWWAuthenticateHeader(auth)
7273
.build();
73-
if (auth.toLowerCase().startsWith("basic")) {
74+
String lowerCaseAuth = auth.toLowerCase(Locale.ENGLISH);
75+
if (lowerCaseAuth.startsWith("basic")) {
7476
req.getHeaders().remove(Header.Authorization.toString());
7577
try {
7678
req.getHeaders().add(Header.Authorization.toString(),
7779
AuthenticatorUtils.computeBasicAuthentication(
7880
realm));
7981
} catch (UnsupportedEncodingException ignored) {
8082
}
81-
} else if (auth.toLowerCase().startsWith("digest")) {
83+
} else if (lowerCaseAuth.startsWith("digest")) {
8284
req.getHeaders().remove(Header.Authorization.toString());
8385
try {
8486
req.getHeaders().add(Header.Authorization.toString(),

providers/grizzly/src/main/java/org/asynchttpclient/providers/grizzly/statushandler/ProxyAuthorizationHandler.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
import java.io.UnsupportedEncodingException;
3737
import java.security.NoSuchAlgorithmException;
38+
import java.util.Locale;
3839

3940
public final class ProxyAuthorizationHandler implements StatusHandler {
4041

@@ -76,7 +77,8 @@ public boolean handleStatus(final HttpResponsePacket responsePacket,
7677
.setUsePreemptiveAuth(true)
7778
.parseProxyAuthenticateHeader(proxyAuth)
7879
.build();
79-
if (proxyAuth.toLowerCase().startsWith("basic")) {
80+
String proxyAuthLowerCase = proxyAuth.toLowerCase(Locale.ENGLISH);
81+
if (proxyAuthLowerCase.startsWith("basic")) {
8082
req.getHeaders().remove(Header.ProxyAuthenticate.toString());
8183
req.getHeaders().remove(Header.ProxyAuthorization.toString());
8284
try {
@@ -85,7 +87,7 @@ public boolean handleStatus(final HttpResponsePacket responsePacket,
8587
realm));
8688
} catch (UnsupportedEncodingException ignored) {
8789
}
88-
} else if (proxyAuth.toLowerCase().startsWith("digest")) {
90+
} else if (proxyAuthLowerCase.startsWith("digest")) {
8991
req.getHeaders().remove(Header.ProxyAuthenticate.toString());
9092
req.getHeaders().remove(Header.ProxyAuthorization.toString());
9193
try {
@@ -98,7 +100,7 @@ public boolean handleStatus(final HttpResponsePacket responsePacket,
98100
} catch (UnsupportedEncodingException e) {
99101
throw new IllegalStateException("Unsupported encoding.", e);
100102
}
101-
} else if (proxyAuth.toLowerCase().startsWith("ntlm")) {
103+
} else if (proxyAuthLowerCase.startsWith("ntlm")) {
102104

103105
req.getHeaders().remove(Header.ProxyAuthenticate.toString());
104106
req.getHeaders().remove(Header.ProxyAuthorization.toString());
@@ -124,7 +126,7 @@ public boolean handleStatus(final HttpResponsePacket responsePacket,
124126
} catch (Exception e1) {
125127
e1.printStackTrace();
126128
}
127-
} else if (proxyAuth.toLowerCase().startsWith("negotiate")) {
129+
} else if (proxyAuthLowerCase.startsWith("negotiate")) {
128130
//this is for kerberos
129131
req.getHeaders().remove(Header.ProxyAuthenticate.toString());
130132
req.getHeaders().remove(Header.ProxyAuthorization.toString());
@@ -208,7 +210,7 @@ private boolean executeRequest(
208210
}
209211

210212
public static boolean isNTLMSecondHandShake(final String proxyAuth) {
211-
return (proxyAuth != null && proxyAuth.toLowerCase()
213+
return (proxyAuth != null && proxyAuth.toLowerCase(Locale.ENGLISH)
212214
.startsWith("ntlm") && !proxyAuth.equalsIgnoreCase("ntlm"));
213215
}
214216

providers/netty/src/main/java/org/asynchttpclient/providers/netty/NettyAsyncHttpProvider.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
import org.slf4j.LoggerFactory;
107107

108108
import javax.net.ssl.SSLEngine;
109+
109110
import java.io.File;
110111
import java.io.FileInputStream;
111112
import java.io.IOException;
@@ -122,6 +123,7 @@
122123
import java.security.NoSuchAlgorithmException;
123124
import java.util.ArrayList;
124125
import java.util.List;
126+
import java.util.Locale;
125127
import java.util.Map.Entry;
126128
import java.util.concurrent.Callable;
127129
import java.util.concurrent.ExecutionException;
@@ -1995,7 +1997,7 @@ public void handle(final ChannelHandlerContext ctx, final MessageEvent e) throws
19951997
int statusCode = response.getStatus().getCode();
19961998

19971999
String ka = response.getHeader(HttpHeaders.Names.CONNECTION);
1998-
future.setKeepAlive(ka == null || !ka.toLowerCase().equals("close"));
2000+
future.setKeepAlive(ka == null || !ka.toLowerCase(Locale.ENGLISH).equals("close"));
19992001

20002002
List<String> wwwAuth = getAuthorizationToken(response.getHeaders(), HttpHeaders.Names.WWW_AUTHENTICATE);
20012003
Realm realm = request.getRealm() != null ? request.getRealm() : config.getRealm();
@@ -2246,7 +2248,7 @@ public void handle(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
22462248
final boolean validUpgrade = response.getHeader(HttpHeaders.Names.UPGRADE) != null;
22472249
String c = response.getHeader(HttpHeaders.Names.CONNECTION);
22482250
if (c == null) {
2249-
c = response.getHeader(HttpHeaders.Names.CONNECTION.toLowerCase());
2251+
c = response.getHeader(HttpHeaders.Names.CONNECTION.toLowerCase(Locale.ENGLISH));
22502252
}
22512253

22522254
final boolean validConnection = c == null ? false : c.equalsIgnoreCase(HttpHeaders.Values.UPGRADE);

providers/netty/src/main/java/org/asynchttpclient/providers/netty/NettyAsyncHttpProviderConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
package org.asynchttpclient.providers.netty;
1818

1919
import java.util.HashMap;
20+
import java.util.Locale;
2021
import java.util.Map;
2122
import java.util.Set;
2223
import java.util.concurrent.ExecutorService;
2324

2425
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
2526
import org.slf4j.Logger;
2627
import org.slf4j.LoggerFactory;
27-
2828
import org.asynchttpclient.AsyncHttpProviderConfig;
2929

3030
/**
@@ -96,7 +96,7 @@ public NettyAsyncHttpProviderConfig() {
9696
*/
9797
public NettyAsyncHttpProviderConfig addProperty(String name, Object value) {
9898

99-
if (name.equals(REUSE_ADDRESS) && value == Boolean.TRUE && System.getProperty("os.name").toLowerCase().contains("win")) {
99+
if (name.equals(REUSE_ADDRESS) && value == Boolean.TRUE && System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("win")) {
100100
LOGGER.warn("Can't enable {} on Windows", REUSE_ADDRESS);
101101
} else {
102102
properties.put(name, value);

0 commit comments

Comments
 (0)