diff --git a/proxy/common/types.py b/proxy/common/types.py index a95a49e8d2..d8f13de9ba 100644 --- a/proxy/common/types.py +++ b/proxy/common/types.py @@ -22,10 +22,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 f65d5bf892..2dc5f5ee61 100644 --- a/proxy/core/event/dispatcher.py +++ b/proxy/core/event/dispatcher.py @@ -84,11 +84,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 841fe12f5b..bbca3cceda 100644 --- a/proxy/core/event/subscriber.py +++ b/proxy/core/event/subscriber.py @@ -61,11 +61,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 c9e9407111..2b66867532 100644 --- a/proxy/plugin/filter_by_url_regex.py +++ b/proxy/plugin/filter_by_url_regex.py @@ -59,9 +59,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") @@ -72,9 +71,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 @@ -92,10 +89,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 3095282d9c..44c2afacc6 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)))