Repository: cxf Updated Branches: refs/heads/master f498b82ee -> 881b630dc
[CXF-6189] Applying a v2 patch from Lucas Pouzac, v3 is still possible Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/881b630d Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/881b630d Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/881b630d Branch: refs/heads/master Commit: 881b630dc5607fa518c4fd386e8a9317d7ced6a3 Parents: f498b82 Author: Sergey Beryozkin <[email protected]> Authored: Tue Jan 13 16:27:18 2015 +0000 Committer: Sergey Beryozkin <[email protected]> Committed: Tue Jan 13 16:27:18 2015 +0000 ---------------------------------------------------------------------- .../org/apache/cxf/common/util/UrlUtils.java | 52 +++++++++++++------- .../apache/cxf/common/util/UrlUtilsTest.java | 7 +++ 2 files changed, 41 insertions(+), 18 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/881b630d/core/src/main/java/org/apache/cxf/common/util/UrlUtils.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/cxf/common/util/UrlUtils.java b/core/src/main/java/org/apache/cxf/common/util/UrlUtils.java index 91a5f45..fc9ac28 100644 --- a/core/src/main/java/org/apache/cxf/common/util/UrlUtils.java +++ b/core/src/main/java/org/apache/cxf/common/util/UrlUtils.java @@ -67,27 +67,43 @@ public final class UrlUtils { } private static String urlDecode(String value, String enc, boolean isPath) { - final byte[] valueBytes = StringUtils.toBytes(value, enc); - ByteBuffer in = ByteBuffer.wrap(valueBytes); - ByteBuffer out = ByteBuffer.allocate(in.capacity()); - while (in.hasRemaining()) { - final int b = in.get(); - if (!isPath && b == PLUS_CHAR) { - out.put((byte) ' '); - } else if (b == ESCAPE_CHAR) { - try { - final int u = digit16((byte) in.get()); - final int l = digit16((byte) in.get()); - out.put((byte) ((u << 4) + l)); - } catch (final ArrayIndexOutOfBoundsException e) { - throw new RuntimeException("Invalid URL encoding: ", e); + + boolean needDecode = false; + int escapesCount = 0; + for (int i = 0; i < value.length(); i++) { + char ch = value.charAt(i); + if (ch == ESCAPE_CHAR) { + escapesCount += 1; + needDecode = true; + } else if (!isPath && ch == PLUS_CHAR) { + needDecode = true; + } + } + if (needDecode) { + final byte[] valueBytes = StringUtils.toBytes(value, enc); + ByteBuffer in = ByteBuffer.wrap(valueBytes); + ByteBuffer out = ByteBuffer.allocate(in.capacity() - 2 * escapesCount); + while (in.hasRemaining()) { + final int b = in.get(); + if (!isPath && b == PLUS_CHAR) { + out.put((byte) ' '); + } else if (b == ESCAPE_CHAR) { + try { + final int u = digit16((byte) in.get()); + final int l = digit16((byte) in.get()); + out.put((byte) ((u << 4) + l)); + } catch (final ArrayIndexOutOfBoundsException e) { + throw new RuntimeException("Invalid URL encoding: ", e); + } + } else { + out.put((byte) b); } - } else { - out.put((byte) b); } + out.flip(); + return Charset.forName(enc).decode(out).toString(); + } else { + return value; } - out.flip(); - return Charset.forName(enc).decode(out).toString(); } private static int digit16(final byte b) { http://git-wip-us.apache.org/repos/asf/cxf/blob/881b630d/core/src/test/java/org/apache/cxf/common/util/UrlUtilsTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/cxf/common/util/UrlUtilsTest.java b/core/src/test/java/org/apache/cxf/common/util/UrlUtilsTest.java index 7cafc2a..3989e9f 100644 --- a/core/src/test/java/org/apache/cxf/common/util/UrlUtilsTest.java +++ b/core/src/test/java/org/apache/cxf/common/util/UrlUtilsTest.java @@ -31,6 +31,13 @@ public class UrlUtilsTest extends Assert { } @Test + public void testUrlDecodeSingleCharMultipleEscapes() { + String s = "Ã"; + String encoded = UrlUtils.urlEncode(s); + assertEquals(s, UrlUtils.urlDecode(encoded)); + } + + @Test public void testUrlDecodeReserved() { assertEquals("!$&'()*,;=", UrlUtils.urlDecode("!$&'()*,;=")); }
