From a3ca47f172240f7b323581e49ab2b08a560f991f Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Tue, 4 Jun 2024 18:51:52 +0200 Subject: [PATCH] fix(deps): update dependency io.micronaut:micronaut-core-bom to v4.5.1 (#677) * fix(deps): update dependency io.micronaut:micronaut-core-bom to v4.5.1 * Implement byteBody --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: yawkat --- .../http/AzureFunctionHttpRequest.java | 35 ++++++++++--------- gradle/libs.versions.toml | 4 +-- .../AzureFunctionHttpServerUnderTest.java | 1 + 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/azure-function-http/src/main/java/io/micronaut/azure/function/http/AzureFunctionHttpRequest.java b/azure-function-http/src/main/java/io/micronaut/azure/function/http/AzureFunctionHttpRequest.java index 6c0b9a889..1ca1cd7e5 100644 --- a/azure-function-http/src/main/java/io/micronaut/azure/function/http/AzureFunctionHttpRequest.java +++ b/azure-function-http/src/main/java/io/micronaut/azure/function/http/AzureFunctionHttpRequest.java @@ -39,21 +39,22 @@ import io.micronaut.http.MutableHttpHeaders; import io.micronaut.http.MutableHttpParameters; import io.micronaut.http.MutableHttpRequest; +import io.micronaut.http.ServerHttpRequest; +import io.micronaut.http.body.AvailableByteBody; import io.micronaut.http.cookie.Cookie; import io.micronaut.http.cookie.Cookies; import io.micronaut.http.simple.SimpleHttpParameters; import io.micronaut.servlet.http.BodyBuilder; -import io.micronaut.servlet.http.ByteArrayByteBuffer; import io.micronaut.servlet.http.MutableServletHttpRequest; import io.micronaut.servlet.http.ParsedBodyHolder; import io.micronaut.servlet.http.ServletExchange; import io.micronaut.servlet.http.ServletHttpRequest; import io.micronaut.servlet.http.ServletHttpResponse; +import io.micronaut.servlet.http.body.AvailableByteArrayBody; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.BufferedReader; -import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -78,6 +79,7 @@ public final class AzureFunctionHttpRequest implements MutableServletHttpRequest>, T>, ServletExchange>, HttpResponseMessage>, + ServerHttpRequest, FullHttpRequest, ParsedBodyHolder { @@ -96,8 +98,6 @@ public final class AzureFunctionHttpRequest implements private T parsedBody; private T overriddenBody; - private ByteArrayByteBuffer servletByteBuffer; - public AzureFunctionHttpRequest( HttpRequestMessage> request, AzureFunctionHttpResponse response, @@ -131,7 +131,17 @@ public byte[] getBodyBytes() throws IOException { } } - protected static HttpMethod parseMethod(Supplier httpMethodConsumer) { + @Override + public @NonNull AvailableByteBody byteBody() { + try { + return new AvailableByteArrayBody(getBodyBytes()); + } catch (IOException e) { + // empty body + return new AvailableByteArrayBody(ArrayUtils.EMPTY_BYTE_ARRAY); + } + } + + private static HttpMethod parseMethod(Supplier httpMethodConsumer) { try { return HttpMethod.valueOf(httpMethodConsumer.get()); } catch (IllegalArgumentException e) { @@ -171,7 +181,7 @@ public MutableHttpParameters getParameters() { @Override public InputStream getInputStream() throws IOException { - return servletByteBuffer != null ? servletByteBuffer.toInputStream() : new ByteArrayInputStream(getBodyBytes()); + return byteBody().split().toInputStream(); } @Override @@ -251,7 +261,7 @@ public Optional getBody(Argument arg) { * @param contentType Content Type * @return returns true if the content type is either application/x-www-form-urlencoded or multipart/form-data */ - protected boolean isFormSubmission(MediaType contentType) { + private boolean isFormSubmission(MediaType contentType) { return MediaType.APPLICATION_FORM_URLENCODED_TYPE.equals(contentType) || MediaType.MULTIPART_FORM_DATA_TYPE.equals(contentType); } @@ -285,14 +295,7 @@ public void setParsedBody(T body) { @Override public @Nullable ByteBuffer contents() { - try { - if (servletByteBuffer == null) { - this.servletByteBuffer = new ByteArrayByteBuffer<>(getInputStream().readAllBytes()); - } - return servletByteBuffer; - } catch (IOException e) { - throw new IllegalStateException("Error getting all body contents", e); - } + return byteBody().split().toByteBuffer(); } @Override @@ -307,7 +310,7 @@ public void setParsedBody(T body) { * @return body bytes * @throws IOException if the body is empty */ - protected byte[] getBodyBytes(@NonNull Supplier bodySupplier, @NonNull BooleanSupplier base64EncodedSupplier) throws IOException { + private byte[] getBodyBytes(@NonNull Supplier bodySupplier, @NonNull BooleanSupplier base64EncodedSupplier) throws IOException { String requestBody = bodySupplier.get(); if (StringUtils.isEmpty(requestBody)) { throw new IOException("Empty Body"); diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 240fab2f3..a16fab25f 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -8,12 +8,12 @@ kotlin = "1.9.23" system-lambda = "1.2.1" # Micronaut -micronaut = "4.4.1" +micronaut = "4.5.1" micronaut-platform = "4.3.8" micronaut-logging = "1.1.2" micronaut-reactor = "3.3.0" micronaut-serde = "2.9.0" -micronaut-servlet = "4.6.0" +micronaut-servlet = "4.9.0" micronaut-test = "4.1.1" micronaut-test-resources = "2.4.0" micronaut-validation = "4.4.4" diff --git a/test-suite-http-server-tck-azure-function-http/src/test/java/io/micronaut/http/server/tck/azurehttpfunction/AzureFunctionHttpServerUnderTest.java b/test-suite-http-server-tck-azure-function-http/src/test/java/io/micronaut/http/server/tck/azurehttpfunction/AzureFunctionHttpServerUnderTest.java index 77c389b46..1098727e8 100644 --- a/test-suite-http-server-tck-azure-function-http/src/test/java/io/micronaut/http/server/tck/azurehttpfunction/AzureFunctionHttpServerUnderTest.java +++ b/test-suite-http-server-tck-azure-function-http/src/test/java/io/micronaut/http/server/tck/azurehttpfunction/AzureFunctionHttpServerUnderTest.java @@ -82,6 +82,7 @@ public void close() throws IOException { private final static Set HEADERS_USED_IN_TEST_SUITE = Set.of( "X-Test-Filter", "X-Captured-Remote-Address", + "X-HEAD-BODY", HttpHeaders.ACCESS_CONTROL_ALLOW_PRIVATE_NETWORK );