From 16d47923c33633aec9ce812ad85c4981c5bc6970 Mon Sep 17 00:00:00 2001 From: Yeuoly <45712896+Yeuoly@users.noreply.github.com> Date: Tue, 14 May 2024 16:01:23 +0800 Subject: [PATCH] fix: requests timeout (#4370) --- api/core/helper/ssrf_proxy.py | 14 ++++++++++++++ api/core/workflow/nodes/http_request/entities.py | 6 +++--- .../nodes/http_request/http_request_node.py | 6 ++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/api/core/helper/ssrf_proxy.py b/api/core/helper/ssrf_proxy.py index c44d4717e6..276c8a34e7 100644 --- a/api/core/helper/ssrf_proxy.py +++ b/api/core/helper/ssrf_proxy.py @@ -42,6 +42,20 @@ def delete(url, *args, **kwargs): if kwargs['follow_redirects']: kwargs['allow_redirects'] = kwargs['follow_redirects'] kwargs.pop('follow_redirects') + if 'timeout' in kwargs: + timeout = kwargs['timeout'] + if timeout is None: + kwargs.pop('timeout') + elif isinstance(timeout, tuple): + # check length of tuple + if len(timeout) == 2: + kwargs['timeout'] = timeout + elif len(timeout) == 1: + kwargs['timeout'] = timeout[0] + elif len(timeout) > 2: + kwargs['timeout'] = (timeout[0], timeout[1]) + else: + kwargs['timeout'] = (timeout, timeout) return _delete(url=url, *args, proxies=requests_proxies, **kwargs) def head(url, *args, **kwargs): diff --git a/api/core/workflow/nodes/http_request/entities.py b/api/core/workflow/nodes/http_request/entities.py index 31d5a679b0..4a81a4176d 100644 --- a/api/core/workflow/nodes/http_request/entities.py +++ b/api/core/workflow/nodes/http_request/entities.py @@ -40,9 +40,9 @@ class HttpRequestNodeData(BaseNodeData): data: Union[None, str] class Timeout(BaseModel): - connect: int = MAX_CONNECT_TIMEOUT - read: int = MAX_READ_TIMEOUT - write: int = MAX_WRITE_TIMEOUT + connect: Optional[int] = MAX_CONNECT_TIMEOUT + read: Optional[int] = MAX_READ_TIMEOUT + write: Optional[int] = MAX_WRITE_TIMEOUT method: Literal['get', 'post', 'put', 'patch', 'delete', 'head'] url: str diff --git a/api/core/workflow/nodes/http_request/http_request_node.py b/api/core/workflow/nodes/http_request/http_request_node.py index bfd686175a..d983a30695 100644 --- a/api/core/workflow/nodes/http_request/http_request_node.py +++ b/api/core/workflow/nodes/http_request/http_request_node.py @@ -95,8 +95,14 @@ class HttpRequestNode(BaseNode): if timeout is None: return HTTP_REQUEST_DEFAULT_TIMEOUT + if timeout.connect is None: + timeout.connect = HTTP_REQUEST_DEFAULT_TIMEOUT.connect timeout.connect = min(timeout.connect, MAX_CONNECT_TIMEOUT) + if timeout.read is None: + timeout.read = HTTP_REQUEST_DEFAULT_TIMEOUT.read timeout.read = min(timeout.read, MAX_READ_TIMEOUT) + if timeout.write is None: + timeout.write = HTTP_REQUEST_DEFAULT_TIMEOUT.write timeout.write = min(timeout.write, MAX_WRITE_TIMEOUT) return timeout