Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -411,10 +411,10 @@ public long open(DataSpec dataSpec) throws HttpDataSourceException {
throw new InvalidContentTypeException(contentType, dataSpec);
}

// If we requested a range starting from a non-zero position and received a 200 rather than a
// 206, then the server does not support partial requests. We'll need to manually skip to the
// requested position.
long bytesToSkip = responseCode == 200 && dataSpec.position != 0 ? dataSpec.position : 0;
// If we requested a range starting from a non-zero position and did not receive a 206, then
// the server does not support partial requests. We'll need to manually skip to the requested
// position.
long bytesToSkip = responseCode != 206 && dataSpec.position != 0 ? dataSpec.position : 0;

// Determine the length of the data to be read, after skipping.
boolean isCompressed = isCompressed(connection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,10 +540,10 @@ public long open(DataSpec dataSpec) throws HttpDataSourceException {
}
}

// If we requested a range starting from a non-zero position and received a 200 rather than a
// 206, then the server does not support partial requests. We'll need to manually skip to the
// requested position.
long bytesToSkip = responseCode == 200 && dataSpec.position != 0 ? dataSpec.position : 0;
// If we requested a range starting from a non-zero position and did not receive a 206, then
// the server does not support partial requests. We'll need to manually skip to the requested
// position.
long bytesToSkip = responseCode != 206 && dataSpec.position != 0 ? dataSpec.position : 0;

// Calculate the content length.
if (!isCompressed(responseInfo)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -670,10 +670,10 @@ public long open(DataSpec dataSpec) throws HttpDataSourceException {
}
}

// If we requested a range starting from a non-zero position and received a 200 rather than a
// 206, then the server does not support partial requests. We'll need to manually skip to the
// requested position.
long bytesToSkip = responseCode == 200 && dataSpec.position != 0 ? dataSpec.position : 0;
// If we requested a range starting from a non-zero position and did not receive a 206, then
// the server does not support partial requests. We'll need to manually skip to the requested
// position.
long bytesToSkip = responseCode != 206 && dataSpec.position != 0 ? dataSpec.position : 0;

// Calculate the content length.
if (!isCompressed(responseInfo)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ private constructor(
throw HttpDataSource.InvalidContentTypeException(contentType, dataSpec)
}

val bytesToSkip = if (responseCode == 200 && dataSpec.position != 0L) dataSpec.position else 0L
val bytesToSkip = if (responseCode != 206 && dataSpec.position != 0L) dataSpec.position else 0L

if (dataSpec.length != C.LENGTH_UNSET.toLong()) {
bytesToRead = dataSpec.length
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,10 @@ public long open(DataSpec dataSpec) throws HttpDataSourceException {
throw new InvalidContentTypeException(contentType, dataSpec);
}

// If we requested a range starting from a non-zero position and received a 200 rather than a
// 206, then the server does not support partial requests. We'll need to manually skip to the
// requested position.
long bytesToSkip = responseCode == 200 && dataSpec.position != 0 ? dataSpec.position : 0;
// If we requested a range starting from a non-zero position and did not receive a 206, then
// the server does not support partial requests. We'll need to manually skip to the requested
// position.
long bytesToSkip = responseCode != 206 && dataSpec.position != 0 ? dataSpec.position : 0;

// Determine the length of the data to be read, after skipping.
if (dataSpec.length != C.LENGTH_UNSET) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ public class HttpDataSourceTestEnv extends ExternalResource {
.setExtraResponseHeaders(EXTRA_HEADERS)
.build();

public static final WebServerDispatcher.Resource RANGE_NOT_SUPPORTED_NON_AUTHORITATIVE =
new WebServerDispatcher.Resource.Builder()
.setPath("/doesnt/support/range-requests-non-authoritative")
.setData(TestUtil.buildTestData(/* length= */ 20, seed++))
.supportsRangeRequests(false)
.setNonRangeResponseCode(203)
.setExtraResponseHeaders(EXTRA_HEADERS)
.build();

public static final WebServerDispatcher.Resource GZIP_ENABLED =
new WebServerDispatcher.Resource.Builder()
.setPath("/gzip/enabled")
Expand Down Expand Up @@ -139,6 +148,7 @@ public ImmutableList<DataSourceContractTest.TestResource> getServedResources() {
createTestResource("range not supported", RANGE_NOT_SUPPORTED),
createTestResource(
"range not supported, length unknown", RANGE_NOT_SUPPORTED_LENGTH_UNKNOWN),
createTestResource("range not supported (203)", RANGE_NOT_SUPPORTED_NON_AUTHORITATIVE),
// TODO: crbug.com/41313195 - Set these back to false when CronetDataSource and
// HttpEngineDataSource are able to set Accept-Encoding: identity.
createTestResource("gzip enabled", GZIP_ENABLED, /* mayResolveToUnknownLength= */ true),
Expand Down Expand Up @@ -196,6 +206,7 @@ protected void before() throws Throwable {
RANGE_SUPPORTED_LENGTH_UNKNOWN,
RANGE_NOT_SUPPORTED,
RANGE_NOT_SUPPORTED_LENGTH_UNKNOWN,
RANGE_NOT_SUPPORTED_NON_AUTHORITATIVE,
GZIP_ENABLED,
GZIP_FORCED,
POST_EMPTY_REQUEST_BODY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public static final class Builder {
private byte[] requestBody;
private byte @MonotonicNonNull [] data;
private boolean supportsRangeRequests;
private int nonRangeResponseCode;
private boolean includesContentLengthInRangeResponses;
private boolean resolvesToUnknownLength;
private @GzipSupport int gzipSupport;
Expand All @@ -131,6 +132,7 @@ public Builder() {
this.httpMethod = HTTP_METHOD_GET;
this.requestHeaders = ImmutableListMultimap.of();
this.requestBody = EMPTY_BYTE_ARRAY;
this.nonRangeResponseCode = 200;
this.includesContentLengthInRangeResponses = true;
this.gzipSupport = GZIP_SUPPORT_DISABLED;
this.extraResponseHeaders = ImmutableListMultimap.of();
Expand All @@ -143,6 +145,7 @@ private Builder(Resource resource) {
this.requestBody = resource.getRequestBody();
this.data = resource.getData();
this.supportsRangeRequests = resource.supportsRangeRequests();
this.nonRangeResponseCode = resource.getNonRangeResponseCode();
this.includesContentLengthInRangeResponses =
resource.includesContentLengthInRangeResponses();
this.resolvesToUnknownLength = resource.resolvesToUnknownLength();
Expand Down Expand Up @@ -229,6 +232,19 @@ public Builder supportsRangeRequests(boolean supportsRangeRequests) {
return this;
}

/**
* Sets the HTTP response code to use when a range is not supported or not requested.
*
* <p>Defaults to 200.
*
* @return this builder, for convenience.
*/
@CanIgnoreReturnValue
public Builder setNonRangeResponseCode(int nonRangeResponseCode) {
this.nonRangeResponseCode = nonRangeResponseCode;
return this;
}

/**
* Sets if RFC 7233 HTTP 206 range responses should include a {@code Content-Length} header.
*
Expand Down Expand Up @@ -304,6 +320,7 @@ public Resource build() {
requestBody,
checkNotNull(data),
supportsRangeRequests,
nonRangeResponseCode,
includesContentLengthInRangeResponses,
resolvesToUnknownLength,
gzipSupport,
Expand All @@ -317,6 +334,7 @@ public Resource build() {
private final byte[] requestBody;
private final byte[] data;
private final boolean supportsRangeRequests;
private final int nonRangeResponseCode;
private final boolean includesContentLengthInRangeResponses;
private final boolean resolvesToUnknownLength;
private final @GzipSupport int gzipSupport;
Expand All @@ -329,6 +347,7 @@ private Resource(
byte[] requestBody,
byte[] data,
boolean supportsRangeRequests,
int nonRangeResponseCode,
boolean includesContentLengthInRangeResponses,
boolean resolvesToUnknownLength,
@GzipSupport int gzipSupport,
Expand All @@ -339,12 +358,18 @@ private Resource(
this.requestBody = requestBody;
this.data = data;
this.supportsRangeRequests = supportsRangeRequests;
this.nonRangeResponseCode = nonRangeResponseCode;
this.includesContentLengthInRangeResponses = includesContentLengthInRangeResponses;
this.resolvesToUnknownLength = resolvesToUnknownLength;
this.gzipSupport = gzipSupport;
this.extraResponseHeaders = extraResponseHeaders;
}

/** The HTTP response code when a range is not supported or not requested. */
public int getNonRangeResponseCode() {
return nonRangeResponseCode;
}

/** Returns the path this resource is available at. */
public String getPath() {
return path;
Expand Down Expand Up @@ -497,6 +522,7 @@ public MockResponse dispatch(RecordedRequest request) {

@Nullable String rangeHeader = request.getHeader(HttpHeaders.RANGE);
if (!resource.supportsRangeRequests() || rangeHeader == null) {
response.setResponseCode(resource.getNonRangeResponseCode());
setResponseBody(
response,
preferredContentCoding,
Expand Down