Skip to content
Merged
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
26 changes: 24 additions & 2 deletions hc5/src/main/java/feign/hc5/ApacheHttp5Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,13 @@ ClassicHttpRequest toClassicHttpRequest(Request request, Request.Options options
// request body
// final Body requestBody = request.requestBody();
byte[] data = request.body();
if (data != null) {
// GET and HEAD are bodyless by RFC 7231 semantics; when their body is absent or
// empty, no entity is set so the Apache client does not send a 'Content-Length: 0'
// header, which picky load balancers reject (see #2085). Every other method keeps
// the empty entity, so an intentionally empty body still sends 'Content-Length: 0'.
boolean omitEmptyEntity =
canOmitEmptyEntity(request.httpMethod()) && (data == null || data.length == 0);
if (data != null && !omitEmptyEntity) {
HttpEntity entity;
if (request.isBinary()) {
entity = new ByteArrayEntity(data, null);
Expand All @@ -176,13 +182,29 @@ ClassicHttpRequest toClassicHttpRequest(Request request, Request.Options options
entity = new DeflateCompressingEntity(entity);
}
requestBuilder.setEntity(entity);
} else {
} else if (!omitEmptyEntity) {
requestBuilder.setEntity(new ByteArrayEntity(new byte[0], null));
}

return requestBuilder.build();
}

/**
* Whether a request whose body is absent or empty can be sent without an (empty) entity. GET and
* HEAD are bodyless per RFC 7231, and Apache HttpClient advertises 'Content-Length: 0' for an
* empty entity, which picky load balancers reject (see #2085). Every other method keeps the empty
* entity, so its request shape stays as before.
*/
private static boolean canOmitEmptyEntity(Request.HttpMethod httpMethod) {
switch (httpMethod) {
case GET:
case HEAD:
return true;
default:
return false;
}
}

private ContentType getContentType(Request request) {
ContentType contentType = null;
for (final Map.Entry<String, Collection<String>> entry : request.headers().entrySet()) {
Expand Down
97 changes: 97 additions & 0 deletions hc5/src/test/java/feign/hc5/ApacheHttp5ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,21 @@
import feign.Feign.Builder;
import feign.FeignException;
import feign.Request;
import feign.RequestLine;
import feign.Response;
import feign.client.AbstractClientTest;
import feign.jaxrs.JAXRSContract;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.RecordedRequest;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.junit.jupiter.api.Test;

/** Tests client-specific behavior, such as ensuring Content-Length is sent when specified. */
Expand Down Expand Up @@ -62,6 +67,88 @@ void queryParamsAreRespectedWhenBodyIsEmpty() throws InterruptedException {
assertThat(request2.getBody().readString(StandardCharsets.UTF_8)).isEqualTo("");
}

/**
* GET is bodyless per RFC 7231, so a request without a body must not carry a 'Content-Length: 0'
* header: picky load balancers reject it. See #2085.
*/
@Test
void getWithoutBodyDoesNotSendContentLength() throws Exception {
server.enqueue(new MockResponse().setBody("foo"));

TestInterface api =
newBuilder().target(TestInterface.class, "http://localhost:" + server.getPort());

api.get();

RecordedRequest recordedRequest = server.takeRequest();
assertThat(recordedRequest.getMethod()).isEqualTo("GET");
assertThat(recordedRequest.getHeader("Content-Length")).isNull();
}

/** An explicitly empty GET body is still bodyless and must not send 'Content-Length: 0'. */
@Test
void getWithEmptyBodyDoesNotSendContentLength() throws Exception {
server.enqueue(new MockResponse().setBody("foo"));

BodylessMethodInterface api =
newBuilder().target(BodylessMethodInterface.class, "http://localhost:" + server.getPort());

api.getWithBody("");

RecordedRequest recordedRequest = server.takeRequest();
assertThat(recordedRequest.getMethod()).isEqualTo("GET");
assertThat(recordedRequest.getHeader("Content-Length")).isNull();
assertThat(recordedRequest.getBody().readUtf8()).isEmpty();
}

/** A GET with a non-empty body keeps the entity, so the body is transmitted. */
@Test
void getWithBodyKeepsTheEntity() throws Exception {
ApacheHttp5Client client = new ApacheHttp5Client();
Request request =
Request.create(
Request.HttpMethod.GET,
"http://localhost/",
Collections.emptyMap(),
Request.Body.create("foo".getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8),
null);

ClassicHttpRequest httpRequest = client.toClassicHttpRequest(request, new Request.Options());

assertThat(httpRequest.getEntity()).isNotNull();
assertThat(EntityUtils.toString(httpRequest.getEntity())).isEqualTo("foo");
}

/** HEAD, like GET, is bodyless per RFC 7231 and must not send 'Content-Length: 0'. */
@Test
void headWithoutBodyDoesNotSendContentLength() throws Exception {
server.enqueue(new MockResponse());

BodylessMethodInterface api =
newBuilder().target(BodylessMethodInterface.class, "http://localhost:" + server.getPort());

api.head();

RecordedRequest recordedRequest = server.takeRequest();
assertThat(recordedRequest.getMethod()).isEqualTo("HEAD");
assertThat(recordedRequest.getHeader("Content-Length")).isNull();
}

/** Methods that can carry a body keep 'Content-Length: 0' for an intentionally empty body. */
@Test
void postWithEmptyBodySendsContentLengthZero() throws Exception {
server.enqueue(new MockResponse().setBody("foo"));

TestInterface api =
newBuilder().target(TestInterface.class, "http://localhost:" + server.getPort());

api.post("");

RecordedRequest recordedRequest = server.takeRequest();
assertThat(recordedRequest.getMethod()).isEqualTo("POST");
assertThat(recordedRequest.getHeader("Content-Length")).isEqualTo("0");
}

@Test
void followRedirectsIsTrue() throws InterruptedException {
final JaxRsTestInterface testInterface = buildTestInterface();
Expand Down Expand Up @@ -136,4 +223,14 @@ public interface JaxRsTestInterface {
@Path("/withRequestOptions")
String withOptions(Request.Options options);
}

/** Used to exercise GET/HEAD requests that carry an explicit (possibly empty) body. */
interface BodylessMethodInterface {

@RequestLine("GET /")
String getWithBody(String body);

@RequestLine("HEAD /")
Response head();
}
}