knox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lmc...@apache.org
Subject [2/6] knox git commit: Revert "KNOX-690 fixing the encoding/decoding in the parser and template"
Date Sat, 17 Jun 2017 14:18:48 GMT
Revert "KNOX-690 fixing the encoding/decoding in the parser and template"

This reverts commit 994836cb656252128d26117d95320f69a35779d5.


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

Branch: refs/heads/master
Commit: 4eeff1a6d156db860245916fa44e2a1505247e43
Parents: 1314f1d
Author: Larry McCay <lmccay@hortonworks.com>
Authored: Tue Jun 13 17:30:02 2017 -0400
Committer: Larry McCay <lmccay@hortonworks.com>
Committed: Tue Jun 13 17:30:02 2017 -0400

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


http://git-wip-us.apache.org/repos/asf/knox/blob/4eeff1a6/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 9c7c66c..cd053de 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,7 +41,9 @@ 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.util.Arrays;
 import java.util.Enumeration;
 import java.util.List;
@@ -125,7 +127,7 @@ public class UrlRewriteRequest extends GatewayRequestWrapper implements
Resolver
     if( url == null ) {
       return EMPTY_STRING_ARRAY;
     } else {
-      String s = url.toEncodedString();
+      String s = url.toString();
       return s.split( "\\?" );
     }
   }
@@ -151,7 +153,12 @@ public class UrlRewriteRequest extends GatewayRequestWrapper implements
Resolver
   public String getQueryString() {
     String[] split = splitTargetUrl( getTargetUrl() );
     if( split.length > 1 ) {
-      return split[1];
+      try {
+        return URLDecoder.decode(split[1], "UTF-8");
+      } catch ( UnsupportedEncodingException e ) {
+        LOG.failedToDecodeQueryString(split[1], e);
+        return split[1];
+      }
     } else {
       return null;
     }

http://git-wip-us.apache.org/repos/asf/knox/blob/4eeff1a6/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 bddf6f7..e59cd32 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,7 +20,6 @@ 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;
@@ -264,10 +263,11 @@ 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,20 +326,10 @@ public class Parser {
       actualPattern = s;
       effectivePattern = actualPattern;
     }
-    final Token token = new Token( decodeValue(paramName), decodeValue(actualPattern), decodeValue(effectivePattern),
builder.isLiteral() );
+    final Token token = new Token( paramName, actualPattern, 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/4eeff1a6/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 6104e76..f2eba5a 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,8 +17,6 @@
  */
 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,69 +149,57 @@ public class Template {
     return hasFragment;
   }
 
-  private void buildScheme( StringBuilder b, boolean encode ) {
+  private void buildScheme( StringBuilder b ) {
     if( hasScheme ) {
       if( scheme != null ) {
-        buildSegmentValue( b, scheme, scheme.getFirstValue(), encode );
+        buildSegmentValue( b, scheme, scheme.getFirstValue() );
       }
       b.append( ':' );
     }
   }
 
-  private void buildAuthority( StringBuilder b, boolean encode ) {
+  private void buildAuthority( StringBuilder b ) {
     if( hasAuthority ) {
       if( !isAuthorityOnly ) {
         b.append( "//" );
       }
       if( username != null || password != null ) {
         if( username != null ) {
-          buildSegmentValue( b, username, username.getFirstValue(), encode );
+          buildSegmentValue( b, username, username.getFirstValue() );
         }
         if( password != null ) {
           b.append( ':' );
-          buildSegmentValue( b, password, password.getFirstValue(), encode );
+          buildSegmentValue( b, password, password.getFirstValue() );
         }
         b.append( "@" );
       }
       if( host != null ) {
-        buildSegmentValue( b, host, host.getFirstValue(), encode );
+        buildSegmentValue( b, host, host.getFirstValue() );
       }
       if( port != null ) {
         b.append( ':' );
-        buildSegmentValue( b, port, port.getFirstValue(), encode );
+        buildSegmentValue( b, port, port.getFirstValue() );
       }
     }
   }
 
-  private void buildSegmentValue( StringBuilder b, Segment s, Segment.Value v, boolean encode
) {
+  private void buildSegmentValue( StringBuilder b, Segment s, Segment.Value v ) {
     String paramName = s.getParamName();
     if( paramName != null && paramName.length() > 0 ) {
       b.append( "{" );
-      if ( encode ) {
-        b.append( encodeValue( s.getParamName() ));
-      } else {
-        b.append( s.getParamName() );
-      }
+      b.append( s.getParamName() );
       String actualPattern = v.getToken().originalPattern;
       if( ( actualPattern != null ) && ( v.getType() != Segment.DEFAULT ) )  {
         b.append( '=' );
-        if ( encode ) {
-          b.append( encodeValue( v.getOriginalPattern() ));
-        } else {
-          b.append( v.getOriginalPattern() );
-        }
+        b.append( v.getOriginalPattern() );
       }
       b.append( '}' );
     } else {
-      if ( encode ) {
-        b.append( encodeValue( s.getFirstValue().getOriginalPattern() ));
-      } else {
-        b.append( s.getFirstValue().getOriginalPattern() );
-      }
+      b.append( s.getFirstValue().getOriginalPattern() );
     }
   }
 
-  private void buildPath( StringBuilder b, boolean encode ) {
+  private void buildPath( StringBuilder b ) {
     if( isAbsolute ) {
       b.append( '/' );
     }
@@ -228,11 +214,7 @@ public class Template {
       Segment.Value firstValue = segment.getFirstValue();
       if( paramName != null && paramName.length() > 0 ) {
         b.append( "{" );
-        if ( encode ) {
-          b.append( encodeValue(segment.getParamName()) );
-        } else {
-          b.append( segment.getParamName() );
-        }
+        b.append( segment.getParamName() );
         String pattern = firstValue.getOriginalPattern();
         if( pattern != null && !pattern.isEmpty() ) {
           b.append( '=' );
@@ -240,11 +222,7 @@ public class Template {
         }
         b.append( '}' );
       } else {
-        if ( encode ) {
-          b.append( encodeValue(firstValue.getOriginalPattern()) );
-        } else {
-          b.append( firstValue.getOriginalPattern() );
-        }
+        b.append( firstValue.getOriginalPattern() );
       }
     }
     if( isDirectory && ( !isAbsolute || path.size() > 0 ) ) {
@@ -252,7 +230,7 @@ public class Template {
     }
   }
 
-  private void buildQuery( StringBuilder b , boolean encode ) {
+  private void buildQuery( StringBuilder b ) {
     if( hasQuery ) {
       int count = 0;
       for( Query segment: query.values() ) {
@@ -264,7 +242,7 @@ public class Template {
           } else {
             b.append( '&' );
           }
-          buildQuerySegment( b, segment, value, encode );
+          buildQuerySegment( b, segment, value );
 //          String valuePattern = value.getPattern();
 //          if( paramName != null && paramName.length() > 0 ) {
 //            b.append( segment.getQueryName() );
@@ -291,7 +269,7 @@ public class Template {
         } else {
           b.append( '&' );
         }
-        buildQuerySegment( b, extra, extra.getFirstValue(), encode );
+        buildQuerySegment( b, extra, extra.getFirstValue() );
       }
       if( count == 0 ) {
         b.append( '?' );
@@ -299,25 +277,17 @@ public class Template {
     }
   }
 
-  private void buildQuerySegment( StringBuilder b, Query segment, Segment.Value value, boolean
encode ) {
+  private void buildQuerySegment( StringBuilder b, Query segment, Segment.Value value ) {
     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( "{" );
-      if (encode) {
-        b.append( encodeValue(segment.getParamName()) );
-      } else {
-        b.append( segment.getParamName() );
-      }
+      b.append( segment.getParamName() );
       if( valuePattern != null ) {
         b.append( '=' );
         b.append( valuePattern );
@@ -332,46 +302,28 @@ public class Template {
     }
   }
 
-  private void buildFragment( StringBuilder b, boolean encode ) {
+  private void buildFragment( StringBuilder b ) {
     if( hasFragment ) {
       b.append( '#' );
       if( fragment != null ) {
-        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
+        b.append( fragment.getFirstValue().getOriginalPattern() );
       }
     }
-    return value;
   }
 
   public String toString() {
-    return toString( false );
-  }
-
-  private String toString( boolean encoded ) {
-    StringBuilder b = new StringBuilder();
-    buildScheme( b, encoded );
-    buildAuthority( b, encoded );
-    buildPath( b, encoded );
-    buildQuery( b, encoded );
-    buildFragment( b , encoded);
-    return b.toString();
-  }
-
-  public String toEncodedString() {
-    return toString(true);
+    String s = image;
+    if( s == null ) {
+      StringBuilder b = new StringBuilder();
+      buildScheme( b );
+      buildAuthority( b );
+      buildPath( b );
+      buildQuery( b );
+      buildFragment( b );
+      s = b.toString();
+      //image = s;
+    }
+    return s;
   }
 
   public int hashCode() {

http://git-wip-us.apache.org/repos/asf/knox/blob/4eeff1a6/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 2e61f0c..44622e5 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
@@ -1005,54 +1005,4 @@ 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