Skip to content

Commit

Permalink
Update hotfix for 302 in current path.
Browse files Browse the repository at this point in the history
  • Loading branch information
VeNoMouS committed Feb 15, 2020
1 parent 31bd997 commit 214e3e7
Showing 1 changed file with 9 additions and 38 deletions.
47 changes: 9 additions & 38 deletions cloudscraper/__init__.py
Expand Up @@ -504,55 +504,26 @@ def updateAttr(obj, name, newValue):
return challengeSubmitResponse
else:
cloudflare_kwargs = deepcopy(kwargs)

# Merge the cookies from the chalengeSubmitResponse with the original request
if cloudflare_kwargs.get('cookies') != challengeSubmitResponse.cookies:
cloudflare_kwargs['cookies'] = updateAttr(
cloudflare_kwargs,
'cookies',
requests.cookies.merge_cookies(
cloudflare_kwargs.get('cookies', requests.cookies.RequestsCookieJar()),
challengeSubmitResponse.cookies
)
)
cloudflare_kwargs['headers'] = updateAttr(
cloudflare_kwargs,
'headers',
{'Referer': resp.url}
)

if not urlparse(challengeSubmitResponse.headers['Location']).netloc:
cloudflare_kwargs['headers'] = updateAttr(
cloudflare_kwargs,
'headers',
{'Referer': '{}://{}'.format(urlParsed.scheme, urlParsed.netloc)}
)

# nasty hack for shitty 302 in current path
# needs to be replaced with soemthing better...
location = challengeSubmitResponse.headers['Location']

if not location.startswith('/') or location.startswith('./'):
if location.startswith('./'):
location = location[2:]

origPath = os.path.dirname(urlParsed.path)
if not origPath.endswith('/'):
origPath = '{}/'.format(origPath)

location = '{}{}'.format(origPath, location)
# end of nasty hack

return self.request(
resp.request.method,
'{}://{}{}'.format(
urlParsed.scheme,
urlParsed.netloc,
location
requests.urllib3.packages.six.moves.urllib.parse.urljoin(
challengeSubmitResponse.url,
challengeSubmitResponse.headers['Location']
),
**cloudflare_kwargs
)
else:
redirectParsed = urlparse(challengeSubmitResponse.headers['Location'])
cloudflare_kwargs['headers'] = updateAttr(
cloudflare_kwargs,
'headers',
{'Referer': '{}://{}'.format(redirectParsed.scheme, redirectParsed.netloc)}
{'Referer': resp.url}
)

return self.request(
Expand Down

0 comments on commit 214e3e7

Please sign in to comment.