Fix CronetDataSource redirect logic

- Remove usage of deprecated postBody field
- Transform POST to GET on redirect, as in DefaultHttpDataSource

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=210092576
This commit is contained in:
olly 2018-08-24 06:53:31 -07:00 committed by Oliver Woodman
parent 924a76d532
commit 3196bc40db

View File

@ -606,11 +606,9 @@ public class CronetDataSource extends BaseDataSource implements HttpDataSource {
if (request != currentUrlRequest) { if (request != currentUrlRequest) {
return; return;
} }
if (currentDataSpec.postBody != null) { if (currentDataSpec.httpMethod == DataSpec.HTTP_METHOD_POST) {
int responseCode = info.getHttpStatusCode(); int responseCode = info.getHttpStatusCode();
// The industry standard is to disregard POST redirects when the status code is 307 or 308. // The industry standard is to disregard POST redirects when the status code is 307 or 308.
// For other redirect response codes the POST request is converted to a GET request and the
// redirect is followed.
if (responseCode == 307 || responseCode == 308) { if (responseCode == 307 || responseCode == 308) {
exception = exception =
new InvalidResponseCodeException(responseCode, info.getAllHeaders(), currentDataSpec); new InvalidResponseCodeException(responseCode, info.getAllHeaders(), currentDataSpec);
@ -627,7 +625,23 @@ public class CronetDataSource extends BaseDataSource implements HttpDataSource {
request.followRedirect(); request.followRedirect();
} else { } else {
currentUrlRequest.cancel(); currentUrlRequest.cancel();
DataSpec redirectUrlDataSpec = currentDataSpec.withUri(Uri.parse(newLocationUrl)); DataSpec redirectUrlDataSpec;
if (currentDataSpec.httpMethod == DataSpec.HTTP_METHOD_POST) {
// For POST redirects that aren't 307 or 308, the redirect is followed but request is
// transformed into a GET.
redirectUrlDataSpec =
new DataSpec(
Uri.parse(newLocationUrl),
DataSpec.HTTP_METHOD_GET,
/* httpBody= */ null,
currentDataSpec.absoluteStreamPosition,
currentDataSpec.position,
currentDataSpec.length,
currentDataSpec.key,
currentDataSpec.flags);
} else {
redirectUrlDataSpec = currentDataSpec.withUri(Uri.parse(newLocationUrl));
}
UrlRequest.Builder requestBuilder; UrlRequest.Builder requestBuilder;
try { try {
requestBuilder = buildRequestBuilder(redirectUrlDataSpec); requestBuilder = buildRequestBuilder(redirectUrlDataSpec);