Revised Charset handling and common StringUtils.uriDecode delegate

Issue: SPR-14492
This commit is contained in:
Juergen Hoeller 2017-01-18 00:09:06 +01:00
parent 8417831602
commit d21b6e596f
7 changed files with 131 additions and 144 deletions

View File

@ -16,6 +16,8 @@
package org.springframework.util;
import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@ -690,6 +692,59 @@ public abstract class StringUtils {
return cleanPath(path1).equals(cleanPath(path2));
}
/**
* Decode the given encoded URI component value. Based on the following rules:
* <ul>
* <li>Alphanumeric characters {@code "a"} through {@code "z"}, {@code "A"} through {@code "Z"},
* and {@code "0"} through {@code "9"} stay the same.</li>
* <li>Special characters {@code "-"}, {@code "_"}, {@code "."}, and {@code "*"} stay the same.</li>
* <li>A sequence "{@code %<i>xy</i>}" is interpreted as a hexadecimal representation of the character.</li>
* </ul>
* @param source the encoded String (may be {@code null})
* @param charset the character set
* @return the decoded value
* @throws IllegalArgumentException when the given source contains invalid encoded sequences
* @since 5.0
* @see java.net.URLDecoder#decode(String, String)
*/
public static String uriDecode(String source, Charset charset) {
if (source == null) {
return null;
}
int length = source.length();
if (length == 0) {
return source;
}
Assert.notNull(charset, "Charset must not be null");
ByteArrayOutputStream bos = new ByteArrayOutputStream(length);
boolean changed = false;
for (int i = 0; i < length; i++) {
int ch = source.charAt(i);
if (ch == '%') {
if (i + 2 < length) {
char hex1 = source.charAt(i + 1);
char hex2 = source.charAt(i + 2);
int u = Character.digit(hex1, 16);
int l = Character.digit(hex2, 16);
if (u == -1 || l == -1) {
throw new IllegalArgumentException("Invalid encoded sequence \"" + source.substring(i) + "\"");
}
bos.write((char) ((u << 4) + l));
i += 2;
changed = true;
}
else {
throw new IllegalArgumentException("Invalid encoded sequence \"" + source.substring(i) + "\"");
}
}
else {
bos.write(ch);
}
}
return (changed ? new String(bos.toByteArray(), charset) : source);
}
/**
* Parse the given {@code localeString} value into a {@link Locale}.
* <p>This is the inverse operation of {@link Locale#toString Locale's toString}.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -30,7 +30,6 @@ import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.util.UriUtils;
/**
* Lookup function used by {@link RouterFunctions#resources(String, Resource)}.
@ -55,7 +54,7 @@ class PathResourceLookupFunction implements Function<ServerRequest, Mono<Resourc
public Mono<Resource> apply(ServerRequest request) {
String path = processPath(request.path());
if (path.contains("%")) {
path = UriUtils.decode(path, StandardCharsets.UTF_8);
path = StringUtils.uriDecode(path, StandardCharsets.UTF_8);
}
if (!StringUtils.hasLength(path) || isInvalidPath(path)) {
return Mono.empty();
@ -116,8 +115,7 @@ class PathResourceLookupFunction implements Function<ServerRequest, Mono<Resourc
return false;
}
private boolean isResourceUnderLocation(Resource resource) throws
IOException {
private boolean isResourceUnderLocation(Resource resource) throws IOException {
if (resource.getClass() != this.location.getClass()) {
return false;
}
@ -148,7 +146,7 @@ class PathResourceLookupFunction implements Function<ServerRequest, Mono<Resourc
}
if (resourcePath.contains("%")) {
if (UriUtils.decode(resourcePath, "UTF-8").contains("../")) {
if (StringUtils.uriDecode(resourcePath, StandardCharsets.UTF_8).contains("../")) {
return false;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -27,7 +27,6 @@ import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.util.UriUtils;
/**
* Common base class for {@link ServerHttpRequest} implementations.
@ -81,7 +80,6 @@ public abstract class AbstractServerHttpRequest implements ServerHttpRequest {
/**
* A method for parsing of the query into name-value pairs. The return
* value is turned into an immutable map and cached.
*
* <p>Note that this method is invoked lazily on first access to
* {@link #getQueryParams()}. The invocation is not synchronized but the
* parsing is thread-safe nevertheless.
@ -102,8 +100,8 @@ public abstract class AbstractServerHttpRequest implements ServerHttpRequest {
return queryParams;
}
private static String decodeQueryParam(String value) {
return (value != null ? UriUtils.decode(value, StandardCharsets.UTF_8) : null);
private String decodeQueryParam(String value) {
return StringUtils.uriDecode(value, StandardCharsets.UTF_8);
}
@Override

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -18,11 +18,9 @@ package org.springframework.web.util;
import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@ -51,6 +49,9 @@ final class HierarchicalUriComponents extends UriComponents {
private static final char PATH_DELIMITER = '/';
private static final String PATH_DELIMITER_STRING = "/";
private final String userInfo;
private final String host;
@ -183,10 +184,9 @@ final class HierarchicalUriComponents extends UriComponents {
* the result as a new {@code UriComponents} instance.
* @param charset the encoding of the values contained in this map
* @return the encoded uri components
* @throws UnsupportedEncodingException if the given encoding is not supported
*/
@Override
public HierarchicalUriComponents encode(Charset charset) throws UnsupportedEncodingException {
public HierarchicalUriComponents encode(Charset charset) {
if (this.encoded) {
return this;
}
@ -200,7 +200,7 @@ final class HierarchicalUriComponents extends UriComponents {
pathTo, paramsTo, fragmentTo, true, false);
}
private MultiValueMap<String, String> encodeQueryParams(Charset charset) throws UnsupportedEncodingException {
private MultiValueMap<String, String> encodeQueryParams(Charset charset) {
int size = this.queryParams.size();
MultiValueMap<String, String> result = new LinkedMultiValueMap<>(size);
for (Map.Entry<String, List<String>> entry : this.queryParams.entrySet()) {
@ -234,21 +234,19 @@ final class HierarchicalUriComponents extends UriComponents {
* @param charset the encoding of the source string
* @param type the URI component for the source
* @return the encoded URI
* @throws IllegalArgumentException when the given uri parameter is not a valid URI
* @throws IllegalArgumentException when the given value is not a valid URI component
*/
static String encodeUriComponent(String source, Charset charset, Type type) {
if (source == null) {
return null;
if (!StringUtils.hasLength(source)) {
return source;
}
byte[] bytes = encodeBytes(source.getBytes(charset), type);
return new String(bytes, StandardCharsets.US_ASCII);
}
private static byte[] encodeBytes(byte[] source, Type type) {
Assert.notNull(source, "Source must not be null");
Assert.notNull(charset, "Charset must not be null");
Assert.notNull(type, "Type must not be null");
ByteArrayOutputStream bos = new ByteArrayOutputStream(source.length);
for (byte b : source) {
byte[] bytes = source.getBytes(charset);
ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length);
boolean changed = false;
for (byte b : bytes) {
if (b < 0) {
b += 256;
}
@ -261,13 +259,14 @@ final class HierarchicalUriComponents extends UriComponents {
char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, 16));
bos.write(hex1);
bos.write(hex2);
changed = true;
}
}
return bos.toByteArray();
return (changed ? new String(bos.toByteArray(), charset) : source);
}
private Type getHostType() {
return (this.host != null && this.host.startsWith("[")) ? Type.HOST_IPV6 : Type.HOST_IPV4;
return (this.host != null && this.host.startsWith("[") ? Type.HOST_IPV6 : Type.HOST_IPV4);
}
@ -586,7 +585,7 @@ final class HierarchicalUriComponents extends UriComponents {
* @see <a href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986, appendix A</a>
*/
protected boolean isAlpha(int c) {
return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z';
return (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z');
}
/**
@ -594,7 +593,7 @@ final class HierarchicalUriComponents extends UriComponents {
* @see <a href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986, appendix A</a>
*/
protected boolean isDigit(int c) {
return c >= '0' && c <= '9';
return (c >= '0' && c <= '9');
}
/**
@ -602,7 +601,7 @@ final class HierarchicalUriComponents extends UriComponents {
* @see <a href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986, appendix A</a>
*/
protected boolean isGenericDelimiter(int c) {
return ':' == c || '/' == c || '?' == c || '#' == c || '[' == c || ']' == c || '@' == c;
return (':' == c || '/' == c || '?' == c || '#' == c || '[' == c || ']' == c || '@' == c);
}
/**
@ -610,8 +609,8 @@ final class HierarchicalUriComponents extends UriComponents {
* @see <a href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986, appendix A</a>
*/
protected boolean isSubDelimiter(int c) {
return '!' == c || '$' == c || '&' == c || '\'' == c || '(' == c || ')' == c || '*' == c || '+' == c ||
',' == c || ';' == c || '=' == c;
return ('!' == c || '$' == c || '&' == c || '\'' == c || '(' == c || ')' == c || '*' == c || '+' == c ||
',' == c || ';' == c || '=' == c);
}
/**
@ -619,7 +618,7 @@ final class HierarchicalUriComponents extends UriComponents {
* @see <a href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986, appendix A</a>
*/
protected boolean isReserved(int c) {
return isGenericDelimiter(c) || isSubDelimiter(c);
return (isGenericDelimiter(c) || isSubDelimiter(c));
}
/**
@ -627,7 +626,7 @@ final class HierarchicalUriComponents extends UriComponents {
* @see <a href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986, appendix A</a>
*/
protected boolean isUnreserved(int c) {
return isAlpha(c) || isDigit(c) || '-' == c || '.' == c || '_' == c || '~' == c;
return (isAlpha(c) || isDigit(c) || '-' == c || '.' == c || '_' == c || '~' == c);
}
/**
@ -635,7 +634,7 @@ final class HierarchicalUriComponents extends UriComponents {
* @see <a href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986, appendix A</a>
*/
protected boolean isPchar(int c) {
return isUnreserved(c) || isSubDelimiter(c) || ':' == c || '@' == c;
return (isUnreserved(c) || isSubDelimiter(c) || ':' == c || '@' == c);
}
}
@ -649,7 +648,7 @@ final class HierarchicalUriComponents extends UriComponents {
List<String> getPathSegments();
PathComponent encode(Charset charset) throws UnsupportedEncodingException;
PathComponent encode(Charset charset);
void verify();
@ -666,7 +665,6 @@ final class HierarchicalUriComponents extends UriComponents {
private final String path;
public FullPathComponent(String path) {
this.path = path;
}
@ -678,13 +676,12 @@ final class HierarchicalUriComponents extends UriComponents {
@Override
public List<String> getPathSegments() {
String delimiter = new String(new char[] {PATH_DELIMITER});
String[] pathSegments = StringUtils.tokenizeToStringArray(path, delimiter);
return Collections.unmodifiableList(Arrays.asList(pathSegments));
String[] segments = StringUtils.tokenizeToStringArray(this.path, PATH_DELIMITER_STRING);
return Collections.unmodifiableList(Arrays.asList(segments));
}
@Override
public PathComponent encode(Charset charset) throws UnsupportedEncodingException {
public PathComponent encode(Charset charset) {
String encodedPath = encodeUriComponent(getPath(), charset, Type.PATH);
return new FullPathComponent(encodedPath);
}
@ -750,7 +747,7 @@ final class HierarchicalUriComponents extends UriComponents {
}
@Override
public PathComponent encode(Charset charset) throws UnsupportedEncodingException {
public PathComponent encode(Charset charset) {
List<String> pathSegments = getPathSegments();
List<String> encodedPathSegments = new ArrayList<>(pathSegments.size());
for (String pathSegment : pathSegments) {
@ -827,7 +824,7 @@ final class HierarchicalUriComponents extends UriComponents {
}
@Override
public PathComponent encode(Charset charset) throws UnsupportedEncodingException {
public PathComponent encode(Charset charset) {
List<PathComponent> encodedComponents = new ArrayList<>(this.pathComponents.size());
for (PathComponent pathComponent : this.pathComponents) {
encodedComponents.add(pathComponent.encode(charset));
@ -873,7 +870,7 @@ final class HierarchicalUriComponents extends UriComponents {
return Collections.emptyList();
}
@Override
public PathComponent encode(Charset charset) throws UnsupportedEncodingException {
public PathComponent encode(Charset charset) {
return this;
}
@Override

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,7 +16,6 @@
package org.springframework.web.util;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
@ -90,7 +89,7 @@ final class OpaqueUriComponents extends UriComponents {
}
@Override
public UriComponents encode(Charset charset) throws UnsupportedEncodingException {
public UriComponents encode(Charset charset) {
return this;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,7 +17,6 @@
package org.springframework.web.util;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
@ -44,7 +43,9 @@ import org.springframework.util.MultiValueMap;
@SuppressWarnings("serial")
public abstract class UriComponents implements Serializable {
/** Captures URI template variable names. */
/**
* Captures URI template variable names.
*/
private static final Pattern NAMES_PATTERN = Pattern.compile("\\{([^/]+?)\\}");
@ -59,57 +60,57 @@ public abstract class UriComponents implements Serializable {
}
// component getters
// Component getters
/**
* Returns the scheme. Can be {@code null}.
* Return the scheme. Can be {@code null}.
*/
public final String getScheme() {
return this.scheme;
}
/**
* Returns the scheme specific part. Can be {@code null}.
* Return the scheme specific part. Can be {@code null}.
*/
public abstract String getSchemeSpecificPart();
/**
* Returns the user info. Can be {@code null}.
* Return the user info. Can be {@code null}.
*/
public abstract String getUserInfo();
/**
* Returns the host. Can be {@code null}.
* Return the host. Can be {@code null}.
*/
public abstract String getHost();
/**
* Returns the port. Returns {@code -1} if no port has been set.
* Return the port. {@code -1} if no port has been set.
*/
public abstract int getPort();
/**
* Returns the path. Can be {@code null}.
* Return the path. Can be {@code null}.
*/
public abstract String getPath();
/**
* Returns the list of path segments. Empty if no path has been set.
* Return the list of path segments. Empty if no path has been set.
*/
public abstract List<String> getPathSegments();
/**
* Returns the query. Can be {@code null}.
* Return the query. Can be {@code null}.
*/
public abstract String getQuery();
/**
* Returns the map of query parameters. Empty if no query has been set.
* Return the map of query parameters. Empty if no query has been set.
*/
public abstract MultiValueMap<String, String> getQueryParams();
/**
* Returns the fragment. Can be {@code null}.
* Return the fragment. Can be {@code null}.
*/
public final String getFragment() {
return this.fragment;
@ -122,13 +123,7 @@ public abstract class UriComponents implements Serializable {
* @return the encoded URI components
*/
public final UriComponents encode() {
try {
return encode(StandardCharsets.UTF_8);
}
catch (UnsupportedEncodingException ex) {
// should not occur
throw new IllegalStateException(ex);
}
return encode(StandardCharsets.UTF_8);
}
/**
@ -136,9 +131,8 @@ public abstract class UriComponents implements Serializable {
* returns the result as a new {@code UriComponents} instance.
* @param charset the encoding of the values contained in this map
* @return the encoded URI components
* @throws UnsupportedEncodingException if the given encoding is not supported
*/
public abstract UriComponents encode(Charset charset) throws UnsupportedEncodingException;
public abstract UriComponents encode(Charset charset) ;
/**
* Replace all URI template variables with the values from a given map.

View File

@ -16,11 +16,10 @@
package org.springframework.web.util;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Utility class for URI encoding and decoding based on RFC 3986.
@ -42,7 +41,7 @@ import org.springframework.util.Assert;
public abstract class UriUtils {
/**
* Encodes the given URI scheme with the given encoding.
* Encode the given URI scheme with the given encoding.
* @param scheme the scheme to be encoded
* @param encoding the character encoding to encode to
* @return the encoded scheme
@ -53,7 +52,7 @@ public abstract class UriUtils {
}
/**
* Encodes the given URI authority with the given encoding.
* Encode the given URI authority with the given encoding.
* @param authority the authority to be encoded
* @param encoding the character encoding to encode to
* @return the encoded authority
@ -64,7 +63,7 @@ public abstract class UriUtils {
}
/**
* Encodes the given URI user info with the given encoding.
* Encode the given URI user info with the given encoding.
* @param userInfo the user info to be encoded
* @param encoding the character encoding to encode to
* @return the encoded user info
@ -75,7 +74,7 @@ public abstract class UriUtils {
}
/**
* Encodes the given URI host with the given encoding.
* Encode the given URI host with the given encoding.
* @param host the host to be encoded
* @param encoding the character encoding to encode to
* @return the encoded host
@ -86,7 +85,7 @@ public abstract class UriUtils {
}
/**
* Encodes the given URI port with the given encoding.
* Encode the given URI port with the given encoding.
* @param port the port to be encoded
* @param encoding the character encoding to encode to
* @return the encoded port
@ -97,7 +96,7 @@ public abstract class UriUtils {
}
/**
* Encodes the given URI path with the given encoding.
* Encode the given URI path with the given encoding.
* @param path the path to be encoded
* @param encoding the character encoding to encode to
* @return the encoded path
@ -108,7 +107,7 @@ public abstract class UriUtils {
}
/**
* Encodes the given URI path segment with the given encoding.
* Encode the given URI path segment with the given encoding.
* @param segment the segment to be encoded
* @param encoding the character encoding to encode to
* @return the encoded segment
@ -119,7 +118,7 @@ public abstract class UriUtils {
}
/**
* Encodes the given URI query with the given encoding.
* Encode the given URI query with the given encoding.
* @param query the query to be encoded
* @param encoding the character encoding to encode to
* @return the encoded query
@ -130,7 +129,7 @@ public abstract class UriUtils {
}
/**
* Encodes the given URI query parameter with the given encoding.
* Encode the given URI query parameter with the given encoding.
* @param queryParam the query parameter to be encoded
* @param encoding the character encoding to encode to
* @return the encoded query parameter
@ -141,7 +140,7 @@ public abstract class UriUtils {
}
/**
* Encodes the given URI fragment with the given encoding.
* Encode the given URI fragment with the given encoding.
* @param fragment the fragment to be encoded
* @param encoding the character encoding to encode to
* @return the encoded fragment
@ -165,72 +164,19 @@ public abstract class UriUtils {
HierarchicalUriComponents.Type type = HierarchicalUriComponents.Type.URI;
return HierarchicalUriComponents.encodeUriComponent(source, encoding, type);
}
// decoding
/**
* Decodes the given encoded source String into an URI. Based on the following rules:
* <ul>
* <li>Alphanumeric characters {@code "a"} through {@code "z"}, {@code "A"} through {@code "Z"}, and
* {@code "0"} through {@code "9"} stay the same.</li>
* <li>Special characters {@code "-"}, {@code "_"}, {@code "."}, and {@code "*"} stay the same.</li>
* <li>A sequence "{@code %<i>xy</i>}" is interpreted as a hexadecimal representation of the character.</li>
* </ul>
* @param source the source string
* Decode the given encoded URI component.
* <p>See {@link StringUtils#uriDecode(String, Charset) for the decoding rules.
* @param source the encoded String
* @param encoding the encoding
* @return the decoded URI
* @return the decoded value
* @throws IllegalArgumentException when the given source contains invalid encoded sequences
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
* @see StringUtils#uriDecode(String, Charset)
* @see java.net.URLDecoder#decode(String, String)
*/
public static String decode(String source, String encoding) throws UnsupportedEncodingException {
return decode(source, Charset.forName(encoding));
}
/**
* Decodes the given encoded source String into an URI. Based on the following rules:
* <ul>
* <li>Alphanumeric characters {@code "a"} through {@code "z"}, {@code "A"} through {@code "Z"}, and
* {@code "0"} through {@code "9"} stay the same.</li>
* <li>Special characters {@code "-"}, {@code "_"}, {@code "."}, and {@code "*"} stay the same.</li>
* <li>A sequence "{@code %<i>xy</i>}" is interpreted as a hexadecimal representation of the character.</li>
* </ul>
* @param source the source string
* @param charset the character set
* @return the decoded URI
* @throws IllegalArgumentException when the given source contains invalid encoded sequences
* @see java.net.URLDecoder#decode(String, String)
*/
public static String decode(String source, Charset charset) {
Assert.notNull(source, "'source' must not be null");
Assert.notNull(charset, "'charset' must not be null");
int length = source.length();
ByteArrayOutputStream bos = new ByteArrayOutputStream(length);
boolean changed = false;
for (int i = 0; i < length; i++) {
int ch = source.charAt(i);
if (ch == '%') {
if ((i + 2) < length) {
char hex1 = source.charAt(i + 1);
char hex2 = source.charAt(i + 2);
int u = Character.digit(hex1, 16);
int l = Character.digit(hex2, 16);
if (u == -1 || l == -1) {
throw new IllegalArgumentException("Invalid encoded sequence \"" + source.substring(i) + "\"");
}
bos.write((char) ((u << 4) + l));
i += 2;
changed = true;
}
else {
throw new IllegalArgumentException("Invalid encoded sequence \"" + source.substring(i) + "\"");
}
}
else {
bos.write(ch);
}
}
return (changed ? new String(bos.toByteArray(), charset) : source);
return StringUtils.uriDecode(source, Charset.forName(encoding));
}
/**