Skip to content

Commit

Permalink
Merge pull request square#2391 from rburgst/master
Browse files Browse the repository at this point in the history
PROPFIND redirects should maintain request body
  • Loading branch information
swankjesse authored Jul 31, 2016
2 parents 2494ec3 + b875778 commit 35283b8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
7 changes: 6 additions & 1 deletion okhttp-tests/src/test/java/okhttp3/CallTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1503,17 +1503,21 @@ private void enqueueRequestTimeoutResponses() {
assertEquals("Hello", request2.getBody().readUtf8());
}

@Test public void propfindRedirectsToPropfind() throws Exception {
@Test public void propfindRedirectsToPropfindAndMaintainsRequestBody() throws Exception {
// given
server.enqueue(new MockResponse()
.setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
.addHeader("Location: /page2")
.setBody("This page has moved!"));
server.enqueue(new MockResponse().setBody("Page 2"));

// when
Response response = client.newCall(new Request.Builder()
.url(server.url("/page1"))
.method("PROPFIND", RequestBody.create(MediaType.parse("text/plain"), "Request Body"))
.build()).execute();

// then
assertEquals("Page 2", response.body().string());

RecordedRequest page1 = server.takeRequest();
Expand All @@ -1522,6 +1526,7 @@ private void enqueueRequestTimeoutResponses() {

RecordedRequest page2 = server.takeRequest();
assertEquals("PROPFIND /page2 HTTP/1.1", page2.getRequestLine());
assertEquals("Request Body", page2.getBody().readUtf8());
}

@Test public void responseCookies() throws Exception {
Expand Down
4 changes: 4 additions & 0 deletions okhttp/src/main/java/okhttp3/internal/http/HttpMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public static boolean permitsRequestBody(String method) {
|| method.equals("LOCK"); // (WebDAV) body: create lock, without body: refresh lock
}

public static boolean redirectsWithBody(String method) {
return method.equals("PROPFIND"); // (WebDAV) redirects should also maintain the request body
}

public static boolean redirectsToGet(String method) {
// All requests but PROPFIND should redirect to a GET request.
return !method.equals("PROPFIND");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.Route;
import okhttp3.internal.connection.RouteException;
Expand Down Expand Up @@ -141,8 +142,8 @@ public StreamAllocation streamAllocation() {
if (priorResponse != null) {
response = response.newBuilder()
.priorResponse(priorResponse.newBuilder()
.body(null)
.build())
.body(null)
.build())
.build();
}

Expand Down Expand Up @@ -304,17 +305,21 @@ private Request followUpRequest(Response userResponse) throws IOException {
boolean sameScheme = url.scheme().equals(userResponse.request().url().scheme());
if (!sameScheme && !client.followSslRedirects()) return null;

// Redirects don't include a request body.
// Most redirects don't include a request body.
Request.Builder requestBuilder = userResponse.request().newBuilder();
if (HttpMethod.permitsRequestBody(method)) {
final boolean maintainBody = HttpMethod.redirectsWithBody(method);
if (HttpMethod.redirectsToGet(method)) {
requestBuilder.method("GET", null);
} else {
requestBuilder.method(method, null);
RequestBody requestBody = maintainBody ? userResponse.request().body() : null;
requestBuilder.method(method, requestBody);
}
if (!maintainBody) {
requestBuilder.removeHeader("Transfer-Encoding");
requestBuilder.removeHeader("Content-Length");
requestBuilder.removeHeader("Content-Type");
}
requestBuilder.removeHeader("Transfer-Encoding");
requestBuilder.removeHeader("Content-Length");
requestBuilder.removeHeader("Content-Type");
}

// When redirecting across hosts, drop all authentication headers. This
Expand Down

0 comments on commit 35283b8

Please sign in to comment.