Skip to content

Commit 06bf001

Browse files
committed
Fault tolerant percent decoding for path and query (ocpsoft#197)
1 parent 30044ba commit 06bf001

2 files changed

Lines changed: 188 additions & 20 deletions

File tree

Lines changed: 70 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,84 @@
11
package org.ocpsoft.urlbuilder.util;
22

3-
import java.io.UnsupportedEncodingException;
4-
import java.net.URI;
5-
import java.net.URISyntaxException;
6-
import java.net.URLDecoder;
3+
import java.nio.charset.StandardCharsets;
74

85
public class Decoder
96
{
7+
108
public static String path(final CharSequence path)
119
{
12-
try
13-
{
14-
final URI uri = new URI("http://0.0.0.0/" + path);
15-
return uri.getPath().substring(1);
16-
}
17-
catch (URISyntaxException e)
18-
{
19-
throw new IllegalArgumentException(e);
20-
}
10+
return decode(path, false);
2111
}
2212

2313
public static String query(final CharSequence query)
2414
{
25-
try
26-
{
27-
return URLDecoder.decode(query.toString(), "UTF-8");
28-
}
29-
catch (UnsupportedEncodingException e)
30-
{
31-
throw new IllegalArgumentException(e);
15+
return decode(query, true);
16+
}
17+
18+
public static String decode(final CharSequence path, final boolean query)
19+
{
20+
21+
StringBuilder decoded = new StringBuilder();
22+
int length = path.length();
23+
int pos = 0;
24+
25+
while (pos < length) {
26+
27+
// '+' -> ' ' for query strings
28+
if (query && path.charAt(pos) == '+') {
29+
decoded.append(' ');
30+
pos++;
31+
}
32+
33+
// percent-encoded values
34+
if (path.charAt(pos) == '%') {
35+
36+
// a single Unicode char may be represented by multiple percent encoded bytes
37+
byte[] bytes = new byte[length / 3];
38+
int i = 0;
39+
boolean invalid = false;
40+
41+
while (pos < length && path.charAt(pos) == '%') {
42+
43+
// make sure we can read the two hex characters
44+
if (pos + 2 < length) {
45+
try {
46+
String hex = path.subSequence(pos + 1, pos + 3).toString();
47+
int b = Integer.parseInt(hex, 16);
48+
bytes[i++] = (byte) b;
49+
}
50+
catch (NumberFormatException e) {
51+
// not a valid hex value
52+
invalid = true;
53+
}
54+
}
55+
pos += 3;
56+
57+
}
58+
59+
// decode the byte sequence with UTF8 if no invalid byte was found
60+
if (!invalid) {
61+
decoded.append(new String(bytes, 0, i, StandardCharsets.UTF_8));
62+
}
63+
64+
// We represent invalid percent encoded values the same way UTF8 does it
65+
// http://unicode-table.com/de/search/?q=%EF%BF%BD
66+
else {
67+
decoded.append('\uFFFD');
68+
}
69+
70+
}
71+
72+
// not escaped
73+
else {
74+
decoded.append(path.charAt(pos));
75+
pos++;
76+
}
77+
3278
}
79+
80+
return decoded.toString();
81+
3382
}
83+
3484
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.urlbuilder.util;
17+
18+
import static org.junit.Assert.assertEquals;
19+
20+
import org.junit.Test;
21+
22+
public class DecoderTest
23+
{
24+
25+
@Test
26+
public void pathNotEncoded()
27+
{
28+
assertEquals("/foobar", Decoder.path("/foobar"));
29+
}
30+
31+
@Test
32+
public void pathSpaceDecoding()
33+
{
34+
assertEquals("/foo bar", Decoder.path("/foo%20bar"));
35+
assertEquals("/foo bar", Decoder.path("/foo bar"));
36+
}
37+
38+
@Test
39+
public void pathQuotationMark()
40+
{
41+
assertEquals("/foo\"bar", Decoder.path("/foo%22bar"));
42+
assertEquals("/foo\"bar", Decoder.path("/foo\"bar"));
43+
}
44+
45+
@Test
46+
public void pathQuestionMark()
47+
{
48+
assertEquals("/foo?bar", Decoder.path("/foo%3Fbar"));
49+
assertEquals("/foo?bar", Decoder.path("/foo%3fbar"));
50+
assertEquals("/foo?bar", Decoder.path("/foo?bar"));
51+
}
52+
53+
@Test
54+
public void pathLetterAWithDiaeresis()
55+
{
56+
assertEquals("/foo\u00E4bar", Decoder.path("/foo%C3%A4bar"));
57+
assertEquals("/foo\u00E4bar", Decoder.path("/foo\u00E4bar"));
58+
}
59+
60+
@Test
61+
public void pathMultipleLettersAWithDiaeresis()
62+
{
63+
assertEquals("/foo\u00E4\u00E4\u00E4bar", Decoder.path("/foo%C3%A4%C3%A4%C3%A4bar"));
64+
}
65+
66+
@Test
67+
public void pathArmenianCapitalLetterCheh()
68+
{
69+
assertEquals("/foo\u0543bar", Decoder.path("/foo%D5%83bar"));
70+
assertEquals("/foo\u0543bar", Decoder.path("/foo\u0543bar"));
71+
}
72+
73+
@Test
74+
public void pathInvalidByteSequenceConvertedToReplacementChars()
75+
{
76+
assertEquals("/foo\uFFFD\uFFFDbar", Decoder.path("/foo%83%83bar"));
77+
}
78+
79+
@Test
80+
public void pathSingleContinuationCharConvertedToReplacementChars()
81+
{
82+
assertEquals("/foo\uFFFD", Decoder.path("/foo%83"));
83+
}
84+
85+
86+
@Test
87+
public void pathTrailingPercentSign()
88+
{
89+
assertEquals("/foo", Decoder.path("/foo%"));
90+
}
91+
92+
@Test
93+
public void pathTrailingIncompleteEscapeSequence()
94+
{
95+
assertEquals("/foo", Decoder.path("/foo%3"));
96+
}
97+
98+
@Test
99+
public void pathNonHexValueAfterPercent()
100+
{
101+
assertEquals("/f\uFFFDbar", Decoder.path("/f%oobar"));
102+
}
103+
104+
@Test
105+
public void pathNonHexValueInContinuationByte()
106+
{
107+
assertEquals("/foo\uFFFDbar", Decoder.path("/foo%C3%xxbar"));
108+
}
109+
110+
@Test
111+
public void querySpaceDecoding()
112+
{
113+
assertEquals("foo bar", Decoder.query("foo+bar"));
114+
assertEquals("foo bar", Decoder.query("foo bar"));
115+
}
116+
117+
118+
}

0 commit comments

Comments
 (0)