Skip to content

基于自定义规则的番剧采集APP,支持流媒体在线观看,支持弹幕,支持实时超分辨率。

fix(rules): stop forcing https when normalizing episode URLs

Prediditdb93a1214c54
diff --git a/lib/utils/episode_url.dart b/lib/utils/episode_url.dart
index 28f9c5da..b6e9e541 100644
--- a/lib/utils/episode_url.dart
+++ b/lib/utils/episode_url.dart
@@ -7,33 +7,35 @@
/// 归一化规则:
/// - 去除首尾空白;空输入返回空串(调用方据此判断"无 URL")。
/// - 相对路径基于 [baseUrl] 补全为绝对 URL。
-/// - 统一协议口径到 `https`,避免 `http`/`https` 造成的失配
-/// (与既有抓取逻辑一致:源页抓取时本就强制 https)。
+/// - 与 [baseUrl] 同站(同 host、同显式端口)的 URL,协议统一到 [baseUrl]
+/// 声明的协议,避免同一集因 `http`/`https` 混用产生两个 key;跨站或端口
+/// 不同的 URL 保持原协议不动——规则站点可能仅支持 http(如自建反代),
+/// 改写协议会导致无法访问。
/// - 去除 path 多余尾斜杠(根路径 `/` 保留)。
/// - 去除空 query。
/// - 幂等:`normalizeEpisodeUrl(b, normalizeEpisodeUrl(b, x))` 等于
/// `normalizeEpisodeUrl(b, x)`。
///
-/// 归一化结果同时作为"身份 key"与可访问的"请求 URL"使用。由于既有抓取逻辑
-/// 已强制将源页 URL 升级为 https,这里统一到 https 不会改变实际可访问性。
+/// 归一化结果同时作为"身份 key"与可访问的"请求 URL"使用,因此归一化
+/// 不得改变 URL 实际指向的端点,协议口径以规则中用户声明的 [baseUrl] 为准。
String normalizeEpisodeUrl(String baseUrl, String raw) {
final trimmed = raw.trim();
if (trimmed.isEmpty) return '';
final rawUri = Uri.tryParse(trimmed);
+ final baseUri = Uri.tryParse(baseUrl.trim());
+ final hasValidBase =
+ baseUri != null && baseUri.hasScheme && baseUri.host.isNotEmpty;
Uri? resolved;
if (rawUri != null && rawUri.hasScheme && rawUri.host.isNotEmpty) {
// 已是绝对 URL。
resolved = rawUri;
- } else {
- final baseUri = Uri.tryParse(baseUrl.trim());
- if (baseUri != null && baseUri.hasScheme && baseUri.host.isNotEmpty) {
- try {
- resolved = baseUri.resolve(trimmed);
- } catch (_) {
- resolved = null;
- }
+ } else if (hasValidBase) {
+ try {
+ resolved = baseUri.resolve(trimmed);
+ } catch (_) {
+ resolved = null;
}
}
@@ -42,9 +44,15 @@ String normalizeEpisodeUrl(String baseUrl, String raw) {
return trimmed;
}
- // 统一协议到 https。
- if (resolved.scheme == 'http') {
- resolved = resolved.replace(scheme: 'https');
+ // 同站 URL 的协议统一到 baseUrl 声明的协议。
+ if (hasValidBase &&
+ _isHttpScheme(baseUri.scheme) &&
+ _isHttpScheme(resolved.scheme) &&
+ resolved.scheme != baseUri.scheme &&
+ resolved.host == baseUri.host &&
+ resolved.hasPort == baseUri.hasPort &&
+ (!resolved.hasPort || resolved.port == baseUri.port)) {
+ resolved = resolved.replace(scheme: baseUri.scheme);
}
// 去除 path 尾斜杠(根路径除外)。
@@ -68,3 +76,5 @@ String normalizeEpisodeUrl(String baseUrl, String raw) {
return normalized.toString();
}
+
+bool _isHttpScheme(String scheme) => scheme == 'http' || scheme == 'https';
diff --git a/test/episode_url_test.dart b/test/episode_url_test.dart
index 4820d156..27cb6218 100644
--- a/test/episode_url_test.dart
+++ b/test/episode_url_test.dart
@@ -24,7 +24,7 @@ void main() {
expect(normalizeEpisodeUrl(baseUrl, absolute), absolute);
});
- test('http 与 https 同站归一到同一 key', () {
+ test('http 与 https 同站归一到同一 key(协议以 baseUrl 为准)', () {
final viaHttp =
normalizeEpisodeUrl(baseUrl, 'http://www.example.com/play/123.html');
final viaHttps =
@@ -33,10 +33,47 @@ void main() {
expect(viaHttp, 'https://www.example.com/play/123.html');
});
- test('http baseUrl 下相对路径也归一到 https', () {
+ test('http baseUrl 下相对路径保持 http', () {
expect(
normalizeEpisodeUrl('http://www.example.com', '/play/123.html'),
- 'https://www.example.com/play/123.html',
+ 'http://www.example.com/play/123.html',
+ );
+ });
+
+ test('http baseUrl 下同站绝对 URL 保持 http', () {
+ expect(
+ normalizeEpisodeUrl(
+ 'http://www.example.com', 'http://www.example.com/play/123.html'),
+ 'http://www.example.com/play/123.html',
+ );
+ });
+
+ test('http baseUrl 下同站 https URL 归一到 http(同一 key)', () {
+ expect(
+ normalizeEpisodeUrl(
+ 'http://www.example.com', 'https://www.example.com/play/123.html'),
+ 'http://www.example.com/play/123.html',
+ );
+ });
+
+ test('跨站 URL 保持原协议不改写', () {
+ expect(
+ normalizeEpisodeUrl(baseUrl, 'http://cdn.other.com/play/123'),
+ 'http://cdn.other.com/play/123',
+ );
+ });
+
+ test('同 host 不同显式端口不改写协议', () {
+ expect(
+ normalizeEpisodeUrl(baseUrl, 'http://www.example.com:8080/play/123'),
+ 'http://www.example.com:8080/play/123',
+ );
+ });
+
+ test('带端口的 http baseUrl 下相对路径保持 http 与端口', () {
+ expect(
+ normalizeEpisodeUrl('http://www.example.com:8080', '/play/123.html'),
+ 'http://www.example.com:8080/play/123.html',
);
});
@@ -106,10 +143,13 @@ void main() {
'https://www.example.com/',
'',
];
- for (final raw in cases) {
- final once = normalizeEpisodeUrl(baseUrl, raw);
- final twice = normalizeEpisodeUrl(baseUrl, once);
- expect(twice, once, reason: 'not idempotent for: "$raw"');
+ for (final base in <String>[baseUrl, 'http://www.example.com']) {
+ for (final raw in cases) {
+ final once = normalizeEpisodeUrl(base, raw);
+ final twice = normalizeEpisodeUrl(base, once);
+ expect(twice, once,
+ reason: 'not idempotent for: "$raw" (base: "$base")');
+ }
}
});
});