http_accept_encoding.py 1.1 KB

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