Only copy body request content if body is present
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
This commit is contained in:
committed by
spencergibb
parent
f2aabac5d0
commit
c4b8f71330
@@ -19,6 +19,7 @@ package org.springframework.cloud.gateway.server.mvc.handler;
|
||||
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;
|
||||
@@ -36,11 +37,22 @@ public class RestClientProxyExchange implements ProxyExchange {
|
||||
|
||||
@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 {
|
||||
|
||||
Reference in New Issue
Block a user