def image_proxy(): url = request.args.get("url").encode() if not url: return "", 400 h = new_hmac(settings["server"]["secret_key"], url) if h != request.args.get("h"): return "", 400 headers = dict_subset(request.headers, {"If-Modified-Since", "If-None-Match"}) headers["User-Agent"] = gen_useragent() resp = requests.get( url, stream=True, timeout=settings["outgoing"]["request_timeout"], headers=headers, proxies=get_global_proxies(), ) if resp.status_code == 304: return "", resp.status_code if resp.status_code != 200: logger.debug("image-proxy: wrong response code: {0}".format( resp.status_code)) if resp.status_code >= 400: return "", resp.status_code return "", 400 if not resp.headers.get("content-type", "").startswith("image/"): logger.debug("image-proxy: wrong content-type: {0}".format( resp.headers.get("content-type"))) return "", 400 img = b"" chunk_counter = 0 for chunk in resp.iter_content(1024 * 1024): chunk_counter += 1 if chunk_counter > 5: return "", 502 # Bad gateway - file is too big (>5M) img += chunk headers = dict_subset( resp.headers, { "Content-Length", "Length", "Date", "Last-Modified", "Expires", "Etag" }, ) return Response(img, mimetype=resp.headers["content-type"], headers=headers)
def image_proxy(): url = request.args.get('url').encode() if not url: return '', 400 h = new_hmac(settings['server']['secret_key'], url) if h != request.args.get('h'): return '', 400 headers = dict_subset(request.headers, {'If-Modified-Since', 'If-None-Match'}) headers['User-Agent'] = gen_useragent() resp = requests.get(url, stream=True, timeout=settings['outgoing']['request_timeout'], headers=headers, proxies=get_global_proxies()) if resp.status_code == 304: return '', resp.status_code if resp.status_code != 200: logger.debug('image-proxy: wrong response code: {0}'.format( resp.status_code)) if resp.status_code >= 400: return '', resp.status_code return '', 400 if not resp.headers.get('content-type', '').startswith('image/'): logger.debug('image-proxy: wrong content-type: {0}'.format( resp.headers.get('content-type'))) return '', 400 img = b'' chunk_counter = 0 for chunk in resp.iter_content(1024 * 1024): chunk_counter += 1 if chunk_counter > 5: return '', 502 # Bad gateway - file is too big (>5M) img += chunk headers = dict_subset(resp.headers, { 'Content-Length', 'Length', 'Date', 'Last-Modified', 'Expires', 'Etag' }) return Response(img, mimetype=resp.headers['content-type'], headers=headers)