Skip to content

Commit

Permalink
Issue #85 - fixes to the CoreSizeLimitHandler
Browse files Browse the repository at this point in the history
Signed-off-by: Lachlan Roberts <[email protected]>
  • Loading branch information
lachlan-roberts committed Dec 12, 2023
1 parent 851f7c6 commit abb6733
Showing 1 changed file with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.MetaData;
import org.eclipse.jetty.io.Content;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.HttpStream;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.Callback;

/**
Expand All @@ -46,8 +46,6 @@ public class CoreSizeLimitHandler extends Handler.Wrapper
{
private final long _requestLimit;
private final long _responseLimit;
private long _read = 0;
private long _written = 0;

/**
* @param requestLimit The request body size limit in bytes or -1 for no limit
Expand All @@ -66,10 +64,10 @@ public boolean handle(Request request, Response response, Callback callback) thr
if (contentLengthField != null)
{
long contentLength = contentLengthField.getLongValue();
if (_requestLimit > 0 && contentLength > _requestLimit) {
response.setStatus(413);
if (_requestLimit >= 0 && contentLength > _requestLimit)
{
String s = "Request body is too large: " + contentLength + ">" + _requestLimit;
response.write(true, BufferUtil.toBuffer(s), callback);
Response.writeError(request, response, callback, HttpStatus.PAYLOAD_TOO_LARGE_413, s);
return true;
}
}
Expand All @@ -79,11 +77,11 @@ public boolean handle(Request request, Response response, Callback callback) thr
@Override
public HttpField onAddField(HttpField field)
{
if ((field.getHeader()!=null) && (field.getHeader().is(HttpHeader.CONTENT_LENGTH.asString())))
if (field.getHeader().is(HttpHeader.CONTENT_LENGTH.asString()))
{
long contentLength = field.getLongValue();
if (_responseLimit > 0 && contentLength > _responseLimit)
throw new HttpException.RuntimeException(500, "Response body is too large: " + contentLength + ">" + _responseLimit);
if (_responseLimit >= 0 && contentLength > _responseLimit)
throw new HttpException.RuntimeException(HttpStatus.INTERNAL_SERVER_ERROR_500, "Response body is too large: " + contentLength + ">" + _responseLimit);
}
return super.onAddField(field);
}
Expand All @@ -100,6 +98,9 @@ public HttpFields.Mutable getHeaders()

request.addHttpStreamWrapper(httpStream -> new HttpStream.Wrapper(httpStream)
{
private long _read = 0;
private long _written = 0;

@Override
public Content.Chunk read()
{
Expand All @@ -116,8 +117,7 @@ public Content.Chunk read()
_read += content.remaining();
if (_requestLimit >= 0 && _read > _requestLimit)
{
System.err.println("we actually read too much content in coreSizeLimitHandler for request size limit");
BadMessageException e = new BadMessageException(413, "Request body is too large: " + _read + ">" + _requestLimit);
BadMessageException e = new BadMessageException(HttpStatus.PAYLOAD_TOO_LARGE_413, "Request body is too large: " + _read + ">" + _requestLimit);
request.fail(e);
return null;
}
Expand All @@ -134,7 +134,7 @@ public void send(MetaData.Request request, MetaData.Response response, boolean l
{
if (_responseLimit >= 0 && (_written + content.remaining()) > _responseLimit)
{
callback.failed(new HttpException.RuntimeException(500, "Response body is too large: " +
callback.failed(new HttpException.RuntimeException(HttpStatus.INTERNAL_SERVER_ERROR_500, "Response body is too large: " +
_written + content.remaining() + ">" + _responseLimit));
return;
}
Expand Down

0 comments on commit abb6733

Please sign in to comment.