3 # Copyright (c) 2010, 2011 Intel, Inc.
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation; version 2 of the License
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc., 59
16 # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 def set_proxy_environ():
26 global _my_noproxy, _my_proxies
29 for key in _my_proxies.keys():
30 os.environ[key + "_proxy"] = _my_proxies[key]
33 os.environ["no_proxy"] = _my_noproxy
35 def unset_proxy_environ():
36 for env in ('http_proxy',
47 def _set_proxies(proxy = None, no_proxy = None):
48 """Return a dictionary of scheme -> proxy server URL mappings.
51 global _my_noproxy, _my_proxies
56 proxies.append(("http_proxy", proxy))
58 proxies.append(("no_proxy", no_proxy))
60 # Get proxy settings from environment if not provided
61 if not proxy and not no_proxy:
62 proxies = os.environ.items()
64 # Remove proxy env variables, urllib2 can't handle them correctly
67 for name, value in proxies:
69 if value and name[-6:] == '_proxy':
71 _my_proxies[name[:-6]] = value
78 for dec in ip.split("."):
79 ipint |= int(dec) << shift
89 ipaddr = ".%d%s" % (dec, ipaddr)
94 if host.replace(".", "").isdigit():
98 def _set_noproxy_list():
99 global _my_noproxy, _my_noproxy_list
100 _my_noproxy_list = []
103 for item in _my_noproxy.split(","):
108 if item[0] != '.' and item.find("/") == -1:
110 _my_noproxy_list.append({"match":0,"needle":item})
113 # Need to match at tail
114 _my_noproxy_list.append({"match":1,"needle":item})
116 elif item.find("/") > 3:
117 # IP/MASK, need to match at head
118 needle = item[0:item.find("/")].strip()
119 ip = _ip_to_int(needle)
121 mask = item[item.find("/")+1:].strip()
125 netmask = ~((1<<(32-netmask)) - 1)
130 for dec in mask.split("."):
131 netmask |= int(dec) << shift
135 _my_noproxy_list.append({"match":2,"needle":ip,"netmask":netmask})
138 (scheme, host, path, parm, query, frag) = urlparse.urlparse(url)
141 user_pass, host = host.split('@', 1)
144 host, port = host.split(':', 1)
146 hostisip = _isip(host)
147 for item in _my_noproxy_list:
148 if hostisip and item["match"] <= 1:
151 if item["match"] == 2 and hostisip:
152 if (_ip_to_int(host) & item["netmask"]) == item["needle"]:
155 if item["match"] == 0:
156 if host == item["needle"]:
159 if item["match"] == 1:
160 if host.rfind(item["needle"]) > 0:
165 def set_proxies(proxy = None, no_proxy = None):
166 _set_proxies(proxy, no_proxy)
170 def get_proxy_for(url):
171 if url.startswith('file:') or _isnoproxy(url):
174 type = url[0:url.index(":")]
176 if _my_proxies.has_key(type):
177 proxy = _my_proxies[type]
178 elif _my_proxies.has_key("http"):
179 proxy = _my_proxies["http"]