__all__ = ['robot_curl']
+from urllib.parse import urlsplit, parse_qsl, quote, quote_plus, urlencode
+
+from m_lib.defenc import default_encoding
import certifi
import pycurl
curl.setopt(pycurl.HTTPGET, 1)
curl.setopt(pycurl.HTTPHEADER, headers)
+ try:
+ url.encode('ascii')
+ except UnicodeEncodeError:
+ url = encode_url(url, bookmark.charset)
curl.setopt(pycurl.URL, url)
try:
curl.perform()
def get_ftp_welcome(self):
return '' # We doen't store welcome message yet
+
+
+def encode_url(url, encoding):
+ if not encoding:
+ encoding = default_encoding
+
+ split_results = urlsplit(url)
+ protocol, netloc, path, query, tag = split_results
+ user = split_results.username
+ password = split_results.password
+ host = split_results.hostname
+ port = split_results.port
+
+ if query:
+ qlist = []
+ for name, value in parse_qsl(query):
+ if isinstance(name, bytes):
+ name = name.decode(default_encoding)
+ value = value.decode(default_encoding)
+ name = name.encode(encoding)
+ value = value.encode(encoding)
+ qlist.append((name, value))
+
+ url = protocol + "://"
+ if user:
+ if isinstance(user, bytes):
+ user = user.decode(default_encoding)
+ url += quote(user.encode(encoding))
+ if password:
+ if isinstance(password, bytes):
+ password = password.decode(default_encoding)
+ url += ':' + quote(password.encode(encoding))
+ url += '@'
+ if host:
+ if isinstance(host, bytes):
+ host = host.decode(encoding)
+ url += host.encode('idna').decode('ascii')
+ if port:
+ url += ':%d' % port
+ if path:
+ if protocol == "file":
+ url += quote(path)
+ else:
+ if isinstance(path, bytes):
+ path = path.decode(default_encoding)
+ url += quote(path.encode(encoding))
+ if query:
+ url += '?' + urlencode(qlist)
+ if tag:
+ if isinstance(tag, bytes):
+ tag = tag.decode(default_encoding)
+ url += '#' + quote_plus(tag.encode(encoding))
+
+ return url