Skip to content

Fix Bug(Gateway MVC): Make sure Retry filter sends correct body #3342

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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 @@ -102,8 +102,18 @@ public static <T> Optional<T> cacheAndReadBody(ServerRequest request, Class<T> t
public static ByteArrayInputStream cacheBody(ServerRequest request) {
try {
byte[] bytes = StreamUtils.copyToByteArray(request.servletRequest().getInputStream());
ByteArrayInputStream body = new ByteArrayInputStream(bytes);
putAttribute(request, MvcUtils.CACHED_REQUEST_BODY_ATTR, body);
ByteArrayInputStream body;
if (bytes.length > 0) {
body = new ByteArrayInputStream(bytes);
putAttribute(request, MvcUtils.CACHED_REQUEST_BODY_ATTR, body);
}
else {
body = getAttribute(request, CACHED_REQUEST_BODY_ATTR);
if (body == null) {
body = new ByteArrayInputStream(bytes);
putAttribute(request, MvcUtils.CACHED_REQUEST_BODY_ATTR, body);
}
}
return body;
}
catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import org.springframework.web.servlet.function.ServerRequest;
import org.springframework.web.servlet.function.ServerResponse;

import static org.springframework.cloud.gateway.server.mvc.filter.BodyFilterFunctions.adaptCachedBody;

public abstract class RetryFilterFunctions {

private RetryFilterFunctions() {
Expand Down Expand Up @@ -75,7 +77,7 @@ public static HandlerFilterFunction<ServerResponse, ServerResponse> retry(RetryC
if (isRetryableStatusCode(serverResponse.statusCode(), config)
&& isRetryableMethod(request.method(), config)) {
// use this to transfer information to HttpStatusRetryPolicy
throw new RetryException(request, serverResponse);
throw new RetryException(adaptCachedBody().apply(request), serverResponse);
}
return serverResponse;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.springframework.web.client.RestClient;
import org.springframework.web.servlet.function.ServerResponse;

import static org.springframework.cloud.gateway.server.mvc.common.MvcUtils.cacheBody;

public class RestClientProxyExchange implements ProxyExchange {

private final RestClient restClient;
Expand All @@ -41,7 +43,7 @@ public ServerResponse exchange(Request request) {
}

private static int copyBody(Request request, OutputStream outputStream) throws IOException {
return StreamUtils.copy(request.getServerRequest().servletRequest().getInputStream(), outputStream);
return StreamUtils.copy(cacheBody(request.getServerRequest()), outputStream);
}

private static ServerResponse doExchange(Request request, ClientHttpResponse clientResponse) throws IOException {
Expand Down