knox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From su...@apache.org
Subject knox git commit: KNOX-690 fixing the encoding/decoding in the parser and template
Date Thu, 07 Apr 2016 17:32:44 GMT
Repository: knox
Updated Branches:
  refs/heads/v0.9.0 c1aee93cf -> 994836cb6


KNOX-690 fixing the encoding/decoding in the parser and template


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

Branch: refs/heads/v0.9.0
Commit: 994836cb656252128d26117d95320f69a35779d5
Parents: c1aee93
Author: Sumit Gupta <sumit@apache.org>
Authored: Thu Apr 7 11:28:26 2016 -0400
Committer: Sumit Gupta <sumit@apache.org>
Committed: Thu Apr 7 13:32:36 2016 -0400

----------------------------------------------------------------------
 .../filter/rewrite/impl/UrlRewriteRequest.java  |  11 +-
 .../hadoop/gateway/util/urltemplate/Parser.java |  16 ++-
 .../gateway/util/urltemplate/Template.java      | 105 ++++++++++++++-----
 .../gateway/util/urltemplate/MatcherTest.java   |  50 +++++++++
 4 files changed, 144 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/knox/blob/994836cb/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/UrlRewriteRequest.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/UrlRewriteRequest.java
b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/UrlRewriteRequest.java
index 73ec5bb..187965c 100644
--- a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/UrlRewriteRequest.java
+++ b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/UrlRewriteRequest.java
@@ -41,9 +41,7 @@ import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
-import java.io.UnsupportedEncodingException;
 import java.net.URISyntaxException;
-import java.net.URLDecoder;
 import java.util.Collections;
 import java.util.Enumeration;
 import java.util.List;
@@ -127,7 +125,7 @@ public class UrlRewriteRequest extends GatewayRequestWrapper implements
Resolver
     if( url == null ) {
       return EMPTY_STRING_ARRAY;
     } else {
-      String s = url.toString();
+      String s = url.toEncodedString();
       return s.split( "\\?" );
     }
   }
@@ -153,12 +151,7 @@ public class UrlRewriteRequest extends GatewayRequestWrapper implements
Resolver
   public String getQueryString() {
     String[] split = splitTargetUrl( getTargetUrl() );
     if( split.length > 1 ) {
-      try {
-        return URLDecoder.decode(split[1], "UTF-8");
-      } catch ( UnsupportedEncodingException e ) {
-        LOG.failedToDecodeQueryString(split[1], e);
-        return split[1];
-      }
+      return split[1];
     } else {
       return null;
     }

http://git-wip-us.apache.org/repos/asf/knox/blob/994836cb/gateway-util-urltemplate/src/main/java/org/apache/hadoop/gateway/util/urltemplate/Parser.java
----------------------------------------------------------------------
diff --git a/gateway-util-urltemplate/src/main/java/org/apache/hadoop/gateway/util/urltemplate/Parser.java
b/gateway-util-urltemplate/src/main/java/org/apache/hadoop/gateway/util/urltemplate/Parser.java
index e59cd32..bddf6f7 100644
--- a/gateway-util-urltemplate/src/main/java/org/apache/hadoop/gateway/util/urltemplate/Parser.java
+++ b/gateway-util-urltemplate/src/main/java/org/apache/hadoop/gateway/util/urltemplate/Parser.java
@@ -20,6 +20,7 @@ package org.apache.hadoop.gateway.util.urltemplate;
 import org.apache.hadoop.gateway.i18n.resources.ResourcesFactory;
 
 import java.net.URISyntaxException;
+import java.net.URLDecoder;
 import java.util.StringTokenizer;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -263,11 +264,10 @@ public class Parser {
         }
       } else {
         String nameValue[] = split( token, '=' );
+        String queryName = decodeValue(nameValue[ 0 ]);
         if( nameValue.length == 1 ) {
-          String queryName = nameValue[ 0 ];
           builder.addQuery( queryName, new Token( Segment.ANONYMOUS_PARAM, null, builder.isLiteral()
) );
         } else {
-          String queryName = nameValue[ 0 ];
           Token paramPattern = parseTemplateToken( builder, nameValue[ 1 ], Segment.GLOB_PATTERN
);
           builder.addQuery( queryName, paramPattern );
         }
@@ -326,10 +326,20 @@ public class Parser {
       actualPattern = s;
       effectivePattern = actualPattern;
     }
-    final Token token = new Token( paramName, actualPattern, effectivePattern, builder.isLiteral()
);
+    final Token token = new Token( decodeValue(paramName), decodeValue(actualPattern), decodeValue(effectivePattern),
builder.isLiteral() );
     return token;
   }
 
+  private static String decodeValue(String value) {
+    try {
+      value = URLDecoder.decode(value, "UTF-8");
+    } catch ( Exception e ) {
+      //log
+    }
+    return value;
+  }
+
+
   // Using this because String.split is very inefficient.
   private static String[] split( String s, char d ) {
     String[] a;

http://git-wip-us.apache.org/repos/asf/knox/blob/994836cb/gateway-util-urltemplate/src/main/java/org/apache/hadoop/gateway/util/urltemplate/Template.java
----------------------------------------------------------------------
diff --git a/gateway-util-urltemplate/src/main/java/org/apache/hadoop/gateway/util/urltemplate/Template.java
b/gateway-util-urltemplate/src/main/java/org/apache/hadoop/gateway/util/urltemplate/Template.java
index 23bdbeb..253b2e0 100644
--- a/gateway-util-urltemplate/src/main/java/org/apache/hadoop/gateway/util/urltemplate/Template.java
+++ b/gateway-util-urltemplate/src/main/java/org/apache/hadoop/gateway/util/urltemplate/Template.java
@@ -17,6 +17,8 @@
  */
 package org.apache.hadoop.gateway.util.urltemplate;
 
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
 import java.util.Collections;
 import java.util.LinkedHashMap;
 import java.util.List;
@@ -151,57 +153,69 @@ public class Template {
     return hasFragment;
   }
 
-  private void buildScheme( StringBuilder b ) {
+  private void buildScheme( StringBuilder b, boolean encode ) {
     if( hasScheme ) {
       if( scheme != null ) {
-        buildSegmentValue( b, scheme, scheme.getFirstValue() );
+        buildSegmentValue( b, scheme, scheme.getFirstValue(), encode );
       }
       b.append( ':' );
     }
   }
 
-  private void buildAuthority( StringBuilder b ) {
+  private void buildAuthority( StringBuilder b, boolean encode ) {
     if( hasAuthority ) {
       if( !isAuthorityOnly ) {
         b.append( "//" );
       }
       if( username != null || password != null ) {
         if( username != null ) {
-          buildSegmentValue( b, username, username.getFirstValue() );
+          buildSegmentValue( b, username, username.getFirstValue(), encode );
         }
         if( password != null ) {
           b.append( ':' );
-          buildSegmentValue( b, password, password.getFirstValue() );
+          buildSegmentValue( b, password, password.getFirstValue(), encode );
         }
         b.append( "@" );
       }
       if( host != null ) {
-        buildSegmentValue( b, host, host.getFirstValue() );
+        buildSegmentValue( b, host, host.getFirstValue(), encode );
       }
       if( port != null ) {
         b.append( ':' );
-        buildSegmentValue( b, port, port.getFirstValue() );
+        buildSegmentValue( b, port, port.getFirstValue(), encode );
       }
     }
   }
 
-  private void buildSegmentValue( StringBuilder b, Segment s, Segment.Value v ) {
+  private void buildSegmentValue( StringBuilder b, Segment s, Segment.Value v, boolean encode
) {
     String paramName = s.getParamName();
     if( paramName != null && paramName.length() > 0 ) {
       b.append( "{" );
-      b.append( s.getParamName() );
+      if ( encode ) {
+        b.append( encodeValue( s.getParamName() ));
+      } else {
+        b.append( s.getParamName() );
+      }
       String actualPattern = v.getToken().originalPattern;
       if( ( actualPattern != null ) && ( v.getType() != Segment.DEFAULT ) )  {
         b.append( '=' );
-        b.append( v.getOriginalPattern() );
+        if ( encode ) {
+          b.append( encodeValue( v.getOriginalPattern() ));
+        } else {
+          b.append( v.getOriginalPattern() );
+        }
       }
       b.append( '}' );
     } else {
-      b.append( s.getFirstValue().getOriginalPattern() );
+      if ( encode ) {
+        b.append( encodeValue( s.getFirstValue().getOriginalPattern() ));
+      } else {
+        b.append( s.getFirstValue().getOriginalPattern() );
+      }
     }
   }
 
-  private void buildPath( StringBuilder b ) {
+  private void buildPath( StringBuilder b, boolean encode ) {
     if( isAbsolute ) {
       b.append( '/' );
     }
@@ -216,7 +230,11 @@ public class Template {
       Segment.Value firstValue = segment.getFirstValue();
       if( paramName != null && paramName.length() > 0 ) {
         b.append( "{" );
-        b.append( segment.getParamName() );
+        if ( encode ) {
+          b.append( encodeValue(segment.getParamName()) );
+        } else {
+          b.append( segment.getParamName() );
+        }
         String pattern = firstValue.getOriginalPattern();
         if( pattern != null && !pattern.isEmpty() ) {
           b.append( '=' );
@@ -224,7 +242,11 @@ public class Template {
         }
         b.append( '}' );
       } else {
-        b.append( firstValue.getOriginalPattern() );
+        if ( encode ) {
+          b.append( encodeValue(firstValue.getOriginalPattern()) );
+        } else {
+          b.append( firstValue.getOriginalPattern() );
+        }
       }
     }
     if( isDirectory && ( !isAbsolute || path.size() > 0 ) ) {
@@ -232,7 +254,7 @@ public class Template {
     }
   }
 
-  private void buildQuery( StringBuilder b ) {
+  private void buildQuery( StringBuilder b , boolean encode ) {
     if( hasQuery ) {
       int count = 0;
       for( Query segment: query.values() ) {
@@ -244,7 +266,7 @@ public class Template {
           } else {
             b.append( '&' );
           }
-          buildQuerySegment( b, segment, value );
+          buildQuerySegment( b, segment, value, encode );
 //          String valuePattern = value.getPattern();
 //          if( paramName != null && paramName.length() > 0 ) {
 //            b.append( segment.getQueryName() );
@@ -271,7 +293,7 @@ public class Template {
         } else {
           b.append( '&' );
         }
-        buildQuerySegment( b, extra, extra.getFirstValue() );
+        buildQuerySegment( b, extra, extra.getFirstValue(), encode );
       }
       if( count == 0 ) {
         b.append( '?' );
@@ -279,17 +301,25 @@ public class Template {
     }
   }
 
-  private void buildQuerySegment( StringBuilder b, Query segment, Segment.Value value ) {
+  private void buildQuerySegment( StringBuilder b, Query segment, Segment.Value value, boolean
encode ) {
     String paramName = segment.getParamName();
     String queryName = segment.getQueryName();
     String valuePattern = value.getOriginalPattern();
+    if (encode) {
+      queryName = encodeValue(queryName);
+      valuePattern = encodeValue(valuePattern);
+    }
     if( paramName != null && paramName.length() > 0 ) {
       if( !Segment.GLOB_PATTERN.equals( queryName ) && !Segment.STAR_PATTERN.equals(
queryName ) ) {
         b.append( segment.getQueryName() );
         b.append( "=" );
       }
       b.append( "{" );
-      b.append( segment.getParamName() );
+      if (encode) {
+        b.append( encodeValue(segment.getParamName()) );
+      } else {
+        b.append( segment.getParamName() );
+      }
       if( valuePattern != null ) {
         b.append( '=' );
         b.append( valuePattern );
@@ -304,30 +334,53 @@ public class Template {
     }
   }
 
-  private void buildFragment( StringBuilder b ) {
+  private void buildFragment( StringBuilder b, boolean encode ) {
     if( hasFragment ) {
       b.append( '#' );
       if( fragment != null ) {
-        b.append( fragment.getFirstValue().getOriginalPattern() );
+        String value = fragment.getFirstValue().getOriginalPattern();
+        if (encode) {
+          value = encodeValue( value );
+        }
+        b.append( value );
+      }
+    }
+  }
+
+  private String encodeValue(String value) {
+    if ( value != null ) {
+      try {
+        return URLEncoder.encode( value, "UTF-8" );
+      } catch ( UnsupportedEncodingException e ) {
+        //log
       }
     }
+    return value;
   }
 
   public String toString() {
+    return toString( false );
+  }
+
+  private String toString( boolean encoded ) {
     String s = image;
     if( s == null ) {
       StringBuilder b = new StringBuilder();
-      buildScheme( b );
-      buildAuthority( b );
-      buildPath( b );
-      buildQuery( b );
-      buildFragment( b );
+      buildScheme( b, encoded );
+      buildAuthority( b, encoded );
+      buildPath( b, encoded );
+      buildQuery( b, encoded );
+      buildFragment( b , encoded);
       s = b.toString();
       //image = s;
     }
     return s;
   }
 
+ public String toEncodedString() {
+    return toString(true);
+  }
+
   public int hashCode() {
     Integer hc = hash;
     if( hc == null ) {

http://git-wip-us.apache.org/repos/asf/knox/blob/994836cb/gateway-util-urltemplate/src/test/java/org/apache/hadoop/gateway/util/urltemplate/MatcherTest.java
----------------------------------------------------------------------
diff --git a/gateway-util-urltemplate/src/test/java/org/apache/hadoop/gateway/util/urltemplate/MatcherTest.java
b/gateway-util-urltemplate/src/test/java/org/apache/hadoop/gateway/util/urltemplate/MatcherTest.java
index 362f26f..c568168 100644
--- a/gateway-util-urltemplate/src/test/java/org/apache/hadoop/gateway/util/urltemplate/MatcherTest.java
+++ b/gateway-util-urltemplate/src/test/java/org/apache/hadoop/gateway/util/urltemplate/MatcherTest.java
@@ -975,4 +975,54 @@ public class MatcherTest {
     assertThat( expandedStr, containsString( "&" ) );
   }
 
+  @Test
+  public void testEncodedUrlMatching() throws Exception {
+    Template template;
+    Template input;
+    Matcher<String> matcher;
+    Matcher<?>.Match match;
+
+    matcher = new Matcher<String>();
+
+    template = Parser.parseTemplate( "*://*:*/example" );
+    matcher.add( template, "test-example" );
+
+    template = Parser.parseTemplate( "*://*:*/|" );
+    matcher.add( template, "test-pipe-in-path" );
+
+    template = Parser.parseTemplate( "*://*:*/test-path?{|=*}" );
+    matcher.add( template, "test-pipe-in-query-param-name" );
+
+    template = Parser.parseTemplate( "*://*:*/test-path?{test-param-name=*}" );
+    matcher.add( template, "test-pipe-in-query-param-value" );
+
+    template = Parser.parseTemplate( "*://*:*/test-path#|" );
+    matcher.add( template, "test-pipe-in-fragment" );
+
+    input = Parser.parseLiteral( "test-scheme://test-host:42/example" );
+    match = matcher.match( input );
+    assertThat( match, notNullValue() );
+    assertThat( (String)match.getValue(), is( "test-example" ) );
+
+    input = Parser.parseLiteral( "test-scheme://test-host:42/%7C" );
+    match = matcher.match( input );
+    assertThat( match, notNullValue() );
+    assertThat( (String)match.getValue(), is( "test-pipe-in-path" ) );
+
+    input = Parser.parseLiteral( "test-scheme://test-host:42/test-path?%7C=test-param-value"
);
+    match = matcher.match( input );
+    assertThat( match, notNullValue() );
+    assertThat( (String)match.getValue(), is( "test-pipe-in-query-param-name" ) );
+
+    input = Parser.parseLiteral( "test-scheme://test-host:42/test-path?test-param-name=%7C"
);
+    match = matcher.match( input );
+    assertThat( match, notNullValue() );
+    assertThat( (String)match.getValue(), is( "test-pipe-in-query-param-value" ) );
+
+    input = Parser.parseLiteral( "test-scheme://test-host:42/test-path#%7C" );
+    match = matcher.match( input );
+    assertThat( match, notNullValue() );
+    assertThat( (String)match.getValue(), is( "test-pipe-in-fragment" ) );
+  }
+
 }


Mime
View raw message