SPR-6291 - UrlPathHelper is too aggressive decoding URLs

This commit is contained in:
Arjen Poutsma 2010-10-12 12:09:43 +00:00
parent 043ec2c8b2
commit c5c1d70aa3
2 changed files with 5 additions and 1 deletions

View File

@ -462,6 +462,7 @@ public abstract class UriUtils {
Assert.hasLength(encoding, "'encoding' must not be empty"); Assert.hasLength(encoding, "'encoding' must not be empty");
int length = source.length(); int length = source.length();
ByteArrayOutputStream bos = new ByteArrayOutputStream(length); ByteArrayOutputStream bos = new ByteArrayOutputStream(length);
boolean changed = false;
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
int ch = source.charAt(i); int ch = source.charAt(i);
if (ch == '%') { if (ch == '%') {
@ -472,6 +473,7 @@ public abstract class UriUtils {
int l = Character.digit(hex2, 16); int l = Character.digit(hex2, 16);
bos.write((char) ((u << 4) + l)); bos.write((char) ((u << 4) + l));
i += 2; i += 2;
changed = true;
} }
else { else {
throw new IllegalArgumentException("Invalid encoded sequence \"" + source.substring(i) + "\""); throw new IllegalArgumentException("Invalid encoded sequence \"" + source.substring(i) + "\"");
@ -481,7 +483,7 @@ public abstract class UriUtils {
bos.write(ch); bos.write(ch);
} }
} }
return new String(bos.toByteArray(), encoding); return changed ? new String(bos.toByteArray(), encoding) : source;
} }
} }

View File

@ -88,11 +88,13 @@ public class UriUtilsTests {
@Test @Test
public void decode() throws UnsupportedEncodingException { public void decode() throws UnsupportedEncodingException {
assertEquals("Invalid encoded URI", "", UriUtils.decode("", ENC));
assertEquals("Invalid encoded URI", "foobar", UriUtils.decode("foobar", ENC)); assertEquals("Invalid encoded URI", "foobar", UriUtils.decode("foobar", ENC));
assertEquals("Invalid encoded URI", "foo bar", UriUtils.decode("foo%20bar", ENC)); assertEquals("Invalid encoded URI", "foo bar", UriUtils.decode("foo%20bar", ENC));
assertEquals("Invalid encoded URI", "foo+bar", UriUtils.decode("foo%2bbar", ENC)); assertEquals("Invalid encoded URI", "foo+bar", UriUtils.decode("foo%2bbar", ENC));
assertEquals("Invalid encoded result", "T\u014dky\u014d", UriUtils.decode("T%C5%8Dky%C5%8D", ENC)); assertEquals("Invalid encoded result", "T\u014dky\u014d", UriUtils.decode("T%C5%8Dky%C5%8D", ENC));
assertEquals("Invalid encoded result", "/Z\u00fcrich", UriUtils.decode("/Z%C3%BCrich", ENC)); assertEquals("Invalid encoded result", "/Z\u00fcrich", UriUtils.decode("/Z%C3%BCrich", ENC));
assertEquals("Invalid encoded result", "T\u014dky\u014d", UriUtils.decode("T\u014dky\u014d", ENC));
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)