431094 Consistent handling of utf8 decoding errors

This commit is contained in:
Greg Wilkins 2014-04-10 15:52:15 +10:00
parent 7adba8d033
commit 866960d5d7
1 changed files with 30 additions and 14 deletions

View File

@ -294,7 +294,7 @@ public class UrlEncoded extends MultiMap<String> implements Cloneable
switch ((char)(0xff&b)) switch ((char)(0xff&b))
{ {
case '&': case '&':
value = buffer.length()==0?"":buffer.toString(); value = buffer.toReplacedString();
buffer.reset(); buffer.reset();
if (key != null) if (key != null)
{ {
@ -314,7 +314,7 @@ public class UrlEncoded extends MultiMap<String> implements Cloneable
buffer.append(b); buffer.append(b);
break; break;
} }
key = buffer.toString(); key = buffer.toReplacedString();
buffer.reset(); buffer.reset();
break; break;
@ -376,7 +376,7 @@ public class UrlEncoded extends MultiMap<String> implements Cloneable
if (key != null) if (key != null)
{ {
value = buffer.length()==0?"":buffer.toReplacedString(); value = buffer.toReplacedString();
buffer.reset(); buffer.reset();
map.add(key,value); map.add(key,value);
} }
@ -510,7 +510,7 @@ public class UrlEncoded extends MultiMap<String> implements Cloneable
switch ((char) b) switch ((char) b)
{ {
case '&': case '&':
value = buffer.length()==0?"":buffer.toString(); value = buffer.toReplacedString();
buffer.reset(); buffer.reset();
if (key != null) if (key != null)
{ {
@ -532,7 +532,7 @@ public class UrlEncoded extends MultiMap<String> implements Cloneable
buffer.append((byte)b); buffer.append((byte)b);
break; break;
} }
key = buffer.toString(); key = buffer.toReplacedString();
buffer.reset(); buffer.reset();
break; break;
@ -542,17 +542,26 @@ public class UrlEncoded extends MultiMap<String> implements Cloneable
case '%': case '%':
int code0=in.read(); int code0=in.read();
boolean decoded=false;
if ('u'==code0) if ('u'==code0)
{ {
int code1=in.read(); code0=in.read(); // XXX: we have to read the next byte, otherwise code0 is always 'u'
if (code1>=0) if (code0>=0)
{ {
int code2=in.read(); int code1=in.read();
if (code2>=0) if (code1>=0)
{ {
int code3=in.read(); int code2=in.read();
if (code3>=0) if (code2>=0)
buffer.getStringBuilder().append(Character.toChars((convertHexDigit(code0)<<12)+(convertHexDigit(code1)<<8)+(convertHexDigit(code2)<<4)+convertHexDigit(code3))); {
int code3=in.read();
if (code3>=0)
{
buffer.getStringBuilder().append(Character.toChars
((convertHexDigit(code0)<<12)+(convertHexDigit(code1)<<8)+(convertHexDigit(code2)<<4)+convertHexDigit(code3)));
decoded=true;
}
}
} }
} }
} }
@ -560,8 +569,15 @@ public class UrlEncoded extends MultiMap<String> implements Cloneable
{ {
int code1=in.read(); int code1=in.read();
if (code1>=0) if (code1>=0)
{
buffer.append((byte)((convertHexDigit(code0)<<4)+convertHexDigit(code1))); buffer.append((byte)((convertHexDigit(code0)<<4)+convertHexDigit(code1)));
decoded=true;
}
} }
if (!decoded)
buffer.getStringBuilder().append(Utf8Appendable.REPLACEMENT);
break; break;
default: default:
@ -586,13 +602,13 @@ public class UrlEncoded extends MultiMap<String> implements Cloneable
if (key != null) if (key != null)
{ {
value = buffer.length()==0?"":buffer.toString(); value = buffer.toReplacedString();
buffer.reset(); buffer.reset();
map.add(key,value); map.add(key,value);
} }
else if (buffer.length()>0) else if (buffer.length()>0)
{ {
map.add(buffer.toString(), ""); map.add(buffer.toReplacedString(), "");
} }
} }
} }