420844 Connection:close on exceptional errors

This commit is contained in:
Greg Wilkins 2013-11-01 13:37:44 +11:00
parent a76ddc1c6a
commit 56fcfa45fb
5 changed files with 31 additions and 24 deletions

View File

@ -373,9 +373,12 @@ public class HttpChannel<T> implements HttpParser.RequestHandler<T>, Runnable
{
try
{
_request.setAttribute(RequestDispatcher.ERROR_EXCEPTION,x);
_request.setAttribute(RequestDispatcher.ERROR_EXCEPTION_TYPE,x.getClass());
if (_state.isSuspended())
{
HttpFields fields = new HttpFields();
fields.add(HttpHeader.CONNECTION,HttpHeaderValue.CLOSE);
ResponseInfo info = new ResponseInfo(_request.getHttpVersion(), fields, 0, HttpStatus.INTERNAL_SERVER_ERROR_500, null, _request.isHead());
boolean committed = sendResponse(info, null, true);
if (!committed)
@ -389,8 +392,7 @@ public class HttpChannel<T> implements HttpParser.RequestHandler<T>, Runnable
}
else
{
_request.setAttribute(RequestDispatcher.ERROR_EXCEPTION,x);
_request.setAttribute(RequestDispatcher.ERROR_EXCEPTION_TYPE,x.getClass());
_response.setHeader(HttpHeader.CONNECTION.asString(),HttpHeaderValue.CLOSE.asString());
_response.sendError(500, x.getMessage());
}
}

View File

@ -448,7 +448,7 @@ public class HttpConnectionTest
offset=0;
requests=
"GET /R1?read=1&error=500 HTTP/1.1\n"+
"GET /R1?read=1&error=499 HTTP/1.1\n"+
"Host: localhost\n"+
"Transfer-Encoding: chunked\n"+
"Content-Type: text/plain; charset=utf-8\n"+
@ -468,7 +468,7 @@ public class HttpConnectionTest
response=connector.getResponses(requests);
offset = checkContains(response,offset,"HTTP/1.1 500");
offset = checkContains(response,offset,"HTTP/1.1 499");
offset = checkContains(response,offset,"HTTP/1.1 200");
offset = checkContains(response,offset,"/R2");
offset = checkContains(response,offset,"encoding=UTF-8");

View File

@ -49,6 +49,8 @@ import javax.servlet.UnavailableException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpHeaderValue;
import org.eclipse.jetty.http.PathMap;
import org.eclipse.jetty.io.EofException;
import org.eclipse.jetty.io.RuntimeIOException;
@ -492,7 +494,7 @@ public class ServletHandler extends ScopedHandler
}
LOG.debug("chain={}",chain);
Throwable th=null;
try
{
if (servlet_holder==null)
@ -540,7 +542,7 @@ public class ServletHandler extends ScopedHandler
}
// unwrap cause
Throwable th=e;
th=e;
if (th instanceof ServletException)
{
if (th instanceof QuietServletException)
@ -573,6 +575,7 @@ public class ServletHandler extends ScopedHandler
request.setAttribute(RequestDispatcher.ERROR_EXCEPTION,th);
if (!response.isCommitted())
{
baseRequest.getResponse().getHttpFields().put(HttpHeader.CONNECTION,HttpHeaderValue.CLOSE);
if (th instanceof UnavailableException)
{
UnavailableException ue = (UnavailableException)th;
@ -586,33 +589,34 @@ public class ServletHandler extends ScopedHandler
}
else
LOG.debug("Response already committed for handling "+th);
// Complete async requests
if (request.isAsyncStarted())
request.getAsyncContext().complete();
}
catch(Error e)
{
if ("ContinuationThrowable".equals(e.getClass().getSimpleName()))
throw e;
th=e;
if (!(DispatcherType.REQUEST.equals(type) || DispatcherType.ASYNC.equals(type)))
throw e;
LOG.warn("Error for "+request.getRequestURI(),e);
if(LOG.isDebugEnabled())LOG.debug(request.toString());
if(LOG.isDebugEnabled())
LOG.debug(request.toString());
request.setAttribute(RequestDispatcher.ERROR_EXCEPTION_TYPE,e.getClass());
request.setAttribute(RequestDispatcher.ERROR_EXCEPTION,e);
if (!response.isCommitted())
{
baseRequest.getResponse().getHttpFields().put(HttpHeader.CONNECTION,HttpHeaderValue.CLOSE);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
else
LOG.debug("Response already committed for handling ",e);
// Complete async requests
if (request.isAsyncStarted())
request.getAsyncContext().complete();
}
finally
{
// Complete async errored requests
if (th!=null && request.isAsyncStarted())
request.getAsyncContext().complete();
if (servlet_holder!=null)
baseRequest.setHandled(true);
}

View File

@ -239,9 +239,8 @@ public abstract class ContinuationBase
int port=_port;
String response=null;
try
try (Socket socket = new Socket("localhost",port);)
{
Socket socket = new Socket("localhost",port);
socket.setSoTimeout(10000);
socket.getOutputStream().write(request.getBytes("UTF-8"));
socket.getOutputStream().flush();
@ -269,6 +268,7 @@ public abstract class ContinuationBase
{}
/* ------------------------------------------------------------ */
@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException
{
final Continuation continuation = ContinuationSupport.getContinuation(request);
@ -306,7 +306,7 @@ public abstract class ContinuationBase
if (continuation.isInitial())
{
((HttpServletResponse)response).addHeader("history","initial");
response.addHeader("history","initial");
if (read_before>0)
{
byte[] buf=new byte[read_before];
@ -325,7 +325,7 @@ public abstract class ContinuationBase
if (suspend_for>0)
continuation.setTimeout(suspend_for);
continuation.addContinuationListener(__listener);
((HttpServletResponse)response).addHeader("history","suspend");
response.addHeader("history","suspend");
continuation.suspend(response);
if (complete_after>0)
@ -404,7 +404,7 @@ public abstract class ContinuationBase
}
else
{
((HttpServletResponse)response).addHeader("history","!initial");
response.addHeader("history","!initial");
if (suspend2_for>=0 && request.getAttribute("2nd")==null)
{
request.setAttribute("2nd","cycle");
@ -412,7 +412,7 @@ public abstract class ContinuationBase
if (suspend2_for>0)
continuation.setTimeout(suspend2_for);
// continuation.addContinuationListener(__listener);
((HttpServletResponse)response).addHeader("history","suspend");
response.addHeader("history","suspend");
continuation.suspend(response);
if (complete2_after>0)
@ -452,7 +452,7 @@ public abstract class ContinuationBase
@Override
public void run()
{
((HttpServletResponse)response).addHeader("history","resume");
response.addHeader("history","resume");
continuation.resume();
}
};
@ -463,7 +463,7 @@ public abstract class ContinuationBase
}
else if (resume2_after==0)
{
((HttpServletResponse)response).addHeader("history","resume");
response.addHeader("history","resume");
continuation.resume();
}
if (undispatch)

View File

@ -23,7 +23,7 @@ import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import junit.framework.Assert;
import org.junit.Assert;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Request;
@ -194,6 +194,7 @@ public class ContinuationTest extends ContinuationBase
class Log extends AbstractLifeCycle implements RequestLog
{
@Override
public void log(Request request, Response response)
{
_log.add(response.getStatus()+" "+response.getContentCount()+" "+request.getRequestURI());