http_accept_encoding.py 991 B

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