Skip to content

Commit d8f7347

Browse files
committed
Consistent comma splitting without regex overhead
Issue: SPR-14635 (cherry picked from commit 03609c1)
1 parent 3b91dec commit d8f7347

File tree

10 files changed

+98
-98
lines changed

10 files changed

+98
-98
lines changed

spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ public static List<MimeType> parseMimeTypes(String mimeTypes) {
277277
if (!StringUtils.hasLength(mimeTypes)) {
278278
return Collections.emptyList();
279279
}
280-
String[] tokens = mimeTypes.split(",\\s*");
280+
String[] tokens = StringUtils.tokenizeToStringArray(mimeTypes, ",");
281281
List<MimeType> result = new ArrayList<MimeType>(tokens.length);
282282
for (String token : tokens) {
283283
result.add(parseMimeType(token));

spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaderAccessor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,10 @@ public boolean isHeartbeat() {
228228

229229
public long[] getHeartbeat() {
230230
String rawValue = getFirstNativeHeader(STOMP_HEARTBEAT_HEADER);
231-
if (!StringUtils.hasText(rawValue)) {
231+
String[] rawValues = StringUtils.split(rawValue, ",");
232+
if (rawValues == null) {
232233
return Arrays.copyOf(DEFAULT_HEARTBEAT, 2);
233234
}
234-
String[] rawValues = StringUtils.commaDelimitedListToStringArray(rawValue);
235235
return new long[] {Long.valueOf(rawValues[0]), Long.valueOf(rawValues[1])};
236236
}
237237

@@ -297,7 +297,7 @@ public void setContentLength(int contentLength) {
297297
}
298298

299299
public void setHeartbeat(long cx, long cy) {
300-
setNativeHeader(STOMP_HEARTBEAT_HEADER, StringUtils.arrayToCommaDelimitedString(new Object[]{cx, cy}));
300+
setNativeHeader(STOMP_HEARTBEAT_HEADER, cx + "," + cy);
301301
}
302302

303303
public void setAck(String ack) {

spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaders.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -223,9 +223,14 @@ public String getPasscode() {
223223
* Applies to the CONNECT and CONNECTED frames.
224224
*/
225225
public void setHeartbeat(long[] heartbeat) {
226-
Assert.notNull(heartbeat);
226+
if (heartbeat == null || heartbeat.length != 2) {
227+
throw new IllegalArgumentException("Heart-beat array must be of length 2, not " +
228+
(heartbeat != null ? heartbeat.length : "null"));
229+
}
227230
String value = heartbeat[0] + "," + heartbeat[1];
228-
Assert.isTrue(heartbeat[0] >= 0 && heartbeat[1] >= 0, "Heart-beat values cannot be negative: " + value);
231+
if (heartbeat[0] < 0 || heartbeat[1] < 0) {
232+
throw new IllegalArgumentException("Heart-beat values cannot be negative: " + value);
233+
}
229234
set(HEARTBEAT, value);
230235
}
231236

@@ -234,10 +239,10 @@ public void setHeartbeat(long[] heartbeat) {
234239
*/
235240
public long[] getHeartbeat() {
236241
String rawValue = getFirst(HEARTBEAT);
237-
if (!StringUtils.hasText(rawValue)) {
242+
String[] rawValues = StringUtils.split(rawValue, ",");
243+
if (rawValues == null) {
238244
return null;
239245
}
240-
String[] rawValues = StringUtils.commaDelimitedListToStringArray(rawValue);
241246
return new long[] {Long.valueOf(rawValues[0]), Long.valueOf(rawValues[1])};
242247
}
243248

@@ -497,14 +502,8 @@ public Set<Entry<String, List<String>>> entrySet() {
497502

498503
@Override
499504
public boolean equals(Object other) {
500-
if (this == other) {
501-
return true;
502-
}
503-
if (!(other instanceof StompHeaders)) {
504-
return false;
505-
}
506-
StompHeaders otherHeaders = (StompHeaders) other;
507-
return this.headers.equals(otherHeaders.headers);
505+
return (this == other || (other instanceof StompHeaders &&
506+
this.headers.equals(((StompHeaders) other).headers)));
508507
}
509508

510509
@Override

spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.springframework.test.web.servlet.request.RequestPostProcessor;
5151
import org.springframework.util.Assert;
5252
import org.springframework.util.ObjectUtils;
53+
import org.springframework.util.StringUtils;
5354
import org.springframework.web.util.UriComponents;
5455
import org.springframework.web.util.UriComponentsBuilder;
5556

@@ -198,7 +199,8 @@ private void parent(MockHttpServletRequest request, RequestBuilder parent) {
198199
* {@link HttpServletRequest#getContextPath()} which states it can be
199200
* an empty string, or it must start with a "/" and not end with a "/".
200201
* @param contextPath a valid contextPath
201-
* @throws IllegalArgumentException if the contextPath is not a valid {@link HttpServletRequest#getContextPath()}
202+
* @throws IllegalArgumentException if the contextPath is not a valid
203+
* {@link HttpServletRequest#getContextPath()}
202204
*/
203205
public void setContextPath(String contextPath) {
204206
MockMvcWebConnection.validateContextPath(contextPath);
@@ -211,9 +213,9 @@ public void setForwardPostProcessor(RequestPostProcessor forwardPostProcessor) {
211213

212214
private void authType(MockHttpServletRequest request) {
213215
String authorization = header("Authorization");
214-
if (authorization != null) {
215-
String[] authzParts = authorization.split(": ");
216-
request.setAuthType(authzParts[0]);
216+
String[] authSplit = StringUtils.split(authorization, ": ");
217+
if (authSplit != null) {
218+
request.setAuthType(authSplit[0]);
217219
}
218220
}
219221

@@ -263,8 +265,8 @@ private void cookies(MockHttpServletRequest request) {
263265
while (tokens.hasMoreTokens()) {
264266
String cookieName = tokens.nextToken().trim();
265267
if (!tokens.hasMoreTokens()) {
266-
throw new IllegalArgumentException("Expected value for cookie name '" + cookieName
267-
+ "'. Full cookie was " + cookieHeaderValue);
268+
throw new IllegalArgumentException("Expected value for cookie name '" + cookieName +
269+
"'. Full cookie was " + cookieHeaderValue);
268270
}
269271
String cookieValue = tokens.nextToken().trim();
270272
processCookie(request, cookies, new Cookie(cookieName, cookieValue));
@@ -352,9 +354,9 @@ private void locales(MockHttpServletRequest request) {
352354
request.addPreferredLocale(Locale.getDefault());
353355
}
354356
else {
355-
String[] locales = locale.split(", ");
356-
for (int i = locales.length - 1; i >= 0; i--) {
357-
request.addPreferredLocale(parseLocale(locales[i]));
357+
String[] tokens = StringUtils.tokenizeToStringArray(locale, ",");
358+
for (int i = tokens.length - 1; i >= 0; i--) {
359+
request.addPreferredLocale(parseLocale(tokens[i]));
358360
}
359361
}
360362
}

spring-web/src/main/java/org/springframework/http/HttpHeaders.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ public List<HttpMethod> getAccessControlAllowMethods() {
474474
List<HttpMethod> result = new ArrayList<HttpMethod>();
475475
String value = getFirst(ACCESS_CONTROL_ALLOW_METHODS);
476476
if (value != null) {
477-
String[] tokens = StringUtils.tokenizeToStringArray(value, ",", true, true);
477+
String[] tokens = StringUtils.tokenizeToStringArray(value, ",");
478478
for (String token : tokens) {
479479
HttpMethod resolved = HttpMethod.resolve(token);
480480
if (resolved != null) {
@@ -578,10 +578,10 @@ public void setAcceptCharset(List<Charset> acceptableCharsets) {
578578
* as specified by the {@code Accept-Charset} header.
579579
*/
580580
public List<Charset> getAcceptCharset() {
581-
List<Charset> result = new ArrayList<Charset>();
582581
String value = getFirst(ACCEPT_CHARSET);
583582
if (value != null) {
584-
String[] tokens = value.split(",\\s*");
583+
String[] tokens = StringUtils.tokenizeToStringArray(value, ",");
584+
List<Charset> result = new ArrayList<Charset>(tokens.length);
585585
for (String token : tokens) {
586586
int paramIdx = token.indexOf(';');
587587
String charsetName;
@@ -595,8 +595,11 @@ public List<Charset> getAcceptCharset() {
595595
result.add(Charset.forName(charsetName));
596596
}
597597
}
598+
return result;
599+
}
600+
else {
601+
return Collections.emptyList();
598602
}
599-
return result;
600603
}
601604

602605
/**
@@ -615,8 +618,8 @@ public void setAllow(Set<HttpMethod> allowedMethods) {
615618
public Set<HttpMethod> getAllow() {
616619
String value = getFirst(ALLOW);
617620
if (!StringUtils.isEmpty(value)) {
618-
List<HttpMethod> result = new LinkedList<HttpMethod>();
619-
String[] tokens = value.split(",\\s*");
621+
String[] tokens = StringUtils.tokenizeToStringArray(value, ",");
622+
List<HttpMethod> result = new ArrayList<HttpMethod>(tokens.length);
620623
for (String token : tokens) {
621624
HttpMethod resolved = HttpMethod.resolve(token);
622625
if (resolved != null) {
@@ -691,7 +694,7 @@ public void setContentDispositionFormData(String name, String filename, Charset
691694
StringBuilder builder = new StringBuilder("form-data; name=\"");
692695
builder.append(name).append('\"');
693696
if (filename != null) {
694-
if(charset == null || Charset.forName("US-ASCII").equals(charset)) {
697+
if (charset == null || charset.name().equals("US-ASCII")) {
695698
builder.append("; filename=\"");
696699
builder.append(filename).append('\"');
697700
}

spring-web/src/main/java/org/springframework/http/HttpRange.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public static List<HttpRange> parseRanges(String ranges) {
132132
}
133133
ranges = ranges.substring(BYTE_RANGE_PREFIX.length());
134134

135-
String[] tokens = ranges.split(",\\s*");
135+
String[] tokens = StringUtils.tokenizeToStringArray(ranges, ",");
136136
List<HttpRange> result = new ArrayList<HttpRange>(tokens.length);
137137
for (String token : tokens) {
138138
result.add(parseRange(token));

spring-web/src/main/java/org/springframework/http/MediaType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ public static List<MediaType> parseMediaTypes(String mediaTypes) {
438438
if (!StringUtils.hasLength(mediaTypes)) {
439439
return Collections.emptyList();
440440
}
441-
String[] tokens = mediaTypes.split(",\\s*");
441+
String[] tokens = StringUtils.tokenizeToStringArray(mediaTypes, ",");
442442
List<MediaType> result = new ArrayList<MediaType>(tokens.length);
443443
for (String token : tokens) {
444444
result.add(parseMediaType(token));

spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ public UriComponentsBuilder fragment(String fragment) {
673673
UriComponentsBuilder adaptFromForwardedHeaders(HttpHeaders headers) {
674674
String forwardedHeader = headers.getFirst("Forwarded");
675675
if (StringUtils.hasText(forwardedHeader)) {
676-
String forwardedToUse = StringUtils.commaDelimitedListToStringArray(forwardedHeader)[0];
676+
String forwardedToUse = StringUtils.tokenizeToStringArray(forwardedHeader, ",")[0];
677677
Matcher matcher = FORWARDED_HOST_PATTERN.matcher(forwardedToUse);
678678
if (matcher.find()) {
679679
host(matcher.group(1).trim());
@@ -686,10 +686,9 @@ UriComponentsBuilder adaptFromForwardedHeaders(HttpHeaders headers) {
686686
else {
687687
String hostHeader = headers.getFirst("X-Forwarded-Host");
688688
if (StringUtils.hasText(hostHeader)) {
689-
String[] hosts = StringUtils.commaDelimitedListToStringArray(hostHeader);
690-
String hostToUse = hosts[0];
691-
if (hostToUse.contains(":")) {
692-
String[] hostAndPort = StringUtils.split(hostToUse, ":");
689+
String hostToUse = StringUtils.tokenizeToStringArray(hostHeader, ",")[0];
690+
String[] hostAndPort = StringUtils.split(hostToUse, ":");
691+
if (hostAndPort != null) {
693692
host(hostAndPort[0]);
694693
port(Integer.parseInt(hostAndPort[1]));
695694
}
@@ -701,14 +700,12 @@ UriComponentsBuilder adaptFromForwardedHeaders(HttpHeaders headers) {
701700

702701
String portHeader = headers.getFirst("X-Forwarded-Port");
703702
if (StringUtils.hasText(portHeader)) {
704-
String[] ports = StringUtils.commaDelimitedListToStringArray(portHeader);
705-
port(Integer.parseInt(ports[0]));
703+
port(Integer.parseInt(StringUtils.tokenizeToStringArray(portHeader, ",")[0]));
706704
}
707705

708706
String protocolHeader = headers.getFirst("X-Forwarded-Proto");
709707
if (StringUtils.hasText(protocolHeader)) {
710-
String[] protocols = StringUtils.commaDelimitedListToStringArray(protocolHeader);
711-
scheme(protocols[0]);
708+
scheme(StringUtils.tokenizeToStringArray(protocolHeader, ",")[0]);
712709
}
713710
}
714711

0 commit comments

Comments
 (0)