From b93a8ac7134ce7f4c49dba3b3bf6305b85d82dc7 Mon Sep 17 00:00:00 2001 From: Yasser Tahiri Date: Wed, 3 Nov 2021 02:21:42 +0100 Subject: [PATCH 1/2] chore: Simplify `if` expression by using `or` --- proxy/common/types.py | 6 ++---- proxy/core/event/dispatcher.py | 6 +----- proxy/core/event/subscriber.py | 5 +---- proxy/plugin/filter_by_url_regex.py | 13 +++---------- proxy/plugin/reverse_proxy.py | 2 +- 5 files changed, 8 insertions(+), 24 deletions(-) diff --git a/proxy/common/types.py b/proxy/common/types.py index a95a49e8d2..7ed1477956 100644 --- a/proxy/common/types.py +++ b/proxy/common/types.py @@ -8,6 +8,7 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ + import queue import ipaddress import sys @@ -22,10 +23,7 @@ else: from typing_extensions import Protocol -if TYPE_CHECKING: - DictQueueType = queue.Queue[Dict[str, Any]] # pragma: no cover -else: - DictQueueType = queue.Queue +DictQueueType = queue.Queue[Dict[str, Any]] if TYPE_CHECKING else queue.Queue class HasFileno(Protocol): diff --git a/proxy/core/event/dispatcher.py b/proxy/core/event/dispatcher.py index 80cb13043d..3a55c67cdf 100644 --- a/proxy/core/event/dispatcher.py +++ b/proxy/core/event/dispatcher.py @@ -83,11 +83,7 @@ def run(self) -> None: self.run_once() except queue.Empty: pass - except BrokenPipeError: - pass - except EOFError: - pass - except KeyboardInterrupt: + except (BrokenPipeError, EOFError, KeyboardInterrupt): pass except Exception as e: logger.exception('Event dispatcher exception', exc_info=e) diff --git a/proxy/core/event/subscriber.py b/proxy/core/event/subscriber.py index 90648e0d87..f808cbe8f2 100644 --- a/proxy/core/event/subscriber.py +++ b/proxy/core/event/subscriber.py @@ -59,11 +59,8 @@ def unsubscribe(self) -> None: try: self.event_queue.unsubscribe(self.relay_sub_id) - except BrokenPipeError: + except (BrokenPipeError, EOFError): pass - except EOFError: - pass - self.relay_shutdown.set() self.relay_thread.join() logger.debug( diff --git a/proxy/plugin/filter_by_url_regex.py b/proxy/plugin/filter_by_url_regex.py index 43a4074799..6af70d6c89 100644 --- a/proxy/plugin/filter_by_url_regex.py +++ b/proxy/plugin/filter_by_url_regex.py @@ -57,9 +57,8 @@ def handle_client_request( request_host = None if request.host: request_host = request.host - else: - if b'host' in request.headers: - request_host = request.header(b'host') + elif b'host' in request.headers: + request_host = request.header(b'host') if not request_host: logger.error("Cannot determine host") @@ -70,9 +69,7 @@ def handle_client_request( request_host, request.path ) - # check URL against list - rule_number = 1 - for blocked_entry in self.filters: + for rule_number, blocked_entry in enumerate(self.filters, start=1): # if regex matches on URL if re.search(text_(blocked_entry['regex']), text_(url)): # log that the request has been filtered @@ -88,10 +85,6 @@ def handle_client_request( headers={b'Connection': b'close'}, reason=b'Blocked', ) - # stop looping through filter list - break - # increment rule number - rule_number += 1 return request def handle_upstream_chunk(self, chunk: memoryview) -> memoryview: diff --git a/proxy/plugin/reverse_proxy.py b/proxy/plugin/reverse_proxy.py index 1a80d1a474..9ad4c32a6f 100644 --- a/proxy/plugin/reverse_proxy.py +++ b/proxy/plugin/reverse_proxy.py @@ -59,7 +59,7 @@ def handle_request(self, request: HttpParser) -> None: upstream = random.choice(ReverseProxyPlugin.REVERSE_PROXY_PASS) url = urlparse.urlsplit(upstream) assert url.hostname - with socket_connection((text_(url.hostname), url.port if url.port else DEFAULT_HTTP_PORT)) as conn: + with socket_connection((text_(url.hostname), url.port or DEFAULT_HTTP_PORT)) as conn: conn.send(request.build()) self.client.queue(memoryview(conn.recv(DEFAULT_BUFFER_SIZE))) From 9a1515204334c5c0fd795efdb18eb55d07a753a0 Mon Sep 17 00:00:00 2001 From: Yasser Tahiri Date: Wed, 3 Nov 2021 14:36:56 +0100 Subject: [PATCH 2/2] Update proxy/common/types.py Co-authored-by: Sviatoslav Sydorenko --- proxy/common/types.py | 1 - 1 file changed, 1 deletion(-) diff --git a/proxy/common/types.py b/proxy/common/types.py index 7ed1477956..d8f13de9ba 100644 --- a/proxy/common/types.py +++ b/proxy/common/types.py @@ -8,7 +8,6 @@ :copyright: (c) 2013-present by Abhinav Singh and contributors. :license: BSD, see LICENSE for more details. """ - import queue import ipaddress import sys