mirror of
https://github.com/wukko/cobalt.git
synced 2025-05-29 13:00:12 +02:00
api/url: replace user-agent argument with headers
in redirect helpers
This commit is contained in:
parent
b1bde25dee
commit
a6240d0192
@ -1,11 +1,11 @@
|
|||||||
import { request } from 'undici';
|
import { request } from 'undici';
|
||||||
const redirectStatuses = new Set([301, 302, 303, 307, 308]);
|
const redirectStatuses = new Set([301, 302, 303, 307, 308]);
|
||||||
|
|
||||||
export async function getRedirectingURL(url, dispatcher, userAgent) {
|
export async function getRedirectingURL(url, dispatcher, headers) {
|
||||||
const location = await request(url, {
|
const location = await request(url, {
|
||||||
dispatcher,
|
dispatcher,
|
||||||
method: 'HEAD',
|
method: 'HEAD',
|
||||||
headers: { 'user-agent': userAgent }
|
headers: headers
|
||||||
}).then(r => {
|
}).then(r => {
|
||||||
if (redirectStatuses.has(r.statusCode) && r.headers['location']) {
|
if (redirectStatuses.has(r.statusCode) && r.headers['location']) {
|
||||||
return r.headers['location'];
|
return r.headers['location'];
|
||||||
|
@ -527,7 +527,7 @@ export default function instagram(obj) {
|
|||||||
// for some reason instagram decides to return HTML
|
// for some reason instagram decides to return HTML
|
||||||
// instead of a redirect when requesting with a normal
|
// instead of a redirect when requesting with a normal
|
||||||
// browser user-agent
|
// browser user-agent
|
||||||
'curl/7.88.1'
|
{'User-Agent': 'curl/7.88.1'}
|
||||||
).then(match => instagram({
|
).then(match => instagram({
|
||||||
...obj, ...match,
|
...obj, ...match,
|
||||||
shareId: undefined
|
shareId: undefined
|
||||||
|
@ -53,27 +53,18 @@ export default async function(obj) {
|
|||||||
const accessToken = await getAccessToken();
|
const accessToken = await getAccessToken();
|
||||||
|
|
||||||
if (params.shortId) {
|
if (params.shortId) {
|
||||||
let url = await fetch(`https://www.reddit.com/video/${params.shortId}`, {
|
params = await resolveRedirectingURL(
|
||||||
headers: {
|
`https://www.reddit.com/video/${params.shortId}`,
|
||||||
'User-Agent': genericUserAgent,
|
obj.dispatcher,
|
||||||
'Authorization': `Bearer ${accessToken}`
|
{'User-Agent': genericUserAgent, 'Authorization': `Bearer ${accessToken}`}
|
||||||
}
|
);
|
||||||
}).then(r => r.url).catch(() => {});
|
|
||||||
|
|
||||||
if (!url) return { error: "fetch.fail" };
|
|
||||||
|
|
||||||
try {
|
|
||||||
params = extract(normalizeURL(url)).patternMatch;
|
|
||||||
} catch (error) {
|
|
||||||
return { error: "fetch.fail" };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!params.id && params.shareId) {
|
if (!params.id && params.shareId) {
|
||||||
params = await resolveRedirectingURL(
|
params = await resolveRedirectingURL(
|
||||||
`https://www.reddit.com/r/${params.sub}/s/${params.shareId}`,
|
`https://www.reddit.com/r/${params.sub}/s/${params.shareId}`,
|
||||||
obj.dispatcher,
|
obj.dispatcher,
|
||||||
genericUserAgent
|
{'User-Agent': genericUserAgent}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -239,11 +239,11 @@ export function extract(url) {
|
|||||||
return { host, patternMatch };
|
return { host, patternMatch };
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function resolveRedirectingURL(url, dispatcher, userAgent) {
|
export async function resolveRedirectingURL(url, dispatcher, headers) {
|
||||||
const originalService = getHostIfValid(normalizeURL(url));
|
const originalService = getHostIfValid(normalizeURL(url));
|
||||||
if (!originalService) return;
|
if (!originalService) return;
|
||||||
|
|
||||||
const canonicalURL = await getRedirectingURL(url, dispatcher, userAgent);
|
const canonicalURL = await getRedirectingURL(url, dispatcher, headers);
|
||||||
if (!canonicalURL) return;
|
if (!canonicalURL) return;
|
||||||
|
|
||||||
const { host, patternMatch } = extract(normalizeURL(canonicalURL));
|
const { host, patternMatch } = extract(normalizeURL(canonicalURL));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user