http_connection.py 869 B

12345678910111213141516171819202122232425262728293031323334353637
  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 __future__ import annotations
  13. from ipaddress import (
  14. IPv4Network,
  15. IPv6Network,
  16. )
  17. import flask
  18. import werkzeug
  19. from searx.tools import config
  20. from ._helpers import too_many_requests
  21. def filter_request(
  22. network: IPv4Network | IPv6Network,
  23. request: flask.Request,
  24. cfg: config.Config,
  25. ) -> werkzeug.Response | None:
  26. if request.headers.get('Connection', '').strip() == 'close':
  27. return too_many_requests(network, "HTTP header 'Connection=close")
  28. return None