Skip to content

Commit 53eec48

Browse files
committed
Fix HttpUrlConnection DELETE without body
The following commit allowed HTTP DELETE with body: 584b831 However it broke buffered requests even without a body since JDK 1.6 and 1.7 do not support calls to getOutputStream with HTTP DELETE. This commit set the doOutput flag back to false if the actual buffered body is 0 length. Issue: SPR-12361
1 parent 1803348 commit 53eec48

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ protected ListenableFuture<ClientHttpResponse> executeInternal(
7878
@Override
7979
public ClientHttpResponse call() throws Exception {
8080
SimpleBufferingClientHttpRequest.addHeaders(connection, headers);
81+
// JDK < 1.8 doesn't support getOutputStream with HTTP DELETE
82+
if (HttpMethod.DELETE.equals(getMethod()) && bufferedOutput.length == 0) {
83+
connection.setDoOutput(false);
84+
}
8185
if (connection.getDoOutput() && outputStreaming) {
8286
connection.setFixedLengthStreamingMode(bufferedOutput.length);
8387
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.http.HttpHeaders;
2727
import org.springframework.http.HttpMethod;
2828
import org.springframework.util.FileCopyUtils;
29+
import org.springframework.util.ObjectUtils;
2930
import org.springframework.util.StringUtils;
3031

3132
/**
@@ -69,6 +70,11 @@ public URI getURI() {
6970
protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException {
7071
addHeaders(this.connection, headers);
7172

73+
// JDK < 1.8 doesn't support getOutputStream with HTTP DELETE
74+
if (HttpMethod.DELETE.equals(getMethod()) && bufferedOutput.length == 0) {
75+
this.connection.setDoOutput(false);
76+
}
77+
7278
if (this.connection.getDoOutput() && this.outputStreaming) {
7379
this.connection.setFixedLengthStreamingMode(bufferedOutput.length);
7480
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@
1818

1919
import static org.junit.Assert.assertEquals;
2020

21+
import java.io.ByteArrayOutputStream;
2122
import java.io.IOException;
23+
import java.io.OutputStream;
2224
import java.net.HttpURLConnection;
2325
import java.net.ProtocolException;
26+
import java.net.URI;
2427
import java.net.URL;
2528

2629
import org.junit.Test;
@@ -56,6 +59,14 @@ public void prepareConnectionWithRequestBody() throws Exception {
5659
testRequestBodyAllowed(uri, "DELETE", true);
5760
}
5861

62+
@Test
63+
public void deleteWithoutBodyDoesNotRaiseException() throws Exception {
64+
HttpURLConnection connection = new TestHttpURLConnection(new URL("http://example.com"));
65+
((SimpleClientHttpRequestFactory) this.factory).prepareConnection(connection, "DELETE");
66+
SimpleBufferingClientHttpRequest request = new SimpleBufferingClientHttpRequest(connection, false);
67+
request.execute();
68+
}
69+
5970
private void testRequestBodyAllowed(URL uri, String httpMethod, boolean allowed) throws IOException {
6071
HttpURLConnection connection = new TestHttpURLConnection(uri);
6172
((SimpleClientHttpRequestFactory) this.factory).prepareConnection(connection, httpMethod);

0 commit comments

Comments
 (0)