http_connection.py 693 B

12345678910111213141516171819202122232425
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """
  4. Method ``http_connection``
  5. --------------------------
  6. The ``http_connection`` method evaluates a request as the request of a bot if
  7. the Connection_ header is set to ``close``.
  8. .. _Connection:
  9. https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Connection
  10. """
  11. # pylint: disable=unused-argument
  12. from typing import Optional, Tuple
  13. import flask
  14. from searx.tools import config
  15. def filter_request(request: flask.Request, cfg: config.Config) -> Optional[Tuple[int, str]]:
  16. if request.headers.get('Connection', '').strip() == 'close':
  17. return 429, "bot detected, HTTP header 'Connection=close'"
  18. return None