From fb43578fdd680e0eec109c3a09fcd49509d3721d Mon Sep 17 00:00:00 2001 From: Yanlong Wang Date: Sat, 8 Mar 2025 17:18:53 +0800 Subject: [PATCH] fix: curl implicit redirect --- src/services/curl.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/services/curl.ts b/src/services/curl.ts index 5256adf..bdc941b 100644 --- a/src/services/curl.ts +++ b/src/services/curl.ts @@ -10,7 +10,7 @@ import { AssertionFailureError, FancyFile } from 'civkit'; import { ServiceBadAttemptError, TempFileManager } from '../shared'; import { createBrotliDecompress, createInflate, createGunzip } from 'zlib'; import { ZSTDDecompress } from 'simple-zstd'; -import _ from 'lodash'; +import _, { set } from 'lodash'; import { Readable } from 'stream'; import { AsyncLocalContext } from './async-context'; @@ -278,9 +278,6 @@ export class CurlControl extends AsyncService { if ([301, 302, 307, 308].includes(r.statusCode)) { const headers = r.headers[r.headers.length - 1]; const location = headers.Location || headers.location; - if (!location) { - throw new AssertionFailureError(`Failed to access ${urlToCrawl}: Bad redirection from ${nextHopUrl}`); - } const setCookieHeader = headers['Set-Cookie'] || headers['set-cookie']; if (setCookieHeader) { @@ -291,6 +288,10 @@ export class CurlControl extends AsyncService { } } + if (!location && !setCookieHeader) { + throw new AssertionFailureError(`Failed to access ${urlToCrawl}: Bad redirection from ${nextHopUrl}`); + } + nextHopUrl = new URL(location, nextHopUrl); fakeHeaderInfos.push(...r.headers); leftRedirection -= 1;