Fix download validation, TLS security and task coordination

This commit is contained in:
tongtong committed 2026-11-20 11:08:31 +08:00
1 parent e9d9281333
commit 94e19b4fe4
11 files changed
+163 -80

No files matched your search

+1
View File
@@ -38,6 +38,7 @@ android {
}
dependencies {
testImplementation 'junit:junit:4.13.2'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(path: ':PublicComponent')
@@ -40,10 +40,11 @@ public final class HttpDGInfoTask implements IInfoTask {
private DGTaskWrapper wrapper;
private final Object LOCK = new Object();
private ExecutorService mPool = null;
private boolean getLenComplete = false;
private volatile boolean getLenComplete = false;
private AtomicInteger count = new AtomicInteger();
private AtomicInteger failCount = new AtomicInteger();
private boolean isStop = false, isCancel = false;
private volatile boolean isStop = false, isCancel = false;
private volatile boolean finished;
public interface DGInfoCallback extends Callback {
@@ -89,15 +90,17 @@ public final class HttpDGInfoTask implements IInfoTask {
public void stop() {
isStop = true;
if (mPool != null) {
mPool.shutdown();
mPool.shutdownNow();
}
notifyLock();
}
@Override public void cancel() {
isCancel = true;
if (mPool != null) {
mPool.shutdown();
mPool.shutdownNow();
}
notifyLock();
}
@Override public void run() {
@@ -108,16 +111,20 @@ public final class HttpDGInfoTask implements IInfoTask {
((DGInfoCallback)callback).onStop(0);
return;
}
if (isStop || isCancel) return;
// 处理组合任务大小未知的情况
if (wrapper.isUnknownSize()) {
mPool = Executors.newCachedThreadPool();
mPool = Executors.newFixedThreadPool(4);
getGroupSize();
try {
synchronized (LOCK) {
LOCK.wait();
while (!finished && !isStop && !isCancel) {
LOCK.wait();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
stop();
}
if (!mPool.isShutdown()) {
mPool.shutdown();
@@ -134,9 +141,12 @@ public final class HttpDGInfoTask implements IInfoTask {
* 获取组合任务大小,使用该方式获取到的组合任务大小,子任务不需要再重新获取文件大小
*/
private void getGroupSize() {
new Thread(new Runnable() {
@Override public void run() {
if (wrapper.getSubTaskWrapper().isEmpty()) {
checkGetSizeComplete(0, 0);
return;
}
for (DTaskWrapper dTaskWrapper : wrapper.getSubTaskWrapper()) {
if (isStop || isCancel) return;
DownloadEntity subEntity = dTaskWrapper.getEntity();
if (subEntity.getFileSize() > 0) {
count.getAndIncrement();
@@ -150,24 +160,28 @@ public final class HttpDGInfoTask implements IInfoTask {
cloneHeader(dTaskWrapper);
HttpDFileInfoTask infoTask = new HttpDFileInfoTask(dTaskWrapper);
infoTask.setCallback(subCallback);
mPool.execute(infoTask);
try {
mPool.execute(infoTask);
} catch (java.util.concurrent.RejectedExecutionException e) {
if (!isStop && !isCancel) throw e;
return;
}
}
}
}).start();
}
/**
* 检查组合任务大小是否获取完成,获取完成后取消阻塞,并设置组合任务大小
*/
private void checkGetSizeComplete(int count, int failCount) {
private synchronized void checkGetSizeComplete(int count, int failCount) {
if (finished) return;
if (isStop || isCancel) {
ALog.w(TAG, "任务已停止或已取消,isStop = " + isStop + ", isCancel = " + isCancel);
notifyLock();
return;
}
if (failCount == wrapper.getSubTaskWrapper().size()) {
callback.onFail(wrapper.getEntity(), new AriaHTTPException("获取子任务长度失败"), false);
notifyLock();
callback.onFail(wrapper.getEntity(), new AriaHTTPException("获取子任务长度失败"), false);
return;
}
if (count == wrapper.getSubTaskWrapper().size()) {
@@ -180,13 +194,14 @@ public final class HttpDGInfoTask implements IInfoTask {
wrapper.getEntity().update();
getLenComplete = true;
ALog.d(TAG, String.format("获取组合任务长度完成,组合任务总长度:%s,失败的子任务数:%s", size, failCount));
callback.onSucceed(wrapper.getKey(), new CompleteInfo());
notifyLock();
callback.onSucceed(wrapper.getKey(), new CompleteInfo());
}
}
private void notifyLock() {
synchronized (LOCK) {
finished = true;
LOCK.notifyAll();
}
}
@@ -114,6 +114,11 @@ final class HttpDThreadTaskAdapter extends BaseHttpThreadTaskAdapter {
}
}
if (mTaskWrapper.isSupportBP()) {
HttpRangeValidator.validate(conn.getResponseCode(), conn.getHeaderField("Content-Range"),
conn.getContentEncoding(), getThreadRecord().startLocation,
getThreadRecord().endLocation);
}
is = new BufferedInputStream(ConnectionHelp.convertInputStream(conn));
if (getThreadConfig().tempUri != null) {
// 分区存储:通过 Uri 写入,支持断点续传
@@ -183,7 +188,6 @@ final class HttpDThreadTaskAdapter extends BaseHttpThreadTaskAdapter {
is.close();
}
if (conn != null) {
conn.getInputStream().close();
conn.disconnect();
}
} catch (IOException e) {
@@ -259,8 +263,10 @@ final class HttpDThreadTaskAdapter extends BaseHttpThreadTaskAdapter {
break;
} else {
bf.flip();
foc.write(bf);
bf.compact();
while (bf.hasRemaining()) {
foc.write(bf);
}
bf.clear();
progress(len);
}
}
@@ -0,0 +1,28 @@
package com.arialyy.aria.http.download;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** Validates a response before any resumed bytes reach the destination. */
final class HttpRangeValidator {
private static final Pattern RANGE = Pattern.compile("bytes (\\d+)-(\\d+)/(\\d+|\\*)");
static void validate(int status, String contentRange, String encoding, long start, long end)
throws IOException {
if (status != 206 || contentRange == null
|| (encoding != null && !"identity".equalsIgnoreCase(encoding))) {
throw new IOException("服务器未返回有效的未压缩 Range 响应");
}
Matcher match = RANGE.matcher(contentRange.trim());
try {
if (!match.matches() || Long.parseLong(match.group(1)) != start
|| Long.parseLong(match.group(2)) != end - 1
|| (!"*".equals(match.group(3)) && Long.parseLong(match.group(3)) < end)) {
throw new IOException("Content-Range 与请求区间不匹配:" + contentRange);
}
} catch (NumberFormatException e) {
throw new IOException("无效的 Content-Range:" + contentRange, e);
}
}
}
@@ -0,0 +1,25 @@
package com.arialyy.aria.http.download;
import java.io.IOException;
import org.junit.Test;
public class HttpRangeValidatorTest {
@Test public void acceptsMatchingRange() throws Exception {
HttpRangeValidator.validate(206, "bytes 10-19/100", null, 10, 20);
}
@Test(expected = IOException.class) public void rejectsIgnoredRange() throws Exception {
HttpRangeValidator.validate(200, null, null, 10, 20);
}
@Test(expected = IOException.class) public void rejectsWrongOffset() throws Exception {
HttpRangeValidator.validate(206, "bytes 0-9/100", null, 10, 20);
}
@Test(expected = IOException.class) public void rejectsTruncatedRange() throws Exception {
HttpRangeValidator.validate(206, "bytes 10-18/100", null, 10, 20);
}
@Test(expected = IOException.class) public void rejectsCompressedRange() throws Exception {
HttpRangeValidator.validate(206, "bytes 10-19/100", "gzip", 10, 20);
}
@Test(expected = IOException.class) public void rejectsOverflow() throws Exception {
HttpRangeValidator.validate(206, "bytes 10-9999999999999999999999/100", null, 10, 20);
}
}