Skip to content

Commit 74763ad

Browse files
committed
Revert "Issue #13683 - UrlParameterDecoder fixed for mixed bytes/chars pct-encoding usage."
This reverts commit b8e8d37.
1 parent b8e8d37 commit 74763ad

File tree

3 files changed

+23
-56
lines changed

3 files changed

+23
-56
lines changed

jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/CharsetStringBuilder.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@
2222
import java.util.Arrays;
2323
import java.util.Objects;
2424

25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
27+
2528
/**
2629
* <p>Build a string from a sequence of bytes and/or characters.</p>
2730
* <p>Implementations of this interface are optimized for processing a mix of calls to already decoded
28-
* character based appends (e.g. {@link #append(char)}) and calls to undecoded byte methods (e.g. {@link #append(char)}).
31+
* character based appends (e.g. {@link #append(char)} and calls to undecoded byte methods (e.g. {@link #append(byte)}.
2932
* This is particularly useful for decoding % encoded strings that are mostly already decoded but may contain
3033
* escaped byte sequences that are not decoded. The standard {@link CharsetDecoder} API is not well suited for this
3134
* use-case.</p>
@@ -271,6 +274,7 @@ public void reset()
271274

272275
class DecoderStringBuilder implements CharsetStringBuilder
273276
{
277+
private static final Logger LOG = LoggerFactory.getLogger(DecoderStringBuilder.class);
274278
private final CharsetDecoder _decoder;
275279
private final StringBuilder _stringBuilder = new StringBuilder(32);
276280
private ByteBuffer _buffer = ByteBuffer.allocate(32);
@@ -304,17 +308,6 @@ public void append(char c)
304308
{
305309
if (_buffer.position() > 0)
306310
{
307-
// We have a (possible) sequence started, need to continue it.
308-
if (c > 0xFF)
309-
{
310-
ensureSpace(2);
311-
_buffer.putChar(c);
312-
}
313-
else
314-
{
315-
ensureSpace(1);
316-
_buffer.put((byte)c);
317-
}
318311
try
319312
{
320313
// Append any data already in the decoder
@@ -328,19 +321,28 @@ public void append(char c)
328321
throw new RuntimeException(e);
329322
}
330323
}
331-
else
332-
{
333-
_stringBuilder.append(c);
334-
}
324+
_stringBuilder.append(c);
335325
}
336326

337327
@Override
338328
public void append(CharSequence chars, int offset, int length)
339329
{
340-
for (int idx = offset; idx < offset + length; idx++)
330+
if (_buffer.position() > 0)
341331
{
342-
append(chars.charAt(idx));
332+
try
333+
{
334+
// Append any data already in the decoder
335+
_stringBuilder.append(_decoder.decode(_buffer.flip()));
336+
_buffer.clear();
337+
}
338+
catch (CharacterCodingException e)
339+
{
340+
// This will be thrown only if the decoder is configured to REPORT,
341+
// otherwise errors will be ignored or replaced and we will not catch here.
342+
throw new RuntimeException(e);
343+
}
343344
}
345+
_stringBuilder.append(chars, offset, offset + length);
344346
}
345347

346348
@Override

jetty-core/jetty-util/src/test/java/org/eclipse/jetty/util/URLEncodedTest.java

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
package org.eclipse.jetty.util;
1515

1616
import java.io.ByteArrayInputStream;
17-
import java.io.IOException;
1817
import java.nio.charset.Charset;
1918
import java.nio.charset.StandardCharsets;
2019
import java.util.ArrayList;
@@ -34,7 +33,6 @@
3433
import static org.hamcrest.MatcherAssert.assertThat;
3534
import static org.hamcrest.Matchers.is;
3635
import static org.junit.jupiter.api.Assertions.assertEquals;
37-
import static org.junit.jupiter.api.Assertions.assertNotNull;
3836
import static org.junit.jupiter.api.Assertions.assertThrows;
3937
import static org.junit.jupiter.api.Assertions.assertTrue;
4038
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
@@ -304,33 +302,4 @@ public void testInvalidDecodeTo(String inputString, Charset charset, Class<? ext
304302
UrlEncoded.decodeTo(inputString, map, charset);
305303
});
306304
}
307-
308-
/**
309-
* Test of non-standard encoding of Shift_JIS.
310-
* This tests a 2 byte sequence making up 1 Shift_JIS character.
311-
* But only the first byte is pct-encoded, other byte is "in the raw" and not encoded.
312-
* Both bytes are required for the KATAKANA LETTER HO: ホ to be decoded.
313-
* See https://unicodeplus.com/U+30DB
314-
*/
315-
@Test
316-
public void testSjisNonStandardForm() throws IOException
317-
{
318-
// Actual x-www-form-urlencoded sent from Google Chrome 1.141 with Shift-JIS encoding.
319-
// The same form data is sent from Firefox 143.0.4
320-
// The same form data is also what is sent from Apache HttpClient 4.5.x and 5.x
321-
// The properly encoded form of this character would be "a=%83%7A"
322-
// Note: "%7A" is the ASCII "z"
323-
String sjis = "a=%83z";
324-
Charset shiftJis = Charset.forName("Shift_JIS");
325-
byte[] bytes = sjis.getBytes(shiftJis);
326-
try (ByteArrayInputStream in = new ByteArrayInputStream(bytes))
327-
{
328-
MultiMap<String> map = new MultiMap<>();
329-
UrlEncoded.decodeTo(in, map, shiftJis, 500, 500);
330-
List<String> result = map.get("a");
331-
assertNotNull(result);
332-
assertEquals(1, result.size());
333-
assertEquals("ホ", result.get(0));
334-
}
335-
}
336305
}

jetty-core/jetty-util/src/test/java/org/eclipse/jetty/util/UrlParameterDecoderTest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.junit.jupiter.params.provider.Arguments;
3232
import org.junit.jupiter.params.provider.CsvSource;
3333
import org.junit.jupiter.params.provider.MethodSource;
34-
import org.junit.jupiter.params.provider.ValueSource;
3534

3635
import static java.nio.charset.StandardCharsets.ISO_8859_1;
3736
import static java.nio.charset.StandardCharsets.US_ASCII;
@@ -260,16 +259,13 @@ public void testBadlyEncodedValue() throws IOException
260259
assertEquals("Euro-€-Symbol", field.getValue(), "Fields[Name]");
261260
}
262261

263-
@ParameterizedTest
264-
@ValueSource(strings = {
265-
"name\n=value+%00%30&name1=&name2&nãme3=value+3", // without BOM
266-
"name\n=value+%FE%FF%00%30&name1=&name2&nãme3=value+3" // with BOM
267-
})
268-
public void testUtf16EncodedString(String input) throws IOException
262+
@Test
263+
public void testUtf16EncodedString() throws IOException
269264
{
270265
Fields fields = new Fields();
271266
CharsetStringBuilder charsetStringBuilder = CharsetStringBuilder.forCharset(UTF_16);
272267
UrlParameterDecoder decoder = new UrlParameterDecoder(charsetStringBuilder, fields::add);
268+
String input = "name\n=value+%FE%FF%00%30&name1=&name2&nãme3=value+3";
273269
assertFalse(decoder.parse(input), "No coding errors");
274270

275271
assertThat("Field count", fields.getSize(), is(4));

0 commit comments

Comments
 (0)