diff options
author | Jeremy Hylton <jeremy@alum.mit.edu> | 2004-02-24 19:40:35 (GMT) |
---|---|---|
committer | Jeremy Hylton <jeremy@alum.mit.edu> | 2004-02-24 19:40:35 (GMT) |
commit | b3ee6f9921a0979b77a64b83f2f3a267ae66916e (patch) | |
tree | b13f188022498f176284594a12ea0d4e9cb2f34d /Lib/urllib2.py | |
parent | f0ae4272b4b69576a51e5749820bf907799d1aca (diff) | |
download | cpython-b3ee6f9921a0979b77a64b83f2f3a267ae66916e.zip cpython-b3ee6f9921a0979b77a64b83f2f3a267ae66916e.tar.gz cpython-b3ee6f9921a0979b77a64b83f2f3a267ae66916e.tar.bz2 |
Fix two bugs in the new do_open() implementation for HTTPHandler.
Invoke the standard error handlers for non-200 responses.
Always supply a "Connection: close" header to prevent the server from
leaving the connection open. Downstream users of the socket may
attempt recv()/read() with no arguments, which would block if the
connection were kept open.
Diffstat (limited to 'Lib/urllib2.py')
-rw-r--r-- | Lib/urllib2.py | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/Lib/urllib2.py b/Lib/urllib2.py index 9f6dc63..cd0cfc1 100644 --- a/Lib/urllib2.py +++ b/Lib/urllib2.py @@ -957,18 +957,29 @@ class AbstractHTTPHandler(BaseHandler): headers = dict(req.headers) headers.update(req.unredirected_hdrs) + # We want to make an HTTP/1.1 request, but the addinfourl + # class isn't prepared to deal with a persistent connection. + # It will try to read all remaining data from the socket, + # which will block while the server waits for the next request. + # So make sure the connection gets closed after the (only) + # request. + headers["Connection"] = "close" try: h.request(req.get_method(), req.get_selector(), req.data, headers) r = h.getresponse() except socket.error, err: # XXX what error? raise URLError(err) - # Pick apart the HTTPResponse object to get the various pieces - # of the - resp = addinfourl(r.fp, r.msg, req.get_full_url()) - resp.code = r.status - resp.msg = r.reason - return resp + if r.status == 200: + # Pick apart the HTTPResponse object to get the addinfourl + # object initialized properly + resp = addinfourl(r.fp, r.msg, req.get_full_url()) + resp.code = r.status + resp.msg = r.reason + return resp + else: + return self.parent.error("http", req, r.fp, r.status, r.msg, + r.msg.dict) class HTTPHandler(AbstractHTTPHandler): |