From e6942da18eec31d4818be07094cc75e3e8ff1e7a Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Fri, 18 Aug 2023 17:08:13 +1000 Subject: [PATCH 1/4] Issue #10337 - fixes for SizeLimitHandler Signed-off-by: Lachlan Roberts --- .../org/eclipse/jetty/server/SizeLimitHandler.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/SizeLimitHandler.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/SizeLimitHandler.java index 5b4b47212d0..b29da977d1a 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/SizeLimitHandler.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/SizeLimitHandler.java @@ -59,8 +59,12 @@ public class SizeLimitHandler extends Handler.Wrapper if (contentLengthField != null) { long contentLength = contentLengthField.getLongValue(); - if (contentLength > _requestLimit) - throw new BadMessageException(413, "Request body is too large: " + contentLength + ">" + _requestLimit); + if (_requestLimit >= 0 && contentLength > _requestLimit) + { + String s = "Request body is too large: " + contentLength + ">" + _requestLimit; + Response.writeError(request, response, callback, 413, s); + return true; + } } HttpFields.Mutable.Wrapper httpFields = new HttpFields.Mutable.Wrapper(response.getHeaders()) @@ -71,7 +75,7 @@ public class SizeLimitHandler extends Handler.Wrapper if (field.getHeader().is(HttpHeader.CONTENT_LENGTH.asString())) { long contentLength = field.getLongValue(); - if (contentLength > _responseLimit) + if (_responseLimit >= 0 && contentLength > _responseLimit) throw new HttpException.RuntimeException(500, "Response body is too large: " + contentLength + ">" + _responseLimit); } return super.onAddField(field); From 2126ac262cbd4fcd92e32ea806e5383d1e8ea71f Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Tue, 22 Aug 2023 10:49:24 +1000 Subject: [PATCH 2/4] Issue #10337 - fixes for SizeLimitHandler Signed-off-by: Lachlan Roberts --- .../main/java/org/eclipse/jetty/server/SizeLimitHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/SizeLimitHandler.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/SizeLimitHandler.java index b29da977d1a..7bf0f319b37 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/SizeLimitHandler.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/SizeLimitHandler.java @@ -62,7 +62,7 @@ public class SizeLimitHandler extends Handler.Wrapper if (_requestLimit >= 0 && contentLength > _requestLimit) { String s = "Request body is too large: " + contentLength + ">" + _requestLimit; - Response.writeError(request, response, callback, 413, s); + callback.failed(new HttpException.RuntimeException(413, s)); return true; } } From 4bd3a8e431a4ab239b50995df4ccf8460d5e88fe Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Thu, 24 Aug 2023 15:05:43 +1000 Subject: [PATCH 3/4] Revert "Issue #10337 - fixes for SizeLimitHandler" This reverts commit 2126ac262cbd4fcd92e32ea806e5383d1e8ea71f. --- .../main/java/org/eclipse/jetty/server/SizeLimitHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/SizeLimitHandler.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/SizeLimitHandler.java index 7bf0f319b37..b29da977d1a 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/SizeLimitHandler.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/SizeLimitHandler.java @@ -62,7 +62,7 @@ public class SizeLimitHandler extends Handler.Wrapper if (_requestLimit >= 0 && contentLength > _requestLimit) { String s = "Request body is too large: " + contentLength + ">" + _requestLimit; - callback.failed(new HttpException.RuntimeException(413, s)); + Response.writeError(request, response, callback, 413, s); return true; } } From 67404f96ce1affa8692edfc9783a07246389f053 Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Fri, 25 Aug 2023 16:24:31 +1000 Subject: [PATCH 4/4] use HttpStatus for 413 and 500 codes in SizeLimitHandler Signed-off-by: Lachlan Roberts --- .../java/org/eclipse/jetty/server/SizeLimitHandler.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/SizeLimitHandler.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/SizeLimitHandler.java index b29da977d1a..862f1459015 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/SizeLimitHandler.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/SizeLimitHandler.java @@ -20,6 +20,7 @@ import org.eclipse.jetty.http.HttpException; 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.util.Callback; @@ -62,7 +63,7 @@ public class SizeLimitHandler extends Handler.Wrapper if (_requestLimit >= 0 && contentLength > _requestLimit) { String s = "Request body is too large: " + contentLength + ">" + _requestLimit; - Response.writeError(request, response, callback, 413, s); + Response.writeError(request, response, callback, HttpStatus.PAYLOAD_TOO_LARGE_413, s); return true; } } @@ -76,7 +77,7 @@ public class SizeLimitHandler extends Handler.Wrapper { long contentLength = field.getLongValue(); if (_responseLimit >= 0 && contentLength > _responseLimit) - throw new HttpException.RuntimeException(500, "Response body is too large: " + contentLength + ">" + _responseLimit); + throw new HttpException.RuntimeException(HttpStatus.INTERNAL_SERVER_ERROR_500, "Response body is too large: " + contentLength + ">" + _responseLimit); } return super.onAddField(field); } @@ -109,7 +110,7 @@ public class SizeLimitHandler extends Handler.Wrapper _read += content.remaining(); if (_requestLimit >= 0 && _read > _requestLimit) { - 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; } @@ -126,7 +127,7 @@ public class SizeLimitHandler extends Handler.Wrapper { 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; }