Commit 661b1402 by olly Committed by Oliver Woodman

Partial cleanup of CronetDataSource II

- Allow null Content-Type in response headers.
- Inline validateResponse, just because it makes it clearer
  what thread it's being executed on when inlined.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=135375063
parent ba56f916
...@@ -85,6 +85,7 @@ public class CronetDataSource extends UrlRequest.Callback implements HttpDataSou ...@@ -85,6 +85,7 @@ public class CronetDataSource extends UrlRequest.Callback implements HttpDataSou
public static final int DEFAULT_READ_TIMEOUT_MILLIS = 8 * 1000; public static final int DEFAULT_READ_TIMEOUT_MILLIS = 8 * 1000;
private static final String TAG = "CronetDataSource"; private static final String TAG = "CronetDataSource";
private static final String CONTENT_TYPE = "Content-Type";
private static final Pattern CONTENT_RANGE_HEADER_PATTERN = private static final Pattern CONTENT_RANGE_HEADER_PATTERN =
Pattern.compile("^bytes (\\d+)-(\\d+)/(\\d+)$"); Pattern.compile("^bytes (\\d+)-(\\d+)/(\\d+)$");
// The size of read buffer passed to cronet UrlRequest.read(). // The size of read buffer passed to cronet UrlRequest.read().
...@@ -337,7 +338,25 @@ public class CronetDataSource extends UrlRequest.Callback implements HttpDataSou ...@@ -337,7 +338,25 @@ public class CronetDataSource extends UrlRequest.Callback implements HttpDataSou
return; return;
} }
try { try {
validateResponse(info); // Check for a valid response code.
int responseCode = info.getHttpStatusCode();
if (responseCode < 200 || responseCode > 299) {
InvalidResponseCodeException exception = new InvalidResponseCodeException(
responseCode, info.getAllHeaders(), currentDataSpec);
if (responseCode == 416) {
exception.initCause(new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE));
}
throw exception;
}
// Check for a valid content type.
if (contentTypePredicate != null) {
List<String> contentTypeHeaders = info.getAllHeaders().get(CONTENT_TYPE);
String contentType = contentTypeHeaders == null || contentTypeHeaders.isEmpty() ? null
: contentTypeHeaders.get(0);
if (!contentTypePredicate.evaluate(contentType)) {
throw new InvalidContentTypeException(contentType, currentDataSpec);
}
}
responseInfo = info; responseInfo = info;
if (getIsCompressed(info)) { if (getIsCompressed(info)) {
...@@ -347,8 +366,7 @@ public class CronetDataSource extends UrlRequest.Callback implements HttpDataSou ...@@ -347,8 +366,7 @@ public class CronetDataSource extends UrlRequest.Callback implements HttpDataSou
contentLength = getContentLength(info.getAllHeaders()); contentLength = getContentLength(info.getAllHeaders());
// If a specific length is requested and a specific length is returned but the 2 don't match // If a specific length is requested and a specific length is returned but the 2 don't match
// it's an error. // it's an error.
if (currentDataSpec.length != C.LENGTH_UNSET if (currentDataSpec.length != C.LENGTH_UNSET && contentLength != C.LENGTH_UNSET
&& contentLength != C.LENGTH_UNSET
&& currentDataSpec.length != contentLength) { && currentDataSpec.length != contentLength) {
throw new OpenException("Content length did not match requested length", currentDataSpec, throw new OpenException("Content length did not match requested length", currentDataSpec,
getStatus(request)); getStatus(request));
...@@ -427,36 +445,14 @@ public class CronetDataSource extends UrlRequest.Callback implements HttpDataSou ...@@ -427,36 +445,14 @@ public class CronetDataSource extends UrlRequest.Callback implements HttpDataSou
private void fillCurrentRequestPostBody(UrlRequest.Builder urlRequestBuilder, DataSpec dataSpec) private void fillCurrentRequestPostBody(UrlRequest.Builder urlRequestBuilder, DataSpec dataSpec)
throws HttpDataSourceException { throws HttpDataSourceException {
if (dataSpec.postBody != null) { if (dataSpec.postBody == null) {
if (!requestProperties.containsKey("Content-Type")) { return;
throw new OpenException("POST requests must set a Content-Type header", dataSpec,
Status.IDLE);
}
urlRequestBuilder.setUploadDataProvider(
new ByteArrayUploadDataProvider(dataSpec.postBody), executor);
}
}
private void validateResponse(UrlResponseInfo info) throws HttpDataSourceException {
// Check for a valid response code.
int responseCode = info.getHttpStatusCode();
if (responseCode < 200 || responseCode > 299) {
InvalidResponseCodeException exception = new InvalidResponseCodeException(
responseCode, info.getAllHeaders(), currentDataSpec);
if (responseCode == 416) {
exception.initCause(new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE));
}
throw exception;
} }
// Check for a valid content type. if (!requestProperties.containsKey(CONTENT_TYPE)) {
try { throw new OpenException("POST request must set Content-Type", dataSpec, Status.IDLE);
String contentType = info.getAllHeaders().get("Content-Type").get(0);
if (contentTypePredicate != null && !contentTypePredicate.evaluate(contentType)) {
throw new InvalidContentTypeException(contentType, currentDataSpec);
}
} catch (IndexOutOfBoundsException e) {
throw new InvalidContentTypeException(null, currentDataSpec);
} }
urlRequestBuilder.setUploadDataProvider(new ByteArrayUploadDataProvider(dataSpec.postBody),
executor);
} }
private boolean blockUntilConnectTimeout() { private boolean blockUntilConnectTimeout() {
...@@ -483,7 +479,6 @@ public class CronetDataSource extends UrlRequest.Callback implements HttpDataSou ...@@ -483,7 +479,6 @@ public class CronetDataSource extends UrlRequest.Callback implements HttpDataSou
} }
private static long getContentLength(Map<String, List<String>> headers) { private static long getContentLength(Map<String, List<String>> headers) {
// Logic copied from {@code DefaultHttpDataSource}
long contentLength = C.LENGTH_UNSET; long contentLength = C.LENGTH_UNSET;
List<String> contentLengthHeader = headers.get("Content-Length"); List<String> contentLengthHeader = headers.get("Content-Length");
if (contentLengthHeader != null if (contentLengthHeader != null
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment