Issue #355 Improve close behaviour

Inverted the logic of the handling of exceptions thrown in CommitCallback.
Now only if a BadMessageException is throw do we attempt to write a 500 response.
All other exceptions just abort the connection
This commit is contained in:
Greg Wilkins 2016-02-24 13:36:06 +01:00
parent c3a54e9d0b
commit 1eeecdaee5
3 changed files with 20 additions and 14 deletions

View File

@ -19,6 +19,12 @@
package org.eclipse.jetty.http;
/* ------------------------------------------------------------------------------- */
/**
* <p>Exception thrown to indicate a Bad HTTP Message has either been received
* or attempted to be generated. Typically these are handled with either 400
* or 500 responses.</p>
*/
@SuppressWarnings("serial")
public class BadMessageException extends RuntimeException
{
final int _code;

View File

@ -228,7 +228,7 @@ public class HttpGenerator
generateRequestLine(info,header);
if (info.getVersion()==HttpVersion.HTTP_0_9)
throw new IllegalArgumentException("HTTP/0.9 not supported");
throw new BadMessageException(500,"HTTP/0.9 not supported");
generateHeaders(info,header,content,last);
@ -256,7 +256,7 @@ public class HttpGenerator
catch(Exception e)
{
String message= (e instanceof BufferOverflowException)?"Request header too large":e.getMessage();
throw new IOException(message,e);
throw new BadMessageException(500,message,e);
}
finally
{
@ -345,7 +345,7 @@ public class HttpGenerator
return Result.NEED_INFO;
HttpVersion version=info.getVersion();
if (version==null)
throw new IllegalStateException("No version");
throw new BadMessageException(500,"No version");
switch(version)
{
case HTTP_1_0:
@ -411,7 +411,7 @@ public class HttpGenerator
catch(Exception e)
{
String message= (e instanceof BufferOverflowException)?"Response header too large":e.getMessage();
throw new IOException(message,e);
throw new BadMessageException(500,message,e);
}
finally
{
@ -762,7 +762,7 @@ public class HttpGenerator
long actual_length = _contentPrepared+BufferUtil.length(content);
if (content_length>=0 && content_length!=actual_length)
throw new IllegalArgumentException("Content-Length header("+content_length+") != actual("+actual_length+")");
throw new BadMessageException(500,"Content-Length header("+content_length+") != actual("+actual_length+")");
// Do we need to tell the headers about it
putContentLength(header,actual_length,content_type,request,response);
@ -787,7 +787,7 @@ public class HttpGenerator
}
case NO_CONTENT:
throw new IllegalStateException();
throw new BadMessageException(500);
case EOF_CONTENT:
_persistent = request!=null;
@ -810,7 +810,7 @@ public class HttpGenerator
if (c.endsWith(HttpHeaderValue.CHUNKED.toString()))
putTo(transfer_encoding,header);
else
throw new IllegalArgumentException("BAD TE");
throw new BadMessageException(500,"BAD TE");
}
else
header.put(TRANSFER_ENCODING_CHUNKED);

View File

@ -785,12 +785,7 @@ public class HttpChannel implements Runnable, HttpOutput.Interceptor
if (LOG.isDebugEnabled())
LOG.debug("Commit failed", x);
if (x instanceof EofException || x instanceof ClosedChannelException)
{
_callback.failed(x);
_response.getHttpOutput().closed();
}
else
if (x instanceof BadMessageException)
{
_transport.send(HttpGenerator.RESPONSE_500_INFO, false, null, true, new Callback()
{
@ -804,11 +799,16 @@ public class HttpChannel implements Runnable, HttpOutput.Interceptor
@Override
public void failed(Throwable th)
{
_transport.abort(x);
_callback.failed(x);
_response.getHttpOutput().closed();
}
});
}
else
{
_transport.abort(x);
_callback.failed(x);
}
}
}