JETTY-1324 Tested not using CESU-8 instead of UTF-8

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@3099 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Greg Wilkins 2011-05-09 07:55:48 +00:00
parent 6f8c330015
commit 1b26b47caa
5 changed files with 176 additions and 208 deletions

View File

@ -16,6 +16,7 @@ jetty-7.4.1-SNAPSHOT
+ JETTY-1326 Invoker names not hashCode based
+ JETTY-1343 IllegalArgumentException for bad % encodings
+ JETTY-1347 Updated ServletHander javadoc
+ JETTY-1324 Tested not using CESU-8 instead of UTF-8
jetty-7.4.0.v20110414
+ 342504 Scanner Listener

View File

@ -15,6 +15,7 @@ import org.eclipse.jetty.io.ByteArrayEndPoint;
import org.eclipse.jetty.io.SimpleBuffers;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.TypeUtil;
import org.eclipse.jetty.util.Utf8StringBuilder;
import org.junit.Before;
import org.junit.Test;
@ -93,6 +94,22 @@ public class HttpWriterTest
_writer.write("How now \uFF22rown cow");
assertArrayEquals("How now \uFF22rown cow".getBytes(StringUtil.__UTF8),_bytes.asArray());
}
@Test
public void testNotCESU8() throws Exception
{
_writer.setCharacterEncoding(StringUtil.__UTF8);
String data="xxx\uD801\uDC00xxx";
_writer.write(data);
assertEquals("787878F0909080787878",TypeUtil.toHexString(_bytes.asArray()));
assertArrayEquals(data.getBytes(StringUtil.__UTF8),_bytes.asArray());
assertEquals(3+4+3,_bytes.length());
Utf8StringBuilder buf = new Utf8StringBuilder();
buf.append(_bytes.asArray(),0,_bytes.length());
assertEquals(data,buf.toString());
}
@Test
public void testMultiByteOverflowUTF8() throws Exception

View File

@ -0,0 +1,142 @@
package org.eclipse.jetty.util;
import java.io.IOException;
public abstract class Utf8Appendable
{
protected final Appendable _appendable;
protected int _more;
protected int _bits;
public Utf8Appendable(Appendable appendable)
{
_appendable=appendable;
}
public abstract int length();
public void append(byte b)
{
try
{
appendByte(b);
}
catch(IOException e)
{
throw new RuntimeException(e);
}
}
public void append(byte[] b,int offset, int length)
{
try
{
int end=offset+length;
for (int i=offset; i<end;i++)
appendByte(b[i]);
}
catch(IOException e)
{
throw new RuntimeException(e);
}
}
public boolean append(byte[] b,int offset, int length, int maxChars)
{
try
{
int end=offset+length;
for (int i=offset; i<end;i++)
{
if (length()>maxChars)
return false;
appendByte(b[i]);
}
return true;
}
catch(IOException e)
{
throw new RuntimeException(e);
}
}
protected void appendByte(byte b) throws IOException
{
if (b>=0)
{
if (_more>0)
{
_appendable.append('?');
_more=0;
_bits=0;
}
else
_appendable.append((char)(0x7f&b));
}
else if (_more==0)
{
if ((b&0xc0)!=0xc0)
{
// 10xxxxxx
_appendable.append('?');
_more=0;
_bits=0;
}
else
{
if ((b & 0xe0) == 0xc0)
{
//110xxxxx
_more=1;
_bits=b&0x1f;
}
else if ((b & 0xf0) == 0xe0)
{
//1110xxxx
_more=2;
_bits=b&0x0f;
}
else if ((b & 0xf8) == 0xf0)
{
//11110xxx
_more=3;
_bits=b&0x07;
}
else if ((b & 0xfc) == 0xf8)
{
//111110xx
_more=4;
_bits=b&0x03;
}
else if ((b & 0xfe) == 0xfc)
{
//1111110x
_more=5;
_bits=b&0x01;
}
else
{
throw new IllegalArgumentException("!utf8");
}
}
}
else
{
if ((b&0xc0)==0xc0)
{ // 11??????
_appendable.append('?');
_more=0;
_bits=0;
throw new IllegalArgumentException("!utf8");
}
else
{
// 10xxxxxx
_bits=(_bits<<6)|(b&0x3f);
if (--_more==0)
_appendable.append(new String(Character.toChars(_bits)));
}
}
}
}

View File

@ -13,6 +13,8 @@
package org.eclipse.jetty.util;
import java.io.IOException;
/* ------------------------------------------------------------ */
/** UTF-8 StringBuffer.
*
@ -27,110 +29,22 @@ package org.eclipse.jetty.util;
*
* This class is not synchronised and should probably be called Utf8StringBuilder
*/
public class Utf8StringBuffer
public class Utf8StringBuffer extends Utf8Appendable
{
StringBuffer _buffer;
int _more;
int _bits;
final StringBuffer _buffer;
public Utf8StringBuffer()
{
_buffer=new StringBuffer();
super(new StringBuffer());
_buffer=(StringBuffer)_appendable;
}
public Utf8StringBuffer(int capacity)
{
_buffer=new StringBuffer(capacity);
super(new StringBuffer(capacity));
_buffer=(StringBuffer)_appendable;
}
public void append(byte[] b,int offset, int length)
{
int end=offset+length;
for (int i=offset; i<end;i++)
append(b[i]);
}
public void append(byte b)
{
if (b>=0)
{
if (_more>0)
{
_buffer.append('?');
_more=0;
_bits=0;
}
else
_buffer.append((char)(0x7f&b));
}
else if (_more==0)
{
if ((b&0xc0)!=0xc0)
{
// 10xxxxxx
_buffer.append('?');
_more=0;
_bits=0;
}
else
{
if ((b & 0xe0) == 0xc0)
{
//110xxxxx
_more=1;
_bits=b&0x1f;
}
else if ((b & 0xf0) == 0xe0)
{
//1110xxxx
_more=2;
_bits=b&0x0f;
}
else if ((b & 0xf8) == 0xf0)
{
//11110xxx
_more=3;
_bits=b&0x07;
}
else if ((b & 0xfc) == 0xf8)
{
//111110xx
_more=4;
_bits=b&0x03;
}
else if ((b & 0xfe) == 0xfc)
{
//1111110x
_more=5;
_bits=b&0x01;
}
else
{
throw new IllegalArgumentException("!utf8");
}
}
}
else
{
if ((b&0xc0)==0xc0)
{ // 11??????
_buffer.append('?');
_more=0;
_bits=0;
throw new IllegalArgumentException("!utf8");
}
else
{
// 10xxxxxx
_bits=(_bits<<6)|(b&0x3f);
if (--_more==0)
_buffer.append(Character.toChars(_bits));
}
}
}
public int length()
{
return _buffer.length();

View File

@ -13,6 +13,8 @@
package org.eclipse.jetty.util;
import java.io.IOException;
/* ------------------------------------------------------------ */
/** UTF-8 StringBuilder.
*
@ -26,128 +28,20 @@ package org.eclipse.jetty.util;
* The UTF-8 code was inspired by http://javolution.org
*
*/
public class Utf8StringBuilder
public class Utf8StringBuilder extends Utf8Appendable
{
StringBuilder _buffer;
int _more;
int _bits;
final StringBuilder _buffer;
public Utf8StringBuilder()
{
_buffer=new StringBuilder();
super(new StringBuilder());
_buffer=(StringBuilder)_appendable;
}
public Utf8StringBuilder(int capacity)
{
_buffer=new StringBuilder(capacity);
}
public void append(byte[] b,int offset, int length)
{
int end=offset+length;
for (int i=offset; i<end;i++)
append(b[i]);
}
/**
* @param b
* @param offset
* @param length
* @param maxChars The maximum characters allowed in the builder
* @return true if all bytes appended
*/
public boolean append(byte[] b,int offset, int length, int maxChars)
{
int end=offset+length;
for (int i=offset; i<end;i++)
{
if (length()>maxChars)
return false;
append(b[i]);
}
return true;
}
public void append(byte b)
{
if (b>=0)
{
if (_more>0)
{
_buffer.append('?');
_more=0;
_bits=0;
}
else
_buffer.append((char)(0x7f&b));
}
else if (_more==0)
{
if ((b&0xc0)!=0xc0)
{
// 10xxxxxx
_buffer.append('?');
_more=0;
_bits=0;
}
else
{
if ((b & 0xe0) == 0xc0)
{
//110xxxxx
_more=1;
_bits=b&0x1f;
}
else if ((b & 0xf0) == 0xe0)
{
//1110xxxx
_more=2;
_bits=b&0x0f;
}
else if ((b & 0xf8) == 0xf0)
{
//11110xxx
_more=3;
_bits=b&0x07;
}
else if ((b & 0xfc) == 0xf8)
{
//111110xx
_more=4;
_bits=b&0x03;
}
else if ((b & 0xfe) == 0xfc)
{
//1111110x
_more=5;
_bits=b&0x01;
}
else
{
throw new IllegalArgumentException("!utf8");
}
}
}
else
{
if ((b&0xc0)==0xc0)
{ // 11??????
_buffer.append('?');
_more=0;
_bits=0;
throw new IllegalArgumentException("!utf8");
}
else
{
// 10xxxxxx
_bits=(_bits<<6)|(b&0x3f);
if (--_more==0)
{
// _buffer.append((char)_bits);
_buffer.append(Character.toChars(_bits));
}
}
}
super(new StringBuilder(capacity));
_buffer=(StringBuilder)_appendable;
}
public int length()