Skip to content
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
77 changes: 74 additions & 3 deletions java11/src/main/java/feign/http2client/Http2Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
import feign.Request.ProtocolVersion;
import feign.Response;
import feign.Util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.io.UncheckedIOException;
import java.lang.ref.SoftReference;
import java.net.URI;
Expand Down Expand Up @@ -73,6 +76,12 @@ public class Http2Client implements Client, AsyncClient<Object> {

private volatile ScheduledExecutorService readTimeoutScheduler;

/**
* Maximum number of response body bytes read eagerly, mirroring the size bound feign core uses
* when it buffers response bodies ({@code InvocationContext.MAX_RESPONSE_BUFFER_SIZE}).
*/
private static final int MAX_RESPONSE_HEAD_SIZE = 8192;

/**
* Creates the new Http2Client using following defaults:
*
Expand Down Expand Up @@ -119,7 +128,7 @@ public Response execute(Request request, Options options) throws IOException {
throw new IOException(e);
}

return withReadTimeout(toFeignResponse(request, httpResponse), options);
return toFeignResponse(request, httpResponse, bufferResponseHead(httpResponse.body(), options));
}

@Override
Expand All @@ -140,14 +149,17 @@ public CompletableFuture<Response> execute(
}

protected Response toFeignResponse(Request request, HttpResponse<InputStream> httpResponse) {
return toFeignResponse(request, httpResponse, httpResponse.body());
}

protected Response toFeignResponse(
Request request, HttpResponse<InputStream> httpResponse, InputStream body) {
final OptionalLong length = httpResponse.headers().firstValueAsLong("Content-Length");
Integer contentLength =
length.isPresent() && length.getAsLong() >= 0 && length.getAsLong() <= Integer.MAX_VALUE
? (int) length.getAsLong()
: null;

InputStream body = httpResponse.body();

if (httpResponse.headers().allValues(CONTENT_ENCODING).contains(ENCODING_GZIP)) {
try {
body = new GZIPInputStream(body);
Expand Down Expand Up @@ -190,6 +202,65 @@ Response withReadTimeout(Response response, Options options) {
.build();
}

/**
* Buffers the head (up to {@value #MAX_RESPONSE_HEAD_SIZE} bytes) of the raw response body, so
* that transport level failures on small responses - eg a malformed chunked transfer encoding -
* surface as an IOException from {@link #execute(Request, Options)}.
*
* <p>Since the body is delivered with {@link BodyHandlers#ofInputStream()}, {@link
* HttpClient#send} returns as soon as the headers arrive and the body is only read later. A
* malformed or truncated body then fails during decoding, after {@link #execute(Request,
* Options)} returned successfully; feign classifies such failures as {@code errorReading} instead
* of the retryable {@code errorExecuting}, and with a {@link Response} return type the raw
* IOException escapes the invocation entirely. Reading the head here restores the retryable
* classification responses had when the client buffered the full body via {@code
* BodyHandlers.ofByteArray()}.
*
* <p>The read is applied to the raw, pre-decompression body: decompression failures are decoder
* concerns and keep surfacing where they did before. Bodies larger than the buffer keep streaming
* - the buffered head is re-attached in front of the remaining stream - preserving the memory
* behavior that motivated the switch to {@code ofInputStream()}. The read timeout guards both the
* eager read and the remaining stream, which is returned with the timeout wrapper already in
* place.
*/
private InputStream bufferResponseHead(InputStream body, Options options) throws IOException {
final InputStream stream = withReadTimeout(body, options);
if (stream == null) {
return null;
}
final byte[] head;
try {
head = readAtMost(stream, MAX_RESPONSE_HEAD_SIZE + 1);
} catch (IOException e) {
ensureClosed(stream);
throw e;
}
if (head.length > MAX_RESPONSE_HEAD_SIZE) {
return new SequenceInputStream(new ByteArrayInputStream(head), stream);
}
ensureClosed(stream);
return new ByteArrayInputStream(head);
}

private InputStream withReadTimeout(InputStream body, Options options) {
if (body == null || options.readTimeout() <= 0) {
return body;
}
return new ReadTimeoutInputStream(
body, readTimeoutScheduler(), options.readTimeoutUnit().toNanos(options.readTimeout()));
}

private static byte[] readAtMost(InputStream stream, int limit) throws IOException {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final byte[] chunk = new byte[1024];
int read;
while (out.size() < limit
&& (read = stream.read(chunk, 0, Math.min(chunk.length, limit - out.size()))) != -1) {
out.write(chunk, 0, read);
}
return out.toByteArray();
}

private ScheduledExecutorService readTimeoutScheduler() {
ScheduledExecutorService scheduler = readTimeoutScheduler;
if (scheduler == null) {
Expand Down
124 changes: 124 additions & 0 deletions java11/src/test/java/feign/http2client/test/Http2ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.RecordedRequest;
Expand Down Expand Up @@ -405,6 +406,129 @@ public void parsesRequestAndResponse() throws IOException, InterruptedException
assertThat(recordedRequest.getBody().readUtf8()).isEqualToIgnoringCase("foo");
}

/**
* Regression test for issue #2689: since the client switched to {@code
* BodyHandlers.ofInputStream()}, a malformed response chunk fails only when the (now lazy) body
* is read, after {@code Http2Client#execute} returned. The IOException then escaped the feign
* invocation instead of being classified as a retryable {@link RetryableException}, so retries
* never fired.
*
* <p>Emulates WireMock's {@code Fault.MALFORMED_RESPONSE_CHUNK}: valid headers with chunked
* transfer encoding followed by garbage where a chunk size line belongs. OkHttp's MockWebServer
* no longer ships a policy for malformed chunks, so this uses a raw socket, like {@code
* getWithRequestBody()}.
*/
@Test
void malformedResponseChunkSurfacesAsRetryableException() throws Exception {
// three attempts per invocation, two invocations: a retryer that makes an attempt only
// after the previous failure was classified as retryable
final int expectedAttempts = 6;
try (ServerSocket httpServer = new ServerSocket(0, 10, InetAddress.getLoopbackAddress())) {
final AtomicInteger attempts = new AtomicInteger();
final Thread serverThread =
new Thread(
() -> {
while (true) {
try (Socket socket = httpServer.accept()) {
socket.setSoTimeout(5000);
drainRequest(socket.getInputStream());
// the headers are valid, the chunk framing is not: the client reads
// "Malformed response chunk!" where a chunk size line belongs
socket
.getOutputStream()
.write(
("HTTP/1.1 200 OK\r\n"
+ "Transfer-Encoding: chunked\r\n"
+ "\r\n"
+ "Malformed response chunk!\r\n")
.getBytes(UTF_8));
socket.getOutputStream().flush();
Thread.sleep(200); // keep the socket open, so the failure is parsing, not EOF
} catch (IOException | InterruptedException e) {
return;
}
}
});
serverThread.start();

final TestInterface api =
newBuilder()
.retryer(new CountingRetryer(attempts))
.target(TestInterface.class, "http://localhost:" + httpServer.getLocalPort());

// a Response return type reads the body outside of feign: the malformed chunk must not
// escape as a raw IOException, it must surface as a retryable exception
assertThrows(RetryableException.class, () -> api.post("some text"));

// a decoded return type must fail as retryable too, not as the non-retryable
// FeignException that errorReading produces once execute() has returned
assertThrows(RetryableException.class, () -> api.get());

// every attempt failed as retryable and was retried until the retryer gave up
assertThat(attempts.get()).isEqualTo(expectedAttempts);

httpServer.close();
serverThread.join(5000);
}
}

/** A bounded retryer that counts every attempt it handles, across all invocations. */
private static final class CountingRetryer implements Retryer {
private final AtomicInteger attempts;
private int invocationAttempts;

private CountingRetryer(AtomicInteger attempts) {
this.attempts = attempts;
}

@Override
public void continueOrPropagate(RetryableException e) {
attempts.incrementAndGet();
if (invocationAttempts++ >= 2) {
throw e;
}
try {
Thread.sleep(1);
} catch (InterruptedException interruptedException) {
Thread.currentThread().interrupt();
throw e;
}
}

@Override
public Retryer clone() {
return new CountingRetryer(attempts);
}
}

/** Reads the request head and, if present, the Content-Length framed body. */
private static void drainRequest(InputStream in) throws IOException {
final StringBuilder head = new StringBuilder();
final byte[] buffer = new byte[8192];
while (head.indexOf("\r\n\r\n") == -1) {
final int read = in.read(buffer);
if (read == -1) {
return;
}
head.append(new String(buffer, 0, read, UTF_8));
}
long contentLength = 0;
for (String line : head.substring(0, head.indexOf("\r\n\r\n")).split("\r\n")) {
if (line.toLowerCase().startsWith("content-length:")) {
contentLength = Long.parseLong(line.substring(line.indexOf(':') + 1).trim());
}
}
long consumed = head.length() - head.indexOf("\r\n\r\n") - 4;
final byte[] sink = new byte[8192];
while (consumed < contentLength) {
final int read = in.read(sink, 0, (int) Math.min(sink.length, contentLength - consumed));
if (read == -1) {
return;
}
consumed += read;
}
}

@Override
public Feign.Builder newBuilder() {
return Feign.builder().client(new Http2Client());
Expand Down