1
0
mirror of https://github.com/square/okhttp.git synced 2026-01-24 04:02:07 +03:00

Drop Content-Length when redirected from POST to GET.

https://github.com/square/okhttp/issues/296
This commit is contained in:
jwilson
2013-08-24 11:56:39 -04:00
parent ca7d09b4e8
commit a948ffce1e
3 changed files with 43 additions and 0 deletions

View File

@@ -331,6 +331,11 @@ public class HttpURLConnectionImpl extends HttpURLConnection implements Policy {
httpEngine = newHttpEngine(retryMethod, rawRequestHeaders, httpEngine.getConnection(),
(RetryableOutputStream) requestBody);
if (requestBody == null) {
// Drop the Content-Length header when redirected from POST to GET.
httpEngine.getRequestHeaders().removeContentLength();
}
}
}

View File

@@ -213,6 +213,18 @@ public final class RequestHeaders {
this.contentLength = contentLength;
}
/**
* Remove the Content-Length headers. Call this when dropping the body on a
* request or response, such as when a redirect changes the method from POST
* to GET.
*/
public void removeContentLength() {
if (contentLength != -1) {
headers.removeAll("Content-Length");
contentLength = -1;
}
}
public void setUserAgent(String userAgent) {
if (this.userAgent != null) {
headers.removeAll("User-Agent");

View File

@@ -1722,6 +1722,32 @@ public final class URLConnectionTest {
assertEquals("GET /page2 HTTP/1.1", page2.getRequestLine());
}
@Test public void redirectedPostStripsRequestBodyHeaders() throws Exception {
server.enqueue(new MockResponse()
.setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
.addHeader("Location: /page2"));
server.enqueue(new MockResponse().setBody("Page 2"));
server.play();
HttpURLConnection connection = client.open(server.getUrl("/page1"));
connection.setDoOutput(true);
connection.addRequestProperty("Content-Length", "4");
connection.addRequestProperty("Content-Type", "text/plain; charset=utf-8");
connection.addRequestProperty("Transfer-Encoding", "identity");
OutputStream outputStream = connection.getOutputStream();
outputStream.write("ABCD".getBytes("UTF-8"));
outputStream.close();
assertEquals("Page 2", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
assertEquals("POST /page1 HTTP/1.1", server.takeRequest().getRequestLine());
RecordedRequest page2 = server.takeRequest();
assertEquals("GET /page2 HTTP/1.1", page2.getRequestLine());
assertContainsNoneMatching(page2.getHeaders(), "Content-Length");
assertContains(page2.getHeaders(), "Content-Type: text/plain; charset=utf-8");
assertContains(page2.getHeaders(), "Transfer-Encoding: identity");
}
@Test public void response305UseProxy() throws Exception {
server.play();
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_USE_PROXY)