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.
26 def set_proxy_environ():
27 global _my_noproxy, _my_proxies
30 for key in _my_proxies.keys():
31 os.environ[key + "_proxy"] = _my_proxies[key]
34 os.environ["no_proxy"] = _my_noproxy
36 def unset_proxy_environ():
37 for env in ('http_proxy',
44 env_upper = env.upper()
45 if env_upper in os.environ:
46 del os.environ[env_upper]
48 def _set_proxies(proxy = None, no_proxy = None):
49 """Return a dictionary of scheme -> proxy server URL mappings.
52 global _my_noproxy, _my_proxies
57 proxies.append(("http_proxy", proxy))
59 proxies.append(("no_proxy", no_proxy))
61 # Get proxy settings from environment if not provided
62 if not proxy and not no_proxy:
63 proxies = os.environ.items()
65 # Remove proxy env variables, urllib2 can't handle them correctly
68 for name, value in proxies:
70 if value and name[-6:] == '_proxy':
72 _my_proxies[name[:-6]] = value
79 for dec in ip.split("."):
80 ipint |= int(dec) << shift
90 ipaddr = ".%d%s" % (dec, ipaddr)
95 if host.replace(".", "").isdigit():
99 def _set_noproxy_list():
100 global _my_noproxy, _my_noproxy_list
101 _my_noproxy_list = []
104 for item in _my_noproxy.split(","):
109 if item[0] != '.' and item.find("/") == -1:
111 _my_noproxy_list.append({"match":0, "needle":item})
114 # Need to match at tail
115 _my_noproxy_list.append({"match":1, "needle":item})
117 elif item.find("/") > 3:
118 # IP/MASK, need to match at head
119 needle = item[0:item.find("/")].strip()
120 ip = _ip_to_int(needle)
122 mask = item[item.find("/")+1:].strip()
126 netmask = ~((1<<(32-netmask)) - 1)
131 for dec in mask.split("."):
132 netmask |= int(dec) << shift
136 _my_noproxy_list.append({"match":2, "needle":ip, "netmask":netmask})
139 host = urlparse.urlparse(url)[1]
140 # urlparse.urlparse(url) returns (scheme, host, path, parm, query, frag)
143 user_pass, host = host.split('@', 1)
146 host, port = host.split(':', 1)
148 hostisip = _isip(host)
149 for item in _my_noproxy_list:
150 if hostisip and item["match"] == 1:
153 if item["match"] == 2 and hostisip:
154 if (_ip_to_int(host) & item["netmask"]) == item["needle"]:
157 if item["match"] == 0:
158 if host == item["needle"]:
161 if item["match"] == 1:
162 if re.match(r".*%s$" % item["needle"], host):
167 def set_proxies(proxy = None, no_proxy = None):
168 _set_proxies(proxy, no_proxy)
172 def get_proxy_for(url):
173 if url.startswith('file:') or _isnoproxy(url):
176 type = url[0:url.index(":")]
178 if _my_proxies.has_key(type):
179 proxy = _my_proxies[type]
180 elif _my_proxies.has_key("http"):
181 proxy = _my_proxies["http"]