http_connection.py 878 B

12345678910111213141516171819202122232425262728293031323334353637
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """
  3. Method ``http_connection``
  4. --------------------------
  5. The ``http_connection`` method evaluates a request as the request of a bot if
  6. the Connection_ header is set to ``close``.
  7. .. _Connection:
  8. https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Connection
  9. """
  10. from __future__ import annotations
  11. from ipaddress import (
  12. IPv4Network,
  13. IPv6Network,
  14. )
  15. import werkzeug
  16. from searx.extended_types import SXNG_Request
  17. from . import config
  18. from ._helpers import too_many_requests
  19. def filter_request(
  20. network: IPv4Network | IPv6Network,
  21. request: SXNG_Request,
  22. cfg: config.Config, # pylint: disable=unused-argument
  23. ) -> werkzeug.Response | None:
  24. if request.headers.get('Connection', '').strip() == 'close':
  25. return too_many_requests(network, "HTTP header 'Connection=close")
  26. return None