From 6df28545ff66db0d75a75dd6ecfc07d655c3c1bf Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Thu, 20 Dec 2012 15:39:08 +0100 Subject: [PATCH] Move InputProxy to utils This class is being used at least by Ceilometer in its Swift middleware, and since it's a general one anyway, it looks good to move it to common.utils. This is a follow-up to Chmouel suggestion in https://review.openstack.org/#/c/18231 Change-Id: I8d0ed8600c4152b91be9a88a3b396c3967d0add2 Signed-off-by: Julien Danjou --- swift/common/middleware/proxy_logging.py | 43 ++---------------------- swift/common/utils.py | 40 ++++++++++++++++++++++ 2 files changed, 42 insertions(+), 41 deletions(-) diff --git a/swift/common/middleware/proxy_logging.py b/swift/common/middleware/proxy_logging.py index a63e10436f..4e3494b186 100644 --- a/swift/common/middleware/proxy_logging.py +++ b/swift/common/middleware/proxy_logging.py @@ -42,47 +42,8 @@ from urllib import quote, unquote from swift.common.swob import Request from swift.common.utils import (get_logger, get_remote_client, - get_valid_utf8_str, config_true_value) - - -class InputProxy(object): - """ - File-like object that counts bytes read. - To be swapped in for wsgi.input for accounting purposes. - """ - def __init__(self, wsgi_input): - """ - :param wsgi_input: file-like object to wrap the functionality of - """ - self.wsgi_input = wsgi_input - self.bytes_received = 0 - self.client_disconnect = False - - def read(self, *args, **kwargs): - """ - Pass read request to the underlying file-like object and - add bytes read to total. - """ - try: - chunk = self.wsgi_input.read(*args, **kwargs) - except Exception: - self.client_disconnect = True - raise - self.bytes_received += len(chunk) - return chunk - - def readline(self, *args, **kwargs): - """ - Pass readline request to the underlying file-like object and - add bytes read to total. - """ - try: - line = self.wsgi_input.readline(*args, **kwargs) - except Exception: - self.client_disconnect = True - raise - self.bytes_received += len(line) - return line + get_valid_utf8_str, config_true_value, + InputProxy) class ProxyLoggingMiddleware(object): diff --git a/swift/common/utils.py b/swift/common/utils.py index bf5311cafd..41ce2d32f4 100644 --- a/swift/common/utils.py +++ b/swift/common/utils.py @@ -1524,3 +1524,43 @@ def reiterate(iterable): return itertools.chain([chunk], iterable) except StopIteration: return [] + + +class InputProxy(object): + """ + File-like object that counts bytes read. + To be swapped in for wsgi.input for accounting purposes. + """ + def __init__(self, wsgi_input): + """ + :param wsgi_input: file-like object to wrap the functionality of + """ + self.wsgi_input = wsgi_input + self.bytes_received = 0 + self.client_disconnect = False + + def read(self, *args, **kwargs): + """ + Pass read request to the underlying file-like object and + add bytes read to total. + """ + try: + chunk = self.wsgi_input.read(*args, **kwargs) + except Exception: + self.client_disconnect = True + raise + self.bytes_received += len(chunk) + return chunk + + def readline(self, *args, **kwargs): + """ + Pass readline request to the underlying file-like object and + add bytes read to total. + """ + try: + line = self.wsgi_input.readline(*args, **kwargs) + except Exception: + self.client_disconnect = True + raise + self.bytes_received += len(line) + return line