JETTY-1066 283357 Send 400 error for request URI parse exceptions

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@555 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Greg Wilkins 2009-07-14 23:13:26 +00:00
parent f62c12800d
commit c2196f51d6
8 changed files with 164 additions and 32 deletions

View File

@ -1,4 +1,6 @@
+ JETTY-1066 283357 400 response for bad URIs
jetty-7.0.0.RC0 8 June 2009
+ JETTY-967 create standalone build for PKCS12Import at codehaus
+ JETTY-1056 update jetty-ant module for Jetty 7 at codehaus trunk

View File

@ -61,6 +61,7 @@ public class HttpURI
int _authority;
int _host;
int _port;
int _portValue;
int _path;
int _param;
int _query;
@ -122,6 +123,7 @@ public class HttpURI
_authority=offset;
_host=offset;
_port=offset;
_portValue=-1;
_path=offset;
_param=_end;
_query=_end;
@ -164,7 +166,7 @@ public class HttpURI
if (Character.isLetterOrDigit(c))
state=SCHEME_OR_PATH;
else
throw new IllegalArgumentException(StringUtil.toString(_raw,offset,length,URIUtil.__CHARSET));
throw new IllegalArgumentException("!(SCHEME|PATH|AUTH):"+StringUtil.toString(_raw,offset,length,URIUtil.__CHARSET));
}
continue;
@ -401,6 +403,9 @@ public class HttpURI
}
}
}
if (_port<_path)
_portValue=TypeUtil.parseInt(_raw, _port+1, _path-_port-1,10);
}
private String toUtf8String(int offset,int length)
@ -448,9 +453,7 @@ public class HttpURI
public int getPort()
{
if (_port==_path)
return -1;
return TypeUtil.parseInt(_raw, _port+1, _path-_port-1,10);
return _portValue;
}
public String getPath()
@ -473,8 +476,10 @@ public class HttpURI
{
byte b = _raw[i];
if (b=='%' && (i+2)<_param)
if (b=='%')
{
if ((i+2)>=_param)
throw new IllegalArgumentException("Bad % encoding: "+this);
b=(byte)(0xff&TypeUtil.parseInt(_raw,i+1,2,16));
i+=2;
}

View File

@ -65,9 +65,6 @@ import org.eclipse.jetty.util.thread.Timeout;
* with the connection via the parser and/or generator.
* </p>
*
*
*
*
*/
public class HttpConnection implements Connection
{
@ -544,9 +541,12 @@ public class HttpConnection implements Connection
while (handling)
{
_request.setHandled(false);
String info=null;
try
{
String info=URIUtil.canonicalPath(_uri.getDecodedPath());
_uri.getPort();
info=URIUtil.canonicalPath(_uri.getDecodedPath());
if (info==null)
throw new HttpException(400);
_request.setPathInfo(info);
@ -582,19 +582,24 @@ public class HttpConnection implements Connection
_response.sendError(e.getStatus(), e.getReason());
error=true;
}
catch (Exception e)
catch (Throwable e)
{
Log.warn(e);
_request.setHandled(true);
_generator.sendError(500, null, null, true);
error=true;
}
catch (Error e)
{
Log.warn(e);
_request.setHandled(true);
_generator.sendError(500, null, null, true);
if (e instanceof ThreadDeath)
throw (ThreadDeath)e;
error=true;
if (info==null)
{
Log.warn(_uri+": "+e);
_request.setHandled(true);
_generator.sendError(400, null, null, true);
}
else
{
Log.warn(""+_uri,e);
_request.setHandled(true);
_generator.sendError(500, null, null, true);
}
}
finally
{
@ -781,7 +786,8 @@ public class HttpConnection implements Connection
}
catch (Exception e)
{
Log.warn(e);
Log.warn(method+" "+uri+" "+version+": "+e);
Log.debug(e);
throw new HttpException(HttpStatus.BAD_REQUEST_400,null,e);
}
}

View File

@ -121,6 +121,40 @@ public class HttpConnectionTest extends TestCase
offset = checkContains(response,offset,"/R1");
}
/* --------------------------------------------------------------- */
public void testBad() throws Exception
{
String response=connector.getResponses("GET & HTTP/1.1\n"+
"Host: localhost\n"+
"\015\012");
checkContains(response,0,"HTTP/1.1 400");
connector.reopen();
response=connector.getResponses("GET http://localhost:WRONG/ HTTP/1.1\n"+
"Host: localhost\n"+
"\015\012");
checkContains(response,0,"HTTP/1.1 400");
connector.reopen();
response=connector.getResponses("GET /foo/bar%1 HTTP/1.1\n"+
"Host: localhost\n"+
"\015\012");
checkContains(response,0,"HTTP/1.1 400");
connector.reopen();
response=connector.getResponses("GET /foo/bar%c0%00 HTTP/1.1\n"+
"Host: localhost\n"+
"\015\012");
checkContains(response,0,"HTTP/1.1 400");
connector.reopen();
response=connector.getResponses("GET /foo/bar%c1 HTTP/1.1\n"+
"Host: localhost\n"+
"\015\012");
checkContains(response,0,"HTTP/1.1 400");
}
/* --------------------------------------------------------------- */
public void testAutoFlush() throws Exception
{

View File

@ -107,11 +107,11 @@ public class Utf8StringBuffer
}
else
{
throw new IllegalArgumentException();
throw new IllegalArgumentException("!utf8");
}
if (_bits==0)
throw new IllegalArgumentException("non-shortest UTF-8 form");
throw new IllegalArgumentException("!utf8");
}
}
else
@ -121,7 +121,7 @@ public class Utf8StringBuffer
_buffer.append('?');
_more=0;
_bits=0;
throw new IllegalArgumentException();
throw new IllegalArgumentException("!utf8");
}
else
{
@ -148,14 +148,14 @@ public class Utf8StringBuffer
public StringBuffer getStringBuffer()
{
if (_more!=0)
throw new IllegalStateException();
throw new IllegalStateException("!utf8");
return _buffer;
}
public String toString()
{
if (_more!=0)
throw new IllegalStateException();
throw new IllegalStateException("!utf8");
return _buffer.toString();
}
}

View File

@ -72,7 +72,6 @@ public class Utf8StringBuilder
_bits=0;
}
else
{
if ((b & 0xe0) == 0xc0)
{
@ -106,11 +105,11 @@ public class Utf8StringBuilder
}
else
{
throw new IllegalArgumentException();
throw new IllegalArgumentException("!utf8");
}
if (_bits==0)
throw new IllegalArgumentException("non-shortest UTF-8 form");
throw new IllegalArgumentException("!utf8");
}
}
else
@ -120,7 +119,7 @@ public class Utf8StringBuilder
_buffer.append('?');
_more=0;
_bits=0;
throw new IllegalArgumentException();
throw new IllegalArgumentException("!utf8");
}
else
{
@ -147,14 +146,14 @@ public class Utf8StringBuilder
public StringBuilder getStringBuilder()
{
if (_more!=0)
throw new IllegalStateException();
throw new IllegalStateException("!utf8");
return _buffer;
}
public String toString()
{
if (_more!=0)
throw new IllegalStateException();
throw new IllegalStateException("!utf8");
return _buffer.toString();
}
}

View File

@ -29,4 +29,47 @@ public class Utf8StringBufferTest extends junit.framework.TestCase
assertTrue(buffer.toString().endsWith("jetty"));
}
public void testShort()
throws Exception
{
String source="abc\u10fb";
byte[] bytes = source.getBytes(StringUtil.__UTF8);
Utf8StringBuffer buffer = new Utf8StringBuffer();
for (int i=0;i<bytes.length-1;i++)
buffer.append(bytes[i]);
try
{
buffer.toString();
assertTrue(false);
}
catch(IllegalStateException e)
{
assertTrue(e.toString().indexOf("!utf8")>=0);
}
}
public void testLong()
throws Exception
{
String source="abcXX";
byte[] bytes = source.getBytes(StringUtil.__UTF8);
bytes[3]=(byte)0xc0;
bytes[4]=(byte)0x00;
Utf8StringBuffer buffer = new Utf8StringBuffer();
try
{
for (int i=0;i<bytes.length;i++)
buffer.append(bytes[i]);
buffer.toString();
assertTrue(false);
}
catch(Exception e)
{
assertTrue(e.toString().indexOf("!utf8")>=0);
}
}
}

View File

@ -28,5 +28,48 @@ public class Utf8StringBuilderTest extends junit.framework.TestCase
assertEquals(source, buffer.toString());
assertTrue(buffer.toString().endsWith("jetty"));
}
public void testShort()
throws Exception
{
String source="abc\u10fb";
byte[] bytes = source.getBytes(StringUtil.__UTF8);
Utf8StringBuilder buffer = new Utf8StringBuilder();
for (int i=0;i<bytes.length-1;i++)
buffer.append(bytes[i]);
try
{
buffer.toString();
assertTrue(false);
}
catch(IllegalStateException e)
{
assertTrue(e.toString().indexOf("!utf8")>=0);
}
}
public void testLong()
throws Exception
{
String source="abcXX";
byte[] bytes = source.getBytes(StringUtil.__UTF8);
bytes[3]=(byte)0xc0;
bytes[4]=(byte)0x00;
Utf8StringBuilder buffer = new Utf8StringBuilder();
try
{
for (int i=0;i<bytes.length;i++)
buffer.append(bytes[i]);
buffer.toString();
assertTrue(false);
}
catch(Exception e)
{
assertTrue(e.toString().indexOf("!utf8")>=0);
}
}
}