http_connection.py 608 B

1234567891011121314151617181920212223
  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. from typing import Optional, Tuple
  12. import flask
  13. def filter_request(request: flask.Request) -> Optional[Tuple[int, str]]:
  14. if request.headers.get('Connection', '').strip() == 'close':
  15. return 429, "bot detected, HTTP header 'Connection=close'"
  16. return None