fix: curl redirection location

This commit is contained in:
Yanlong Wang 2025-03-08 17:20:38 +08:00
parent fb43578fdd
commit 63a2e15f4d
No known key found for this signature in database
GPG Key ID: C0A623C0BADF9F37

View File

@ -10,7 +10,7 @@ import { AssertionFailureError, FancyFile } from 'civkit';
import { ServiceBadAttemptError, TempFileManager } from '../shared'; import { ServiceBadAttemptError, TempFileManager } from '../shared';
import { createBrotliDecompress, createInflate, createGunzip } from 'zlib'; import { createBrotliDecompress, createInflate, createGunzip } from 'zlib';
import { ZSTDDecompress } from 'simple-zstd'; import { ZSTDDecompress } from 'simple-zstd';
import _, { set } from 'lodash'; import _ from 'lodash';
import { Readable } from 'stream'; import { Readable } from 'stream';
import { AsyncLocalContext } from './async-context'; import { AsyncLocalContext } from './async-context';
@ -277,7 +277,7 @@ export class CurlControl extends AsyncService {
if ([301, 302, 307, 308].includes(r.statusCode)) { if ([301, 302, 307, 308].includes(r.statusCode)) {
const headers = r.headers[r.headers.length - 1]; const headers = r.headers[r.headers.length - 1];
const location = headers.Location || headers.location; const location: string | undefined = headers.Location || headers.location;
const setCookieHeader = headers['Set-Cookie'] || headers['set-cookie']; const setCookieHeader = headers['Set-Cookie'] || headers['set-cookie'];
if (setCookieHeader) { if (setCookieHeader) {
@ -292,7 +292,7 @@ export class CurlControl extends AsyncService {
throw new AssertionFailureError(`Failed to access ${urlToCrawl}: Bad redirection from ${nextHopUrl}`); throw new AssertionFailureError(`Failed to access ${urlToCrawl}: Bad redirection from ${nextHopUrl}`);
} }
nextHopUrl = new URL(location, nextHopUrl); nextHopUrl = new URL(location || '', nextHopUrl);
fakeHeaderInfos.push(...r.headers); fakeHeaderInfos.push(...r.headers);
leftRedirection -= 1; leftRedirection -= 1;
continue; continue;