From 1610329d3eff744c99d3d1767f6e53e121ab310d Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Fri, 1 Feb 2013 15:31:18 +1100 Subject: [PATCH] 399703 made encoding error handling consistent --- .../org/eclipse/jetty/util/UrlEncoded.java | 52 +++++++++++++++---- .../eclipse/jetty/util/Utf8Appendable.java | 43 +++++++++++++++ .../eclipse/jetty/util/Utf8StringBuffer.java | 6 --- .../eclipse/jetty/util/Utf8StringBuilder.java | 6 +-- .../eclipse/jetty/util/URLEncodedTest.java | 33 ++++++++++-- 5 files changed, 115 insertions(+), 25 deletions(-) diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/UrlEncoded.java b/jetty-util/src/main/java/org/eclipse/jetty/util/UrlEncoded.java index 6824efcdd34..8460db28303 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/UrlEncoded.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/UrlEncoded.java @@ -336,10 +336,20 @@ public class UrlEncoded extends MultiMap implements Cloneable i++; if (i+40) { - map.add(buffer.toString(),""); + map.add(buffer.toReplacedString(),""); } } } @@ -763,7 +773,10 @@ public class UrlEncoded extends MultiMap implements Cloneable buffer.getStringBuffer().append(unicode); } else + { i=length; + buffer.getStringBuffer().append(Utf8Appendable.REPLACEMENT); + } } else { @@ -773,13 +786,22 @@ public class UrlEncoded extends MultiMap implements Cloneable buffer.append(b); } } + catch(NotUtf8Exception e) + { + LOG.warn(e.toString()); + LOG.debug(e); + } catch(NumberFormatException nfe) { + LOG.debug(nfe); buffer.getStringBuffer().append(Utf8Appendable.REPLACEMENT); } } else + { + buffer.getStringBuffer().append(Utf8Appendable.REPLACEMENT); i=length; + } } else if (buffer!=null) buffer.getStringBuffer().append(c); @@ -792,7 +814,7 @@ public class UrlEncoded extends MultiMap implements Cloneable return encoded.substring(offset,offset+length); } - return buffer.toString(); + return buffer.toReplacedString(); } else { @@ -843,12 +865,20 @@ public class UrlEncoded extends MultiMap implements Cloneable { if ('u'==encoded.charAt(offset+i+1)) { - int o=offset+i+2; - i+=6; - String unicode = new String(Character.toChars(TypeUtil.parseInt(encoded,o,4,16))); - byte[] reencoded = unicode.getBytes(charset); - System.arraycopy(reencoded,0,ba,n,reencoded.length); - n+=reencoded.length; + if (i+6 map = new MultiMap(); + UrlEncoded.decodeUtf8To(bad,0,bad.length,map); + assertEquals("encoded param size",1, map.size()); + assertEquals("encoded get", "\ufffd\ufffd\ufffd", map.getString("Name")); - assertEquals("xxx",UrlEncoded.decodeString("xxx%u123",0,5,"UTF-8")); + url_encoded.clear(); + url_encoded.decode("Name=%FF%FF%FF", "UTF-8"); + assertEquals("encoded param size",1, url_encoded.size()); + assertEquals("encoded get", "\ufffd\ufffd\ufffd", url_encoded.getString("Name")); + + url_encoded.clear(); + url_encoded.decode("Name=%EF%EF%EF", "UTF-8"); + assertEquals("encoded param size",1, url_encoded.size()); + assertEquals("encoded get", "\ufffd\ufffd", url_encoded.getString("Name")); + + assertEquals("x",UrlEncoded.decodeString("x",0,1,"UTF-8")); + assertEquals("x\ufffd",UrlEncoded.decodeString("x%",0,2,"UTF-8")); + assertEquals("x\ufffd",UrlEncoded.decodeString("x%2",0,3,"UTF-8")); + assertEquals("x ",UrlEncoded.decodeString("x%20",0,4,"UTF-8")); + + assertEquals("xxx",UrlEncoded.decodeString("xxx",0,3,"UTF-8")); + assertEquals("xxx\ufffd",UrlEncoded.decodeString("xxx%",0,4,"UTF-8")); + assertEquals("xxx\ufffd",UrlEncoded.decodeString("xxx%u",0,5,"UTF-8")); + assertEquals("xxx\ufffd",UrlEncoded.decodeString("xxx%u1",0,6,"UTF-8")); + assertEquals("xxx\ufffd",UrlEncoded.decodeString("xxx%u12",0,7,"UTF-8")); + assertEquals("xxx\ufffd",UrlEncoded.decodeString("xxx%u123",0,8,"UTF-8")); + assertEquals("xxx\u1234",UrlEncoded.decodeString("xxx%u1234",0,9,"UTF-8")); }