http_accept.py 599 B

123456789101112131415161718192021222324
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """
  4. Method ``http_accept``
  5. ----------------------
  6. The ``http_accept`` method evaluates a request as the request of a bot if the
  7. Accept_ header ..
  8. - did not contain ``text/html``
  9. .. _Accept:
  10. https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept
  11. """
  12. from typing import Optional, Tuple
  13. import flask
  14. def filter_request(request: flask.Request) -> Optional[Tuple[int, str]]:
  15. if 'text/html' not in request.accept_mimetypes:
  16. return 429, "bot detected, HTTP header Accept did not contain text/html"
  17. return None