From: Oleg Broytman Date: Thu, 22 Aug 2024 16:54:36 +0000 (+0300) Subject: Refactor(bkmk_rcurl): Split off `CurlWrapper` X-Git-Tag: 6.1.0~9 X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=8bb7584ad58062de489ade990a10244de9854b54;p=bookmarks_db.git Refactor(bkmk_rcurl): Split off `CurlWrapper` Will be used in multi-curl robot(s). --- 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)