http_accept_encoding.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """
  3. Method ``http_accept_encoding``
  4. -------------------------------
  5. The ``http_accept_encoding`` method evaluates a request as the request of a
  6. bot if the Accept-Encoding_ header ..
  7. - did not contain ``gzip`` AND ``deflate`` (if both values are missed)
  8. - did not contain ``text/html``
  9. .. _Accept-Encoding:
  10. https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding
  11. """
  12. # pylint: disable=unused-argument
  13. from __future__ import annotations
  14. from ipaddress import (
  15. IPv4Network,
  16. IPv6Network,
  17. )
  18. import flask
  19. import werkzeug
  20. from . import config
  21. from ._helpers import too_many_requests
  22. def filter_request(
  23. network: IPv4Network | IPv6Network,
  24. request: flask.Request,
  25. cfg: config.Config,
  26. ) -> werkzeug.Response | None:
  27. accept_list = [l.strip() for l in request.headers.get('Accept-Encoding', '').split(',')]
  28. if not ('gzip' in accept_list or 'deflate' in accept_list):
  29. return too_many_requests(network, "HTTP header Accept-Encoding did not contain gzip nor deflate")
  30. return None