Implements by adding a geturl method to HTTPResponse objects
MODIFY
urllib3/request.py
urllib3/request.py
@@ -60,6 +60,8 @@ class RequestMethods(object):
"""
method = method.upper()
+ urlopen_kw['request_url'] = url
+
if method in self._encode_url_methods:
return self.request_encode_url(method, url, fields=fields,
headers=headers,

MODIFY
urllib3/response.py
urllib3/response.py
@@ -136,7 +136,8 @@ class HTTPResponse(io.IOBase):
def __init__(self, body='', headers=None, status=0, version=0, reason=None,
strict=0, preload_content=True, decode_content=True,
original_response=None, pool=None, connection=None, msg=None,
- retries=None, enforce_content_length=False, request_method=None):
+ retries=None, enforce_content_length=False,
+ request_method=None, request_url=None):
if isinstance(headers, HTTPHeaderDict):
self.headers = headers
@@ -156,6 +157,7 @@ class HTTPResponse(io.IOBase):
self._original_response = original_response
self._fp_bytes_read = 0
self.msg = msg
+ self._request_url = request_url
if body and isinstance(body, (basestring, binary_type)):
self._body = body
@@ -661,3 +663,14 @@ class HTTPResponse(io.IOBase):
# We read everything; close the "file".
if self._original_response:
self._original_response.close()
+
+ def geturl(self):
+ """
+ Returns the URL that was the source of this response.
+ If the request that generated this response redirected, this method
+ will return the final redirect location.
+ """
+ if self.retries is not None and len(self.retries.history):
+ return self.retries.history[-1].redirect_location
+ else:
+ return self._request_url

Implements #1272 by adding a geturl method to HTTPResponse objects