http_accept_encoding.py 931 B

1234567891011121314151617181920212223242526272829
  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 typing import Optional, Tuple
  15. import flask
  16. from searx.tools import config
  17. def filter_request(request: flask.Request, cfg: config.Config) -> Optional[Tuple[int, str]]:
  18. accept_list = [l.strip() for l in request.headers.get('Accept-Encoding', '').split(',')]
  19. if not ('gzip' in accept_list or 'deflate' in accept_list):
  20. return 429, "bot detected, HTTP header Accept-Encoding did not contain gzip nor deflate"
  21. return None