fix(apigatewayv2): encode binary Lambda request bodies (#2203)

* test(apigatewayv2): cover binary Lambda request payload

Change-Id: Iae344954b576b49826164e0f5e68964f86198cd4

* fix(apigatewayv2): encode binary Lambda request bodies

Change-Id: I5e32fe304c932878afdbf5be0c3c773595364ff0

* test(apigatewayv2): cover request body encoding matrix

Change-Id: Id16e0cc81f6806afa50d8db8c6ac1cb7897af6f6

* fix(apigatewayv2): accept UTF-8 text media parameters

Change-Id: Ibf82062b9345ebd80d722b497a15e60d62dce86d

* test(apigatewayv2): make binary payload cleanup best effort

Change-Id: Ib62dc80c73eb1836007b4492ccfda965da03f6ac

* revert(apigatewayv2): keep measured AWS media matrix

Change-Id: Ibbf8d8bca8d9967bf84628773cc08a991ffea6a1
This commit is contained in:
Langning Zhang authored and GitHub committed 2026-08-10 19:44:07 -05:00
1 parent bd01c1a70b
commit 28e9f6ed63
3 files changed
+289 -2

No files matched your search

@@ -43,8 +43,10 @@ import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
@@ -66,6 +68,15 @@ public class ApiGatewayExecuteController {
private static final Logger LOG = Logger.getLogger(ApiGatewayExecuteController.class);
private static final TypeReference<Map<String, Object>> MAP_TYPE = new TypeReference<>() {};
private static final Set<String> V2_TEXT_CONTENT_TYPES = Set.of(
MediaType.TEXT_PLAIN,
MediaType.TEXT_HTML,
"text/csv",
MediaType.TEXT_XML,
MediaType.APPLICATION_JSON,
MediaType.APPLICATION_XML,
"application/javascript",
"application/graphql");
private final ApiGatewayService apiGatewayService;
private final ApiGatewayV2Service apiGatewayV2Service;
@@ -1915,8 +1926,11 @@ public class ApiGatewayExecuteController {
? headers.getHeaderString("User-Agent") : "");
if (body != null && body.length > 0) {
event.put("body", new String(body));
event.put("isBase64Encoded", false);
boolean isText = isV2TextContentType(headers.getHeaderString(HttpHeaders.CONTENT_TYPE));
event.put("body", isText
? new String(body, StandardCharsets.UTF_8)
: Base64.getEncoder().encodeToString(body));
event.put("isBase64Encoded", !isText);
} else {
event.putNull("body");
event.put("isBase64Encoded", false);
@@ -1929,6 +1943,25 @@ public class ApiGatewayExecuteController {
}
}
private static boolean isV2TextContentType(String contentType) {
if (contentType == null || contentType.isBlank()) {
return false;
}
try {
MediaType mediaType = MediaType.valueOf(contentType);
String type = (mediaType.getType() + "/" + mediaType.getSubtype()).toLowerCase(Locale.ROOT);
if (mediaType.getParameters().isEmpty()) {
return V2_TEXT_CONTENT_TYPES.contains(type);
}
return (MediaType.TEXT_PLAIN.equals(type) || MediaType.APPLICATION_JSON.equals(type))
&& mediaType.getParameters().size() == 1
&& StandardCharsets.UTF_8.name().equalsIgnoreCase(mediaType.getParameters().get("charset"));
} catch (IllegalArgumentException e) {
return false;
}
}
private String jsonMessage(String message) {
return objectMapper.createObjectNode().put("message", message).toString();
}