Skip to content

Commit

Permalink
better handling of unexpected errors in proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Sep 9, 2015
1 parent ee8f196 commit 71de6c6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
5 changes: 0 additions & 5 deletions src/netius/extra/proxy_r.py
Expand Up @@ -358,11 +358,6 @@ def _on_prx_close(self, client, _connection):
if state: self.releaser(state); _connection.state = None
netius.servers.ProxyServer._on_prx_close(self, client, _connection)

def _on_prx_error(self, client, _connection, error):
state = _connection.state if hasattr(_connection, "state") else None
if state: self.releaser(state); _connection.state = None
netius.servers.ProxyServer._on_prx_error(self, client, _connection, error)

if __name__ == "__main__":
import logging
regex = (
Expand Down
18 changes: 14 additions & 4 deletions src/netius/servers/proxy.py
Expand Up @@ -386,13 +386,22 @@ def _on_prx_close(self, client, _connection):
del self.conn_map[_connection]

def _on_prx_error(self, client, _connection, error):
error_m = str(error) or "Unknown proxy relay error"
# retrieves the front-end connection associated with
# the proxy connection, this value is going to be
# if sending the message to the final client
connection = self.conn_map.get(_connection, None)
if not connection: return

if not _connection.waiting: return

connection.send_response(
# constructs the message string that is going to be
# sent as part of the response from the proxy indicating
# the unexpected error, then in case the connection is
# still under the (initial) waiting state send the same
# message to the final client (setting the callback to
# the closing of the proxy), in case the connection is no
# longer in the waiting state the closing of the proxy
# is performed immediately (sending message is not possible)
error_m = str(error) or "Unknown proxy relay error"
if _connection.waiting: connection.send_response(
data = error_m,
headers = dict(
connection = "close"
Expand All @@ -402,6 +411,7 @@ def _on_prx_error(self, client, _connection, error):
apply = True,
callback = self._prx_close
)
else: self._prx_close(_connection)

def _on_raw_connect(self, client, _connection):
connection = self.conn_map[_connection]
Expand Down

0 comments on commit 71de6c6

Please sign in to comment.