Merge remote-tracking branch 'origin/jetty-9.3.x'

This commit is contained in:
Greg Wilkins 2016-02-16 16:03:34 +01:00
commit 779317dc30
2 changed files with 100 additions and 121 deletions

View File

@ -35,9 +35,7 @@ import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.server.AsyncContextEvent;
import org.eclipse.jetty.server.Dispatcher;
import org.eclipse.jetty.server.HttpOutput;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.server.Server;
@ -47,23 +45,27 @@ import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
/**
* <p>Component that handles Error Pages.</p>
* <p>An ErrorHandler is registered with {@link ContextHandler#setErrorHandler(ErrorHandler)} or
* {@link org.eclipse.jetty.server.Server#addBean(Object)}.</p>
* <p>It is called by {@link HttpServletResponse#sendError(int)} to write an error page via
* {@link #handle(String, Request, HttpServletRequest, HttpServletResponse)}
* or via {@link #badMessageError(int, String, HttpFields)} for bad requests for which a
* dispatch cannot be done.</p>
/* ------------------------------------------------------------ */
/** Handler for Error pages
* An ErrorHandler is registered with {@link ContextHandler#setErrorHandler(ErrorHandler)} or
* {@link org.eclipse.jetty.server.Server#addBean(Object)}.
* It is called by the HttpResponse.sendError method to write a error page via {@link #handle(String, Request, HttpServletRequest, HttpServletResponse)}
* or via {@link #badMessageError(int, String, HttpFields)} for bad requests for which a dispatch cannot be done.
*
*/
public class ErrorHandler extends AbstractHandler
{
{
private static final Logger LOG = Log.getLogger(ErrorHandler.class);
public final static String ERROR_PAGE="org.eclipse.jetty.server.error_page";
boolean _showStacks=true;
boolean _showMessageInTitle=true;
String _cacheControl="must-revalidate,no-cache,no-store";
private boolean _showStacks = true;
private boolean _showMessageInTitle = true;
private String _cacheControl = "must-revalidate,no-cache,no-store";
/* ------------------------------------------------------------ */
/*
* @see org.eclipse.jetty.server.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
*/
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException
{
@ -73,31 +75,31 @@ public class ErrorHandler extends AbstractHandler
baseRequest.setHandled(true);
return;
}
if (this instanceof ErrorPageMapper)
{
String error_page = ((ErrorPageMapper)this).getErrorPage(request);
ServletContext context = request.getServletContext();
if (context == null)
String error_page=((ErrorPageMapper)this).getErrorPage(request);
if (error_page!=null && request.getServletContext()!=null)
{
AsyncContextEvent event = baseRequest.getHttpChannelState().getAsyncContextEvent();
context = event == null ? null : event.getServletContext();
}
if (error_page != null && context != null)
{
Dispatcher dispatcher = (Dispatcher)context.getRequestDispatcher(error_page);
if (dispatcher != null)
String old_error_page=(String)request.getAttribute(ERROR_PAGE);
if (old_error_page==null || !old_error_page.equals(error_page))
{
request.setAttribute(ERROR_PAGE, error_page);
Dispatcher dispatcher = (Dispatcher) request.getServletContext().getRequestDispatcher(error_page);
try
{
dispatcher.error(request, response);
return;
if(dispatcher!=null)
{
dispatcher.error(request, response);
return;
}
LOG.warn("No error page "+error_page);
}
catch (ServletException x)
catch (ServletException e)
{
throw new IOException(x);
LOG.warn(Log.EXCEPTION, e);
return;
}
}
} else {
@ -105,124 +107,112 @@ public class ErrorHandler extends AbstractHandler
{
LOG.debug("No Error Page mapping for request({} {}) (using default)",request.getMethod(),request.getRequestURI());
}
else
{
LOG.warn("Could not dispatch to error page: {}", error_page);
// Fall through to provide the default error page.
}
}
}
baseRequest.setHandled(true);
HttpOutput out = baseRequest.getResponse().getHttpOutput();
if (!out.isAsync())
{
response.setContentType(MimeTypes.Type.TEXT_HTML_8859_1.asString());
String cacheHeader = getCacheControl();
if (cacheHeader != null)
response.setHeader(HttpHeader.CACHE_CONTROL.asString(), cacheHeader);
ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(4096);
String reason = (response instanceof Response) ? ((Response)response).getReason() : null;
handleErrorPage(request, writer, response.getStatus(), reason);
writer.flush();
response.setContentLength(writer.size());
writer.writeTo(response.getOutputStream());
writer.destroy();
}
baseRequest.setHandled(true);
response.setContentType(MimeTypes.Type.TEXT_HTML_8859_1.asString());
if (_cacheControl!=null)
response.setHeader(HttpHeader.CACHE_CONTROL.asString(), _cacheControl);
ByteArrayISO8859Writer writer= new ByteArrayISO8859Writer(4096);
String reason=(response instanceof Response)?((Response)response).getReason():null;
handleErrorPage(request, writer, response.getStatus(), reason);
writer.flush();
response.setContentLength(writer.size());
writer.writeTo(response.getOutputStream());
writer.destroy();
}
/* ------------------------------------------------------------ */
protected void handleErrorPage(HttpServletRequest request, Writer writer, int code, String message)
throws IOException
throws IOException
{
writeErrorPage(request, writer, code, message, isShowStacks());
writeErrorPage(request, writer, code, message, _showStacks);
}
/* ------------------------------------------------------------ */
protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks)
throws IOException
throws IOException
{
if (message == null)
message = HttpStatus.getMessage(code);
message=HttpStatus.getMessage(code);
writer.write("<html>\n<head>\n");
writeErrorPageHead(request, writer, code, message);
writeErrorPageHead(request,writer,code,message);
writer.write("</head>\n<body>");
writeErrorPageBody(request, writer, code, message, showStacks);
writeErrorPageBody(request,writer,code,message,showStacks);
writer.write("\n</body>\n</html>\n");
}
/* ------------------------------------------------------------ */
protected void writeErrorPageHead(HttpServletRequest request, Writer writer, int code, String message)
throws IOException
{
throws IOException
{
writer.write("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"/>\n");
writer.write("<title>Error ");
writer.write(Integer.toString(code));
if (getShowMessageInTitle())
if (_showMessageInTitle)
{
writer.write(' ');
write(writer, message);
write(writer,message);
}
writer.write("</title>\n");
}
/* ------------------------------------------------------------ */
protected void writeErrorPageBody(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks)
throws IOException
throws IOException
{
String uri = request.getRequestURI();
String uri= request.getRequestURI();
writeErrorPageMessage(request, writer, code, message, uri);
writeErrorPageMessage(request,writer,code,message,uri);
if (showStacks)
writeErrorPageStacks(request, writer);
writeErrorPageStacks(request,writer);
Request.getBaseRequest(request).getHttpChannel().getHttpConfiguration()
.writePoweredBy(writer, "<hr>", "<hr/>\n");
.writePoweredBy(writer,"<hr>","<hr/>\n");
}
/* ------------------------------------------------------------ */
protected void writeErrorPageMessage(HttpServletRequest request, Writer writer, int code, String message, String uri)
throws IOException
protected void writeErrorPageMessage(HttpServletRequest request, Writer writer, int code, String message,String uri)
throws IOException
{
writer.write("<h2>HTTP ERROR ");
writer.write(Integer.toString(code));
writer.write("</h2>\n<p>Problem accessing ");
write(writer, uri);
write(writer,uri);
writer.write(". Reason:\n<pre> ");
write(writer, message);
write(writer,message);
writer.write("</pre></p>");
}
/* ------------------------------------------------------------ */
protected void writeErrorPageStacks(HttpServletRequest request, Writer writer)
throws IOException
throws IOException
{
Throwable th = (Throwable)request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
while (th != null)
while(th!=null)
{
writer.write("<h3>Caused by:</h3><pre>");
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
th.printStackTrace(pw);
pw.flush();
write(writer, sw.getBuffer().toString());
write(writer,sw.getBuffer().toString());
writer.write("</pre>\n");
th = th.getCause();
th =th.getCause();
}
}
/* ------------------------------------------------------------ */
/**
* <p>Generate a error response body to be sent for a bad message.</p>
* <p>In this case there is something wrong with the request, so either
/** Bad Message Error body
* <p>Generate a error response body to be sent for a bad message.
* In this case there is something wrong with the request, so either
* a request cannot be built, or it is not safe to build a request.
* This method allows for a simple error page body to be returned
* and some response headers to be set.</p>
*
* This method allows for a simple error page body to be returned
* and some response headers to be set.
* @param status The error code that will be sent
* @param reason The reason for the error code (may be null)
* @param fields The header fields that will be sent with the response.
@ -230,14 +220,14 @@ public class ErrorHandler extends AbstractHandler
*/
public ByteBuffer badMessageError(int status, String reason, HttpFields fields)
{
if (reason == null)
reason = HttpStatus.getMessage(status);
fields.put(HttpHeader.CONTENT_TYPE, MimeTypes.Type.TEXT_HTML_8859_1.asString());
if (reason==null)
reason=HttpStatus.getMessage(status);
fields.put(HttpHeader.CONTENT_TYPE,MimeTypes.Type.TEXT_HTML_8859_1.asString());
return BufferUtil.toBuffer("<h1>Bad Message " + status + "</h1><pre>reason: " + reason + "</pre>");
}
}
/* ------------------------------------------------------------ */
/**
/** Get the cacheControl.
* @return the cacheControl header to set on error responses.
*/
public String getCacheControl()
@ -246,7 +236,7 @@ public class ErrorHandler extends AbstractHandler
}
/* ------------------------------------------------------------ */
/**
/** Set the cacheControl.
* @param cacheControl the cacheControl header to set on error responses.
*/
public void setCacheControl(String cacheControl)
@ -256,7 +246,7 @@ public class ErrorHandler extends AbstractHandler
/* ------------------------------------------------------------ */
/**
* @return whether stack traces are shown in the error pages
* @return True if stack traces are shown in the error pages
*/
public boolean isShowStacks()
{
@ -265,7 +255,7 @@ public class ErrorHandler extends AbstractHandler
/* ------------------------------------------------------------ */
/**
* @param showStacks whether stack traces are shown in the error pages
* @param showStacks True if stack traces are shown in the error pages
*/
public void setShowStacks(boolean showStacks)
{
@ -274,27 +264,25 @@ public class ErrorHandler extends AbstractHandler
/* ------------------------------------------------------------ */
/**
* @return whether the error message appears in page title
*/
public boolean getShowMessageInTitle()
{
return _showMessageInTitle;
}
/* ------------------------------------------------------------ */
/**
* @param showMessageInTitle whether the error message appears in page title
* @param showMessageInTitle if true, the error message appears in page title
*/
public void setShowMessageInTitle(boolean showMessageInTitle)
{
_showMessageInTitle = showMessageInTitle;
}
/* ------------------------------------------------------------ */
protected void write(Writer writer, String string)
throws IOException
public boolean getShowMessageInTitle()
{
if (string == null)
return _showMessageInTitle;
}
/* ------------------------------------------------------------ */
protected void write(Writer writer,String string)
throws IOException
{
if (string==null)
return;
writer.write(StringUtil.sanitizeXmlString(string));
@ -309,22 +297,11 @@ public class ErrorHandler extends AbstractHandler
/* ------------------------------------------------------------ */
public static ErrorHandler getErrorHandler(Server server, ContextHandler context)
{
ErrorHandler error_handler = null;
if (context != null)
error_handler = context.getErrorHandler();
if (error_handler == null)
{
synchronized (ErrorHandler.class)
{
error_handler = server.getBean(ErrorHandler.class);
if (error_handler == null)
{
error_handler = new ErrorHandler();
error_handler.setServer(server);
server.addManaged(error_handler);
}
}
}
ErrorHandler error_handler=null;
if (context!=null)
error_handler=context.getErrorHandler();
if (error_handler==null && server!=null)
error_handler = server.getBean(ErrorHandler.class);
return error_handler;
}
}

View File

@ -43,6 +43,7 @@ import org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainer
import org.eclipse.jetty.websocket.server.WebSocketServerFactory;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
public class SessionTrackingTest
@ -129,6 +130,7 @@ public class SessionTrackingTest
}
@Test
@Ignore
public void testAddRemoveSessions() throws Exception
{
// Create Client