Repository: locustio/locust
File: locust/web.py (Python)

Code before:
        def login():
            if not self.web_login:
                return redirect(url_for("index"))

            return render_template_from(
                "auth.html",
                auth_args=self.auth_args,
            )

        @app.route("/user", methods=["POST"])
        def update_user():
            assert request.method == "POST"

            user_settings = json.loads(request.data)
            self.environment.update_user_class(user_settings)

            return {}, 201

    @property
    def login_manager(self):
        if self.web_login:
            login_manager = LoginManager()
            login_manager.init_app(self.app)
            login_manager.login_view = "login"
            return login_manager

        raise AttributeError("The login_manager is only available with --web-login.\n")

    def start(self):
        self.greenlet = gevent.spawn(self.start_server)
        self.greenlet.link_exception(greenlet_exception_handler)

    def start_server(self):
        if self.tls_cert and self.tls_key:
            self.server = pywsgi.WSGIServer(
                (self.host, self.port), self.app, log=None, keyfile=self.tls_key, certfile=self.tls_cert
            )
        else:

            class RewriteFilter(logging.Filter):
                def filter(self, record) -> bool:
                    msg = record.msg
                    if "gevent._socket3.socket at" in msg and "Invalid HTTP method: '\x16\x03" in msg:
                        record.msg = f"An https request was made against Locust's Web UI (which was expecting http). Underlying error was: {record.msg}"
                    return True

            logger.addFilter(RewriteFilter())
            self.server = pywsgi.WSGIServer((self.host, self.port), self.app, log=None, error_log=logger)

        self.server.serve_forever()


Review comment (suggestion):
We could also quit locust here I was just thinking maybe an AttributeError is a bit nicer

Code after:
            if not self.web_login:
                return redirect(url_for("index"))

            return render_template_from(
                "auth.html",
                auth_args=self.auth_args,
            )

        @app.route("/user", methods=["POST"])
        def update_user():
            assert request.method == "POST"

            user_settings = json.loads(request.data)
            self.environment.update_user_class(user_settings)

            return {}, 201

    @property
    def login_manager(self):
        if self.web_login:
            login_manager = LoginManager()
            login_manager.init_app(self.app)
            login_manager.login_view = "login"
            return login_manager

        raise AttributeError("The login_manager is only available with --web-login.\n")

    def start(self):
        self.greenlet = gevent.spawn(self.start_server)
        self.greenlet.link_exception(greenlet_exception_handler)

    def start_server(self):
        if self.tls_cert and self.tls_key:
            self.server = pywsgi.WSGIServer(
                (self.host, self.port), self.app, log=None, keyfile=self.tls_key, certfile=self.tls_cert
            )
        else:

            class RewriteFilter(logging.Filter):
                def filter(self, record) -> bool:
                    msg = record.msg
                    if "gevent._socket3.socket at" in msg and "Invalid HTTP method: '\x16\x03" in msg:
                        record.msg = f"An https request was made against Locust's Web UI (which was expecting http). Underlying error was: {record.msg}"
                    return True

            logger.addFilter(RewriteFilter())
            self.server = pywsgi.WSGIServer((self.host, self.port), self.app, log=None, error_log=logger)

        self.server.serve_forever()

    def stop(self):