369602: ignore NotUtf8Exception for decodeToUtf8

This commit is contained in:
Greg Wilkins 2012-01-25 15:40:24 +11:00
parent f3735fab7b
commit ee9e195b49
3 changed files with 69 additions and 45 deletions

View File

@ -462,52 +462,60 @@ public class UrlEncoded extends MultiMap implements Cloneable
int totalLength=0; int totalLength=0;
while ((b=in.read())>=0) while ((b=in.read())>=0)
{ {
switch ((char) b) try
{ {
case '&': switch ((char) b)
value = buffer.length()==0?"":buffer.toString(); {
buffer.reset(); case '&':
if (key != null) value = buffer.length()==0?"":buffer.toString();
{ buffer.reset();
map.add(key,value); if (key != null)
} {
else if (value!=null&&value.length()>0) map.add(key,value);
{ }
map.add(value,""); else if (value!=null&&value.length()>0)
} {
key = null; map.add(value,"");
value=null; }
if (maxKeys>0 && map.size()>maxKeys) key = null;
{ value=null;
LOG.warn("maxFormKeys limit exceeded keys>{}",maxKeys); if (maxKeys>0 && map.size()>maxKeys)
return; {
} LOG.warn("maxFormKeys limit exceeded keys>{}",maxKeys);
break; return;
}
break;
case '=': case '=':
if (key!=null) if (key!=null)
{ {
buffer.append((byte)b);
break;
}
key = buffer.toString();
buffer.reset();
break;
case '+':
buffer.append((byte)' ');
break;
case '%':
int dh=in.read();
int dl=in.read();
if (dh<0||dl<0)
break;
buffer.append((byte)((TypeUtil.convertHexDigit((byte)dh)<<4) + TypeUtil.convertHexDigit((byte)dl)));
break;
default:
buffer.append((byte)b); buffer.append((byte)b);
break; break;
} }
key = buffer.toString(); }
buffer.reset(); catch(NotUtf8Exception e)
break; {
LOG.warn(e.toString());
case '+': LOG.debug(e);
buffer.append((byte)' ');
break;
case '%':
int dh=in.read();
int dl=in.read();
if (dh<0||dl<0)
break;
buffer.append((byte)((TypeUtil.convertHexDigit((byte)dh)<<4) + TypeUtil.convertHexDigit((byte)dl)));
break;
default:
buffer.append((byte)b);
break;
} }
if (maxLength>=0 && (++totalLength > maxLength)) if (maxLength>=0 && (++totalLength > maxLength))
throw new IllegalStateException("Form too large"); throw new IllegalStateException("Form too large");

View File

@ -40,7 +40,7 @@ import java.io.IOException;
**/ **/
public abstract class Utf8Appendable public abstract class Utf8Appendable
{ {
private final char REPLACEMENT = '\ufffd'; public static final char REPLACEMENT = '\ufffd';
private static final int UTF8_ACCEPT = 0; private static final int UTF8_ACCEPT = 0;
private static final int UTF8_REJECT = 12; private static final int UTF8_REJECT = 12;

View File

@ -220,7 +220,6 @@ public class URLEncodedTest
*/ */
} }
/* -------------------------------------------------------------- */ /* -------------------------------------------------------------- */
@Test @Test
public void testUtf8() public void testUtf8()
@ -236,4 +235,21 @@ public class URLEncodedTest
String expected = new String(TypeUtil.fromHexString(hex),"utf-8"); String expected = new String(TypeUtil.fromHexString(hex),"utf-8");
assertEquals(expected,url_encoded.get("text")); assertEquals(expected,url_encoded.get("text"));
} }
/* -------------------------------------------------------------- */
@Test
public void testNotUtf8() throws Exception
{
String query="name=X%c0%afZ";
MultiMap<String> map = new MultiMap<String>();
UrlEncoded.decodeUtf8To(query.getBytes(StringUtil.__ISO_8859_1),0,query.length(),map);
assertEquals("X"+Utf8Appendable.REPLACEMENT+Utf8Appendable.REPLACEMENT+"Z",map.getValue("name",0));
map.clear();
UrlEncoded.decodeUtf8To(new ByteArrayInputStream(query.getBytes(StringUtil.__ISO_8859_1)),map,100,2);
assertEquals("X"+Utf8Appendable.REPLACEMENT+Utf8Appendable.REPLACEMENT+"Z",map.getValue("name",0));
}
} }