Skip to content

Commit cb4d44b

Browse files
committed
JdkClientHttpRequest does not support Content-Length 0
This commit ensures the correct HttpRequest.BodyPublisher is used with Content-Length 0. Closes gh-31451
1 parent 3cb700c commit cb4d44b

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

spring-web/src/main/java/org/springframework/http/client/JdkClientHttpRequest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2023 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -129,9 +129,12 @@ private HttpRequest.BodyPublisher bodyPublisher(HttpHeaders headers, @Nullable B
129129
BYTE_MAPPER, this.executor);
130130

131131
long contentLength = headers.getContentLength();
132-
if (contentLength != -1) {
132+
if (contentLength > 0) {
133133
return HttpRequest.BodyPublishers.fromPublisher(outputStreamPublisher, contentLength);
134134
}
135+
else if (contentLength == 0) {
136+
return HttpRequest.BodyPublishers.noBody();
137+
}
135138
else {
136139
return HttpRequest.BodyPublishers.fromPublisher(outputStreamPublisher);
137140
}

spring-web/src/test/java/org/springframework/http/client/JdkClientHttpRequestFactoryTests.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2023 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
import org.junit.jupiter.api.Test;
2525

2626
import org.springframework.http.HttpMethod;
27+
import org.springframework.http.HttpStatus;
2728
import org.springframework.http.HttpStatusCode;
2829
import org.springframework.lang.Nullable;
2930

@@ -67,12 +68,22 @@ public void httpMethods() throws Exception {
6768

6869
@Test
6970
public void customizeDisallowedHeaders() throws IOException {
70-
ClientHttpRequest request = factory.createRequest(URI.create(this.baseUrl + "/status/299"), HttpMethod.PUT);
71+
ClientHttpRequest request = this.factory.createRequest(URI.create(this.baseUrl + "/status/299"), HttpMethod.PUT);
7172
request.getHeaders().set("Expect", "299");
7273

7374
try (ClientHttpResponse response = request.execute()) {
7475
assertThat(response.getStatusCode()).as("Invalid status code").isEqualTo(HttpStatusCode.valueOf(299));
7576
}
7677
}
7778

79+
@Test // gh-31451
80+
public void contentLength0() throws IOException {
81+
BufferingClientHttpRequestFactory bufferingFactory = new BufferingClientHttpRequestFactory(this.factory);
82+
ClientHttpRequest request = bufferingFactory.createRequest(URI.create(this.baseUrl + "/methods/get"), HttpMethod.GET);
83+
84+
try (ClientHttpResponse response = request.execute()) {
85+
assertThat(response.getStatusCode()).as("Invalid response status").isEqualTo(HttpStatus.OK);
86+
}
87+
}
88+
7889
}

0 commit comments

Comments
 (0)