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
  • Loading branch information
benba committed Apr 22, 2024
1 parent 58c8441 commit cab0818
Showing 1 changed file with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.IOException;
import java.io.OutputStream;
import java.io.UncheckedIOException;

import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.StreamUtils;
Expand All @@ -34,10 +35,21 @@ 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().available() > 0;
}
catch (IOException e) {
throw new UncheckedIOException(e);
}
}

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

0 comments on commit cab0818

Please sign in to comment.