From 5908d123ac06fd7fc5ad3a627c75097d8cb4e71f Mon Sep 17 00:00:00 2001 From: Bill Burke Date: Wed, 7 Jul 2021 15:41:19 -0400 Subject: [PATCH] lambda http binary types finish base64 remove unneeded lambda source fix encoding --- .../amazon/lambda/http/LambdaHttpHandler.java | 21 +++++-------------- .../amazon/lambda/http/LambdaHttpHandler.java | 12 +++++------ 2 files changed, 10 insertions(+), 23 deletions(-) diff --git a/extensions/amazon-lambda-http/runtime/src/main/java/io/quarkus/amazon/lambda/http/LambdaHttpHandler.java b/extensions/amazon-lambda-http/runtime/src/main/java/io/quarkus/amazon/lambda/http/LambdaHttpHandler.java index 1fcc653280fa2d..1594918ee194f9 100644 --- a/extensions/amazon-lambda-http/runtime/src/main/java/io/quarkus/amazon/lambda/http/LambdaHttpHandler.java +++ b/extensions/amazon-lambda-http/runtime/src/main/java/io/quarkus/amazon/lambda/http/LambdaHttpHandler.java @@ -9,11 +9,9 @@ import java.nio.charset.StandardCharsets; import java.util.Base64; import java.util.HashMap; -import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Set; import java.util.concurrent.CompletableFuture; import org.jboss.logging.Logger; @@ -138,7 +136,7 @@ public void handleMessage(Object msg) { if (baos != null) { if (isBinary(responseBuilder.getHeaders().get("Content-Type"))) { responseBuilder.setIsBase64Encoded(true); - responseBuilder.setBody(Base64.getMimeEncoder().encodeToString(baos.toByteArray())); + responseBuilder.setBody(Base64.getEncoder().encodeToString(baos.toByteArray())); } else { responseBuilder.setBody(new String(baos.toByteArray(), StandardCharsets.UTF_8)); } @@ -187,7 +185,7 @@ private APIGatewayV2HTTPResponse nettyDispatch(InetSocketAddress clientAddress, HttpContent requestContent = LastHttpContent.EMPTY_LAST_CONTENT; if (request.getBody() != null) { if (request.getIsBase64Encoded()) { - ByteBuf body = Unpooled.wrappedBuffer(Base64.getMimeDecoder().decode(request.getBody())); + ByteBuf body = Unpooled.wrappedBuffer(Base64.getDecoder().decode(request.getBody())); requestContent = new DefaultLastHttpContent(body); } else { ByteBuf body = Unpooled.copiedBuffer(request.getBody(), StandardCharsets.UTF_8); //TODO: do we need to look at the request encoding? @@ -213,23 +211,14 @@ private ByteArrayOutputStream createByteStream() { return baos; } - static Set binaryTypes = new HashSet<>(); - - static { - binaryTypes.add("application/octet-stream"); - binaryTypes.add("image/jpeg"); - binaryTypes.add("image/png"); - binaryTypes.add("image/gif"); - } - private boolean isBinary(String contentType) { if (contentType != null) { + String ct = contentType.toLowerCase(); int index = contentType.indexOf(';'); if (index >= 0) { - return binaryTypes.contains(contentType.substring(0, index)); - } else { - return binaryTypes.contains(contentType); + ct = contentType.substring(0, index); } + return !(ct.startsWith("text") || ct.contains("json") || ct.contains("xml") || ct.contains("yaml")); } return false; } diff --git a/extensions/amazon-lambda-rest/runtime/src/main/java/io/quarkus/amazon/lambda/http/LambdaHttpHandler.java b/extensions/amazon-lambda-rest/runtime/src/main/java/io/quarkus/amazon/lambda/http/LambdaHttpHandler.java index 76f36531ecd77b..cbeb13854fca10 100644 --- a/extensions/amazon-lambda-rest/runtime/src/main/java/io/quarkus/amazon/lambda/http/LambdaHttpHandler.java +++ b/extensions/amazon-lambda-rest/runtime/src/main/java/io/quarkus/amazon/lambda/http/LambdaHttpHandler.java @@ -13,7 +13,6 @@ import org.jboss.logging.Logger; -import com.amazonaws.serverless.proxy.internal.LambdaContainerHandler; import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.RequestHandler; @@ -124,7 +123,7 @@ public void handleMessage(Object msg) { if (baos != null) { if (isBinary(responseBuilder.getMultiValueHeaders().getFirst("Content-Type"))) { responseBuilder.setBase64Encoded(true); - responseBuilder.setBody(Base64.getMimeEncoder().encodeToString(baos.toByteArray())); + responseBuilder.setBody(Base64.getEncoder().encodeToString(baos.toByteArray())); } else { responseBuilder.setBody(new String(baos.toByteArray(), StandardCharsets.UTF_8)); } @@ -193,7 +192,7 @@ private AwsProxyResponse nettyDispatch(InetSocketAddress clientAddress, AwsProxy HttpContent requestContent = LastHttpContent.EMPTY_LAST_CONTENT; if (request.getBody() != null) { if (request.isBase64Encoded()) { - ByteBuf body = Unpooled.wrappedBuffer(Base64.getMimeDecoder().decode(request.getBody())); + ByteBuf body = Unpooled.wrappedBuffer(Base64.getDecoder().decode(request.getBody())); requestContent = new DefaultLastHttpContent(body); } else { ByteBuf body = Unpooled.copiedBuffer(request.getBody(), StandardCharsets.UTF_8); //TODO: do we need to look at the request encoding? @@ -221,14 +220,13 @@ private ByteArrayOutputStream createByteStream() { private boolean isBinary(String contentType) { if (contentType != null) { + String ct = contentType.toLowerCase(); int index = contentType.indexOf(';'); if (index >= 0) { - return LambdaContainerHandler.getContainerConfig().isBinaryContentType(contentType.substring(0, index)); - } else { - return LambdaContainerHandler.getContainerConfig().isBinaryContentType(contentType); + ct = contentType.substring(0, index); } + return !(ct.startsWith("text") || ct.contains("json") || ct.contains("xml") || ct.contains("yaml")); } return false; } - }