Example #1
0
 def send_error_response(self, code, message, headers=None) -> None:
     try:
         response = http.make_error_response(code, message, headers)
         self.send_response(response)
     except (exceptions.NetlibException, h2.exceptions.H2Error,
             exceptions.Http2ProtocolException):
         self.log(
             "Failed to send error response to client: {}".format(message),
             "debug")
Example #2
0
    def handle(self):
        self.log("clientconnect", "debug")

        root_layer = None
        try:
            root_layer = self._create_root_layer()
            root_layer = self.channel.ask("clientconnect", root_layer)
            if root_layer:
                root_layer()
        except exceptions.Kill:
            self.log(flow.Error.KILLED_MESSAGE, "info")
        except exceptions.ProtocolException as e:
            if isinstance(e, exceptions.ClientHandshakeException):
                self.log(
                    "Client Handshake failed. "
                    "The client may not trust Selenium Wire's certificate for {}.".format(e.server),
                    "debug"
                )
                self.log(repr(e), "debug")
            elif isinstance(e, exceptions.InvalidServerCertificate):
                self.log(str(e), "warn")
                self.log("Invalid certificate, closing connection. Pass --ssl-insecure to disable validation.", "warn")
            else:
                if self.config.options.suppress_connection_errors:
                    self.log(repr(e), "debug")
                else:
                    self.log(str(e), "warn")

            # If an error propagates to the topmost level,
            # we send an HTTP error response, which is both
            # understandable by HTTP clients and humans.
            try:
                error_response = http.make_error_response(502, repr(e))
                self.client_conn.send(http1.assemble_response(error_response))
            except exceptions.TcpException:
                pass
        except Exception:
            self.log(traceback.format_exc(), "error")

        self.log("clientdisconnect", "debug")
        if root_layer is not None:
            self.channel.tell("clientdisconnect", root_layer)
        self.client_conn.finish()