From 1a2203038c22f5e0af444b8a90fe880d5819ac78 Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Tue, 30 Jul 2024 11:18:06 +0800 Subject: [PATCH] add timeouts to all HTTP requests to avoid hangs --- app/auth/recaptcha3.py | 2 +- app/utils.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/auth/recaptcha3.py b/app/auth/recaptcha3.py index 3800672f..db72f891 100644 --- a/app/auth/recaptcha3.py +++ b/app/auth/recaptcha3.py @@ -83,7 +83,7 @@ class Recaptcha3Validator(object): 'response': response } - http_response = requests.post(RECAPTCHA_VERIFY_SERVER, data) + http_response = requests.post(RECAPTCHA_VERIFY_SERVER, data, timeout=10) if http_response.status_code != 200: return False diff --git a/app/utils.py b/app/utils.py index d4aa6e0e..af8e8056 100644 --- a/app/utils.py +++ b/app/utils.py @@ -204,7 +204,7 @@ def is_video_url(url): def mime_type_using_head(url): # Find the mime type of a url by doing a HEAD request - this is the same as GET except only the HTTP headers are transferred try: - response = requests.head(url) + response = requests.head(url, timeout=5) response.raise_for_status() # Raise an exception for HTTP errors content_type = response.headers.get('Content-Type') if content_type: @@ -1053,7 +1053,8 @@ def in_sorted_list(arr, target): # Makes a still image from a video url, without downloading the whole video file def generate_image_from_video_url(video_url, output_path, length=2): - response = requests.get(video_url, stream=True, headers={'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:127.0) Gecko/20100101 Firefox/127.0'}) # Imgur requires a user agent + response = requests.get(video_url, stream=True, timeout=5, + headers={'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:127.0) Gecko/20100101 Firefox/127.0'}) # Imgur requires a user agent content_type = response.headers.get('Content-Type') if content_type: if 'video/mp4' in content_type: