Skip to content

Commit

Permalink
UNDERTOW-967 Range requests do not handle ranges that exceed the reso…
Browse files Browse the repository at this point in the history
…urce content length correctly
  • Loading branch information
stuartwdouglas committed Jan 24, 2017
1 parent 1b80b44 commit 2e3d5a3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
12 changes: 5 additions & 7 deletions core/src/main/java/io/undertow/util/ByteRange.java
Expand Up @@ -147,15 +147,13 @@ public RangeResponseResult getResponseResult(final long resourceContentLength, S

if(start == -1 ) {
//suffix range
long toWrite = end;
if(toWrite >= 0) {
rangeLength = toWrite;
} else {
if(end < 0){
//ignore the range request
return new RangeResponseResult(0, 0, 0, "bytes */" + resourceContentLength, StatusCodes.REQUEST_RANGE_NOT_SATISFIABLE);
}
start = resourceContentLength - end;
start = Math.max(resourceContentLength - end, 0);
end = resourceContentLength - 1;
rangeLength = resourceContentLength - start;
} else if(end == -1) {
//prefix range
long toWrite = resourceContentLength - start;
Expand All @@ -167,11 +165,11 @@ public RangeResponseResult getResponseResult(final long resourceContentLength, S
}
end = resourceContentLength - 1;
} else {
end = Math.min(end, resourceContentLength - 1);
if(start >= resourceContentLength || start > end) {
return new RangeResponseResult(0, 0, 0, "bytes */" + resourceContentLength, StatusCodes.REQUEST_RANGE_NOT_SATISFIABLE);
}
long toWrite = end - start + 1;
rangeLength = toWrite;
rangeLength = end - start + 1;
}
return new RangeResponseResult(start, end, rangeLength, "bytes " + start + "-" + end + "/" + resourceContentLength, StatusCodes.PARTIAL_CONTENT);
}
Expand Down
Expand Up @@ -95,6 +95,22 @@ public void runTest(String path, boolean etag) throws IOException, InterruptedEx
Assert.assertEquals( "bytes 2-3/10", result.getFirstHeader(Headers.CONTENT_RANGE_STRING).getValue());

get = new HttpGet(DefaultServer.getDefaultServerURL() + path);
get.addHeader(Headers.RANGE_STRING, "bytes=3-1000");
result = client.execute(get);
Assert.assertEquals(StatusCodes.PARTIAL_CONTENT, result.getStatusLine().getStatusCode());
response = EntityUtils.toString(result.getEntity());
Assert.assertEquals("3456789", response);
Assert.assertEquals( "bytes 3-9/10", result.getFirstHeader(Headers.CONTENT_RANGE_STRING).getValue());

get = new HttpGet(DefaultServer.getDefaultServerURL() + path);
get.addHeader(Headers.RANGE_STRING, "bytes=3-9");
result = client.execute(get);
Assert.assertEquals(StatusCodes.PARTIAL_CONTENT, result.getStatusLine().getStatusCode());
response = EntityUtils.toString(result.getEntity());
Assert.assertEquals("3456789", response);
Assert.assertEquals( "bytes 3-9/10", result.getFirstHeader(Headers.CONTENT_RANGE_STRING).getValue());
get = new HttpGet(DefaultServer.getDefaultServerURL() + path);

get.addHeader(Headers.RANGE_STRING, "bytes=0-0");
result = client.execute(get);
Assert.assertEquals(StatusCodes.PARTIAL_CONTENT, result.getStatusLine().getStatusCode());
Expand Down
Expand Up @@ -117,6 +117,21 @@ public void testRangeRequest() throws IOException, InterruptedException {
String response = HttpClientUtils.readResponse(result);
Assert.assertEquals("--", response);

get = new HttpGet(uri);
get.addHeader(Headers.RANGE_STRING, "bytes=3-100");
result = client.execute(get);
Assert.assertEquals(StatusCodes.PARTIAL_CONTENT, result.getStatusLine().getStatusCode());
response = EntityUtils.toString(result.getEntity());
Assert.assertEquals("3456789", response);
Assert.assertEquals("bytes 3-9/10", result.getFirstHeader(Headers.CONTENT_RANGE_STRING).getValue());

get = new HttpGet(uri);
get.addHeader(Headers.RANGE_STRING, "bytes=3-9");
result = client.execute(get);
Assert.assertEquals(StatusCodes.PARTIAL_CONTENT, result.getStatusLine().getStatusCode());
response = EntityUtils.toString(result.getEntity());
Assert.assertEquals("3456789", response);
Assert.assertEquals("bytes 3-9/10", result.getFirstHeader(Headers.CONTENT_RANGE_STRING).getValue());

get = new HttpGet(uri);
get.addHeader(Headers.RANGE_STRING, "bytes=2-3");
Expand Down Expand Up @@ -166,6 +181,14 @@ public void testRangeRequest() throws IOException, InterruptedException {
Assert.assertEquals("9", response);
Assert.assertEquals("bytes 9-9/10", result.getFirstHeader(Headers.CONTENT_RANGE_STRING).getValue());

get = new HttpGet(uri);
get.addHeader(Headers.RANGE_STRING, "bytes=-100");
result = client.execute(get);
Assert.assertEquals(StatusCodes.PARTIAL_CONTENT, result.getStatusLine().getStatusCode());
response = EntityUtils.toString(result.getEntity());
Assert.assertEquals("0123456789", response);
Assert.assertEquals("bytes 0-9/10", result.getFirstHeader(Headers.CONTENT_RANGE_STRING).getValue());

get = new HttpGet(uri);
get.addHeader(Headers.RANGE_STRING, "bytes=99-100");
result = client.execute(get);
Expand Down

0 comments on commit 2e3d5a3

Please sign in to comment.