Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the un-required ByteBody split #964

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ final class FnServletRequest<B> implements ServletHttpRequest<InputEvent, B>, Se
private Cookies cookies;
private final MediaTypeCodecRegistry codecRegistry;
private final ByteBody byteBody;
private Object cachedBody;
private URI uri;

public FnServletRequest(
Expand All @@ -116,7 +117,7 @@ public boolean isAsyncSupported() {

@Override
public InputStream getInputStream() {
return byteBody.split(SplitBackpressureMode.FASTEST).toInputStream();
return byteBody.toInputStream();
}

/**
Expand Down Expand Up @@ -144,32 +145,44 @@ public <T> Optional<T> getBody(@NonNull Argument<T> arg) {
final MediaType contentType = getContentType().orElse(MediaType.APPLICATION_JSON_TYPE);

if (isFormSubmission()) {
return consumeBody(inputStream -> {
ConvertibleMultiValues<?> form;
if (cachedBody instanceof ConvertibleMultiValues<?> storedForm) {
form = storedForm;
} else {
try {
String content = IOUtils.readText(new BufferedReader(new InputStreamReader(inputStream, getCharacterEncoding())));
ConvertibleMultiValues<?> form = parseFormData(content);
if (ConvertibleValues.class == type || Object.class == type) {
return Optional.of((T) form);
} else {
return conversionService.convert(form.asMap(), arg);
}
String content = IOUtils.readText(new BufferedReader(new InputStreamReader(byteBody.toInputStream(), getCharacterEncoding())));
form = parseFormData(content);
} catch (IOException e) {
throw new RuntimeException("Unable to parse body", e);
}
});
cachedBody = form;
}

if (ConvertibleValues.class == type || Object.class == type) {
return Optional.of((T) form);
} else {
return conversionService.convert(form.asMap(), arg);
}
}

final MediaTypeCodec codec = codecRegistry.findCodec(contentType, type).orElse(null);
if (codec == null) {
return Optional.empty();
}
if (ConvertibleValues.class == type || Object.class == type) {
final Map map = consumeBody(inputStream -> codec.decode(Map.class, inputStream));
if (cachedBody instanceof ConvertibleValues) {
return Optional.of((T) cachedBody);
}
final Map map = codec.decode(Map.class, byteBody.toInputStream());
ConvertibleValues result = ConvertibleValues.of(map);
cachedBody = result;
return Optional.of((T) result);
} else {
if (cachedBody != null && cachedBody.getClass().isAssignableFrom(type)) {
return Optional.of((T) cachedBody);
}
final T value = consumeBody(inputStream -> codec.decode(arg, inputStream));
cachedBody = value;
return Optional.of(value);
}
}
Expand Down
Loading