Skip to content

Commit

Permalink
修复链接预览功能无法正确拼接301跳转中的相对路径的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyuesaves committed Jun 24, 2024
1 parent 0e9e4a0 commit 10e32dc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
24 changes: 18 additions & 6 deletions src/main_modules/getWeb.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ export function get(url, redirects = 0) {
error: "Too many redirects",
});
}
log("请求地址", url);
const urlData = new URL(url);
const defaultHeaders = {
"User-Agent": config.global.UA,
accept: "text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8",
"accept-language": "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
"accept-encoding": "gzip, identity",
"cache-control": "no-cache",
connection: "keep-alive",
};
if (config.proxy.enabled && config.proxy.url) {
const proxy = new URL(config.proxy.url);
return new Promise((resolve) => {
Expand All @@ -27,8 +36,8 @@ export function get(url, redirects = 0) {
port: proxy.port,
path: urlData.href,
headers: {
...defaultHeaders,
Host: urlData.host,
"User-Agent": config.global.UA,
},
},
(res) => {
Expand All @@ -50,9 +59,7 @@ export function get(url, redirects = 0) {
{
hostname: urlData.hostname,
path: urlData.pathname,
headers: {
"User-Agent": config.global.UA,
},
headers: defaultHeaders,
},
(res) => {
handle(res, resolve);
Expand All @@ -70,11 +77,16 @@ export function get(url, redirects = 0) {

function handle(res, resolve) {
const contentType = res.headers["content-type"];
log(res.headers);
log("返回请求头", res.req.path, res.statusCode, res.headers);
// 处理重定向
if ([301, 302].includes(res.statusCode)) {
res.destroy();
resolve(get(res.headers.location, ++redirects));
if (res.headers.location.startsWith("http")) {
resolve(get(res.headers.location, ++redirects));
} else {
const href = new URL(res.headers.location, res.req.path).href;
resolve(get(href, ++redirects));
}
} else if (res.statusCode === 200) {
if (contentType?.startsWith("text/html")) {
let chunks = [];
Expand Down
1 change: 0 additions & 1 deletion src/main_modules/getWebPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ onUpdateConfig(() => {
* 如果URL不是HTML页面,则成功状态将为false,并提供错误消息。
*/
async function getMeatData(url) {
log("目标为HTML,开始请求内容");
const res = await get(url);
if (!res.success) {
return res;
Expand Down

0 comments on commit 10e32dc

Please sign in to comment.