Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Lib/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ def __init__(self, sock, debuglevel=0, method=None, url=None):
self.length = _UNKNOWN # number of bytes left in response
self.will_close = _UNKNOWN # conn will close at end of response

# URL of the resource, set by urllib but otherwise passed in here so
# that geturl()/url work on responses created directly via http.client.
self.url = url

def _read_status(self):
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
if len(line) > _MAXLINE:
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_httplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,22 @@ def test_response_headers(self):
cookies = r.getheader("Set-Cookie")
self.assertEqual(cookies, hdr)

def test_url_set_at_init(self):
# bpo-42062: HTTPResponse.url (returned by geturl()) is initialized in
# __init__, so it is available on responses created directly via
# http.client and not only on those returned by urllib. Accessing it
# previously raised AttributeError.
body = 'HTTP/1.1 200 OK\r\n\r\nText'
url = 'http://www.python.org/'
resp = client.HTTPResponse(FakeSocket(body), url=url)
self.assertEqual(resp.url, url)
self.assertEqual(resp.geturl(), url)

# When no URL is supplied, geturl() returns None instead of raising.
resp = client.HTTPResponse(FakeSocket(body))
self.assertIsNone(resp.url)
self.assertIsNone(resp.geturl())

def test_read_head(self):
# Test that the library doesn't attempt to read any data
# from a HEAD request. (Tickles SF bug #622042.)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
The :attr:`~http.client.HTTPResponse.url` attribute is now always set when an
:class:`~http.client.HTTPResponse` is created. Previously it was left unset on
responses created directly via :mod:`http.client`, so accessing ``url`` (or the
deprecated ``geturl()``) raised :exc:`AttributeError`. Patch by Felipe
Rodrigues.
Loading