don't stop on 0
Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/d4850e0a Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/d4850e0a Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/d4850e0a Branch: refs/heads/wicket-1.4.x Commit: d4850e0acaaf81cd158cfdbfcaf78017bc5bdacb Parents: 29a7c13 Author: Carl-Eric Menzel <cmen...@wicketbuch.de> Authored: Tue Jul 3 13:51:46 2012 +0200 Committer: Carl-Eric Menzel <cmen...@wicketbuch.de> Committed: Tue Jul 3 13:51:46 2012 +0200 ---------------------------------------------------------------------- .../wicket/protocol/http/WicketURLEncoder.java | 30 ++++++-------- .../apache/wicket/protocol/http/WicketURLTest.java | 6 +++ 2 files changed, 19 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/d4850e0a/wicket/src/main/java/org/apache/wicket/protocol/http/WicketURLEncoder.java ---------------------------------------------------------------------- diff --git a/wicket/src/main/java/org/apache/wicket/protocol/http/WicketURLEncoder.java b/wicket/src/main/java/org/apache/wicket/protocol/http/WicketURLEncoder.java index a25505d..1ff1135 100644 --- a/wicket/src/main/java/org/apache/wicket/protocol/http/WicketURLEncoder.java +++ b/wicket/src/main/java/org/apache/wicket/protocol/http/WicketURLEncoder.java @@ -64,9 +64,6 @@ public class WicketURLEncoder // list of what not to decode protected BitSet dontNeedEncoding; - // E.g. "?" for FULL_PATH encoding when querystring has already been encoded. - private final char stopChar; - // used in decoding protected static final int caseDiff = ('a' - 'A'); @@ -76,7 +73,7 @@ public class WicketURLEncoder * * For example: http://org.acme/notthis/northis/oreventhis?buthis=isokay&asis=thispart */ - public static final WicketURLEncoder QUERY_INSTANCE = new WicketURLEncoder(Type.QUERY, '\0'); + public static final WicketURLEncoder QUERY_INSTANCE = new WicketURLEncoder(Type.QUERY); /** * Encoder used to encode components of a path.<br/> @@ -84,7 +81,7 @@ public class WicketURLEncoder * * For example: http://org.acme/foo/thispart/orthispart?butnot=thispart */ - public static final WicketURLEncoder PATH_INSTANCE = new WicketURLEncoder(Type.PATH, '\0'); + public static final WicketURLEncoder PATH_INSTANCE = new WicketURLEncoder(Type.PATH); /** * Encoder used to encode all path segments. Querystring will be excluded.<br/> @@ -92,8 +89,9 @@ public class WicketURLEncoder * * For example: http://org.acme/foo/thispart/orthispart?butnot=thispart */ - public static final WicketURLEncoder FULL_PATH_INSTANCE = new WicketURLEncoder(Type.FULL_PATH, - '?'); + public static final WicketURLEncoder FULL_PATH_INSTANCE = new WicketURLEncoder(Type.FULL_PATH); + + private final Type type; /** * Allow subclass to call constructor. @@ -103,9 +101,8 @@ public class WicketURLEncoder * @param stopChar * stop encoding when stopChar found */ - protected WicketURLEncoder(Type type, char stopChar) + protected WicketURLEncoder(Type type) { - this.stopChar = stopChar; /* * This note from java.net.URLEncoder ================================== @@ -155,6 +152,7 @@ public class WicketURLEncoder * query =( pchar / "/" / "?" ) */ + this.type = type; // unreserved dontNeedEncoding = new BitSet(256); int i; @@ -195,7 +193,7 @@ public class WicketURLEncoder // encoding type-specific switch (type) { - // this code consistent with java.net.URLEncoder version + // this code consistent with java.net.URLEncoder version case QUERY : dontNeedEncoding.set(' '); /* * encoding a space to a + is done in the encode() @@ -261,9 +259,9 @@ public class WicketURLEncoder * @return encoded string * @see java.net.URLEncoder#encode(String, String) */ - public String encode(String s, String enc) + public String encode(String unsafeInput, String enc) { - boolean needToChange = false; + final String s = unsafeInput.replace("\0", "NULL"); StringBuffer out = new StringBuffer(s.length()); Charset charset; CharArrayWriter charArrayWriter = new CharArrayWriter(); @@ -291,7 +289,7 @@ public class WicketURLEncoder { int c = s.charAt(i); - if ((stopEncoding == false) && (c == stopChar)) + if ((stopEncoding == false) && (c == '?' && type == Type.FULL_PATH)) { stopEncoding = true; } @@ -302,7 +300,6 @@ public class WicketURLEncoder if (c == ' ') { c = '+'; - needToChange = true; } // System.out.println("Storing: " + c); out.append((char)c); @@ -376,10 +373,9 @@ public class WicketURLEncoder out.append(ch); } charArrayWriter.reset(); - needToChange = true; } } - return (needToChange ? out.toString() : s); + return out.toString(); } -} \ No newline at end of file +} http://git-wip-us.apache.org/repos/asf/wicket/blob/d4850e0a/wicket/src/test/java/org/apache/wicket/protocol/http/WicketURLTest.java ---------------------------------------------------------------------- diff --git a/wicket/src/test/java/org/apache/wicket/protocol/http/WicketURLTest.java b/wicket/src/test/java/org/apache/wicket/protocol/http/WicketURLTest.java index eb493df..ebcd305 100644 --- a/wicket/src/test/java/org/apache/wicket/protocol/http/WicketURLTest.java +++ b/wicket/src/test/java/org/apache/wicket/protocol/http/WicketURLTest.java @@ -64,4 +64,10 @@ public class WicketURLTest extends TestCase assertEquals("http://www.devil.com/highway?destination=NULLhell", decoded); } + public void testDontStopEncodingOnNullByte() throws Exception + { + assertEquals("someone's%20badNULL%20url", + WicketURLEncoder.FULL_PATH_INSTANCE.encode("someone's bad\0 url", "UTF-8")); + } + }