kennethreitz/requests — pull request #1628 by Scorpil (2013-09-26 09:44:58+00:00)
Title: Update host header on redirect.

If you manually specify HTTP Host header, any redirect to host different from specified will result in TooManyRedirects exception.

Example:

>>> import requests
>>> requests.get('http://reddit.com', headers={"Host": "reddit.com"})
Traceback (most recent call last):
File "", line 1, in
File "requests/api.py", line 55, in get
return request('get', url, **kwargs)
File "requests/api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "requests/sessions.py", line 357, in request
resp = self.send(prep, **send_kwargs)
File "requests/sessions.py", line 480, in send
history = [resp for resp in gen] if allow_redirects else []
File "requests/sessions.py", line 82, in resolve_redirects
raise TooManyRedirects('Exceeded %s redirects.' % self.max_redirects)
requests.exceptions.TooManyRedirects: Exceeded 30 redirects.

After receiving first 301 response, requests library makes a request for url specified in it's "Location" field (in the example above it was http://www.reddit.com/), but it also preserves all headers set by user, so you end up having header "Host: reddit.com" while host is now **www**.reddit.com. Server checks "host" header and responds with 301 again. And now we're in a loop.

I'd be glad to add tests for this fix, but httpbin does not support subdomains.