mirror of
https://github.com/HChaZZY/PiliPlus.git
synced 2025-12-06 09:13:48 +08:00
opt: parseRedirect use head (#787)
This commit is contained in:
committed by
GitHub
parent
ddf7d82656
commit
a46bde68f5
@@ -1,3 +1,4 @@
|
|||||||
|
import 'package:PiliPlus/utils/accounts/account.dart';
|
||||||
import 'package:PiliPlus/utils/page_utils.dart';
|
import 'package:PiliPlus/utils/page_utils.dart';
|
||||||
import 'package:dio/dio.dart';
|
import 'package:dio/dio.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
@@ -14,45 +15,30 @@ class UrlUtils {
|
|||||||
String url, [
|
String url, [
|
||||||
bool returnOri = false,
|
bool returnOri = false,
|
||||||
]) async {
|
]) async {
|
||||||
|
String? redirectUrl;
|
||||||
try {
|
try {
|
||||||
final response = await Request().get(
|
final response = await Request.dio.head(
|
||||||
url,
|
url,
|
||||||
options: Options(
|
options: Options(
|
||||||
followRedirects: false,
|
followRedirects: false,
|
||||||
validateStatus: (status) {
|
validateStatus: (status) {
|
||||||
return status == 200 || status == 301 || status == 302;
|
return 200 <= status! && status < 400;
|
||||||
},
|
},
|
||||||
),
|
extra: {'account': AnonymousAccount()}),
|
||||||
);
|
);
|
||||||
if (response.statusCode == 302 || response.statusCode == 301) {
|
if (response.isRedirect) {
|
||||||
String? redirectUrl = response.headers['location']?.firstOrNull;
|
redirectUrl = response.headers['location']?.firstOrNull;
|
||||||
debugPrint('redirectUrl: $redirectUrl');
|
debugPrint('redirectUrl: $redirectUrl');
|
||||||
if (redirectUrl != null) {
|
if (redirectUrl != null && !redirectUrl.startsWith('http')) {
|
||||||
if (redirectUrl.startsWith('/')) {
|
redirectUrl = Uri.parse(url).resolve(redirectUrl).toString();
|
||||||
return returnOri ? url : null;
|
|
||||||
}
|
|
||||||
if (redirectUrl.endsWith('/')) {
|
|
||||||
redirectUrl = redirectUrl.substring(0, redirectUrl.length - 1);
|
|
||||||
}
|
|
||||||
if (url.contains(redirectUrl)) {
|
|
||||||
if (url.endsWith('/')) {
|
|
||||||
url = url.substring(0, url.length - 1);
|
|
||||||
}
|
|
||||||
return url;
|
|
||||||
}
|
|
||||||
return redirectUrl;
|
|
||||||
} else {
|
|
||||||
if (returnOri && url.endsWith('/')) {
|
|
||||||
url = url.substring(0, url.length - 1);
|
|
||||||
}
|
|
||||||
return returnOri ? url : null;
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return returnOri ? url : null;
|
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (_) {}
|
||||||
return returnOri ? url : null;
|
if (returnOri && redirectUrl == null) redirectUrl = url;
|
||||||
|
if (redirectUrl?.endsWith('/') == true) {
|
||||||
|
redirectUrl = redirectUrl!.substring(0, redirectUrl.length - 1);
|
||||||
}
|
}
|
||||||
|
return redirectUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 匹配url路由跳转
|
// 匹配url路由跳转
|
||||||
|
|||||||
Reference in New Issue
Block a user