Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cannot download chunked resource #346

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public long getContentLength() {
}

public long getRangeRight() {
if (contentLength == CHUNKED_CONTENT_LENGTH) {
return CHUNKED_CONTENT_LENGTH;
}
return startOffset + contentLength - 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ boolean isRunSyncThreadValid() {

public void inspectComplete(int blockIndex) throws IOException {
final BlockInfo blockInfo = info.getBlock(blockIndex);
if (blockInfo.getContentLength() == Util.CHUNKED_CONTENT_LENGTH) return;
if (!Util.isCorrectFull(blockInfo.getCurrentOffset(), blockInfo.getContentLength())) {
throw new IOException("The current offset on block-info isn't update correct, "
+ blockInfo.getCurrentOffset() + " != " + blockInfo.getContentLength()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ public DownloadConnection.Connected interceptConnect(DownloadChain chain) throws
}

String range = "bytes=" + blockInfo.getRangeLeft() + "-";
range += blockInfo.getRangeRight();
if (!info.isChunked()) {
range += blockInfo.getRangeRight();
}

connection.addHeader(RANGE, range);
Util.d(TAG, "AssembleHeaderRange (" + task.getId() + ") block(" + blockIndex + ") "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,6 @@ public void getRangeRight() {
public void chunked() {
BlockInfo info = new BlockInfo(0, CHUNKED_CONTENT_LENGTH);
assertThat(info.getContentLength()).isEqualTo(CHUNKED_CONTENT_LENGTH);
assertThat(info.getRangeRight()).isEqualTo(CHUNKED_CONTENT_LENGTH);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,17 @@ public void inspectComplete_notFull() throws IOException {
multiPointOutputStream.inspectComplete(1);
}

@Test
public void inspectComplete_ignoreWithChunkResource() throws IOException {
final BlockInfo blockInfo = mock(BlockInfo.class);
when(info.getBlock(1)).thenReturn(blockInfo);

when(blockInfo.getContentLength()).thenReturn(-1L);
when(blockInfo.getCurrentOffset()).thenReturn(10L);

multiPointOutputStream.inspectComplete(1);
}

@Test(expected = IOException.class)
public void inspectComplete_syncException() throws IOException {
multiPointOutputStream.syncException = new IOException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,29 @@ public void interceptConnect_range() throws IOException {
assertThat(nameCaptor.getAllValues()).containsExactlyInAnyOrder(RANGE, USER_AGENT);
assertThat(valueCaptor.getAllValues())
.containsExactlyInAnyOrder("bytes=20-29", "OkDownload/" + BuildConfig.VERSION_NAME);

}

@Test
public void interceptConnect_chunkResourceRange() throws IOException {
final DownloadChain chain = mockDownloadChain();
DownloadConnection connection = chain.getConnectionOrCreate();
final BreakpointInfo info = chain.getInfo();

when(info.isChunked()).thenReturn(true);
when(info.getBlockCount()).thenReturn(1);
when(info.getBlock(0)).thenReturn(new BlockInfo(0, -1));

interceptor.interceptConnect(chain);

ArgumentCaptor<String> nameCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> valueCaptor = ArgumentCaptor.forClass(String.class);

verify(connection, times(2)).addHeader(nameCaptor.capture(), valueCaptor.capture());

assertThat(nameCaptor.getAllValues()).containsExactlyInAnyOrder(RANGE, USER_AGENT);
assertThat(valueCaptor.getAllValues())
.containsExactlyInAnyOrder("bytes=0-", "OkDownload/" + BuildConfig.VERSION_NAME);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ class SingleActivity : BaseSampleActivity() {
initAction(actionView, actionTv, statusTv, progressBar)
}

// http://www.fyuniot.com:1885/Tools/Down.ashx?Code=93d698e8-674f-4324-8707-9282e8e07802, this
// url return 200 with range 0-0 get method.
// http://www.httpwatch.com/httpgallery/chunked/chunkedimage.aspx?0.04400023248109086, this url
// is chunked resource.
private fun initTask() {
val filename = "single-test"
val url = "https://cdn.llscdn.com/yy/files/xs8qmxn8-lls-LLS-5.8-800-20171207-111607.apk"
Expand Down Expand Up @@ -128,13 +132,15 @@ class SingleActivity : BaseSampleActivity() {
progressBar: ProgressBar,
actionTv: TextView
) {
var totalLength: Long = 0
var totalLength: Long = -1
var readableTotalLength: String? = null
task?.enqueue4WithSpeed(
onTaskStart = { statusTv.setText(R.string.task_start) },
onInfoReadyWithSpeed = { _, info, _, _ ->
statusTv.setText(R.string.info_ready)
totalLength = info.totalLength
if (!info.isChunked) {
totalLength = info.totalLength
}
readableTotalLength = Util.humanReadableBytes(totalLength, true)
DemoUtil.calcProgressToView(progressBar, info.totalOffset, totalLength)
},
Expand All @@ -155,6 +161,9 @@ class SingleActivity : BaseSampleActivity() {
val statusWithSpeed = cause.toString() + " " + taskSpeed.averageSpeed()
statusTv.text = statusWithSpeed
actionTv.setText(R.string.start)
if (task.info?.isChunked == true) {
DemoUtil.calcProgressToView(progressBar, 1, 1)
}
// remove mark
task.tag = null
if (cause == EndCause.COMPLETED) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,16 @@ public class DemoUtil {
"https://cdn.llscdn.com/yy/files/tkzpx40x-lls-LLS-5.7-785-20171108-111118.apk";

public static void calcProgressToView(ProgressBar progressBar, long offset, long total) {
final float percent = (float) offset / total;
progressBar.setProgress((int) (percent * progressBar.getMax()));
if (total < 0) {
progressBar.setIndeterminate(true);
} else if (total > 0) {
progressBar.setIndeterminate(false);
final float percent = (float) offset / total;
progressBar.setProgress((int) (percent * progressBar.getMax()));
} else {
progressBar.setIndeterminate(false);
progressBar.setProgress(progressBar.getMax());
}
}


Expand Down
2 changes: 2 additions & 0 deletions sample/src/main/res/xml/network_security_config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@
<domain includeSubdomains="true">d1.music.126.net</domain>
<domain includeSubdomains="true">dldir1.qq.com</domain>
<domain includeSubdomains="true">imgsrc.baidu.com</domain>
<domain includeSubdomains="true">www.fyuniot.com</domain>
<domain includeSubdomains="true">www.httpwatch.com</domain>
</domain-config>
</network-security-config>