.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
raise_for_httperror.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Raise exception for an HTTP response is an error.
3
4"""
5
6import typing as t
7from searx.exceptions import (
8 SearxEngineCaptchaException,
9 SearxEngineTooManyRequestsException,
10 SearxEngineAccessDeniedException,
11)
12from searx import get_setting
13
14if t.TYPE_CHECKING:
15 from searx.extended_types import SXNG_Response
16
17
18def is_cloudflare_challenge(resp: "SXNG_Response"):
19 if resp.status_code in [429, 503]:
20 if ('__cf_chl_jschl_tk__=' in resp.text) or (
21 '/cdn-cgi/challenge-platform/' in resp.text
22 and 'orchestrate/jsch/v1' in resp.text
23 and 'window._cf_chl_enter(' in resp.text
24 ):
25 return True
26 if resp.status_code == 403 and '__cf_chl_captcha_tk__=' in resp.text:
27 return True
28 return False
29
30
31def is_cloudflare_firewall(resp: "SXNG_Response"):
32 return resp.status_code == 403 and '<span class="cf-error-code">1020</span>' in resp.text
33
34
35def raise_for_cloudflare_captcha(resp: "SXNG_Response"):
36 if resp.headers.get('Server', '').startswith('cloudflare'):
38 # https://support.cloudflare.com/hc/en-us/articles/200170136-Understanding-Cloudflare-Challenge-Passage-Captcha-
39 # suspend for 2 weeks
41 message='Cloudflare CAPTCHA', suspended_time=get_setting('search.suspended_times.cf_SearxEngineCaptcha')
42 )
43
46 message='Cloudflare Firewall',
47 suspended_time=get_setting('search.suspended_times.cf_SearxEngineAccessDenied'),
48 )
49
50
51def raise_for_recaptcha(resp: "SXNG_Response"):
52 if resp.status_code == 503 and '"https://www.google.com/recaptcha/' in resp.text:
54 message='ReCAPTCHA', suspended_time=get_setting('search.suspended_times.recaptcha_SearxEngineCaptcha')
55 )
56
57
58def raise_for_captcha(resp: "SXNG_Response"):
61
62
63def raise_for_httperror(resp: "SXNG_Response") -> None:
64 """Raise exception for an HTTP response is an error.
65
66 Args:
67 resp (requests.Response): Response to check
68
69 Raises:
70 requests.HTTPError: raise by resp.raise_for_status()
71 searx.exceptions.SearxEngineAccessDeniedException: raise when the HTTP status code is 402 or 403.
72 searx.exceptions.SearxEngineTooManyRequestsException: raise when the HTTP status code is 429.
73 searx.exceptions.SearxEngineCaptchaException: raise when if CATPCHA challenge is detected.
74 """
75 if resp.status_code and resp.status_code >= 400:
77 if resp.status_code in (402, 403):
78 raise SearxEngineAccessDeniedException(message='HTTP error ' + str(resp.status_code))
79 if resp.status_code == 429:
81 resp.raise_for_status()
is_cloudflare_firewall("SXNG_Response" resp)
raise_for_cloudflare_captcha("SXNG_Response" resp)
raise_for_recaptcha("SXNG_Response" resp)
is_cloudflare_challenge("SXNG_Response" resp)
t.Any get_setting(str name, t.Any default=_unset)
Definition __init__.py:74