Skip to content

Commit

Permalink
431094 Consistent handling of utf8 decoding errors
Browse files Browse the repository at this point in the history
  • Loading branch information
gregw committed Apr 10, 2014
1 parent 7adba8d commit 866960d
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions jetty-util/src/main/java/org/eclipse/jetty/util/UrlEncoded.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public static void decodeUtf8To(byte[] raw,int offset, int length, MultiMap<Stri
switch ((char)(0xff&b))
{
case '&':
value = buffer.length()==0?"":buffer.toString();
value = buffer.toReplacedString();
buffer.reset();
if (key != null)
{
Expand All @@ -314,7 +314,7 @@ else if (value!=null&&value.length()>0)
buffer.append(b);
break;
}
key = buffer.toString();
key = buffer.toReplacedString();
buffer.reset();
break;

Expand Down Expand Up @@ -376,7 +376,7 @@ else if (value!=null&&value.length()>0)

if (key != null)
{
value = buffer.length()==0?"":buffer.toReplacedString();
value = buffer.toReplacedString();
buffer.reset();
map.add(key,value);
}
Expand Down Expand Up @@ -510,7 +510,7 @@ public static void decodeUtf8To(InputStream in, MultiMap<String> map, int maxLen
switch ((char) b)
{
case '&':
value = buffer.length()==0?"":buffer.toString();
value = buffer.toReplacedString();
buffer.reset();
if (key != null)
{
Expand All @@ -532,7 +532,7 @@ else if (value!=null&&value.length()>0)
buffer.append((byte)b);
break;
}
key = buffer.toString();
key = buffer.toReplacedString();
buffer.reset();
break;

Expand All @@ -542,26 +542,42 @@ else if (value!=null&&value.length()>0)

case '%':
int code0=in.read();
boolean decoded=false;
if ('u'==code0)
{
int code1=in.read();
if (code1>=0)
code0=in.read(); // XXX: we have to read the next byte, otherwise code0 is always 'u'
if (code0>=0)
{
int code2=in.read();
if (code2>=0)
int code1=in.read();
if (code1>=0)
{
int code3=in.read();
if (code3>=0)
buffer.getStringBuilder().append(Character.toChars((convertHexDigit(code0)<<12)+(convertHexDigit(code1)<<8)+(convertHexDigit(code2)<<4)+convertHexDigit(code3)));
int code2=in.read();
if (code2>=0)
{
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;
}
}
}
}
}
else if (code0>=0)
{
int code1=in.read();
if (code1>=0)
{
buffer.append((byte)((convertHexDigit(code0)<<4)+convertHexDigit(code1)));
decoded=true;
}
}

if (!decoded)
buffer.getStringBuilder().append(Utf8Appendable.REPLACEMENT);

break;

default:
Expand All @@ -586,13 +602,13 @@ else if (code0>=0)

if (key != null)
{
value = buffer.length()==0?"":buffer.toString();
value = buffer.toReplacedString();
buffer.reset();
map.add(key,value);
}
else if (buffer.length()>0)
{
map.add(buffer.toString(), "");
map.add(buffer.toReplacedString(), "");
}
}
}
Expand Down

0 comments on commit 866960d

Please sign in to comment.