@@ -94,7 +94,7 @@ from searx.plugins import plugins
9494from searx.plugins.oa_doi_rewrite import get_doi_resolver
9595from searx.preferences import Preferences, ValidationException, LANGUAGE_CODES
9696from searx.answerers import answerers
97−from searx.network import stream as http_stream, set_context_network_name
97+from searx.network import stream as http_stream
9898from searx.answerers import ask
9999from searx.metrology.error_recorder import errors_per_engines
100100from searx.settings_loader import get_default_settings_path
@@ -921,8 +921,6 @@ def _is_selected_language_supported(engine, preferences):
921921
922922@app.route('/image_proxy', methods=['GET'])
923923def image_proxy():
924− # pylint: disable=too-many-return-statements, too-many-branches
925−
926924 url = request.args.get('url')
927925
928926 if not url:
@@ -934,21 +932,14 @@ def image_proxy():
934932 return '', 400
935933
936934 maximum_size = 5 * 1024 * 1024
937− forward_resp = False
938− resp = None
935+
939936 try:
940− request_headers = {
941− 'User-Agent': gen_useragent(),
942− 'Accept': 'image/webp,*/*',
943− 'Accept-Encoding': 'gzip, deflate',
944− 'Sec-GPC': '1',
945− 'DNT': '1',
946− }
947− set_context_network_name('image_proxy')
937+ headers = dict_subset(request.headers, {'If-Modified-Since', 'If-None-Match'})
938+ headers['User-Agent'] = gen_useragent()
948939 stream = http_stream(
949940 method='GET',
950941 url=url,
951− headers=request_headers,
942+ headers=headers,
952943 timeout=settings['outgoing']['request_timeout'],
953944 follow_redirects=True,
954945 max_redirects=20)
@@ -958,37 +949,25 @@ def image_proxy():
958949 if content_length and content_length.isdigit() and int(content_length) > maximum_size:
959950 return 'Max size', 400
960951
952+ if resp.status_code == 304:
953+ return '', resp.status_code
954+
961955 if resp.status_code != 200:
962956 logger.debug('image-proxy: wrong response code: {0}'.format(resp.status_code))
963957 if resp.status_code >= 400:
964958 return '', resp.status_code
965959 return '', 400
966960
967− if not resp.headers.get('Content-Type', '').startswith('image/'):
968− logger.debug('image-proxy: wrong content-type: %s', resp.headers.get('Content-Type', ''))
961+ if not resp.headers.get('content-type', '').startswith('image/'):
962+ logger.debug('image-proxy: wrong content-type: {0}'.format(resp.headers.get('content-type')))
969963 return '', 400
970964
971− forward_resp = True
972− except httpx.HTTPError:
973− logger.exception('HTTP error')
974− return '', 400
975− finally:
976− if resp and not forward_resp:
977− # the code is about to return an HTTP 400 error to the browser
978− # we make sure to close the response between searxng and the HTTP server
979− try:
980− resp.close()
981− except httpx.HTTPError:
982− logger.exception('HTTP error on closing')
965+ headers = dict_subset(resp.headers, {'Content-Length', 'Length', 'Date', 'Last-Modified', 'Expires', 'Etag'})
983966
984− try:
985− headers = dict_subset(
986− resp.headers,
987− {'Content-Type', 'Content-Encoding', 'Content-Length', 'Length'}
988− )
967+ total_length = 0
989968
990969 def forward_chunk():
991− total_length = 0
970+ nonlocal total_length
992971 for chunk in stream:
993972 total_length += len(chunk)
994973 if total_length > maximum_size:
@@ -1169,13 +1148,6 @@ def run():
11691148 )
11701149
11711150
1172−def patch_application(app):
1173− # serve pages with HTTP/1.1
1174− WSGIRequestHandler.protocol_version = "HTTP/{}".format(settings['server']['http_protocol_version'])
1175− # patch app to handle non root url-s behind proxy & wsgi
1176− app.wsgi_app = ReverseProxyPathFix(ProxyFix(app.wsgi_app))
1177−
1178−
11791151class ReverseProxyPathFix:
11801152 '''Wrap the application in this middleware and configure the
11811153 front-end server to add these headers, to let you quietly bind