Permalink
Browse files

Merge pull request #1892 from Lukasa/netrcauth

Repopulate ~/.netrc auth.
  • Loading branch information...
kennethreitz committed Jan 31, 2014
2 parents 4f12bfa + d9f34c6 commit f1893c835570d72823c970fbd6e0e42c13b1f0f2
Showing with 15 additions and 0 deletions.
  1. +15 −0 requests/sessions.py
View
@@ -158,6 +158,21 @@ def resolve_redirects(self, resp, req, stream=False, timeout=None,
prepared_request._cookies.update(self.cookies)
prepared_request.prepare_cookies(prepared_request._cookies)
+ # If we get redirected to a new host, we should strip out any
+ # authentication headers.
+ original_parsed = urlparse(resp.request.url)
+ redirect_parsed = urlparse(url)
+
+ if (original_parsed.hostname != redirect_parsed.hostname and
+ 'Authorization' in headers):
+ del headers['Authorization']
+
+ # However, .netrc might have more auth for us. Let's get it if it
+ # does.
+ new_auth = get_netrc_auth(url) if self.trust_env else None
+ if new_auth is not None:
+ prepared_request.prepare_auth(new_auth)
+
resp = self.send(
prepared_request,
stream=stream,

0 comments on commit f1893c8

Please sign in to comment.