From d4ba0aa297fcccfc2d5234733b720ea121426385 Mon Sep 17 00:00:00 2001 From: Sumit Jamgade Date: Mon, 18 Mar 2019 11:21:03 +0100 Subject: [PATCH] trap Exception during SimpleHTTPRequestHandler.handle call There could be any error during a normal GET operation or even during a 'server-up' check before making an upgrade call. These errors are currently unhandled, as a result the exception rises to the top of the stack and if run_once is not used the entire process crashes with an exception. This problem become widely visible when using the WebSocketProxy which uses a request handler class. To handle every request a new process is spawned and if it is an invalid request (empty request, SSL HELLO etc) the process crashes. With this patch in such a case the request is caught and an error is sent back, allowing a graceful termination of process. --- websockify/websockifyserver.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/websockify/websockifyserver.py b/websockify/websockifyserver.py index 9d9cfb9..322e0f3 100644 --- a/websockify/websockifyserver.py +++ b/websockify/websockifyserver.py @@ -315,7 +315,11 @@ class WebSockifyRequestHandler(WebSocketRequestHandlerMixIn, SimpleHTTPRequestHa if self.run_once: self.handle_one_request() else: - SimpleHTTPRequestHandler.handle(self) + try: + SimpleHTTPRequestHandler.handle(self) + except Exception: + exc = sys.exc_info()[1] + self.send_error(400, str(exc)) def log_request(self, code='-', size='-'): if self.verbose: