mirror of
https://github.com/wakatime/sublime-wakatime.git
synced 2023-08-10 21:13:02 +03:00
Prevent installing global url opener
This commit is contained in:
parent
847223cdce
commit
da17125b97
85
WakaTime.py
85
WakaTime.py
@ -40,9 +40,9 @@ try:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
from configparser import ConfigParser, Error as ConfigParserError
|
from configparser import ConfigParser, Error as ConfigParserError
|
||||||
try:
|
try:
|
||||||
from urllib2 import urlopen, urlretrieve, ProxyHandler, build_opener, install_opener, HTTPError
|
from urllib2 import Request, urlopen, HTTPError
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from urllib.request import urlopen, urlretrieve, ProxyHandler, build_opener, install_opener
|
from urllib.request import Request, urlopen
|
||||||
from urllib.error import HTTPError
|
from urllib.error import HTTPError
|
||||||
|
|
||||||
|
|
||||||
@ -847,34 +847,27 @@ def reportMissingPlatformSupport(osname, arch):
|
|||||||
|
|
||||||
|
|
||||||
def request(url, last_modified=None):
|
def request(url, last_modified=None):
|
||||||
|
req = Request(url)
|
||||||
|
req.add_header('User-Agent', 'github.com/wakatime/sublime-wakatime')
|
||||||
|
|
||||||
proxy = SETTINGS.get('proxy')
|
proxy = SETTINGS.get('proxy')
|
||||||
if proxy:
|
if proxy:
|
||||||
opener = build_opener(ProxyHandler({
|
req.set_proxy(proxy, 'https')
|
||||||
'http': proxy,
|
|
||||||
'https': proxy,
|
|
||||||
}))
|
|
||||||
else:
|
|
||||||
opener = build_opener()
|
|
||||||
|
|
||||||
headers = [('User-Agent', 'github.com/wakatime/sublime-wakatime')]
|
|
||||||
if last_modified:
|
if last_modified:
|
||||||
headers.append(('If-Modified-Since', last_modified))
|
req.add_header('If-Modified-Since', last_modified)
|
||||||
|
|
||||||
opener.addheaders = headers
|
|
||||||
|
|
||||||
install_opener(opener)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
resp = urlopen(url)
|
resp = urlopen(req)
|
||||||
headers = dict(resp.getheaders()) if is_py2 else resp.headers
|
headers = dict(resp.getheaders()) if is_py2 else resp.headers
|
||||||
return headers, resp.read(), resp.getcode()
|
return headers, resp.read(), resp.getcode()
|
||||||
except HTTPError as err:
|
except HTTPError as err:
|
||||||
if err.code == 304:
|
if err.code == 304:
|
||||||
return None, None, 304
|
return None, None, 304
|
||||||
if is_py2:
|
if is_py2:
|
||||||
ssl._create_default_https_context = ssl._create_unverified_context
|
with SSLCertVerificationDisabled():
|
||||||
try:
|
try:
|
||||||
resp = urlopen(url)
|
resp = urlopen(req)
|
||||||
headers = dict(resp.getheaders()) if is_py2 else resp.headers
|
headers = dict(resp.getheaders()) if is_py2 else resp.headers
|
||||||
return headers, resp.read(), resp.getcode()
|
return headers, resp.read(), resp.getcode()
|
||||||
except HTTPError as err2:
|
except HTTPError as err2:
|
||||||
@ -889,7 +882,7 @@ def request(url, last_modified=None):
|
|||||||
raise
|
raise
|
||||||
except IOError:
|
except IOError:
|
||||||
if is_py2:
|
if is_py2:
|
||||||
ssl._create_default_https_context = ssl._create_unverified_context
|
with SSLCertVerificationDisabled():
|
||||||
try:
|
try:
|
||||||
resp = urlopen(url)
|
resp = urlopen(url)
|
||||||
headers = dict(resp.getheaders()) if is_py2 else resp.headers
|
headers = dict(resp.getheaders()) if is_py2 else resp.headers
|
||||||
@ -905,24 +898,46 @@ def request(url, last_modified=None):
|
|||||||
|
|
||||||
|
|
||||||
def download(url, filePath):
|
def download(url, filePath):
|
||||||
|
req = Request(url)
|
||||||
|
req.add_header('User-Agent', 'github.com/wakatime/sublime-wakatime')
|
||||||
|
|
||||||
proxy = SETTINGS.get('proxy')
|
proxy = SETTINGS.get('proxy')
|
||||||
if proxy:
|
if proxy:
|
||||||
opener = build_opener(ProxyHandler({
|
req.set_proxy(proxy, 'https')
|
||||||
'http': proxy,
|
|
||||||
'https': proxy,
|
|
||||||
}))
|
|
||||||
else:
|
|
||||||
opener = build_opener()
|
|
||||||
opener.addheaders = [('User-Agent', 'github.com/wakatime/sublime-wakatime')]
|
|
||||||
|
|
||||||
install_opener(opener)
|
|
||||||
|
|
||||||
|
with open(filePath, 'wb') as fh:
|
||||||
try:
|
try:
|
||||||
urlretrieve(url, filePath)
|
resp = urlopen(req)
|
||||||
|
fh.write(resp.read())
|
||||||
|
except HTTPError as err:
|
||||||
|
if err.code == 304:
|
||||||
|
return None, None, 304
|
||||||
|
if is_py2:
|
||||||
|
with SSLCertVerificationDisabled():
|
||||||
|
try:
|
||||||
|
resp = urlopen(req)
|
||||||
|
fh.write(resp.read())
|
||||||
|
return
|
||||||
|
except HTTPError as err2:
|
||||||
|
log(DEBUG, err.read().decode())
|
||||||
|
log(DEBUG, err2.read().decode())
|
||||||
|
raise
|
||||||
|
except IOError:
|
||||||
|
raise
|
||||||
|
log(DEBUG, err.read().decode())
|
||||||
|
raise
|
||||||
except IOError:
|
except IOError:
|
||||||
if is_py2:
|
if is_py2:
|
||||||
ssl._create_default_https_context = ssl._create_unverified_context
|
with SSLCertVerificationDisabled():
|
||||||
urlretrieve(url, filePath)
|
try:
|
||||||
|
resp = urlopen(url)
|
||||||
|
fh.write(resp.read())
|
||||||
|
return
|
||||||
|
except HTTPError as err:
|
||||||
|
log(DEBUG, err.read().decode())
|
||||||
|
raise
|
||||||
|
except IOError:
|
||||||
|
raise
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
||||||
@ -949,3 +964,13 @@ def createSymlink():
|
|||||||
os.chmod(link, 509) # 755
|
os.chmod(link, 509) # 755
|
||||||
except:
|
except:
|
||||||
log(traceback.format_exc())
|
log(traceback.format_exc())
|
||||||
|
|
||||||
|
|
||||||
|
class SSLCertVerificationDisabled(object):
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
self.original_context = ssl._create_default_https_context
|
||||||
|
ssl._create_default_https_context = ssl._create_unverified_context
|
||||||
|
|
||||||
|
def __exit__(self, *args, **kwargs):
|
||||||
|
ssl._create_default_https_context = self.original_context
|
||||||
|
Loading…
Reference in New Issue
Block a user