Skip to content

Commit

Permalink
http2client should not cache the InputStream eg:HTTP 1.1 Chunked_tran…
Browse files Browse the repository at this point in the history
…sfer_encoding (#2420)
  • Loading branch information
hdfg159 authored May 17, 2024
1 parent 4a89277 commit 80e6ecb
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions java11/src/main/java/feign/http2client/Http2Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import feign.Request.ProtocolVersion;
import feign.Response;
import feign.Util;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -97,9 +97,9 @@ public Response execute(Request request, Options options) throws IOException {
}

HttpClient clientForRequest = getOrCreateClient(options);
HttpResponse<byte[]> httpResponse;
HttpResponse<InputStream> httpResponse;
try {
httpResponse = clientForRequest.send(httpRequest, BodyHandlers.ofByteArray());
httpResponse = clientForRequest.send(httpRequest, BodyHandlers.ofInputStream());
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
throw new IOException(e);
Expand All @@ -119,19 +119,17 @@ public CompletableFuture<Response> execute(
}

HttpClient clientForRequest = getOrCreateClient(options);
CompletableFuture<HttpResponse<byte[]>> future =
clientForRequest.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofByteArray());
CompletableFuture<HttpResponse<InputStream>> future =
clientForRequest.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofInputStream());
return future.thenApply(httpResponse -> toFeignResponse(request, httpResponse));
}

protected Response toFeignResponse(Request request, HttpResponse<byte[]> httpResponse) {
protected Response toFeignResponse(Request request, HttpResponse<InputStream> httpResponse) {
final OptionalLong length = httpResponse.headers().firstValueAsLong("Content-Length");

return Response.builder()
.protocolVersion(enumForName(ProtocolVersion.class, httpResponse.version()))
.body(
new ByteArrayInputStream(httpResponse.body()),
length.isPresent() ? (int) length.getAsLong() : null)
.body(httpResponse.body(), length.isPresent() ? (int) length.getAsLong() : null)
.reason(httpResponse.headers().firstValue("Reason-Phrase").orElse(null))
.request(request)
.status(httpResponse.statusCode())
Expand Down

0 comments on commit 80e6ecb

Please sign in to comment.