mirror of
https://github.com/searxng/searxng
synced 2024-01-01 19:24:07 +01:00
Pass tests
This commit is contained in:
parent
54993ad2fa
commit
d5ce6a27bb
1 changed files with 76 additions and 77 deletions
145
searx/webapp.py
145
searx/webapp.py
|
@ -299,80 +299,79 @@ def get_result_template(theme_name: str, template_name: str):
|
||||||
return 'result_templates/' + template_name
|
return 'result_templates/' + template_name
|
||||||
|
|
||||||
|
|
||||||
def get_favicon_or_logo(imgtype: str, request):
|
def get_favicon_or_logo(imgtype: str):
|
||||||
# This method returns a favicon or regular image depending on the imgtype parameter
|
# This method returns a favicon or regular image depending on the imgtype parameter
|
||||||
# It is used to not repeat code for /favicon.ico and /logo
|
# It is used to not repeat code for /favicon.ico and /logo
|
||||||
# Called by logo() and favicon()
|
# Called by logo() and favicon()
|
||||||
theme = request.preferences.get_value("theme")
|
theme = request.preferences.get_value("theme")
|
||||||
path = os.path.expanduser(settings.get("brand").get(imgtype))
|
path = os.path.expanduser(settings.get("brand").get(imgtype))
|
||||||
relpath = os.path.join(app.root_path, path)
|
relpath = os.path.join(app.root_path, path)
|
||||||
mimetype = 'image/vnd.microsoft.icon' if imgtype == "favicon" else None
|
mimetype = 'image/vnd.microsoft.icon' if imgtype == "favicon" else None
|
||||||
fallback = ( send_from_directory(
|
fallback = send_from_directory(
|
||||||
os.path.join(app.root_path, settings['ui']['static_path'], 'themes', theme, 'img'), # pyright: ignore
|
os.path.join(app.root_path, settings['ui']['static_path'], 'themes', theme, 'img'), # pyright: ignore
|
||||||
'favicon.png' if imgtype == "favicon" else "searxng.png",
|
'favicon.png' if imgtype == "favicon" else "searxng.png",
|
||||||
mimetype=mimetype
|
mimetype=mimetype,
|
||||||
) )
|
)
|
||||||
|
|
||||||
# If path is a URL
|
# If path is a URL
|
||||||
if "://" in path:
|
if "://" in path:
|
||||||
resp_ok = False
|
resp_ok = False
|
||||||
resp = None
|
resp = None
|
||||||
# TODO: Make a a function for this used by both this function and the image proxy
|
# TODO: Make a a function for this used by both this function and the image proxy
|
||||||
try:
|
# ASAP to avoid repeated code like this
|
||||||
# Pull image from it
|
# pylint: disable=fixme
|
||||||
request_headers = {
|
try:
|
||||||
'User-Agent': gen_useragent(),
|
# Pull image from it
|
||||||
'Accept': 'image/webp,*/*',
|
request_headers = {
|
||||||
'Accept-Encoding': 'gzip, deflate',
|
'User-Agent': gen_useragent(),
|
||||||
'Sec-GPC': '1',
|
'Accept': 'image/webp,*/*',
|
||||||
'DNT': '1',
|
'Accept-Encoding': 'gzip, deflate',
|
||||||
}
|
'Sec-GPC': '1',
|
||||||
resp, stream = http_stream(method='GET', url=path, headers=request_headers, allow_redirects=True)
|
'DNT': '1',
|
||||||
if resp.status_code != 200:
|
}
|
||||||
logger.debug(f"{imgtype}: Bad status code %i", resp.status_code)
|
resp, stream = http_stream(method='GET', url=path, headers=request_headers, allow_redirects=True)
|
||||||
if resp.status_code >= 400:
|
if resp.status_code != 200:
|
||||||
return fallback
|
logger.debug(f"{imgtype}: Bad status code %i", resp.status_code)
|
||||||
resp_ok = True
|
if resp.status_code >= 400:
|
||||||
except httpx.HTTPError:
|
return fallback
|
||||||
logger.debug(f"{imgtype}: HTTP error")
|
resp_ok = True
|
||||||
return fallback
|
except httpx.HTTPError:
|
||||||
finally:
|
logger.debug(f"{imgtype}: HTTP error")
|
||||||
if resp and not resp_ok:
|
return fallback
|
||||||
try:
|
finally:
|
||||||
resp.close()
|
if resp and not resp_ok:
|
||||||
except httpx.HTTPError:
|
try:
|
||||||
logger.exception(f'{imgtype}: HTTP error on closing')
|
resp.close()
|
||||||
def close_stream():
|
except httpx.HTTPError:
|
||||||
nonlocal resp, stream
|
logger.exception(f'{imgtype}: HTTP error on closing')
|
||||||
try:
|
|
||||||
if resp:
|
|
||||||
resp.close()
|
|
||||||
del resp
|
|
||||||
del stream
|
|
||||||
except httpx.HTTPError as e:
|
|
||||||
logger.debug(f'{imgtype}Exception while closing response', e)
|
|
||||||
try:
|
|
||||||
headers = dict_subset(resp.headers, {'Content-Type', 'Content-Encoding', 'Content-Length', 'Length'})
|
|
||||||
response = Response(stream, mimetype=resp.headers['Content-Type'], headers=headers, direct_passthrough=True)
|
|
||||||
response.call_on_close(close_stream)
|
|
||||||
return response
|
|
||||||
except httpx.HTTPError:
|
|
||||||
close_stream()
|
|
||||||
return fallback
|
|
||||||
|
|
||||||
|
def close_stream():
|
||||||
|
nonlocal resp, stream
|
||||||
|
try:
|
||||||
|
if resp:
|
||||||
|
resp.close()
|
||||||
|
del resp
|
||||||
|
del stream
|
||||||
|
except httpx.HTTPError as e:
|
||||||
|
logger.debug(f'{imgtype}Exception while closing response', e)
|
||||||
|
|
||||||
|
try:
|
||||||
|
headers = dict_subset(resp.headers, {'Content-Type', 'Content-Encoding', 'Content-Length', 'Length'})
|
||||||
|
response = Response(stream, mimetype=resp.headers['Content-Type'], headers=headers, direct_passthrough=True)
|
||||||
|
response.call_on_close(close_stream)
|
||||||
|
return response
|
||||||
|
except httpx.HTTPError:
|
||||||
|
close_stream()
|
||||||
|
return fallback
|
||||||
|
|
||||||
|
elif not os.path.isfile(path) and not os.path.isfile(relpath):
|
||||||
|
# If path doesn't exist neither relatively nor absolutely, fallback to whatever is in the theme
|
||||||
|
return fallback
|
||||||
|
else:
|
||||||
|
# Otherwise we're good to go, send whatever is specified.
|
||||||
|
# Works with both relative and absolute.
|
||||||
|
return send_from_directory(os.path.dirname(path), os.path.basename(path), mimetype=mimetype)
|
||||||
|
|
||||||
elif not os.path.isfile(path) and not os.path.isfile(relpath):
|
|
||||||
# If path doesn't exist neither relatively nor absolutely, fallback to whatever is in the theme
|
|
||||||
return fallback
|
|
||||||
else:
|
|
||||||
# Otherwise we're good to go, send whatever is specified.
|
|
||||||
# Works with both relative and absolute.
|
|
||||||
return send_from_directory(
|
|
||||||
os.path.dirname(path),
|
|
||||||
os.path.basename(path),
|
|
||||||
mimetype=mimetype
|
|
||||||
)
|
|
||||||
|
|
||||||
def custom_url_for(endpoint: str, **values):
|
def custom_url_for(endpoint: str, **values):
|
||||||
suffix = ""
|
suffix = ""
|
||||||
|
@ -1378,12 +1377,12 @@ def opensearch():
|
||||||
|
|
||||||
@app.route('/favicon.ico')
|
@app.route('/favicon.ico')
|
||||||
def favicon():
|
def favicon():
|
||||||
return get_favicon_or_logo("favicon", request)
|
return get_favicon_or_logo("favicon", request)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/logo')
|
@app.route('/logo')
|
||||||
def logo():
|
def logo():
|
||||||
return get_favicon_or_logo("logo", request)
|
return get_favicon_or_logo("logo", request)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/clear_cookies')
|
@app.route('/clear_cookies')
|
||||||
|
|
Loading…
Add table
Reference in a new issue