mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(sfn): encode HTTP Task object bodies as form data (#6010)
* feat(sfn): encode HTTP Task object bodies as form data * refactor(sfn): simplify HTTP Task payload encoding Reuse the shared RFC 3986 URI encoder and remove the unused form payload branch while preserving buffer and no-body requests.
This commit is contained in:
1 parent
87aa6f38a1
commit
f8ab55f319
5 files changed
+246
-14
No files matched your search
@@ -4784,7 +4784,8 @@ public class AslExecutor {
|
||||
validateConnectionArn(input);
|
||||
validateHttpHeaders(headers);
|
||||
|
||||
HttpRequestPayload requestPayload = requestPayload(requestBody, requestBodyEncoding);
|
||||
HttpRequestPayload requestPayload = requestPayload(requestBody, requestBodyEncoding,
|
||||
input.path("Transform").path("RequestEncodingOptions"));
|
||||
MultiMap requestHeaders = requestHeaders(headers, requestPayload.contentType());
|
||||
MultiMap requestQueryParameters = queryParameters(queryParameters);
|
||||
|
||||
@@ -4814,9 +4815,6 @@ public class AslExecutor {
|
||||
}
|
||||
|
||||
private HttpResponse<Buffer> sendHttpRequest(HttpRequest<Buffer> request, HttpRequestPayload payload) throws NoStackTraceTimeoutException {
|
||||
if (payload.form() != null) {
|
||||
return request.sendFormAndAwait(payload.form());
|
||||
}
|
||||
if (payload.body() != null) {
|
||||
return request.sendBufferAndAwait(payload.body());
|
||||
}
|
||||
@@ -4910,19 +4908,22 @@ public class AslExecutor {
|
||||
}
|
||||
}
|
||||
|
||||
private HttpRequestPayload requestPayload(JsonNode requestBody, String requestBodyEncoding) {
|
||||
private HttpRequestPayload requestPayload(JsonNode requestBody, String requestBodyEncoding, JsonNode options) {
|
||||
if ("URL_ENCODED".equalsIgnoreCase(requestBodyEncoding)) {
|
||||
// TODO: Implement Transform.RequestBodyEncoding URL_ENCODED with AWS-compatible array formats.
|
||||
throw new FailStateException("States.TaskFailed", "URL-encoded request bodies are not supported yet");
|
||||
try {
|
||||
return new HttpRequestPayload(Buffer.buffer(HttpTaskFormEncoder.encode(requestBody, options)),
|
||||
"application/x-www-form-urlencoded");
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new FailStateException("States.TaskFailed", e.getMessage());
|
||||
}
|
||||
} else if ("NONE".equalsIgnoreCase(requestBodyEncoding)) {
|
||||
try {
|
||||
if (requestBody.isMissingNode() || requestBody.isNull()) {
|
||||
return new HttpRequestPayload(null, null, null);
|
||||
return new HttpRequestPayload(null, null);
|
||||
}
|
||||
|
||||
return new HttpRequestPayload(
|
||||
Buffer.buffer(objectMapper.writeValueAsString(requestBody)),
|
||||
null,
|
||||
"application/json");
|
||||
} catch (Exception e) {
|
||||
throw new FailStateException("States.TaskFailed",
|
||||
@@ -4934,7 +4935,7 @@ public class AslExecutor {
|
||||
}
|
||||
}
|
||||
|
||||
private record HttpRequestPayload(Buffer body, MultiMap form, String contentType) {
|
||||
private record HttpRequestPayload(Buffer body, String contentType) {
|
||||
}
|
||||
|
||||
private record HttpTaskResponse(
|
||||
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
package io.github.hectorvent.floci.services.stepfunctions;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.github.hectorvent.floci.core.common.auth.SigV4Canonicalization;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
/** Object-body and scalar-array encodings documented for Step Functions HTTP Tasks. */
|
||||
final class HttpTaskFormEncoder {
|
||||
|
||||
private enum ArrayFormat { INDICES, REPEAT, COMMAS, BRACKETS }
|
||||
|
||||
private HttpTaskFormEncoder() {
|
||||
}
|
||||
|
||||
// https://docs.aws.amazon.com/step-functions/latest/dg/call-https-apis.html#connect-http-task-url-encoding
|
||||
static String encode(JsonNode body, JsonNode options) {
|
||||
if (!options.isMissingNode() && !options.isObject()) {
|
||||
throw new IllegalArgumentException("RequestEncodingOptions must be a JSON object");
|
||||
}
|
||||
JsonNode configuredFormat = options.path("ArrayFormat");
|
||||
ArrayFormat format = ArrayFormat.INDICES;
|
||||
if (!configuredFormat.isMissingNode()) {
|
||||
if (!configuredFormat.isTextual()) {
|
||||
throw new IllegalArgumentException("ArrayFormat must be a supported string value");
|
||||
}
|
||||
try {
|
||||
format = ArrayFormat.valueOf(configuredFormat.textValue());
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new IllegalArgumentException("Unsupported ArrayFormat: " + configuredFormat.textValue());
|
||||
}
|
||||
}
|
||||
if (body.isMissingNode() || body.isNull()) {
|
||||
return "";
|
||||
}
|
||||
if (!body.isObject()) {
|
||||
throw new IllegalArgumentException("Floci URL_ENCODED request bodies currently require a JSON object");
|
||||
}
|
||||
List<String> pairs = new ArrayList<>();
|
||||
ArrayFormat selected = format;
|
||||
body.fields().forEachRemaining(entry -> append(pairs, entry.getKey(), entry.getValue(), selected));
|
||||
return String.join("&", pairs);
|
||||
}
|
||||
|
||||
private static void append(List<String> pairs, String key, JsonNode value, ArrayFormat format) {
|
||||
if (value.isObject()) {
|
||||
value.fields().forEachRemaining(entry ->
|
||||
append(pairs, key + "[" + entry.getKey() + "]", entry.getValue(), format));
|
||||
} else if (value.isArray()) {
|
||||
if (value.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (format == ArrayFormat.COMMAS) {
|
||||
StringJoiner values = new StringJoiner(",");
|
||||
for (JsonNode element : value) {
|
||||
if (element.isContainerNode()) {
|
||||
throw new IllegalArgumentException("Floci COMMAS arrays currently require scalar values");
|
||||
}
|
||||
values.add(SigV4Canonicalization.uriEncode(element.isNull() ? "" : element.asText()));
|
||||
}
|
||||
pairs.add(SigV4Canonicalization.uriEncode(key) + "=" + values);
|
||||
return;
|
||||
}
|
||||
for (int index = 0; index < value.size(); index++) {
|
||||
String arrayKey = switch (format) {
|
||||
case INDICES -> key + "[" + index + "]";
|
||||
case BRACKETS -> key + "[]";
|
||||
default -> key;
|
||||
};
|
||||
append(pairs, arrayKey, value.get(index), format);
|
||||
}
|
||||
} else {
|
||||
pairs.add(SigV4Canonicalization.uriEncode(key) + "="
|
||||
+ SigV4Canonicalization.uriEncode(value.isNull() ? "" : value.asText()));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user