Skip to content

Commit

Permalink
lambda http binary types
Browse files Browse the repository at this point in the history
finish base64

remove unneeded lambda source

fix encoding
  • Loading branch information
patriot1burke committed Jul 8, 2021
1 parent 2ce9718 commit 5908d12
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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?
Expand All @@ -213,23 +211,14 @@ private ByteArrayOutputStream createByteStream() {
return baos;
}

static Set<String> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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;
}

}

0 comments on commit 5908d12

Please sign in to comment.