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