nomagick commited on
Commit
512f225
·
unverified ·
1 Parent(s): c19ca21

fix: sideload redirections

Browse files
Files changed (1) hide show
  1. src/services/curl.ts +9 -5
src/services/curl.ts CHANGED
@@ -303,6 +303,7 @@ export class CurlControl extends AsyncService {
303
  const r = await this.urlToFile1Shot(nextHopUrl, opts);
304
 
305
  if ([301, 302, 307, 308].includes(r.statusCode)) {
 
306
  const headers = r.headers[r.headers.length - 1];
307
  const location: string | undefined = headers.Location || headers.location;
308
 
@@ -319,14 +320,18 @@ export class CurlControl extends AsyncService {
319
  }
320
 
321
  if (!location && !setCookieHeader) {
322
- throw new ServiceBadAttemptError(`Failed to access ${urlToCrawl}: Bad redirection from ${nextHopUrl}`);
 
 
 
 
 
323
  }
324
  if (!location && cookieRedirects > 1) {
325
  throw new ServiceBadAttemptError(`Failed to access ${urlToCrawl}: Browser required to solve complex cookie preconditions.`);
326
  }
327
 
328
  nextHopUrl = new URL(location || '', nextHopUrl);
329
- fakeHeaderInfos.push(...r.headers);
330
  leftRedirection -= 1;
331
  continue;
332
  }
@@ -360,10 +365,9 @@ export class CurlControl extends AsyncService {
360
  }
361
  if (headers.result?.code && [301, 302, 307, 308].includes(headers.result.code)) {
362
  const location = headers.Location || headers.location;
363
- if (!location) {
364
- throw new Error(`Bad redirection: ${curlResult.headers.length} times`);
365
  }
366
- finalURL = new URL(location, finalURL);
367
  }
368
  }
369
  const lastHeaders = curlResult.headers[curlResult.headers.length - 1];
 
303
  const r = await this.urlToFile1Shot(nextHopUrl, opts);
304
 
305
  if ([301, 302, 307, 308].includes(r.statusCode)) {
306
+ fakeHeaderInfos.push(...r.headers);
307
  const headers = r.headers[r.headers.length - 1];
308
  const location: string | undefined = headers.Location || headers.location;
309
 
 
320
  }
321
 
322
  if (!location && !setCookieHeader) {
323
+ // Follow curl behavior
324
+ return {
325
+ statusCode: r.statusCode,
326
+ data: r.data,
327
+ headers: fakeHeaderInfos.concat(r.headers),
328
+ };
329
  }
330
  if (!location && cookieRedirects > 1) {
331
  throw new ServiceBadAttemptError(`Failed to access ${urlToCrawl}: Browser required to solve complex cookie preconditions.`);
332
  }
333
 
334
  nextHopUrl = new URL(location || '', nextHopUrl);
 
335
  leftRedirection -= 1;
336
  continue;
337
  }
 
365
  }
366
  if (headers.result?.code && [301, 302, 307, 308].includes(headers.result.code)) {
367
  const location = headers.Location || headers.location;
368
+ if (location) {
369
+ finalURL = new URL(location, finalURL);
370
  }
 
371
  }
372
  }
373
  const lastHeaders = curlResult.headers[curlResult.headers.length - 1];