From: Ben Darnell Date: Sun, 17 Feb 2013 23:28:44 +0000 (-0500) Subject: Centralize some shared logic bewteen HTTPClient implementations. X-Git-Tag: v3.0.0~113 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5b459c076b1179fe3f58829e0f065e6175e8d420;p=thirdparty%2Ftornado.git Centralize some shared logic bewteen HTTPClient implementations. --- diff --git a/tornado/curl_httpclient.py b/tornado/curl_httpclient.py index dcc601179..73ecfb0e1 100644 --- a/tornado/curl_httpclient.py +++ b/tornado/curl_httpclient.py @@ -39,11 +39,8 @@ except ImportError: class CurlAsyncHTTPClient(AsyncHTTPClient): - def initialize(self, io_loop=None, max_clients=10, defaults=None): - self.io_loop = io_loop - self.defaults = dict(HTTPRequest._DEFAULTS) - if defaults is not None: - self.defaults.update(defaults) + def initialize(self, io_loop, max_clients=10, defaults=None): + super(CurlAsyncHTTPClient, self).initialize(io_loop, defaults=defaults) self._multi = pycurl.CurlMulti() self._multi.setopt(pycurl.M_TIMERFUNCTION, self._set_timeout) self._multi.setopt(pycurl.M_SOCKETFUNCTION, self._handle_socket) @@ -90,10 +87,7 @@ class CurlAsyncHTTPClient(AsyncHTTPClient): self._closed = True super(CurlAsyncHTTPClient, self).close() - def fetch(self, request, callback, **kwargs): - if not isinstance(request, HTTPRequest): - request = HTTPRequest(url=request, **kwargs) - request = _RequestProxy(request, self.defaults) + def fetch_impl(self, request, callback): self._requests.append((request, stack_context.wrap(callback))) self._process_queue() self._set_timeout(0) diff --git a/tornado/httpclient.py b/tornado/httpclient.py index 73a88af16..18e53bd22 100644 --- a/tornado/httpclient.py +++ b/tornado/httpclient.py @@ -144,6 +144,12 @@ class AsyncHTTPClient(Configurable): cls._async_clients()[io_loop] = instance return instance + def initialize(self, io_loop, defaults=None): + self.io_loop = io_loop + self.defaults = dict(HTTPRequest._DEFAULTS) + if defaults is not None: + self.defaults.update(defaults) + def close(self): """Destroys this http client, freeing any file descriptors used. Not needed in normal use, but may be helpful in unittests that @@ -165,6 +171,16 @@ class AsyncHTTPClient(Configurable): encountered during the request. You can call response.rethrow() to throw the exception (if any) in the callback. """ + if not isinstance(request, HTTPRequest): + request = HTTPRequest(url=request, **kwargs) + # We may modify this (to add Host, Accept-Encoding, etc), + # so make sure we don't modify the caller's object. This is also + # where normal dicts get converted to HTTPHeaders objects. + request.headers = httputil.HTTPHeaders(request.headers) + request = _RequestProxy(request, self.defaults) + self.fetch_impl(request, callback) + + def fetch_impl(self, request, callback): raise NotImplementedError() @classmethod diff --git a/tornado/simple_httpclient.py b/tornado/simple_httpclient.py index bbd6b70b8..a2d18a339 100644 --- a/tornado/simple_httpclient.py +++ b/tornado/simple_httpclient.py @@ -45,7 +45,7 @@ class SimpleAsyncHTTPClient(AsyncHTTPClient): are not reused, and callers cannot select the network interface to be used. """ - def initialize(self, io_loop=None, max_clients=10, + def initialize(self, io_loop, max_clients=10, hostname_mapping=None, max_buffer_size=104857600, resolver=None, defaults=None): """Creates a AsyncHTTPClient. @@ -67,25 +67,16 @@ class SimpleAsyncHTTPClient(AsyncHTTPClient): max_buffer_size is the number of bytes that can be read by IOStream. It defaults to 100mb. """ - self.io_loop = io_loop + super(SimpleAsyncHTTPClient, self).initialize(io_loop, + defaults=defaults) self.max_clients = max_clients self.queue = collections.deque() self.active = {} self.hostname_mapping = hostname_mapping self.max_buffer_size = max_buffer_size self.resolver = resolver or Resolver(io_loop=io_loop) - self.defaults = dict(HTTPRequest._DEFAULTS) - if defaults is not None: - self.defaults.update(defaults) - - def fetch(self, request, callback, **kwargs): - if not isinstance(request, HTTPRequest): - request = HTTPRequest(url=request, **kwargs) - # We're going to modify this (to add Host, Accept-Encoding, etc), - # so make sure we don't modify the caller's object. This is also - # where normal dicts get converted to HTTPHeaders objects. - request.headers = HTTPHeaders(request.headers) - request = _RequestProxy(request, self.defaults) + + def fetch_impl(self, request, callback): callback = stack_context.wrap(callback) self.queue.append((request, callback)) self._process_queue()