cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: [CXF-6189] Optimizing URL decoding code, patch from Lucas Pouzac applied with minor modifications
Date Tue, 13 Jan 2015 12:08:43 GMT
Repository: cxf
Updated Branches:
  refs/heads/3.0.x-fixes 67ec8299e -> 556d4b6ad


[CXF-6189] Optimizing URL decoding code, patch from Lucas Pouzac applied with minor modifications


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/556d4b6a
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/556d4b6a
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/556d4b6a

Branch: refs/heads/3.0.x-fixes
Commit: 556d4b6ad700f208be879c985c778ddadabbf66e
Parents: 67ec829
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Tue Jan 13 12:06:45 2015 +0000
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Tue Jan 13 12:08:28 2015 +0000

----------------------------------------------------------------------
 .../org/apache/cxf/common/util/StringUtils.java | 14 ++---
 .../org/apache/cxf/common/util/UrlUtils.java    | 61 +++++++++++++-------
 2 files changed, 44 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/556d4b6a/core/src/main/java/org/apache/cxf/common/util/StringUtils.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/common/util/StringUtils.java b/core/src/main/java/org/apache/cxf/common/util/StringUtils.java
index 7df3d52..aa34a6c 100644
--- a/core/src/main/java/org/apache/cxf/common/util/StringUtils.java
+++ b/core/src/main/java/org/apache/cxf/common/util/StringUtils.java
@@ -200,22 +200,18 @@ public final class StringUtils {
     }    
     
     public static byte[] toBytesUTF8(String str) {
-        try {
-            return toBytes(str, "UTF-8");
-        } catch (UnsupportedEncodingException ex) { 
-            throw new RuntimeException(ex);
-        }
+        return toBytes(str, "UTF-8");
     }
     public static byte[] toBytesASCII(String str) {
+        return toBytes(str, "US-ASCII");
+    }
+    public static byte[] toBytes(String str, String enc) {
         try {
-            return toBytes(str, "US-ASCII");
+            return str.getBytes(enc);
         } catch (UnsupportedEncodingException ex) { 
             throw new RuntimeException(ex);
         }
     }
-    public static byte[] toBytes(String str, String enc) throws UnsupportedEncodingException
{
-        return str.getBytes(enc);
-    }
 
     public static String toHexString(byte[] bytes) {
         StringBuilder hexString = new StringBuilder();

http://git-wip-us.apache.org/repos/asf/cxf/blob/556d4b6a/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 e33fe8f..91a5f45 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
@@ -20,14 +20,12 @@
 package org.apache.cxf.common.util;
 
 import java.io.UnsupportedEncodingException;
-import java.net.URLDecoder;
 import java.net.URLEncoder;
+import java.nio.ByteBuffer;
+import java.nio.charset.Charset;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.StringTokenizer;
-import java.util.logging.Logger;
-
-import org.apache.cxf.common.logging.LogUtils;
 
 /**
  * Utility class for decoding and encoding URLs
@@ -35,10 +33,9 @@ import org.apache.cxf.common.logging.LogUtils;
  */
 public final class UrlUtils {
     
-    private static final Logger LOG = LogUtils.getL7dLogger(UrlUtils.class);
-    
-    private static final String[] RESERVED_CHARS = {"+"};
-    private static final String[] ENCODED_CHARS = {"%2b"};
+    private static final int RADIX = 16;
+    private static final byte ESCAPE_CHAR = '%';
+    private static final byte PLUS_CHAR = '+';
     
     private UrlUtils() {
         
@@ -66,13 +63,41 @@ public final class UrlUtils {
      * @param enc encoding
      */
     public static String urlDecode(String value, String enc) {
-        try {
-            value = URLDecoder.decode(value, enc);
-        } catch (UnsupportedEncodingException e) {
-            LOG.warning("UTF-8 encoding can not be used to decode " + value);          
+        return urlDecode(value, enc, false);
+    }
+
+    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);
+                }
+            } else {
+                out.put((byte) b);
+            }
         }
-        return value;
+        out.flip();
+        return Charset.forName(enc).decode(out).toString();
     }
+
+    private static int digit16(final byte b) {
+        final int i = Character.digit((char) b, RADIX);
+        if (i == -1) {
+            throw new RuntimeException("Invalid URL encoding: not a valid digit (radix "
+ RADIX + "): " + b);
+        }
+        return i;
+    }
+
     
     public static String urlDecode(String value) {
         return urlDecode(value, "UTF-8");
@@ -84,15 +109,7 @@ public final class UrlUtils {
      * @param value value to decode
      */
     public static String pathDecode(String value) {
-        // TODO: we actually need to do a proper URI analysis here according to
-        // http://tools.ietf.org/html/rfc3986
-        for (int i = 0; i < RESERVED_CHARS.length; i++) {
-            if (value.indexOf(RESERVED_CHARS[i]) != -1) {
-                value = value.replace(RESERVED_CHARS[i], ENCODED_CHARS[i]);
-            }
-        }
-        
-        return urlDecode(value);
+        return urlDecode(value, "UTF-8", true);
     }
     
     


Mime
View raw message