Skip to content

Commit 57ef11a

Browse files
authored
fix: update NetHttpRequest to prevent silent retry of DELETE requests (#1472)
HttpURLConnection will silently retry `DELETE` requests. This behavior is similar to other existing JDK bugs (JDK-6382788[1], JDK-6427251[2]). google-http-java-client already contains a workaround for POST and PUT requests NetHttpRequest.java#L108-L112, but does not account for `DELETE` with an empty body. This change adds handling for DELETE to leverage the same workaround as POST and PUT. [1] https://bugs.java.com/bugdatabase/view_bug.do?bug_id=6382788 [2] https://bugs.java.com/bugdatabase/view_bug.do?bug_id=6427251 Fixes #1471
1 parent 135bd25 commit 57ef11a

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

google-http-client/src/main/java/com/google/api/client/http/javanet/NetHttpRequest.java

+3
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ LowLevelHttpResponse execute(final OutputWriter outputWriter) throws IOException
141141
Preconditions.checkArgument(
142142
contentLength == 0, "%s with non-zero content length is not supported", requestMethod);
143143
}
144+
} else if ("DELETE".equals(connection.getRequestMethod())) {
145+
connection.setDoOutput(true);
146+
connection.setFixedLengthStreamingMode(0L);
144147
}
145148
// connect
146149
boolean successfulConnection = false;

google-http-client/src/main/java/com/google/api/client/testing/http/javanet/MockHttpURLConnection.java

+24
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public class MockHttpURLConnection extends HttpURLConnection {
4242

4343
/** Whether {@link #doOutput} was called. */
4444
private boolean doOutputCalled;
45+
/** Whether {@link #setFixedLengthStreamingMode(int)} was called. */
46+
private boolean setFixedLengthStreamingModeIntCalled = false;
47+
/** Whether {@link #setFixedLengthStreamingMode(long)} was called. */
48+
private boolean setFixedLengthStreamingModeLongCalled = false;
4549

4650
/**
4751
* Output stream or {@code null} to throw an {@link UnknownServiceException} when {@link
@@ -205,4 +209,24 @@ public String getHeaderField(String name) {
205209
public int getChunkLength() {
206210
return chunkLength;
207211
}
212+
213+
@Override
214+
public void setFixedLengthStreamingMode(int contentLength) {
215+
this.setFixedLengthStreamingModeIntCalled = true;
216+
super.setFixedLengthStreamingMode(contentLength);
217+
}
218+
219+
@Override
220+
public void setFixedLengthStreamingMode(long contentLength) {
221+
this.setFixedLengthStreamingModeLongCalled = true;
222+
super.setFixedLengthStreamingMode(contentLength);
223+
}
224+
225+
public boolean isSetFixedLengthStreamingModeIntCalled() {
226+
return setFixedLengthStreamingModeIntCalled;
227+
}
228+
229+
public boolean isSetFixedLengthStreamingModeLongCalled() {
230+
return setFixedLengthStreamingModeLongCalled;
231+
}
208232
}

google-http-client/src/test/java/com/google/api/client/http/javanet/NetHttpRequestTest.java

+13
Original file line numberDiff line numberDiff line change
@@ -233,4 +233,17 @@ public void testChunkedLengthNotSet() throws Exception {
233233
assertEquals(connection.getChunkLength(), -1);
234234
assertEquals("6", request.getRequestProperty("Content-Length"));
235235
}
236+
237+
// see https://github.com/googleapis/google-http-java-client/issues/1471 for more details
238+
@Test
239+
public void testDeleteSetsContentLengthToZeroWithoutContent() throws Exception {
240+
MockHttpURLConnection connection = new MockHttpURLConnection(new URL(HttpTesting.SIMPLE_URL));
241+
connection.setRequestMethod("DELETE");
242+
NetHttpRequest request = new NetHttpRequest(connection);
243+
request.execute();
244+
245+
assertTrue(connection.doOutputCalled());
246+
assertTrue(connection.isSetFixedLengthStreamingModeLongCalled());
247+
assertFalse(connection.isSetFixedLengthStreamingModeIntCalled());
248+
}
236249
}

0 commit comments

Comments
 (0)