ssl = None
+class HTTPTimeoutError(HTTPError):
+ """Error raised by SimpleAsyncHTTPClient on timeout.
+
+ For historical reasons, this is a subclass of `.HTTPClientError`
+ which simulates a response code of 599.
+
+ .. versionadded:: 5.1
+ """
+ def __init__(self, message):
+ super(HTTPTimeoutError, self).__init__(599, message=message)
+
+ def __str__(self):
+ return self.message
+
+
+class HTTPStreamClosedError(HTTPError):
+ """Error raised by SimpleAsyncHTTPClient when the underlying stream is closed.
+
+ When a more specific exception is available (such as `ConnectionResetError`),
+ it may be raised instead of this one.
+
+ For historical reasons, this is a subclass of `.HTTPClientError`
+ which simulates a response code of 599.
+
+ .. versionadded:: 5.1
+ """
+ def __init__(self, message):
+ super(HTTPStreamClosedError, self).__init__(599, message=message)
+
+ def __str__(self):
+ return self.message
+
+
class SimpleAsyncHTTPClient(AsyncHTTPClient):
"""Non-blocking HTTP client with no external dependencies.
error_message = "Timeout {0}".format(info) if info else "Timeout"
timeout_response = HTTPResponse(
- request, 599, error=HTTPError(599, error_message),
+ request, 599, error=HTTPTimeoutError(error_message),
request_time=self.io_loop.time() - request.start_time)
self.io_loop.add_callback(callback, timeout_response)
del self.waiting[key]
def _on_timeout(self, info=None):
"""Timeout callback of _HTTPConnection instance.
- Raise a timeout HTTPError when a timeout occurs.
+ Raise a `HTTPTimeoutError` when a timeout occurs.
:info string key: More detailed timeout information.
"""
self._timeout = None
error_message = "Timeout {0}".format(info) if info else "Timeout"
if self.final_callback is not None:
- raise HTTPError(599, error_message)
+ raise HTTPTimeoutError(error_message)
def _remove_timeout(self):
if self._timeout is not None:
self._remove_timeout()
if isinstance(value, StreamClosedError):
if value.real_error is None:
- value = HTTPError(599, "Stream closed")
+ value = HTTPStreamClosedError("Stream closed")
else:
value = value.real_error
self._run_callback(HTTPResponse(self.request, 599, error=value,
if self.stream.error:
raise self.stream.error
try:
- raise HTTPError(599, message)
- except HTTPError:
+ raise HTTPStreamClosedError(message)
+ except HTTPStreamClosedError:
self._handle_exception(*sys.exc_info())
def headers_received(self, first_line, headers):
from tornado.escape import to_unicode
from tornado import gen
-from tornado.httpclient import AsyncHTTPClient, HTTPError
+from tornado.httpclient import AsyncHTTPClient
from tornado.httputil import HTTPHeaders, ResponseStartLine
from tornado.ioloop import IOLoop
from tornado.iostream import UnsatisfiableReadError
from tornado.log import gen_log
from tornado.concurrent import Future
from tornado.netutil import Resolver, bind_sockets
-from tornado.simple_httpclient import SimpleAsyncHTTPClient
+from tornado.simple_httpclient import SimpleAsyncHTTPClient, HTTPStreamClosedError
from tornado.test.httpclient_test import ChunkHandler, CountdownHandler, HelloWorldHandler, RedirectHandler # noqa: E501
from tornado.test import httpclient_test
from tornado.testing import (AsyncHTTPTestCase, AsyncHTTPSTestCase, AsyncTestCase,
self.assertEqual(response.code, 599)
self.assertTrue(timeout_min < response.request_time < timeout_max,
response.request_time)
- self.assertEqual(str(response.error), "HTTP 599: Timeout while connecting")
+ self.assertEqual(str(response.error), "Timeout while connecting")
@skipOnTravis
def test_request_timeout(self):
self.assertEqual(response.code, 599)
self.assertTrue(timeout_min < response.request_time < timeout_max,
response.request_time)
- self.assertEqual(str(response.error), "HTTP 599: Timeout during request")
+ self.assertEqual(str(response.error), "Timeout during request")
# trigger the hanging request to let it clean up after itself
self.triggers.popleft()()
self.assertEqual(response.code, 599)
self.assertTrue(response.request_time < 1, response.request_time)
- self.assertEqual(str(response.error), "HTTP 599: Timeout in request queue")
+ self.assertEqual(str(response.error), "Timeout in request queue")
self.triggers.popleft()()
fut1.add_done_callback(self.stop)
self.wait()
def test_large_body(self):
with ExpectLog(gen_log, "Malformed HTTP message from None: Content-Length too long"):
- with self.assertRaises(HTTPError):
+ with self.assertRaises(HTTPStreamClosedError):
self.fetch('/large', raise_error=True)
# Make sure the invalid headers are detected
with ExpectLog(gen_log, ("Malformed HTTP message from None: Response "
"with both Transfer-Encoding and Content-Length")):
- with self.assertRaises(HTTPError):
+ with self.assertRaises(HTTPStreamClosedError):
self.fetch('/chunkwithcl', raise_error=True)