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("."):
82 ipint |= int(dec) << shift
92 ipaddr = ".%d%s" % (dec, ipaddr)
97 if host.replace(".", "").isdigit():
101 def _set_noproxy_list():
102 global _my_noproxy, _my_noproxy_list
103 _my_noproxy_list = []
106 for item in _my_noproxy.split(","):
111 if item[0] != '.' and item.find("/") == -1:
113 _my_noproxy_list.append({"match":0, "needle":item})
116 # Need to match at tail
117 _my_noproxy_list.append({"match":1, "needle":item})
119 elif item.find("/") > 3:
120 # IP/MASK, need to match at head
121 needle = item[0:item.find("/")].strip()
122 ip = _ip_to_int(needle)
124 mask = item[item.find("/")+1:].strip()
128 netmask = ~((1<<(32-netmask)) - 1)
133 for dec in mask.split("."):
134 if not dec.isdigit():
136 netmask |= int(dec) << shift
140 _my_noproxy_list.append({"match":2, "needle":ip, "netmask":netmask})
143 host = urlparse.urlparse(url)[1]
144 # urlparse.urlparse(url) returns (scheme, host, path, parm, query, frag)
147 user_pass, host = host.split('@', 1)
150 host, port = host.split(':', 1)
152 hostisip = _isip(host)
153 for item in _my_noproxy_list:
154 if hostisip and item["match"] == 1:
157 if item["match"] == 2 and hostisip:
158 if (_ip_to_int(host) & item["netmask"]) == item["needle"]:
161 if item["match"] == 0:
162 if host == item["needle"]:
165 if item["match"] == 1:
166 if re.match(r".*%s$" % item["needle"], host):
171 def set_proxies(proxy = None, no_proxy = None):
172 _set_proxies(proxy, no_proxy)
176 def get_proxy_for(url):
177 if url.startswith('file:') or _isnoproxy(url):
180 type = url[0:url.index(":")]
182 if _my_proxies.has_key(type):
183 proxy = _my_proxies[type]
184 elif _my_proxies.has_key("http"):
185 proxy = _my_proxies["http"]