From 8bb7584ad58062de489ade990a10244de9854b54 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Thu, 22 Aug 2024 19:54:36 +0300 Subject: [PATCH] Refactor(bkmk_rcurl): Split off `CurlWrapper` Will be used in multi-curl robot(s). --- Robots/bkmk_rcurl.py | 49 ++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/Robots/bkmk_rcurl.py b/Robots/bkmk_rcurl.py index 3c7b4df..f1258b1 100644 --- a/Robots/bkmk_rcurl.py +++ b/Robots/bkmk_rcurl.py @@ -24,9 +24,32 @@ class robot_curl(robot_base): return str(pycurl.version) async def get(self, url, req_headers, use_proxy=False): + curlw = CurlWrapper(self, url, req_headers, use_proxy) + + try: + curlw.perform() + except pycurl.error as e: + error = str(e) + return error, None, None, None, None + + status = curlw.getinfo(pycurl.HTTP_CODE) + curlw.close() + + if status >= 400: + return "Error %d" % status, status, None, None, None + if status >= 300: + return None, status, curlw.resp_headers['Location'], None, None + return None, None, None, curlw.resp_headers, curlw.body + + def get_ftp_welcome(self): + return '' # We don't store welcome message yet + + +class CurlWrapper: + def __init__(self, robot, url, req_headers, use_proxy): req_headers = ['%s: %s' % (k, v) for k, v in req_headers.items()] - curl = pycurl.Curl() + self.curl = curl = pycurl.Curl() self.resp_headers = {} self.body = b'' @@ -39,16 +62,16 @@ class robot_curl(robot_base): curl.setopt(curl.CAINFO, certifi.where()) # Set timeouts to avoid hanging too long if url.startswith('ftp://'): - timeout = self.ftp_timeout + timeout = robot.ftp_timeout else: - timeout = self.timeout + timeout = robot.timeout curl.setopt(pycurl.CONNECTTIMEOUT, timeout) curl.setopt(pycurl.TIMEOUT, timeout) # Parse Last-Modified curl.setopt(pycurl.OPT_FILETIME, 1) if use_proxy: - curl.setopt(pycurl.PROXY, self.proxy) + curl.setopt(pycurl.PROXY, robot.proxy) # Set up a callback to capture the headers and the body curl.setopt(pycurl.HEADERFUNCTION, self.header_callback) @@ -61,20 +84,9 @@ class robot_curl(robot_base): except UnicodeEncodeError: url = encode_url(url) curl.setopt(pycurl.URL, url) - try: - curl.perform() - except pycurl.error as e: - error = str(e) - return error, None, None, None, None - status = curl.getinfo(pycurl.HTTP_CODE) - curl.close() - - if status >= 400: - return "Error %d" % status, status, None, None, None - if status >= 300: - return None, status, self.resp_headers['Location'], None, None - return None, None, None, self.resp_headers, self.body + def __getattr__(self, attr): + return getattr(self.curl, attr) def header_callback(self, data): for encoding in 'ascii', 'latin1', 'utf-8': @@ -94,9 +106,6 @@ class robot_curl(robot_base): def body_callback(self, data): self.body += data - def get_ftp_welcome(self): - return '' # We don't store welcome message yet - def encode_url(url, encoding='latin1'): split_results = urlsplit(url) -- 2.39.5