Skip to content

Commit

Permalink
Customizable error page buffer size (#11654)
Browse files Browse the repository at this point in the history
Customizable error page buffer size

---------

Co-authored-by: Greg Wilkins <gregw@webtide.com>
  • Loading branch information
dkaukov and gregw committed Aug 29, 2024
1 parent ed1cadc commit 4755fa3
Showing 1 changed file with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class ErrorHandler implements Request.Handler
boolean _showStacks = false;
boolean _showCauses = false;
boolean _showMessageInTitle = true;
int _bufferSize = -1;
String _defaultResponseMimeType = Type.TEXT_HTML.asString();
HttpField _cacheControl = new PreEncodedHttpField(HttpHeader.CACHE_CONTROL, "must-revalidate,no-cache,no-store");

Expand Down Expand Up @@ -198,8 +199,7 @@ else if (charsets.contains(StandardCharsets.ISO_8859_1))
return false;
}

int bufferSize = request.getConnectionMetaData().getHttpConfiguration().getOutputBufferSize();
bufferSize = Math.min(8192, bufferSize); // TODO ?
int bufferSize = getBufferSize() <= 0 ? computeBufferSize(request) : getBufferSize();
ByteBufferPool byteBufferPool = request.getComponents().getByteBufferPool();
RetainableByteBuffer buffer = byteBufferPool.acquire(bufferSize, false);

Expand Down Expand Up @@ -266,6 +266,13 @@ else if (charsets.contains(StandardCharsets.ISO_8859_1))
}
}

protected int computeBufferSize(Request request)
{
int bufferSize = request.getConnectionMetaData().getHttpConfiguration().getOutputBufferSize();
bufferSize = Math.min(8192, bufferSize);
return bufferSize;
}

protected void writeErrorHtml(Request request, Writer writer, Charset charset, int code, String message, Throwable cause, boolean showStacks) throws IOException
{
if (message == null)
Expand Down Expand Up @@ -530,6 +537,25 @@ public static Request.Handler getErrorHandler(Server server, ContextHandler cont
return errorHandler;
}

/**
* @return Buffer size for entire error response. If error page is bigger than buffer size, it will be truncated.
* With a -1 meaning that a heuristic will be used (e.g. min(8K, httpConfig.bufferSize))
*/
@ManagedAttribute("Buffer size for entire error response")
public int getBufferSize()
{
return _bufferSize;
}

/**
* @param bufferSize Buffer size for entire error response. If error page is bigger than buffer size, it will be truncated.
* With a -1 meaning that a heuristic will be used (e.g. min(8K, httpConfig.bufferSize))
*/
public void setBufferSize(int bufferSize)
{
this._bufferSize = bufferSize;
}

public static class ErrorRequest extends Request.AttributesWrapper
{
private static final Set<String> ATTRIBUTES = Set.of(ERROR_MESSAGE, ERROR_EXCEPTION, ERROR_STATUS);
Expand Down

0 comments on commit 4755fa3

Please sign in to comment.