mirror of
https://github.com/qaiu/netdisk-fast-download.git
synced 2026-08-27 20:12:02 +00:00
fix: use tryParseJson in CeTool version detection to avoid premature promise failure on non-JSON responses
This commit is contained in:
committed by
GitHub
parent
7299fd8762
commit
4bc880a41b
@@ -391,6 +391,28 @@ public abstract class PanBase implements IPanTool, Closeable {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 尝试将响应体解析为 JsonObject,失败时静默返回 null(不调用 fail())。
|
||||
* 适用于版本探测等场景:响应可能是 HTML,此时不应立即终止解析流程。
|
||||
*
|
||||
* @param res HttpResponse
|
||||
* @return JsonObject,若响应体不是合法 JSON 则返回 null
|
||||
*/
|
||||
protected JsonObject tryParseJson(HttpResponse<?> res) {
|
||||
String contentEncoding = res.getHeader("Content-Encoding");
|
||||
try {
|
||||
if ("gzip".equalsIgnoreCase(contentEncoding)) {
|
||||
String decompressed = decompressGzip((Buffer) res.body());
|
||||
return new JsonObject(decompressed);
|
||||
} else {
|
||||
return res.bodyAsJsonObject();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.debug("响应体不是合法JSON,跳过: {}", e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* body To text的封装, 会自动处理异常, 会自动解压gzip
|
||||
* @param res HttpResponse
|
||||
|
||||
@@ -81,18 +81,15 @@ public class CeTool extends PanBase {
|
||||
// 禁止跟随重定向:assertPublicHost 只校验初始 host,自动 30x 会绕过 SSRF 防护
|
||||
clientNoRedirects.getAbs(pingUrlV4).send().onSuccess(res -> {
|
||||
if (res.statusCode() == 200) {
|
||||
try {
|
||||
JsonObject json = asJson(res);
|
||||
// v4 ping 成功且返回有效JSON,使用 Ce4Tool
|
||||
if (json != null && !json.isEmpty()) {
|
||||
log.debug("检测到Cloudreve 4.x (通过v4 ping)");
|
||||
delegateToCe4Tool();
|
||||
return;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// JSON解析失败,继续尝试 v3
|
||||
log.debug("v4 ping返回非JSON响应,尝试v3");
|
||||
// 使用 tryParseJson 而非 asJson,避免非JSON响应(如HTML)提前终止整个解析流程
|
||||
JsonObject json = tryParseJson(res);
|
||||
// v4 ping 成功且返回有效JSON,使用 Ce4Tool
|
||||
if (json != null && !json.isEmpty()) {
|
||||
log.debug("检测到Cloudreve 4.x (通过v4 ping)");
|
||||
delegateToCe4Tool();
|
||||
return;
|
||||
}
|
||||
log.debug("v4 ping返回非JSON响应,尝试v3");
|
||||
}
|
||||
// v4 ping失败或返回非JSON,尝试 v3
|
||||
tryV3Ping(baseUrl, key, pwd);
|
||||
@@ -111,18 +108,15 @@ public class CeTool extends PanBase {
|
||||
|
||||
clientNoRedirects.getAbs(pingUrlV3).send().onSuccess(res -> {
|
||||
if (res.statusCode() == 200) {
|
||||
try {
|
||||
JsonObject json = asJson(res);
|
||||
// v3 ping 成功且返回有效JSON,进一步验证是否为 v3
|
||||
if (json != null && !json.isEmpty()) {
|
||||
// 尝试调用 v3 share API 来确认
|
||||
verifyV3AndParse(baseUrl, key, pwd);
|
||||
return;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// JSON解析失败,不是Cloudreve盘
|
||||
log.debug("v3 ping返回非JSON响应,不是Cloudreve盘");
|
||||
// 使用 tryParseJson 而非 asJson,避免非JSON响应(如HTML)提前终止整个解析流程
|
||||
JsonObject json = tryParseJson(res);
|
||||
// v3 ping 成功且返回有效JSON,进一步验证是否为 v3
|
||||
if (json != null && !json.isEmpty()) {
|
||||
// 尝试调用 v3 share API 来确认
|
||||
verifyV3AndParse(baseUrl, key, pwd);
|
||||
return;
|
||||
}
|
||||
log.debug("v3 ping返回非JSON响应,不是Cloudreve盘");
|
||||
}
|
||||
// v3 ping失败,不是Cloudreve盘
|
||||
log.debug("v3 ping失败,尝试下一个解析器");
|
||||
|
||||
Reference in New Issue
Block a user