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''
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)
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':
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)