Cache error status and message

Instead of returning a generic error status and message when hitting the
bad_hostnames_ports cache, we cache and return the original error.
This commit is contained in:
Vangelis Banos 2019-05-14 19:35:46 +00:00
parent f0d2898326
commit 5b30dd4576

View File

@ -385,14 +385,16 @@ class MitmProxyHandler(http_server.BaseHTTPRequestHandler):
self._determine_host_port()
assert self.url
# Check if target hostname:port is in `bad_hostnames_ports` cache
# to avoid retrying to connect.
# to avoid retrying to connect. cached is a tuple containing
# (status_code, error message)
cached = None
hostname_port = self._hostname_port_cache_key()
with self.server.bad_hostnames_ports_lock:
hostname_port = self._hostname_port_cache_key()
if hostname_port in self.server.bad_hostnames_ports:
self.logger.info('Cannot connect to %s (cache)',
hostname_port)
self.send_error(502, 'message timed out')
return
cached = self.server.bad_hostnames_ports.get(hostname_port)
if cached:
self.logger.info('Cannot connect to %s (cache)', hostname_port)
self.send_error(cached[0], cached[1])
return
# Connect to destination
self._connect_to_remote_server()
except warcprox.RequestBlockedByRule as e:
@ -406,7 +408,7 @@ class MitmProxyHandler(http_server.BaseHTTPRequestHandler):
if type(e) in (socket.timeout, NewConnectionError):
host_port = self._hostname_port_cache_key()
with self.server.bad_hostnames_ports_lock:
self.server.bad_hostnames_ports[host_port] = 1
self.server.bad_hostnames_ports[host_port] = (500, str(e))
self.logger.info('bad_hostnames_ports cache size: %d',
len(self.server.bad_hostnames_ports))
self.logger.error(
@ -557,7 +559,7 @@ class MitmProxyHandler(http_server.BaseHTTPRequestHandler):
if type(e) == http_client.RemoteDisconnected:
host_port = self._hostname_port_cache_key()
with self.server.bad_hostnames_ports_lock:
self.server.bad_hostnames_ports[host_port] = 1
self.server.bad_hostnames_ports[host_port] = (502, str(e))
self.logger.info('bad_hostnames_ports cache size: %d',
len(self.server.bad_hostnames_ports))