You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2015/04/17 03:38:58 UTC

[3/3] camel git commit: CAMEL-8649 Fixed the %2520 issue of RAW()

CAMEL-8649 Fixed the %2520 issue of RAW()


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

Branch: refs/heads/camel-2.14.x
Commit: feb4ac9adbddf8715d685ce5056bde46db7e54c9
Parents: 4200308
Author: Willem Jiang <wi...@gmail.com>
Authored: Fri Apr 17 00:12:09 2015 +0800
Committer: Willem Jiang <wi...@gmail.com>
Committed: Fri Apr 17 09:38:36 2015 +0800

----------------------------------------------------------------------
 .../java/org/apache/camel/util/URISupport.java  |   8 +-
 .../camel/util/UnsafeUriCharactersEncoder.java  |  64 ++++++++++-
 .../component/file/FileURLDecodingTest.java     | 105 +++++++++++++++++++
 .../org/apache/camel/util/URISupportTest.java   |   8 +-
 4 files changed, 180 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/feb4ac9a/camel-core/src/main/java/org/apache/camel/util/URISupport.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/URISupport.java b/camel-core/src/main/java/org/apache/camel/util/URISupport.java
index 1742432..a251df3 100644
--- a/camel-core/src/main/java/org/apache/camel/util/URISupport.java
+++ b/camel-core/src/main/java/org/apache/camel/util/URISupport.java
@@ -429,8 +429,10 @@ public final class URISupport {
         if (value != null) {
             rc.append("=");
             if (value.startsWith(RAW_TOKEN_START) && value.endsWith(RAW_TOKEN_END)) {
-                // do not encode RAW parameters
-                rc.append(value);
+                // do not encode RAW parameters unless it has %
+                // need to replace % with %25 to avoid losing "%" when decoding
+                String s = StringHelper.replaceAll(value, "%", "%25");
+                rc.append(s);
             } else {
                 rc.append(URLEncoder.encode(value, CHARSET));
             }
@@ -467,7 +469,7 @@ public final class URISupport {
      */
     public static String normalizeUri(String uri) throws URISyntaxException, UnsupportedEncodingException {
 
-        URI u = new URI(UnsafeUriCharactersEncoder.encode(uri));
+        URI u = new URI(UnsafeUriCharactersEncoder.encode(uri, true));
         String path = u.getSchemeSpecificPart();
         String scheme = u.getScheme();
 

http://git-wip-us.apache.org/repos/asf/camel/blob/feb4ac9a/camel-core/src/main/java/org/apache/camel/util/UnsafeUriCharactersEncoder.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/UnsafeUriCharactersEncoder.java b/camel-core/src/main/java/org/apache/camel/util/UnsafeUriCharactersEncoder.java
index 9b9445d..e0775a0 100644
--- a/camel-core/src/main/java/org/apache/camel/util/UnsafeUriCharactersEncoder.java
+++ b/camel-core/src/main/java/org/apache/camel/util/UnsafeUriCharactersEncoder.java
@@ -16,7 +16,12 @@
  */
 package org.apache.camel.util;
 
+import java.util.ArrayList;
 import java.util.BitSet;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
 
 /**
  * Encoder for unsafe URI characters.
@@ -78,6 +83,63 @@ public final class UnsafeUriCharactersEncoder {
     }
     
     public static String encode(String s, BitSet unsafeCharacters) {
+        return encode(s, unsafeCharacters, false);
+    }
+    
+    public static String encode(String s, boolean checkRaw) {
+        return encode(s, unsafeCharactersRfc1738, checkRaw);
+    }
+    
+    public static String encodeHttpURI(String s, boolean checkRaw) {
+        return encode(s, unsafeCharactersHttp, checkRaw);
+    }
+    
+    private static List<Pair> checkRAW(String s) {
+        Pattern pattern = Pattern.compile("RAW\\([^\\)]+\\)");
+        Matcher matcher = pattern.matcher(s);
+        List<Pair> answer = new ArrayList<Pair>();
+        // Check all occurrences
+        while (matcher.find()) {
+            answer.add(new Pair(matcher.start(), matcher.end()));
+        }
+        return answer;
+    }
+    
+    private static boolean isRaw(int index, List<Pair>pairs) {
+        for (Pair pair : pairs) {
+            if (index < pair.left) {
+                return false;
+            } else {
+                if (index >= pair.left) {
+                    if (index <= pair.right) {
+                        return true;
+                    } else {
+                        continue;
+                    }
+                }
+            }
+        }
+        return false;
+    }
+    
+    private static class Pair {
+        int left;
+        int right;
+        public Pair(int left, int right) {
+            this.left = left;
+            this.right = right;
+        }
+    }
+    
+    // Just skip the encode for isRAW part
+    public static String encode(String s, BitSet unsafeCharacters, boolean checkRaw) {
+        List<Pair> rawPairs;
+        if (checkRaw) {
+            rawPairs = checkRAW(s); 
+        } else {
+            rawPairs = new ArrayList<Pair>();
+        }
+   
         int n = s == null ? 0 : s.length();
         if (n == 0) {
             return s;
@@ -108,7 +170,7 @@ public final class UnsafeUriCharactersEncoder {
                     char next = i + 1 < chars.length ? chars[i + 1] : ' ';
                     char next2 = i + 2 < chars.length ? chars[i + 2] : ' ';
 
-                    if (isHexDigit(next) && isHexDigit(next2)) {
+                    if (isHexDigit(next) && isHexDigit(next2) && !isRaw(i, rawPairs)) {
                         // its already encoded (decimal encoded) so just append as is
                         sb.append(ch);
                     } else {

http://git-wip-us.apache.org/repos/asf/camel/blob/feb4ac9a/camel-core/src/test/java/org/apache/camel/component/file/FileURLDecodingTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/file/FileURLDecodingTest.java b/camel-core/src/test/java/org/apache/camel/component/file/FileURLDecodingTest.java
new file mode 100644
index 0000000..9f7c0c6
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/component/file/FileURLDecodingTest.java
@@ -0,0 +1,105 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.file;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.nio.file.Paths;
+
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+
+
+public class FileURLDecodingTest extends ContextTestSupport {
+    
+    static final String TARGET_DIR = "target/files";
+    
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        deleteDirectory(TARGET_DIR);
+        super.setUp();
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        context.stop();
+        super.tearDown();
+    }
+    
+    public void testSimpleFile() throws Exception {
+        assertTargetFile("data.txt", "data.txt");
+    }
+    
+    public void testFilePlus() throws Exception {
+        assertTargetFile("data+.txt", "data .txt");
+    }
+    
+    public void testFileSpace() throws Exception {
+        assertTargetFile("data%20.txt", "data .txt");
+    }
+    
+    public void testFile2B() throws Exception {
+        assertTargetFile("data%2B.txt", "data .txt");
+    }
+    public void testFileRaw2B() throws Exception {
+        assertTargetFile("RAW(data%2B.txt)", "data%2B.txt");
+    }
+
+    public void testFileRawPlus() throws Exception {
+        assertTargetFile("RAW(data+.txt)", "data+.txt");
+    }
+   
+    public void testFileRawSpace() throws Exception {
+        assertTargetFile("RAW(data%20.txt)", "data%20.txt");
+    }
+
+    public void testFileRaw2520() throws Exception {
+        assertTargetFile("RAW(data%2520.txt)", "data%2520.txt");
+    }
+    
+    public void testFileWithTwoHundredPercent() throws Exception {
+        assertTargetFile("RAW(data%%.txt)", "data%%.txt");
+    }
+   
+   
+    private void assertTargetFile(final String encoded, final String expected) throws Exception {
+        
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").to("file:" + TARGET_DIR + "?fileName=" + encoded);
+            }
+        });
+        
+        context.start();
+            
+        String result = template.requestBody("direct:start", "Kermit", String.class);
+        assertEquals("Kermit", result);
+        
+        BufferedReader br = new BufferedReader(new FileReader(Paths.get(TARGET_DIR, expected).toFile()));
+        assertEquals("Kermit", br.readLine());
+        br.close();
+    }
+
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/feb4ac9a/camel-core/src/test/java/org/apache/camel/util/URISupportTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/util/URISupportTest.java b/camel-core/src/test/java/org/apache/camel/util/URISupportTest.java
index cdefcec..7e66b9e 100644
--- a/camel-core/src/test/java/org/apache/camel/util/URISupportTest.java
+++ b/camel-core/src/test/java/org/apache/camel/util/URISupportTest.java
@@ -255,7 +255,8 @@ public class URISupportTest extends ContextTestSupport {
         assertEquals("xmpp://camel-user@localhost:123/test-user@localhost?password=RAW(++?w0rd)&serviceName=some+chat", out);
 
         String out2 = URISupport.normalizeUri("xmpp://camel-user@localhost:123/test-user@localhost?password=RAW(foo %% bar)&serviceName=some chat");
-        assertEquals("xmpp://camel-user@localhost:123/test-user@localhost?password=RAW(foo %% bar)&serviceName=some+chat", out2);
+        // Just make sure the RAW parameter can be resolved rightly, we need to replace the % into %25
+        assertEquals("xmpp://camel-user@localhost:123/test-user@localhost?password=RAW(foo %25%25 bar)&serviceName=some+chat", out2);
     }
 
     public void testParseQuery() throws Exception {
@@ -273,6 +274,11 @@ public class URISupportTest extends ContextTestSupport {
         assertEquals(2, map.size());
         assertEquals("RAW(++?)w&rd)", map.get("password"));
         assertEquals("somechat", map.get("serviceName"));
+        
+        map = URISupport.parseQuery("password=RAW(%2520w&rd)&serviceName=somechat");
+        assertEquals(2, map.size());
+        assertEquals("RAW(%2520w&rd)", map.get("password"));
+        assertEquals("somechat", map.get("serviceName"));
     }
 
     public void testResolveRawParameterValues() throws Exception {