]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Detect the connection being closed by the server in SimpleAsyncHTTPClient.
authorBen Darnell <ben@bendarnell.com>
Tue, 15 Feb 2011 04:10:27 +0000 (20:10 -0800)
committerBen Darnell <ben@bendarnell.com>
Tue, 15 Feb 2011 04:11:59 +0000 (20:11 -0800)
tornado/simple_httpclient.py

index a19805d8be33d5568b7a1339367ea627b47f8b08..f550980bbecb463d9052f825527039482f985b4d 100644 (file)
@@ -162,16 +162,17 @@ class _HTTPConnection(object):
                 self._connect_timeout = self.io_loop.add_timeout(
                     self.start_time + timeout,
                     self._on_timeout)
+            self.stream.set_close_callback(self._on_close)
             self.stream.connect((host, port),
                                 functools.partial(self._on_connect, parsed))
 
     def _on_timeout(self):
         self._timeout = None
-        self.stream.close()
         if self.callback is not None:
             self.callback(HTTPResponse(self.request, 599,
                                        error=HTTPError(599, "Timeout")))
             self.callback = None
+        self.stream.close()
 
     def _on_connect(self, parsed):
         if self._timeout is not None:
@@ -229,6 +230,13 @@ class _HTTPConnection(object):
                 self.callback(HTTPResponse(self.request, 599, error=e))
                 self.callback = None
 
+    def _on_close(self):
+        if self.callback is not None:
+            self.callback(HTTPResponse(self.request, 599,
+                                       error=HTTPError(599, 
+                                                       "Connection closed")))
+            self.callback = None
+
     def _on_headers(self, data):
         first_line, _, header_data = data.partition("\r\n")
         match = re.match("HTTP/1.[01] ([0-9]+) .*", first_line)