Separate the parsing of content length and body buffer initialization
@@ -69,8 +69,8 @@ public class BodyHandlerImpl implements BodyHandler {
// we need to keep state since we can be called again on reroute
Boolean handled = context.get(BODY_HANDLED);
if (handled == null || !handled) {
- int bodyBufferInitialSize = getInitialBodyBufferSize(context.request());
- BHandler handler = new BHandler(context, bodyBufferInitialSize);
+ Long contentLength = parseContentLengthHeader(request);
+ BHandler handler = new BHandler(context, contentLength);
request.handler(handler);
request.endHandler(v -> handler.end());
context.put(BODY_HANDLED, true);
@@ -108,21 +108,18 @@ public class BodyHandlerImpl implements BodyHandler {
return this;
}
- private int getInitialBodyBufferSize(HttpServerRequest request) {
+ private Long parseContentLengthHeader(HttpServerRequest request) {
String contentLength = request.getHeader(HttpHeaders.CONTENT_LENGTH);
if(contentLength == null || contentLength == "") {
- return DEFAULT_INITIAL_BODY_BUFFER_SIZE;
+ return null;
}
-
try{
long parsedContentLength = Long.parseLong(contentLength);
- if(parsedContentLength < 0) {
- return DEFAULT_INITIAL_BODY_BUFFER_SIZE;
- }
- return (int)Math.min(parsedContentLength, Integer.MAX_VALUE);
+
+ return parsedContentLength < 0 ? null : parsedContentLength;
}
catch (NumberFormatException ex) {
- return DEFAULT_INITIAL_BODY_BUFFER_SIZE;
+ return null;
}
}
@@ -139,7 +136,7 @@ public class BodyHandlerImpl implements BodyHandler {
final boolean isMultipart;
final boolean isUrlEncoded;
- public BHandler(RoutingContext context, int bodyBufferInitialSize) {
+ public BHandler(RoutingContext context, Long contentLength) {
this.context = context;
Set<FileUpload> fileUploads = context.fileUploads();
@@ -153,7 +150,7 @@ public class BodyHandlerImpl implements BodyHandler {
isUrlEncoded = lowerCaseContentType.startsWith(HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED.toString());
}
- initBodyBuffer(bodyBufferInitialSize);
+ initBodyBuffer(contentLength);
if (isMultipart || isUrlEncoded) {
makeUploadDir(context.vertx().fileSystem());
@@ -188,14 +185,23 @@ public class BodyHandlerImpl implements BodyHandler {
});
}
- private void initBodyBuffer(int initialBodyBufferSize) {
- if(bodyLimit < 0) {
- this.body = Buffer.buffer(initialBodyBufferSize);
+ private void initBodyBuffer(Long contentLength) {
+ int initialBodyBufferSize;
+ if(contentLength == null || contentLength < 0) {
+ initialBodyBufferSize = DEFAULT_INITIAL_BODY_BUFFER_SIZE;
+ }
+ else if(contentLength > Integer.MAX_VALUE) {
+ initialBodyBufferSize = Integer.MAX_VALUE;
}
else {
- int bufferSize = (int) Math.min(bodyLimit, initialBodyBufferSize);
- this.body = Buffer.buffer(bufferSize);
+ initialBodyBufferSize = contentLength.intValue();
}
+
+ if(bodyLimit != -1) {
+ initialBodyBufferSize = (int)Math.min(initialBodyBufferSize, bodyLimit);
+ }
+
+ this.body = Buffer.buffer(initialBodyBufferSize);
}
private void makeUploadDir(FileSystem fileSystem) {
