do not 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/396a0b41 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/396a0b41 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/396a0b41 Branch: refs/heads/wicket-1.5.x Commit: 396a0b410afcefa3f329f933f5d3321987f4239d Parents: 3f5cd4a Author: Carl-Eric Menzel <cmen...@wicketbuch.de> Authored: Tue Jul 3 00:13:23 2012 +0200 Committer: Carl-Eric Menzel <cmen...@wicketbuch.de> Committed: Tue Jul 3 00:13:23 2012 +0200 ---------------------------------------------------------------------- .../java/org/apache/wicket/request/UrlEncoder.java | 31 ++++++--------- .../org/apache/wicket/request/UrlEncoderTest.java | 12 +++++- 2 files changed, 22 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/396a0b41/wicket-request/src/main/java/org/apache/wicket/request/UrlEncoder.java ---------------------------------------------------------------------- diff --git a/wicket-request/src/main/java/org/apache/wicket/request/UrlEncoder.java b/wicket-request/src/main/java/org/apache/wicket/request/UrlEncoder.java index f3b9ae7..681f713 100644 --- a/wicket-request/src/main/java/org/apache/wicket/request/UrlEncoder.java +++ b/wicket-request/src/main/java/org/apache/wicket/request/UrlEncoder.java @@ -59,10 +59,6 @@ public class UrlEncoder // 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'); @@ -72,7 +68,7 @@ public class UrlEncoder * * For example: http://org.acme/notthis/northis/oreventhis?buthis=isokay&asis=thispart */ - public static final UrlEncoder QUERY_INSTANCE = new UrlEncoder(Type.QUERY, '\0'); + public static final UrlEncoder QUERY_INSTANCE = new UrlEncoder(Type.QUERY); /** * Encoder used to encode components of a path.<br/> @@ -80,7 +76,7 @@ public class UrlEncoder * * For example: http://org.acme/foo/thispart/orthispart?butnot=thispart */ - public static final UrlEncoder PATH_INSTANCE = new UrlEncoder(Type.PATH, '\0'); + public static final UrlEncoder PATH_INSTANCE = new UrlEncoder(Type.PATH); /** * Encoder used to encode all path segments. Querystring will be excluded.<br/> @@ -88,20 +84,18 @@ public class UrlEncoder * * For example: http://org.acme/foo/thispart/orthispart?butnot=thispart */ - public static final UrlEncoder FULL_PATH_INSTANCE = new UrlEncoder(Type.FULL_PATH, '?'); + public static final UrlEncoder FULL_PATH_INSTANCE = new UrlEncoder(Type.FULL_PATH); + + private final Type type; /** * Allow subclass to call constructor. * * @param type * encoder type - * @param stopChar - * stop encoding when stopChar found */ - protected UrlEncoder(final Type type, final char stopChar) + protected UrlEncoder(final Type type) { - this.stopChar = stopChar; - /* * This note from java.net.URLEncoder ================================== * @@ -150,6 +144,7 @@ public class UrlEncoder * query =( pchar / "/" / "?" ) */ + this.type = type; // unreserved dontNeedEncoding = new BitSet(256); int i; @@ -191,7 +186,7 @@ public class UrlEncoder // encoding type-specific switch (type) { - // this code consistent with java.net.URLEncoder version + // this code consistent with java.net.URLEncoder version case QUERY : // encoding a space to a + is done in the encode() method dontNeedEncoding.set(' '); @@ -257,9 +252,9 @@ public class UrlEncoder * @return encoded string * @see java.net.URLEncoder#encode(String, String) */ - public String encode(final String s, final String charsetName) + public String encode(final String unsafeInput, final String charsetName) { - boolean needToChange = false; + final String s = unsafeInput.replace("\0", "NULL"); StringBuilder out = new StringBuilder(s.length()); Charset charset; CharArrayWriter charArrayWriter = new CharArrayWriter(); @@ -284,7 +279,7 @@ public class UrlEncoder { int c = s.charAt(i); - if ((stopEncoding == false) && (c == stopChar)) + if ((stopEncoding == false) && (c == '?' && type == Type.FULL_PATH)) { stopEncoding = true; } @@ -295,7 +290,6 @@ public class UrlEncoder if (c == ' ') { c = '+'; - needToChange = true; } // System.out.println("Storing: " + c); out.append((char)c); @@ -369,10 +363,9 @@ public class UrlEncoder 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/396a0b41/wicket-request/src/test/java/org/apache/wicket/request/UrlEncoderTest.java ---------------------------------------------------------------------- diff --git a/wicket-request/src/test/java/org/apache/wicket/request/UrlEncoderTest.java b/wicket-request/src/test/java/org/apache/wicket/request/UrlEncoderTest.java index f44234f..851ecc6 100644 --- a/wicket-request/src/test/java/org/apache/wicket/request/UrlEncoderTest.java +++ b/wicket-request/src/test/java/org/apache/wicket/request/UrlEncoderTest.java @@ -40,13 +40,21 @@ public class UrlEncoderTest extends Assert /** * Do not encode semicolon in the Url's path because it is used in ';jsessionid=...' - * + * * https://issues.apache.org/jira/browse/WICKET-4409 */ @Test public void dontEncodeSemicolon() { - String encoded = UrlEncoder.PATH_INSTANCE.encode("path;jsessionid=1234567890", CharEncoding.UTF_8); + String encoded = UrlEncoder.PATH_INSTANCE.encode("path;jsessionid=1234567890", + CharEncoding.UTF_8); assertEquals("path;jsessionid=1234567890", encoded); } + + @Test + public void dontStopOnNullByte() throws Exception + { + assertEquals("someone%27s%20badNULL%20url", + UrlEncoder.FULL_PATH_INSTANCE.encode("someone's bad\0 url", CharEncoding.UTF_8)); + } }