Skip to content

Commit

Permalink
Improve text format handling in Amazon Lambda
Browse files Browse the repository at this point in the history
Fixes: #46147
  • Loading branch information
geoand committed Feb 11, 2025
1 parent 7ff0fcf commit 374d58d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ public void handleMessage(Object msg) {
}
if (msg instanceof LastHttpContent) {
if (baos != null) {
if (isBinary(responseBuilder.getHeaders().get("Content-Type"))) {
if (isText(responseBuilder.getHeaders().get("Content-Type"))) {
responseBuilder.setBody(baos.toString(StandardCharsets.UTF_8));
} else {
responseBuilder.setIsBase64Encoded(true);
responseBuilder.setBody(Base64.getEncoder().encodeToString(baos.toByteArray()));
} else {
responseBuilder.setBody(baos.toString(StandardCharsets.UTF_8));
}
}
future.complete(responseBuilder);
Expand Down Expand Up @@ -259,10 +259,11 @@ private ByteArrayOutputStream createByteStream() {
return baos;
}

private boolean isBinary(String contentType) {
private boolean isText(String contentType) {
if (contentType != null) {
String ct = contentType.toLowerCase(Locale.ROOT);
return !(ct.startsWith("text") || ct.contains("json") || ct.contains("xml") || ct.contains("yaml"));
return (ct.startsWith("text") || ct.contains("json") || (ct.contains("xml") && !ct.contains("openxmlformats"))
|| ct.contains("yaml"));
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ public void handleMessage(Object msg) {
}
if (msg instanceof LastHttpContent) {
if (baos != null) {
if (isBinary(responseBuilder.getMultiValueHeaders().getFirst("Content-Type"))) {
if (isText(responseBuilder.getMultiValueHeaders().getFirst("Content-Type"))) {
responseBuilder.setBody(baos.toString(StandardCharsets.UTF_8));
} else {
responseBuilder.setBase64Encoded(true);
responseBuilder.setBody(Base64.getEncoder().encodeToString(baos.toByteArray()));
} else {
responseBuilder.setBody(baos.toString(StandardCharsets.UTF_8));
}
}
future.complete(responseBuilder);
Expand Down Expand Up @@ -242,10 +242,11 @@ private ByteArrayOutputStream createByteStream() {
return baos;
}

private boolean isBinary(String contentType) {
private boolean isText(String contentType) {
if (contentType != null) {
String ct = contentType.toLowerCase(Locale.ROOT);
return !(ct.startsWith("text") || ct.contains("json") || ct.contains("xml") || ct.contains("yaml"));
return (ct.startsWith("text") || ct.contains("json") || (ct.contains("xml") && !ct.contains("openxmlformats"))
|| ct.contains("yaml"));
}
return false;
}
Expand Down

0 comments on commit 374d58d

Please sign in to comment.