Kaydet (Commit) b3ee6f99 authored tarafından Jeremy Hylton's avatar Jeremy Hylton

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.
üst f0ae4272
...@@ -461,7 +461,8 @@ class HandlerTests(unittest.TestCase): ...@@ -461,7 +461,8 @@ class HandlerTests(unittest.TestCase):
self.assertEqual(http.method, method) self.assertEqual(http.method, method)
self.assertEqual(http.selector, "/") self.assertEqual(http.selector, "/")
self.assertEqual(http.req_headers, self.assertEqual(http.req_headers,
[("Foo", "bar"), ("Spam", "eggs")]) [("Connection", "close"),
("Foo", "bar"), ("Spam", "eggs")])
self.assertEqual(http.data, data) self.assertEqual(http.data, data)
# check socket.error converted to URLError # check socket.error converted to URLError
......
...@@ -957,18 +957,29 @@ class AbstractHTTPHandler(BaseHandler): ...@@ -957,18 +957,29 @@ class AbstractHTTPHandler(BaseHandler):
headers = dict(req.headers) headers = dict(req.headers)
headers.update(req.unredirected_hdrs) 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: try:
h.request(req.get_method(), req.get_selector(), req.data, headers) h.request(req.get_method(), req.get_selector(), req.data, headers)
r = h.getresponse() r = h.getresponse()
except socket.error, err: # XXX what error? except socket.error, err: # XXX what error?
raise URLError(err) raise URLError(err)
# Pick apart the HTTPResponse object to get the various pieces if r.status == 200:
# of the # Pick apart the HTTPResponse object to get the addinfourl
resp = addinfourl(r.fp, r.msg, req.get_full_url()) # object initialized properly
resp.code = r.status resp = addinfourl(r.fp, r.msg, req.get_full_url())
resp.msg = r.reason resp.code = r.status
return resp 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): class HTTPHandler(AbstractHTTPHandler):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment