Skip to content

Commit

Permalink
Only copy body request content if body is present
Browse files Browse the repository at this point in the history
This avoids to have a "Transfer-Encoding: chunked" with an empty body if downstream exchanges are negotiated in HTTP 1.1

Fixes gh-3325
Fixes gh-3368
  • Loading branch information
benba authored and spencergibb committed Sep 26, 2024
1 parent f2aabac commit c4b8f71
Showing 1 changed file with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;

import org.springframework.cloud.gateway.server.mvc.common.MvcUtils;
import org.springframework.http.client.ClientHttpResponse;
Expand All @@ -36,11 +37,22 @@ public RestClientProxyExchange(RestClient restClient) {

@Override
public ServerResponse exchange(Request request) {
return restClient.method(request.getMethod())
.uri(request.getUri())
.headers(httpHeaders -> httpHeaders.putAll(request.getHeaders()))
.body(outputStream -> copyBody(request, outputStream))
.exchange((clientRequest, clientResponse) -> doExchange(request, clientResponse), false);
var requestSpec = restClient.method(request.getMethod())
.uri(request.getUri())
.headers(httpHeaders -> httpHeaders.putAll(request.getHeaders()));
if (isBodyPresent(request)) {
requestSpec.body(outputStream -> copyBody(request, outputStream));
}
return requestSpec.exchange((clientRequest, clientResponse) -> doExchange(request, clientResponse), false);
}

private static boolean isBodyPresent(Request request) {
try {
return !request.getServerRequest().servletRequest().getInputStream().isFinished();
}
catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private static int copyBody(Request request, OutputStream outputStream) throws IOException {
Expand Down

0 comments on commit c4b8f71

Please sign in to comment.