View | Details | Raw Unified | Return to bug 180921 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/core/tests/net/NetTest.java (-68 / +215 lines)
Lines 1-26 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2007 IBM Corporation and others.
2
 * Copyright (c) 2007 IBM Corporation and others. All rights reserved. This
3
 * All rights reserved. This program and the accompanying materials
3
 * program and the accompanying materials are made available under the terms of
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * the Eclipse Public License v1.0 which accompanies this distribution, and is
5
 * which accompanies this distribution, and is available at
5
 * available at http://www.eclipse.org/legal/epl-v10.html
6
 * http://www.eclipse.org/legal/epl-v10.html
6
 * 
7
 *
7
 * Contributors: IBM Corporation - initial API and implementation
8
 * Contributors:
8
 ******************************************************************************/
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.core.tests.net;
9
package org.eclipse.core.tests.net;
12
10
13
import java.util.*;
11
import java.net.URI;
14
12
import java.net.URISyntaxException;
15
import junit.framework.*;
13
import java.util.HashMap;
14
import java.util.Map;
15
import java.util.Properties;
16
17
import junit.framework.Test;
18
import junit.framework.TestCase;
19
import junit.framework.TestSuite;
16
20
21
import org.eclipse.core.internal.net.AbstractProxyProvider;
17
import org.eclipse.core.internal.net.ProxyType;
22
import org.eclipse.core.internal.net.ProxyType;
23
import org.eclipse.core.internal.net.UnixProxyProvider;
24
import org.eclipse.core.internal.net.WindowsProxyProvider;
18
import org.eclipse.core.net.proxy.IProxyData;
25
import org.eclipse.core.net.proxy.IProxyData;
19
import org.eclipse.core.net.proxy.IProxyService;
26
import org.eclipse.core.net.proxy.IProxyService;
20
import org.eclipse.core.runtime.CoreException;
27
import org.eclipse.core.runtime.CoreException;
21
28
22
public class NetTest extends TestCase {
29
public class NetTest extends TestCase {
23
	
30
24
	private boolean isSetEnabled;
31
	private boolean isSetEnabled;
25
	private Map dataCache = new HashMap();
32
	private Map dataCache = new HashMap();
26
33
Lines 31-37 Link Here
31
	public NetTest(String name) {
38
	public NetTest(String name) {
32
		super(name);
39
		super(name);
33
	}
40
	}
34
	
41
35
	public static Test suite() {
42
	public static Test suite() {
36
		return new TestSuite(NetTest.class);
43
		return new TestSuite(NetTest.class);
37
	}
44
	}
Lines 43-49 Link Here
43
		dataCache.clear();
50
		dataCache.clear();
44
		ProxyType.socksSystemPropertySetting = ProxyType.ALWAYS_SET;
51
		ProxyType.socksSystemPropertySetting = ProxyType.ALWAYS_SET;
45
	}
52
	}
46
	
53
47
	protected void tearDown() throws Exception {
54
	protected void tearDown() throws Exception {
48
		super.tearDown();
55
		super.tearDown();
49
		setProxiesEnabled(false);
56
		setProxiesEnabled(false);
Lines 54-75 Link Here
54
		}
61
		}
55
		getProxyManager().setProxyData(data);
62
		getProxyManager().setProxyData(data);
56
	}
63
	}
57
	
64
58
	private IProxyService getProxyManager() {
65
	private IProxyService getProxyManager() {
59
		return Activator.getDefault().getProxyService();
66
		return Activator.getDefault().getProxyService();
60
	}
67
	}
61
68
62
	private void assertProxyDataEqual(IProxyData expectedData) {
69
	private void assertProxyDataEqual(IProxyData expectedData) {
63
		IProxyData data = getProxyManager().getProxyData(expectedData.getType());
70
		IProxyData data = getProxyManager()
71
				.getProxyData(expectedData.getType());
64
		assertEquals(expectedData.getType(), data.getType());
72
		assertEquals(expectedData.getType(), data.getType());
65
		assertEquals(expectedData.getHost(), data.getHost());
73
		assertEquals(expectedData.getHost(), data.getHost());
66
		assertEquals(expectedData.getPort(), data.getPort());
74
		assertEquals(expectedData.getPort(), data.getPort());
67
		assertEquals(expectedData.getUserId(), data.getUserId());
75
		assertEquals(expectedData.getUserId(), data.getUserId());
68
		assertEquals(expectedData.getPassword(), data.getPassword());
76
		assertEquals(expectedData.getPassword(), data.getPassword());
69
		assertEquals(expectedData.isRequiresAuthentication(), data.isRequiresAuthentication());
77
		assertEquals(expectedData.isRequiresAuthentication(), data
78
				.isRequiresAuthentication());
70
		assertSystemPropertiesMatch(data);
79
		assertSystemPropertiesMatch(data);
71
	}
80
	}
72
	
81
73
	public void assertSystemPropertiesMatch(IProxyData proxyData) {
82
	public void assertSystemPropertiesMatch(IProxyData proxyData) {
74
		if (proxyData.getType().equals(IProxyData.HTTP_PROXY_TYPE)) {
83
		if (proxyData.getType().equals(IProxyData.HTTP_PROXY_TYPE)) {
75
			assertHttpSystemProperties(proxyData, "http");
84
			assertHttpSystemProperties(proxyData, "http");
Lines 80-104 Link Here
80
		}
89
		}
81
	}
90
	}
82
91
83
	private void assertHttpSystemProperties(IProxyData proxyData, String keyPrefix) {
92
	private void assertHttpSystemProperties(IProxyData proxyData,
93
			String keyPrefix) {
84
		Properties sysProps = System.getProperties();
94
		Properties sysProps = System.getProperties();
85
		
95
86
		if (this.getProxyManager().isProxiesEnabled()) {
96
		if (this.getProxyManager().isProxiesEnabled()) {
87
			boolean isSet = Boolean.getBoolean(keyPrefix + ".proxySet");
97
			boolean isSet = Boolean.getBoolean(keyPrefix + ".proxySet");
88
			assertEquals(proxyData.getHost() != null, isSet); //$NON-NLS-1$
98
			assertEquals(proxyData.getHost() != null, isSet); //$NON-NLS-1$
89
			assertEquals(proxyData.getHost(), sysProps.get(keyPrefix + ".proxyHost")); //$NON-NLS-1$
99
			assertEquals(proxyData.getHost(), sysProps.get(keyPrefix
90
			String portString = (String)sysProps.get(keyPrefix + ".proxyPort"); //$NON-NLS-1$
100
					+ ".proxyHost")); //$NON-NLS-1$
101
			String portString = (String) sysProps.get(keyPrefix + ".proxyPort"); //$NON-NLS-1$
91
			int port = -1;
102
			int port = -1;
92
			if (portString != null)
103
			if (portString != null)
93
				port = Integer.parseInt(portString);
104
				port = Integer.parseInt(portString);
94
			assertEquals(proxyData.getPort(), port);
105
			assertEquals(proxyData.getPort(), port);
95
			if (isSet)
106
			if (isSet)
96
				assertEquals(ProxyType.convertHostsToPropertyString(this.getProxyManager().getNonProxiedHosts()), sysProps.get(keyPrefix + ".nonProxyHosts")); //$NON-NLS-1$
107
				assertEquals(ProxyType.convertHostsToPropertyString(this
97
			else 
108
						.getProxyManager().getNonProxiedHosts()), sysProps
109
						.get(keyPrefix + ".nonProxyHosts")); //$NON-NLS-1$
110
			else
98
				assertNull(sysProps.get(keyPrefix + ".nonProxyHosts"));
111
				assertNull(sysProps.get(keyPrefix + ".nonProxyHosts"));
99
			assertEquals(proxyData.getUserId(), sysProps.get(keyPrefix + ".proxyUser")); //$NON-NLS-1$
112
			assertEquals(proxyData.getUserId(), sysProps.get(keyPrefix
100
			assertEquals(proxyData.getUserId(), sysProps.get(keyPrefix + ".proxyUserName")); //$NON-NLS-1$
113
					+ ".proxyUser")); //$NON-NLS-1$
101
			assertEquals(proxyData.getPassword(), sysProps.get(keyPrefix + ".proxyPassword")); //$NON-NLS-1$
114
			assertEquals(proxyData.getUserId(), sysProps.get(keyPrefix
115
					+ ".proxyUserName")); //$NON-NLS-1$
116
			assertEquals(proxyData.getPassword(), sysProps.get(keyPrefix
117
					+ ".proxyPassword")); //$NON-NLS-1$
102
		} else {
118
		} else {
103
			assertNull(sysProps.get(keyPrefix + ".proxySet"));
119
			assertNull(sysProps.get(keyPrefix + ".proxySet"));
104
			assertNull(sysProps.get(keyPrefix + ".proxyHost"));
120
			assertNull(sysProps.get(keyPrefix + ".proxyHost"));
Lines 114-120 Link Here
114
		Properties sysProps = System.getProperties();
130
		Properties sysProps = System.getProperties();
115
		if (this.getProxyManager().isProxiesEnabled()) {
131
		if (this.getProxyManager().isProxiesEnabled()) {
116
			assertEquals(proxyData.getHost(), sysProps.get("socksProxyHost")); //$NON-NLS-1$
132
			assertEquals(proxyData.getHost(), sysProps.get("socksProxyHost")); //$NON-NLS-1$
117
			String portString = (String)sysProps.get("socksProxyPort"); //$NON-NLS-1$
133
			String portString = (String) sysProps.get("socksProxyPort"); //$NON-NLS-1$
118
			int port = -1;
134
			int port = -1;
119
			if (portString != null)
135
			if (portString != null)
120
				port = Integer.parseInt(portString);
136
				port = Integer.parseInt(portString);
Lines 124-139 Link Here
124
			assertNull(sysProps.get("socksProxyPort"));
140
			assertNull(sysProps.get("socksProxyPort"));
125
		}
141
		}
126
	}
142
	}
127
	
143
128
	private IProxyData getProxyData(String type) {
144
	private IProxyData getProxyData(String type) {
129
		IProxyData data = (IProxyData)dataCache.get(type);
145
		IProxyData data = (IProxyData) dataCache.get(type);
130
		if (data == null) {
146
		if (data == null) {
131
			data = this.getProxyManager().getProxyData(type);
147
			data = this.getProxyManager().getProxyData(type);
132
			assertProxyDataEqual(data);
148
			assertProxyDataEqual(data);
133
		}
149
		}
134
		return data;
150
		return data;
135
	}
151
	}
136
	
152
137
	private void setProxyData(IProxyData data) throws CoreException {
153
	private void setProxyData(IProxyData data) throws CoreException {
138
		if (isSetEnabled) {
154
		if (isSetEnabled) {
139
			this.getProxyManager().setProxyData(new IProxyData[] { data });
155
			this.getProxyManager().setProxyData(new IProxyData[] { data });
Lines 142-153 Link Here
142
			dataCache.put(data.getType(), data);
158
			dataCache.put(data.getType(), data);
143
		}
159
		}
144
	}
160
	}
145
	
161
146
	private void disableProxy(IProxyData proxyData) throws CoreException {
162
	private void disableProxy(IProxyData proxyData) throws CoreException {
147
		proxyData.disable();
163
		proxyData.disable();
148
		setProxyData(proxyData);
164
		setProxyData(proxyData);
149
	}
165
	}
150
	
166
151
	private void changeProxyData(IProxyData oldData, IProxyData data)
167
	private void changeProxyData(IProxyData oldData, IProxyData data)
152
			throws CoreException {
168
			throws CoreException {
153
		// Make sure that setting the host doesn't change the persisted settings
169
		// Make sure that setting the host doesn't change the persisted settings
Lines 156-162 Link Here
156
		// Now set it in the manager and assert that it is set
172
		// Now set it in the manager and assert that it is set
157
		setProxyData(data);
173
		setProxyData(data);
158
	}
174
	}
159
	
175
160
	private void setHost(String type) throws CoreException {
176
	private void setHost(String type) throws CoreException {
161
		String host = "www.eclipse.org";
177
		String host = "www.eclipse.org";
162
		setHost(type, host);
178
		setHost(type, host);
Lines 168-206 Link Here
168
		data.setHost(host);
184
		data.setHost(host);
169
		changeProxyData(oldData, data);
185
		changeProxyData(oldData, data);
170
	}
186
	}
171
	
187
172
	private void setPort(String type, int port) throws CoreException {
188
	private void setPort(String type, int port) throws CoreException {
173
		IProxyData data = getProxyData(type);
189
		IProxyData data = getProxyData(type);
174
		IProxyData oldData = getProxyData(type);
190
		IProxyData oldData = getProxyData(type);
175
		data.setPort(port);
191
		data.setPort(port);
176
		changeProxyData(oldData, data);
192
		changeProxyData(oldData, data);
177
	}
193
	}
178
	
194
179
	private void setUser(String type, String user, String password) throws CoreException {
195
	private void setUser(String type, String user, String password)
196
			throws CoreException {
180
		IProxyData data = getProxyData(type);
197
		IProxyData data = getProxyData(type);
181
		IProxyData oldData = getProxyData(type);
198
		IProxyData oldData = getProxyData(type);
182
		data.setUserid(user);
199
		data.setUserid(user);
183
		data.setPassword(password);
200
		data.setPassword(password);
184
		changeProxyData(oldData, data);
201
		changeProxyData(oldData, data);
185
	}
202
	}
186
	
203
187
	private void setDataTest(String type) throws CoreException {
204
	private void setDataTest(String type) throws CoreException {
188
		setHost(type, "www.eclipse.org");
205
		setHost(type, "www.eclipse.org");
189
		setPort(type, 1024);
206
		setPort(type, 1024);
190
		setUser(type, "me", "passw0rd");
207
		setUser(type, "me", "passw0rd");
191
	}
208
	}
192
	
209
193
	private void setProxiesEnabled(boolean enabled) {
210
	private void setProxiesEnabled(boolean enabled) {
194
		this.getProxyManager().setProxiesEnabled(enabled);
211
		this.getProxyManager().setProxiesEnabled(enabled);
195
		assertEquals(enabled, this.getProxyManager().isProxiesEnabled());
212
		assertEquals(enabled, this.getProxyManager().isProxiesEnabled());
196
	}
213
	}
197
	
214
198
	private void delaySettingData() {
215
	private void delaySettingData() {
199
		isSetEnabled = false;
216
		isSetEnabled = false;
200
	}
217
	}
201
	
218
202
	private void performSettingData() throws CoreException {
219
	private void performSettingData() throws CoreException {
203
		IProxyData[] data = (IProxyData[]) dataCache.values().toArray(new IProxyData[dataCache.size()]);
220
		IProxyData[] data = (IProxyData[]) dataCache.values().toArray(
221
				new IProxyData[dataCache.size()]);
204
		this.getProxyManager().setProxyData(data);
222
		this.getProxyManager().setProxyData(data);
205
		for (int i = 0; i < data.length; i++) {
223
		for (int i = 0; i < data.length; i++) {
206
			IProxyData proxyData = data[i];
224
			IProxyData proxyData = data[i];
Lines 209-215 Link Here
209
		isSetEnabled = true;
227
		isSetEnabled = true;
210
		dataCache.clear();
228
		dataCache.clear();
211
	}
229
	}
212
	
230
213
	public void testIndividualSetAndClear() throws CoreException {
231
	public void testIndividualSetAndClear() throws CoreException {
214
		setDataTest(IProxyData.HTTP_PROXY_TYPE);
232
		setDataTest(IProxyData.HTTP_PROXY_TYPE);
215
		setDataTest(IProxyData.HTTPS_PROXY_TYPE);
233
		setDataTest(IProxyData.HTTPS_PROXY_TYPE);
Lines 220-233 Link Here
220
			disableProxy(proxyData);
238
			disableProxy(proxyData);
221
		}
239
		}
222
	}
240
	}
223
	
241
224
	public void testAllSetAndClear() throws CoreException {
242
	public void testAllSetAndClear() throws CoreException {
225
		delaySettingData();
243
		delaySettingData();
226
		setDataTest(IProxyData.HTTP_PROXY_TYPE);
244
		setDataTest(IProxyData.HTTP_PROXY_TYPE);
227
		setDataTest(IProxyData.HTTPS_PROXY_TYPE);
245
		setDataTest(IProxyData.HTTPS_PROXY_TYPE);
228
		setDataTest(IProxyData.SOCKS_PROXY_TYPE);
246
		setDataTest(IProxyData.SOCKS_PROXY_TYPE);
229
		performSettingData();
247
		performSettingData();
230
		
248
231
		delaySettingData();
249
		delaySettingData();
232
		IProxyData[] data = this.getProxyManager().getProxyData();
250
		IProxyData[] data = this.getProxyManager().getProxyData();
233
		for (int i = 0; i < data.length; i++) {
251
		for (int i = 0; i < data.length; i++) {
Lines 250-301 Link Here
250
		setProxiesEnabled(false);
268
		setProxiesEnabled(false);
251
		assertProxyDataEqual(data);
269
		assertProxyDataEqual(data);
252
	}
270
	}
253
	
271
254
	public void testSimpleHost() throws CoreException {
272
	public void testSimpleHost() throws CoreException {
255
		setDataTest(IProxyData.HTTP_PROXY_TYPE);
273
		setDataTest(IProxyData.HTTP_PROXY_TYPE);
256
		setDataTest(IProxyData.HTTPS_PROXY_TYPE);
274
		setDataTest(IProxyData.HTTPS_PROXY_TYPE);
257
		setDataTest(IProxyData.SOCKS_PROXY_TYPE);
275
		setDataTest(IProxyData.SOCKS_PROXY_TYPE);
258
		
276
259
		IProxyData[] allData = this.getProxyManager().getProxyDataForHost("www.randomhot.com");
277
		IProxyData[] allData = this.getProxyManager().getProxyDataForHost(
278
				"www.randomhot.com");
260
		assertEquals(3, allData.length);
279
		assertEquals(3, allData.length);
261
		
280
262
		IProxyData data = this.getProxyManager().getProxyDataForHost("www.randomhot.com", IProxyData.HTTP_PROXY_TYPE);
281
		IProxyData data = this.getProxyManager().getProxyDataForHost(
282
				"www.randomhot.com", IProxyData.HTTP_PROXY_TYPE);
263
		assertNotNull(data);
283
		assertNotNull(data);
264
		
284
265
		allData = this.getProxyManager().getProxyDataForHost("localhost");
285
		allData = this.getProxyManager().getProxyDataForHost("localhost");
266
		assertEquals(0, allData.length);
286
		assertEquals(0, allData.length);
267
		
287
268
		data = this.getProxyManager().getProxyDataForHost("localhost", IProxyData.HTTP_PROXY_TYPE);
288
		data = this.getProxyManager().getProxyDataForHost("localhost",
289
				IProxyData.HTTP_PROXY_TYPE);
269
		assertNull(data);
290
		assertNull(data);
270
	}
291
	}
271
	
292
272
	public void testHostPattern() throws CoreException {
293
	public void testHostPattern() throws CoreException {
273
		setDataTest(IProxyData.HTTP_PROXY_TYPE);
294
		setDataTest(IProxyData.HTTP_PROXY_TYPE);
274
		setDataTest(IProxyData.HTTPS_PROXY_TYPE);
295
		setDataTest(IProxyData.HTTPS_PROXY_TYPE);
275
		setDataTest(IProxyData.SOCKS_PROXY_TYPE);
296
		setDataTest(IProxyData.SOCKS_PROXY_TYPE);
276
		
297
277
		String[] oldHosts = this.getProxyManager().getNonProxiedHosts();
298
		String[] oldHosts = this.getProxyManager().getNonProxiedHosts();
278
		this.getProxyManager().setNonProxiedHosts(new String[] { "*ignore.com" });
299
		this.getProxyManager().setNonProxiedHosts(
279
		
300
				new String[] { "*ignore.com" });
280
		IProxyData[] allData = this.getProxyManager().getProxyDataForHost("www.randomhot.com");
301
302
		IProxyData[] allData = this.getProxyManager().getProxyDataForHost(
303
				"www.randomhot.com");
281
		assertEquals(3, allData.length);
304
		assertEquals(3, allData.length);
282
		
305
283
		IProxyData data = this.getProxyManager().getProxyDataForHost("www.randomhot.com", IProxyData.HTTP_PROXY_TYPE);
306
		IProxyData data = this.getProxyManager().getProxyDataForHost(
307
				"www.randomhot.com", IProxyData.HTTP_PROXY_TYPE);
284
		assertNotNull(data);
308
		assertNotNull(data);
285
		
309
286
		allData = this.getProxyManager().getProxyDataForHost("www.ignore.com");
310
		allData = this.getProxyManager().getProxyDataForHost("www.ignore.com");
287
		assertEquals(0, allData.length);
311
		assertEquals(0, allData.length);
288
		
312
289
		data = this.getProxyManager().getProxyDataForHost("www.ignore.com", IProxyData.HTTP_PROXY_TYPE);
313
		data = this.getProxyManager().getProxyDataForHost("www.ignore.com",
314
				IProxyData.HTTP_PROXY_TYPE);
290
		assertNull(data);
315
		assertNull(data);
291
		
316
292
		allData = this.getProxyManager().getProxyDataForHost("ignore.com");
317
		allData = this.getProxyManager().getProxyDataForHost("ignore.com");
293
		assertEquals(0, allData.length);
318
		assertEquals(0, allData.length);
294
		
319
295
		data = this.getProxyManager().getProxyDataForHost("ignore.com", IProxyData.HTTP_PROXY_TYPE);
320
		data = this.getProxyManager().getProxyDataForHost("ignore.com",
321
				IProxyData.HTTP_PROXY_TYPE);
296
		assertNull(data);
322
		assertNull(data);
297
		
323
298
		this.getProxyManager().setNonProxiedHosts(oldHosts);
324
		this.getProxyManager().setNonProxiedHosts(oldHosts);
299
	}
325
	}
300
326
327
	// This test case has to be manually setup and run because it depends on
328
	// whether the proxy is set in the native environment. It is here as
329
	// a convenience to test the native support.
330
	public void testNativeLinux() throws Exception {
331
332
		// Leave this test off for the automatic test runs so we don't
333
		// get confused by the settings that happen to be on the test machine
334
		if (true)
335
			return;
336
337
		AbstractProxyProvider pp = new UnixProxyProvider();
338
339
		IProxyData[] pd;
340
341
		// Test the following cases
342
		// 1) No proxy set anywhere
343
		// 2) http_proxy environment variable (use proxyIsSet)
344
		// 3) gconf-edit proxy (use proxyIsSet and proxyAuthIsSet)
345
		// 4) KDE proxy (use proxyIsSet and proxyAuthIsSet)
346
347
		// Toggle this if you have the proxy set up in the native environment
348
		boolean proxyIsSet = true;
349
350
		// Toggle this if you have the proxy authentication information setup
351
		boolean proxyAuthIsSet = true;
352
353
		String proxyHost = null;
354
		String proxyUser = null;
355
		String proxyPassword = null;
356
		boolean proxyReqAuth = false;
357
		int proxyPort = 0;
358
359
		if (proxyIsSet) {
360
			proxyHost = "berlioz";
361
			proxyPort = 3128;
362
			if (proxyAuthIsSet) {
363
				proxyUser = "user";
364
				proxyPassword = "password";
365
				proxyReqAuth = true;
366
			}
367
		}
368
369
		// Test HTTP
370
		pd = pp.select(new URI("http://bbc.com"));
371
		if (proxyIsSet) {
372
			assertEquals(1, pd.length);
373
			assertEquals(proxyHost, pd[0].getHost());
374
			assertEquals(proxyPort, pd[0].getPort());
375
			if (proxyAuthIsSet) {
376
				assertEquals(proxyUser, pd[0].getUserId());
377
				assertEquals(proxyPassword, pd[0].getPassword());
378
				assertEquals(proxyReqAuth, pd[0].isRequiresAuthentication());
379
			}
380
381
		} else {
382
			assertEquals(0, pd.length);
383
		}
384
385
		// Test HTTPS
386
		pd = pp.select(new URI("https://bbc.com"));
387
		if (proxyIsSet) {
388
			assertEquals(1, pd.length);
389
			assertEquals("https_" + proxyHost, pd[0].getHost());
390
			assertEquals(1 + proxyPort, pd[0].getPort());
391
		} else {
392
			assertEquals(0, pd.length);
393
		}
394
395
		// Test SOCKS
396
		pd = pp.select(new URI("SOCKS://bbc.com"));
397
		if (proxyIsSet) {
398
			assertEquals(1, pd.length);
399
			assertEquals("socks_" + proxyHost, pd[0].getHost());
400
			assertEquals(2 + proxyPort, pd[0].getPort());
401
		} else {
402
			assertEquals(0, pd.length);
403
		}
404
405
		// Test FTP
406
		pd = pp.select(new URI("FTP://bbc.com"));
407
		if (proxyIsSet) {
408
			assertEquals(1, pd.length);
409
			assertEquals("ftp_" + proxyHost, pd[0].getHost());
410
			assertEquals(3 + proxyPort, pd[0].getPort());
411
		} else {
412
			assertEquals(0, pd.length);
413
		}
414
415
		// Assume that localhost is a non-proxy host
416
		pd = pp.select(new URI("http://localhost"));
417
		assertEquals(0, pd.length);
418
	}
419
420
	// This test case has to be manually setup and run because it depends on
421
	// whether the proxy is set in the native environment. It is here as
422
	// a convenience to test the native support.
423
	public void testNativeWin32() throws Exception {
424
425
		// Leave this test off for the automatic test runs so we don't
426
		// get confused by the settings that happen to be on the test machine
427
		if (false)
428
			return;
429
430
		AbstractProxyProvider pp = new WindowsProxyProvider();
431
432
		// loop here - while looping play with the internet options ;-)
433
		while (true) {
434
			proxiesFor(pp,"http://www.eclipse.org");
435
			proxiesFor(pp,"https://www.eclipse.org");
436
			proxiesFor(pp,"ftp://www.eclipse.org");
437
			proxiesFor(pp,"socks://www.eclipse.org");
438
		}
439
	}
440
	
441
	public void proxiesFor( AbstractProxyProvider pp,String url ) throws URISyntaxException{
442
		IProxyData[] pd = pp.select(new URI(url));
443
		System.out.println("Url: "+ url + ":");
444
		for (int i = 0; i < pd.length; i++) {
445
			System.out.println("\t"+pd[i]);
446
		} 
447
	}
301
}
448
}
(-)src/org/eclipse/core/internal/net/ProxyData.java (+19 lines)
Lines 74-79 Link Here
74
	public boolean isRequiresAuthentication() {
74
	public boolean isRequiresAuthentication() {
75
		return requiresAuthentication;
75
		return requiresAuthentication;
76
	}
76
	}
77
	
78
	public void setRequiresAuthentication(boolean requires) {
79
		requiresAuthentication = requires;
80
	}
77
81
78
	public void disable() {
82
	public void disable() {
79
		host = null;
83
		host = null;
Lines 83-86 Link Here
83
		requiresAuthentication = false;
87
		requiresAuthentication = false;
84
	}
88
	}
85
89
90
	public String toString() {
91
		StringBuffer stringBuffer = new StringBuffer();
92
		stringBuffer.append("host: "); //$NON-NLS-1$
93
		stringBuffer.append(host);
94
		stringBuffer.append(" port: "); //$NON-NLS-1$
95
		stringBuffer.append(port);
96
		stringBuffer.append(" user: "); //$NON-NLS-1$
97
		stringBuffer.append(user);
98
		stringBuffer.append(" password: "); //$NON-NLS-1$
99
		stringBuffer.append(password);
100
		stringBuffer.append(" reqAuth: "); //$NON-NLS-1$
101
		stringBuffer.append(requiresAuthentication);
102
		return stringBuffer.toString(); 
103
	}
104
86
}
105
}
(-)src/org/eclipse/core/internal/net/proxy/win32/winhttp/WinHttpCurrentUserIEProxyConfig.java (+73 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007, 2008 compeople AG and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 * 	compeople AG (Stefan Liebig) - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.core.internal.net.proxy.win32.winhttp;
12
13
import org.eclipse.core.internal.net.proxy.win32.util.StringUtils;
14
15
/**
16
 * Wrapper for Win32 WINHTTP_CURRENT_USER_IE_PROXY_CONFIG structure
17
 */
18
public class WinHttpCurrentUserIEProxyConfig {
19
20
	private boolean isAutoDetect;
21
	private String autoConfigUrl;
22
	private String proxy;
23
	private String proxyBypass;
24
25
	/**
26
	 * @return the autoConfigUrl
27
	 */
28
	public String getAutoConfigUrl() {
29
		return autoConfigUrl;
30
	}
31
32
	/**
33
	 * @return the isAutoDetect
34
	 */
35
	public boolean isAutoDetect() {
36
		return isAutoDetect;
37
	}
38
39
	/**
40
	 * @return the proxy
41
	 */
42
	public String getProxy() {
43
		return proxy;
44
	}
45
46
	/**
47
	 * @return the proxyBypass
48
	 */
49
	public String getProxyBypass() {
50
		return proxyBypass;
51
	}
52
53
	/**
54
	 * @see java.lang.Object#equals(java.lang.Object)
55
	 */
56
	public boolean equals( Object obj ) {
57
		if ( obj instanceof WinHttpCurrentUserIEProxyConfig ) {
58
			WinHttpCurrentUserIEProxyConfig that = (WinHttpCurrentUserIEProxyConfig) obj;
59
			return ( this.isAutoDetect == that.isAutoDetect ) && StringUtils.equals( this.autoConfigUrl, that.autoConfigUrl )
60
				&& StringUtils.equals( this.proxy, that.proxy ) && StringUtils.equals( this.proxyBypass, that.proxyBypass );
61
		}
62
63
		return false;
64
	}
65
66
	/**
67
	 * @see java.lang.Object#hashCode()
68
	 */
69
	public int hashCode() {
70
		return ( autoConfigUrl + proxy ).hashCode();
71
	}
72
73
}
(-)src/org/eclipse/core/internal/net/proxy/win32/util/StringUtils.java (+105 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007, 2008 compeople AG and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 * 	compeople AG (Stefan Liebig) - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.core.internal.net.proxy.win32.util;
12
13
/**
14
 * Collection of String/CharSequence utilities.
15
 */
16
public final class StringUtils {
17
18
	private StringUtils() {
19
		// Utility class
20
	}
21
22
	/**
23
	 * Tests ´simple´ emptiness. A character sequence is empty either if it is
24
	 * null or it has a zero length.
25
	 * 
26
	 * @param sequence
27
	 * @return true if null or zero length
28
	 */
29
	public static final boolean isEmpty(final CharSequence sequence) {
30
		return sequence == null || sequence.length() == 0;
31
	}
32
33
	/**
34
	 * Tests whether the char sequence has content. A character sequence is
35
	 * given either if it is not null and it has a none zero length.
36
	 * 
37
	 * @param sequence
38
	 * @return true if not null and not zero length
39
	 */
40
	public static final boolean isGiven(final CharSequence sequence) {
41
		return sequence != null && sequence.length() != 0;
42
	}
43
44
	/**
45
	 * Tests ´deep´ emptiness. A string is empty if it is either null or its
46
	 * trimmed version has a zero length.
47
	 * 
48
	 * @param string
49
	 * @return true if null or the trimmed string length is zero
50
	 */
51
	public static final boolean isDeepEmpty(final String string) {
52
		return string == null || string.trim().length() == 0;
53
	}
54
55
	/**
56
	 * Tests equality of the given strings.
57
	 * 
58
	 * @param sequence1
59
	 *            candidate 1, may be null
60
	 * @param sequence2
61
	 *            candidate 2, may be null
62
	 * @return true if both sequences are null or the sequences are equal
63
	 */
64
	public static final boolean equals(final CharSequence sequence1,
65
			final CharSequence sequence2) {
66
		if (sequence1 == sequence2) {
67
			return true;
68
		} else if (sequence1 == null || sequence2 == null) {
69
			return false;
70
		} else {
71
			return sequence1.equals(sequence2);
72
		}
73
	}
74
75
	/**
76
	 * Replace within <code>source</code> the occurrences of <code>from</code> with <code>to</code>.
77
	 * @param source
78
	 * @param from
79
	 * @param to
80
	 * @return the substituted string
81
	 */
82
	public static String replace(String source, String from, String to) {
83
		if (from.length() == 0 )
84
			return source;
85
		StringBuffer buffer = new StringBuffer();
86
		int current = 0;
87
		int pos = 0;
88
		while (pos != -1) {
89
			pos = source.indexOf(from, current);
90
			if (pos == -1) {
91
				buffer.append(source.substring(current));
92
			} else {
93
				buffer.append(source.substring(current, pos));
94
				buffer.append(to);
95
				current = pos + from.length();
96
			}
97
		}
98
		return buffer.toString();
99
	}
100
101
	// TODO convert to unit test
102
	public static void main(String[] args) {
103
		System.out.println(replace("xaaaaa", "aa", "")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
104
	}
105
}
(-)natives/unix/README.txt (+12 lines)
Added Link Here
1
The source for getting the native proxy info is in the top level unix directory.  If
2
there are more specific compile options, makefiles, etc, then they should be moved
3
to subdirectories, like in org.eclipse.core.filesystem.
4
5
The Debug directory actually has the make file; this is the makefile as generated by the CDT.
6
7
To build this, go into Debug and type "make", the library will be built in that same
8
directory.  Then you will need to copy the library to the correct org.eclipse.core.net.*
9
fragment.
10
11
There is no automatic build process for building the native code, instead the binary
12
shared libraries are checked in for each fragment, just like in org.eclipse.code.filesystem.
(-)src/org/eclipse/core/internal/net/WindowsProxyProvider.java (+28 lines)
Added Link Here
1
package org.eclipse.core.internal.net;
2
3
import java.net.URI;
4
import java.util.List;
5
6
import org.eclipse.core.internal.net.proxy.win32.winhttp.WinHttpProxySelector;
7
import org.eclipse.core.net.proxy.IProxyData;
8
9
10
public class WindowsProxyProvider extends AbstractProxyProvider {
11
12
	static {
13
		System.loadLibrary("jWinHttp"); //$NON-NLS-1$
14
	}
15
16
	private WinHttpProxySelector winHttpProxySelector;
17
18
	public WindowsProxyProvider() {
19
		Activator.logInfo("winProxyProvider initialized", null); //$NON-NLS-1$
20
		winHttpProxySelector = new WinHttpProxySelector();
21
	}
22
23
	protected IProxyData[] getProxyData(URI uri) {
24
		List proxies = winHttpProxySelector.select(uri);
25
		return (IProxyData[]) proxies.toArray(new IProxyData[proxies.size()]);
26
	}
27
28
}
(-)src/org/eclipse/core/internal/net/proxy/win32/winhttp/WinHttp.java (+83 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007, 2008 compeople AG and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 * 	compeople AG (Stefan Liebig) - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.core.internal.net.proxy.win32.winhttp;
12
13
14
/**
15
 * This is the Win32 WinHttp wrapper.
16
 * (Not complete, but offers what we currently need!)
17
 */
18
public final class WinHttp {
19
20
	/**
21
	 * WinHttpOpen prettifiers for optional proxy name parameter
22
	 */
23
	static final String NO_PROXY_NAME = null;
24
25
	/**
26
	 * WinHttpOpen prettifiers for optional proxy bypass parameter
27
	 */
28
	static final String NO_PROXY_BYPASS = null;
29
30
	/**
31
	 * WinHttpOpen - see Microsoft SDK Documentation 
32
	 * 
33
	 * @param userAgent
34
	 * @param accessType
35
	 * @param proxyName
36
	 * @param proxyBypass
37
	 * @param flags
38
	 * @return the handle
39
	 */
40
	public static native int open( String userAgent, int accessType, String proxyName, String proxyBypass, int flags );
41
42
	/**
43
	 * WinHttpCloseHandle - see Microsoft SDK Documentation
44
	 *  
45
	 * @param hInternet
46
	 * @return true on success
47
	 */
48
	public static native boolean closeHandle( int hInternet );
49
50
	/**
51
	 * WinHttpGetIEProxyConfigForCurrentUser - see Microsoft SDK Documentation
52
	 *  
53
	 * @param proxyConfig
54
	 * @return true on success
55
	 */
56
	public static native boolean getIEProxyConfigForCurrentUser( WinHttpCurrentUserIEProxyConfig proxyConfig );
57
58
	/**
59
	 * WinHttpGetProxyForUrl - see Microsoft SDK Documentation
60
	 *  
61
	 * @param hSession
62
	 * @param url
63
	 * @param autoProxyOptions
64
	 * @param proxyInfo
65
	 * @return true on success
66
	 */
67
	public static native boolean getProxyForUrl( int hSession, String url, WinHttpAutoProxyOptions autoProxyOptions, WinHttpProxyInfo proxyInfo );
68
69
	/**
70
	 * GetLastError - see Microsoft SDK Documentation
71
	 *  
72
	 * @return the last error code (win32)
73
	 */
74
	public static native int getLastError();
75
76
	/**
77
	 * GetLastErrorMessage - formats the last error
78
	 *  
79
	 * @return the readable last error code
80
	 */
81
	public static native String getLastErrorMessage();
82
83
}
(-)src/org/eclipse/core/internal/net/proxy/win32/winhttp/WinHttpProxySelector.java (+170 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007, 2008 compeople AG and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 * 	compeople AG (Stefan Liebig) - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.core.internal.net.proxy.win32.winhttp;
12
13
import java.net.URI;
14
import java.util.ArrayList;
15
import java.util.Collections;
16
import java.util.List;
17
18
import org.eclipse.core.internal.net.Activator;
19
import org.eclipse.core.internal.net.proxy.win32.util.ProxyBypass;
20
import org.eclipse.core.internal.net.proxy.win32.util.ProxySelectorPACUtils;
21
import org.eclipse.core.internal.net.proxy.win32.util.StringUtils;
22
23
24
/**
25
 * ProxySelector that gets its settings from the "internet options >> connection
26
 * settings"
27
 */
28
public class WinHttpProxySelector {
29
30
	private WinHttpCurrentUserIEProxyConfig proxyConfig = null;
31
	private WinHttpConfig winHttpConfig;
32
	private boolean pacFailed = false;
33
	private boolean wpadFailed = false;
34
	private List wpadProxies = null;
35
36
	private final static int ERROR_WINHTTP_AUTODETECTION_FAILED = 12180;
37
	private static final String MY_NAME = WinHttpProxySelector.class.getName();
38
39
	public List select(URI uri) {
40
		WinHttpCurrentUserIEProxyConfig newProxyConfig = new WinHttpCurrentUserIEProxyConfig();
41
		if (!WinHttp.getIEProxyConfigForCurrentUser(newProxyConfig)) {
42
			Activator
43
					.logError(
44
							"WinHttp.GetIEProxyConfigForCurrentUser failed with error '" + WinHttp.getLastErrorMessage() + "' #" + WinHttp.getLastError() + ".", null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
45
			return Collections.EMPTY_LIST;
46
		}
47
48
		// Let´s see if we are still up-to-date.
49
		boolean proxyConfigChanged = !newProxyConfig.equals(proxyConfig);
50
		if (proxyConfigChanged) {
51
			proxyConfig = newProxyConfig;
52
			// Retry pac and wpad
53
			pacFailed = false;
54
			wpadFailed = false;
55
			wpadProxies = Collections.EMPTY_LIST;
56
		}
57
58
		List proxies = new ArrayList();
59
60
		// Explicit proxies defined?
61
		if (StringUtils.isGiven(proxyConfig.getProxy())) {
62
			// Yes, let´s see if we are still up-to-date or not yet initialized.
63
			if (proxyConfigChanged || winHttpConfig == null) {
64
				winHttpConfig = new WinHttpConfig(proxyConfig);
65
			}
66
67
			if (!winHttpConfig.bypassProxyFor(uri)) {
68
				if (winHttpConfig.useProtocolSpecificProxies()) {
69
					proxies.addAll(winHttpConfig
70
							.getProtocolSpecificProxies(uri));
71
				} else {
72
					proxies.addAll(winHttpConfig.getUniversalProxies());
73
				}
74
			}
75
		}
76
77
		boolean isPac = proxyConfig.getAutoConfigUrl() != null;
78
		boolean isWpad = proxyConfig.isAutoDetect();
79
80
		if (isPac || isWpad) {
81
			// Create the WinHTTP session.
82
			int hHttpSession = WinHttp.open(MY_NAME,
83
					WinHttpProxyInfo.WINHTTP_ACCESS_TYPE_NO_PROXY,
84
					WinHttp.NO_PROXY_NAME, WinHttp.NO_PROXY_BYPASS, 0);
85
			if (hHttpSession == 0) {
86
				Activator
87
						.logError(
88
								"WinHttp.Open failed with error'" + WinHttp.getLastErrorMessage() + "' #" + WinHttp.getLastError() + ".", null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
89
			} else {
90
				try {
91
					// PAC file?
92
					if (isPac && !pacFailed) {
93
						proxies.addAll(pacSelect(hHttpSession, uri));
94
					}
95
96
					// WPAD?
97
					if (isWpad && !wpadFailed) {
98
						if (wpadProxies == null || wpadProxies.size() == 0) {
99
							wpadProxies = wpadSelect(hHttpSession, uri);
100
						}
101
						proxies.addAll(wpadProxies);
102
					}
103
				} finally {
104
					WinHttp.closeHandle(hHttpSession);
105
				}
106
			}
107
		}
108
109
		// resort(proxies);
110
		return proxies;
111
	}
112
113
	protected List pacSelect(int hHttpSession, URI uri) {
114
		// Set up the autoproxy call.
115
		WinHttpAutoProxyOptions autoProxyOptions = new WinHttpAutoProxyOptions();
116
		autoProxyOptions
117
				.setFlags(WinHttpAutoProxyOptions.WINHTTP_AUTOPROXY_CONFIG_URL);
118
		autoProxyOptions.setAutoConfigUrl(proxyConfig.getAutoConfigUrl());
119
		autoProxyOptions.setAutoLogonIfChallenged(true);
120
		WinHttpProxyInfo proxyInfo = new WinHttpProxyInfo();
121
122
		boolean ok = WinHttp.getProxyForUrl(hHttpSession, uri.toString(),
123
				autoProxyOptions, proxyInfo);
124
		if (!ok) {
125
			pacFailed = true;
126
			Activator
127
					.logError(
128
							"WinHttp.GetProxyForUrl for pac failed with error '" + WinHttp.getLastErrorMessage() + "' #" + WinHttp.getLastError() + ".", null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
129
			return Collections.EMPTY_LIST;
130
		}
131
		ProxyBypass proxyBypass = new ProxyBypass(proxyInfo.getProxyBypass());
132
		if (proxyBypass.bypassProxyFor(uri))
133
			return Collections.EMPTY_LIST;
134
		return ProxySelectorPACUtils.getProxies(proxyInfo.getProxy());
135
	}
136
137
	protected List wpadSelect(int hHttpSession, URI uri) {
138
		// Set up the autoproxy call.
139
		WinHttpAutoProxyOptions autoProxyOptions = new WinHttpAutoProxyOptions();
140
		autoProxyOptions
141
				.setFlags(WinHttpAutoProxyOptions.WINHTTP_AUTOPROXY_AUTO_DETECT);
142
		autoProxyOptions
143
				.setAutoDetectFlags(WinHttpAutoProxyOptions.WINHTTP_AUTO_DETECT_TYPE_DHCP
144
						| WinHttpAutoProxyOptions.WINHTTP_AUTO_DETECT_TYPE_DNS_A);
145
		autoProxyOptions.setAutoLogonIfChallenged(true);
146
		WinHttpProxyInfo proxyInfo = new WinHttpProxyInfo();
147
148
		boolean ok = WinHttp.getProxyForUrl(hHttpSession, uri.toString(),
149
				autoProxyOptions, proxyInfo);
150
		if (!ok) {
151
			wpadFailed = WinHttp.getLastError() == ERROR_WINHTTP_AUTODETECTION_FAILED;
152
			Activator
153
					.logError(
154
							"WinHttp.GetProxyForUrl for wpad failed with error '" + WinHttp.getLastErrorMessage() + "' #" + WinHttp.getLastError() + ".", null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
155
			return Collections.EMPTY_LIST;
156
		}
157
		ProxyBypass proxyBypass = new ProxyBypass(proxyInfo.getProxyBypass());
158
		if (proxyBypass.bypassProxyFor(uri))
159
			return Collections.EMPTY_LIST;
160
		return ProxySelectorPACUtils.getProxies(proxyInfo.getProxy());
161
	}
162
163
	/**
164
	 * @see java.lang.Object#toString()
165
	 */
166
	public String toString() {
167
		return MY_NAME;
168
	}
169
170
}
(-)src/org/eclipse/core/internal/net/proxy/win32/util/ProxySelectorUtils.java (+157 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007, 2008 compeople AG and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 * 	compeople AG (Stefan Liebig) - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.core.internal.net.proxy.win32.util;
12
13
import java.net.URI;
14
import java.util.ArrayList;
15
import java.util.List;
16
import java.util.Map;
17
import java.util.StringTokenizer;
18
19
import org.eclipse.core.internal.net.Activator;
20
import org.eclipse.core.internal.net.ProxyData;
21
import org.eclipse.core.net.proxy.IProxyData;
22
23
/**
24
 * 
25
 */
26
public final class ProxySelectorUtils {
27
28
	static private int PROXY_DEFAULT_PORT = 80;
29
	static private int HTTPPROXY_DEFAULT_PORT = PROXY_DEFAULT_PORT;
30
	static private int HTTPSPROXY_DEFAULT_PORT = 443;
31
	static private int SOCKSPROXY_DEFAULT_PORT = 1080;
32
33
//	private static final List emptyProxyList = Collections.EMPTY_LIST;
34
35
	private ProxySelectorUtils() {
36
		super();
37
	}
38
39
	/**
40
	 * Scan the proxy list string and fill this information in the correct list
41
	 * or map. <br>
42
	 * The proxy list contains one or more of the following strings separated by
43
	 * semicolons:<br>
44
	 * <code><pre>
45
	 * ([&lt;scheme&gt;=][&lt;scheme&gt; &quot;://&quot; ]&lt;server&gt;[ &quot;:&quot; &lt;port&gt;])
46
	 * </pre></code>
47
	 * 
48
	 * @param proxyList the proxy list as a string
49
	 * @param universalProxies the list of proxies receiving the universal proxies
50
	 * @param protocolSpecificProxies a map from http schemes to the list of scheme specific proxies
51
	 */
52
	public static void fillProxyLists(String proxyList, List universalProxies,
53
			Map protocolSpecificProxies) {
54
		StringTokenizer tokenizer = new StringTokenizer(proxyList, ";"); //$NON-NLS-1$
55
		while (tokenizer.hasMoreTokens()) {
56
			createProxy(tokenizer.nextToken(), universalProxies,
57
					protocolSpecificProxies);
58
		}
59
	}
60
61
	private static void createProxy(final String proxyDefinition,
62
			List universalProxies, Map protocolSpecificProxies) {
63
		String protocol = null;
64
		String host = null;
65
		int port = 0;
66
67
		int urlStart = 0;
68
		// if there is no '=' character within the proxy definition we have a
69
		// proxy
70
		// definition that serves all protocols. In this case we MUST ignore the
71
		// protocol,
72
		// otherwise the protocol MUST be used to determine the specific proxy
73
		// settings
74
		if (proxyDefinition.indexOf("=") != -1) { //$NON-NLS-1$
75
			protocol = proxyDefinition.substring(0, proxyDefinition
76
					.indexOf("=")); //$NON-NLS-1$
77
			urlStart = proxyDefinition.indexOf("=") + 1; //$NON-NLS-1$
78
		}
79
80
		try {
81
			// The scheme of the uri is irrelevant. We add the http://
82
			// scheme to enable class URI to parse the stuff
83
			String augmentedURI = proxyDefinition.substring(urlStart);
84
			if (augmentedURI.indexOf("://") == -1) { //$NON-NLS-1$
85
				augmentedURI = "http://" + augmentedURI; //$NON-NLS-1$
86
			}
87
			URI uri = new URI(augmentedURI);
88
			host = uri.getHost();
89
			port = uri.getPort() > 0 ? uri.getPort()
90
					: getProxyDefaultPort(protocol);
91
		} catch (Exception ex) {
92
			Activator
93
					.logError(
94
							"not a valid proxy definition: '" + proxyDefinition + "'.", ex); //$NON-NLS-1$ //$NON-NLS-2$
95
			return;
96
		}
97
98
		if (host == null) {
99
			Activator
100
					.logError(
101
							"not a valid proxy definition: '" + proxyDefinition + "'.", null); //$NON-NLS-1$ //$NON-NLS-2$
102
			return;
103
		}
104
105
		if (protocol == null) {
106
			universalProxies.add(createProxy(IProxyData.HTTP_PROXY_TYPE, host,
107
					port));
108
		} else {
109
			addProtocolSpecificProxy(protocolSpecificProxies, protocol,
110
					createProxy(resolveProxyType(protocol), host, port));
111
		}
112
	}
113
114
	private static int getProxyDefaultPort(String protocol) {
115
		if (protocol == null)
116
			return PROXY_DEFAULT_PORT;
117
		if ("http".equalsIgnoreCase(protocol)) //$NON-NLS-1$
118
			return HTTPPROXY_DEFAULT_PORT;
119
		if ("https".equalsIgnoreCase(protocol)) //$NON-NLS-1$
120
			return HTTPSPROXY_DEFAULT_PORT;
121
		if ("socks".equalsIgnoreCase(protocol)) //$NON-NLS-1$
122
			return SOCKSPROXY_DEFAULT_PORT;
123
		if ("socket".equalsIgnoreCase(protocol)) //$NON-NLS-1$
124
			return SOCKSPROXY_DEFAULT_PORT;
125
126
		return PROXY_DEFAULT_PORT;
127
	}
128
129
	private static void addProtocolSpecificProxy(Map protocolSpecificProxies,
130
			String protocol, IProxyData proxy) {
131
		List list = (List) protocolSpecificProxies.get(protocol);
132
		if (list == null) {
133
			list = new ArrayList();
134
			protocolSpecificProxies.put(protocol, list);
135
		}
136
137
		list.add(proxy);
138
	}
139
140
	private static String resolveProxyType(String protocol) {
141
		// TODO: return HTTP proxy for well-known high level protocols only?
142
		if (protocol.equalsIgnoreCase("socks") || protocol.equalsIgnoreCase("socket")) //$NON-NLS-1$ //$NON-NLS-2$
143
			return IProxyData.SOCKS_PROXY_TYPE;
144
		if (protocol.equalsIgnoreCase("https")) //$NON-NLS-1$ 
145
			return IProxyData.HTTPS_PROXY_TYPE;
146
		return IProxyData.HTTP_PROXY_TYPE;
147
	}
148
149
	private static IProxyData createProxy(String scheme, String host, int port) {
150
		String type = resolveProxyType(scheme);
151
		IProxyData proxy = new ProxyData(type);
152
		proxy.setHost(host);
153
		proxy.setPort(port);
154
		return proxy;
155
	}
156
157
}
(-)natives/unix/Debug/subdir.mk (+24 lines)
Added Link Here
1
################################################################################
2
# Automatically-generated file. Do not edit!
3
################################################################################
4
5
# Add inputs and outputs from these tool invocations to the build variables 
6
C_SRCS += \
7
../getsystemproxy.c 
8
9
OBJS += \
10
./getsystemproxy.o 
11
12
C_DEPS += \
13
./getsystemproxy.d 
14
15
16
# Each subdirectory must supply rules for building sources it contributes
17
%.o: ../%.c
18
	@echo 'Building file: $<'
19
	@echo 'Invoking: GCC C Compiler'
20
	gcc -I/usr/include/gconf/2 -I/usr/include/orbit-2.0 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include/ -O0 -fPIC -g3 -Wall -c -fmessage-length=0 -m32 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o"$@" "$<"
21
	@echo 'Finished building: $<'
22
	@echo ' '
23
24
(-)natives/unix/Debug/sources.mk (+17 lines)
Added Link Here
1
################################################################################
2
# Automatically-generated file. Do not edit!
3
################################################################################
4
5
O_SRCS := 
6
C_SRCS := 
7
S_SRCS := 
8
OBJ_SRCS := 
9
ASM_SRCS := 
10
OBJS := 
11
C_DEPS := 
12
LIBRARIES := 
13
14
# Every subdirectory with source files must be described here
15
SUBDIRS := \
16
. \
17
(-)src/org/eclipse/core/internal/net/proxy/win32/util/ProxyBypass.java (+86 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007, 2008 compeople AG and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 * 	compeople AG (Stefan Liebig) - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.core.internal.net.proxy.win32.util;
12
13
import java.net.URI;
14
import java.util.regex.Pattern;
15
16
/**
17
 * Encapsulates the proxy bypass list.
18
 * 
19
 * For more information see
20
 * {@link "http://msdn2.microsoft.com/en-us/library/aa383912(VS.85).aspx"}
21
 */
22
public class ProxyBypass {
23
24
	private final String proxyBypass;
25
	private final Pattern proxyBypassPattern;
26
27
	private final static String BYPASS_LOCAL_ADDESSES_TOKEN = "<local>"; //$NON-NLS-1$
28
29
	/**
30
	 * Create a ProxyBypass instance from the proxy bypass list string.
31
	 * 
32
	 * @param proxyBypass
33
	 */
34
	public ProxyBypass(String proxyBypass) {
35
		this.proxyBypass = proxyBypass != null ? proxyBypass : ""; //$NON-NLS-1$
36
37
		if (proxyBypass != null) {
38
			String regExp = StringUtils.replace(proxyBypass, ";", "|"); //$NON-NLS-1$ //$NON-NLS-2$
39
			regExp = StringUtils.replace(regExp, ".", "\\."); //$NON-NLS-1$ //$NON-NLS-2$
40
			regExp = StringUtils.replace(regExp, "*", ".*"); //$NON-NLS-1$ //$NON-NLS-2$
41
			this.proxyBypassPattern = Pattern.compile(regExp);
42
		} else {
43
			this.proxyBypassPattern = Pattern.compile(""); //$NON-NLS-1$
44
		}
45
	}
46
47
	/**
48
	 * Check whether the given uri should bypass the proxy.
49
	 * 
50
	 * @param uri
51
	 * @return true if the the uri should bypass the proxy; otherwise false
52
	 */
53
	public boolean bypassProxyFor(URI uri) {
54
		final String host = uri.getHost();
55
		if (host == null)
56
			return false;
57
		return (isLocal(host) && isBypassLocalAddresses(proxyBypass))
58
				|| isInBypassList(host);
59
	}
60
61
	/**
62
	 * @param proxyBypass
63
	 * @param uri
64
	 * @return
65
	 */
66
	private boolean isInBypassList(String host) {
67
		return proxyBypassPattern.matcher(host).matches();
68
	}
69
70
	/**
71
	 * @param uri
72
	 * @return
73
	 */
74
	private static boolean isLocal(String host) {
75
		return host.indexOf(".") == -1; //$NON-NLS-1$
76
	}
77
78
	/**
79
	 * @param addressListString
80
	 * @return
81
	 */
82
	private static boolean isBypassLocalAddresses(String proxyBypass) {
83
		return proxyBypass.indexOf(BYPASS_LOCAL_ADDESSES_TOKEN) != -1;
84
	}
85
86
}
(-)natives/unix/Debug/makefile (+43 lines)
Added Link Here
1
################################################################################
2
# Automatically-generated file. Do not edit!
3
################################################################################
4
5
-include ../makefile.init
6
7
RM := rm -rf
8
9
# All of the sources participating in the build are defined here
10
-include sources.mk
11
-include subdir.mk
12
-include objects.mk
13
14
ifneq ($(MAKECMDGOALS),clean)
15
ifneq ($(strip $(C_DEPS)),)
16
-include $(C_DEPS)
17
endif
18
endif
19
20
-include ../makefile.defs
21
22
# Add inputs and outputs from these tool invocations to the build variables 
23
24
# All Target
25
all: libproxysupport.so
26
27
# Tool invocations
28
libproxysupport.so: $(OBJS) $(USER_OBJS)
29
	@echo 'Building target: $@'
30
	@echo 'Invoking: GCC C Linker'
31
	gcc -m32 -shared -o"libproxysupport.so" $(OBJS) $(USER_OBJS) $(LIBS)
32
	@echo 'Finished building target: $@'
33
	@echo ' '
34
35
# Other Targets
36
clean:
37
	-$(RM) $(OBJS)$(C_DEPS)$(LIBRARIES) libproxysupport.so
38
	-@echo ' '
39
40
.PHONY: all clean dependents
41
.SECONDARY:
42
43
-include ../makefile.targets
(-)src/org/eclipse/core/internal/net/proxy/win32/winhttp/WinHttpProxyInfo.java (+48 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007, 2008 compeople AG and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 * 	compeople AG (Stefan Liebig) - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.core.internal.net.proxy.win32.winhttp;
12
13
/**
14
 * Wrapper for Win32 WINHTTP_PROXY_INFO Structure
15
 */
16
public class WinHttpProxyInfo {
17
18
	// WinHttpOpen dwAccessType values (also for WINHTTP_PROXY_INFO::dwAccessType)
19
	public static final int WINHTTP_ACCESS_TYPE_DEFAULT_PROXY = 0;
20
	public static final int WINHTTP_ACCESS_TYPE_NO_PROXY = 1;
21
	public static final int WINHTTP_ACCESS_TYPE_NAMED_PROXY = 3;
22
23
	private int accessType;
24
	private String proxy;
25
	private String proxyBypass;
26
27
	/**
28
	 * @return the accessType
29
	 */
30
	public int getAccessType() {
31
		return accessType;
32
	}
33
34
	/**
35
	 * @return the proxy
36
	 */
37
	public String getProxy() {
38
		return proxy;
39
	}
40
41
	/**
42
	 * @return the proxyBypass
43
	 */
44
	public String getProxyBypass() {
45
		return proxyBypass;
46
	}
47
48
}
(-)src/org/eclipse/core/internal/net/proxy/win32/winhttp/WinHttpConfig.java (+60 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007, 2008 compeople AG and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 * 	compeople AG (Stefan Liebig) - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.core.internal.net.proxy.win32.winhttp;
12
13
import java.net.URI;
14
import java.util.ArrayList;
15
import java.util.HashMap;
16
import java.util.List;
17
import java.util.Map;
18
19
import org.eclipse.core.internal.net.proxy.win32.util.ProxyBypass;
20
import org.eclipse.core.internal.net.proxy.win32.util.ProxySelectorUtils;
21
22
23
/**
24
 * WinHttpConfig unwraps the information from WinHttpCurrentIEProxyConfig
25
 */
26
public class WinHttpConfig {
27
28
	private List universalProxies = new ArrayList();
29
	private Map protocolSpecificProxies = new HashMap();
30
	private ProxyBypass proxyBypass;
31
32
	/**
33
	 * @param proxyConfig
34
	 */
35
	public WinHttpConfig(WinHttpCurrentUserIEProxyConfig proxyConfig) {
36
		ProxySelectorUtils.fillProxyLists(proxyConfig.getProxy(),
37
				universalProxies, protocolSpecificProxies);
38
		proxyBypass = new ProxyBypass(proxyConfig.getProxyBypass());
39
	}
40
41
	public boolean useUniversalProxy() {
42
		return !universalProxies.isEmpty();
43
	}
44
45
	public boolean useProtocolSpecificProxies() {
46
		return !protocolSpecificProxies.isEmpty();
47
	}
48
49
	public List getProtocolSpecificProxies(URI uri) {
50
		return (List)protocolSpecificProxies.get(uri.getScheme());
51
	}
52
53
	public List getUniversalProxies() {
54
		return universalProxies;
55
	}
56
57
	public boolean bypassProxyFor(URI uri) {
58
		return proxyBypass.bypassProxyFor(uri);
59
	}
60
}
(-)natives/unix/getsystemproxy.c (+237 lines)
Added Link Here
1
/*
2
 * Copyright 2008 Oakland Software Incorporated and others
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors:
9
 *     Oakland Software Incorporated - initial API and implementation
10
 */
11
12
#include <jni.h>
13
14
#include <glib.h>
15
#include <glib/gslist.h>
16
#include <gconf/gconf-value.h>
17
#include <gconf/gconf-client.h>
18
19
#ifdef __linux__
20
#include <string.h>
21
#else
22
#include <strings.h>
23
#endif
24
25
static GConfClient *client= NULL;
26
27
static jclass proxyInfoClass;
28
static jclass stringClass;
29
static jmethodID proxyInfoConstructor;
30
static jmethodID toString;
31
32
static jmethodID hostMethod;
33
static jmethodID portMethod;
34
static jmethodID requiresAuthenticationMethod;
35
static jmethodID userMethod;
36
static jmethodID passwordMethod;
37
38
#define CHECK_NULL(X) { if ((X) == NULL) fprintf (stderr,"JNI error at line %d\n", __LINE__); } 
39
40
static void gconfInit(JNIEnv *env) {
41
	client = gconf_client_get_default();
42
	jclass cls= NULL;
43
	CHECK_NULL(cls = (*env)->FindClass(env, "org/eclipse/core/internal/net/ProxyData"));
44
	proxyInfoClass = (*env)->NewGlobalRef(env, cls);
45
46
	CHECK_NULL(cls = (*env)->FindClass(env, "java/lang/String"));
47
	stringClass = (*env)->NewGlobalRef(env, cls);
48
49
	CHECK_NULL(proxyInfoConstructor = (*env)->GetMethodID(env, proxyInfoClass, "<init>", "(Ljava/lang/String;)V"));
50
51
	CHECK_NULL(toString = (*env)->GetMethodID(env, proxyInfoClass, "toString", "()Ljava/lang/String;"));
52
53
	CHECK_NULL(hostMethod = (*env)->GetMethodID(env, proxyInfoClass, "setHost",
54
					"(Ljava/lang/String;)V"));
55
	CHECK_NULL(portMethod = (*env)->GetMethodID(env, proxyInfoClass, "setPort",
56
					"(I)V"));
57
	CHECK_NULL(requiresAuthenticationMethod= (*env)->GetMethodID(env, proxyInfoClass, "setRequiresAuthentication",
58
					"(Z)V"));
59
	CHECK_NULL(userMethod = (*env)->GetMethodID(env, proxyInfoClass, "setUserid",
60
					"(Ljava/lang/String;)V"));
61
	CHECK_NULL(passwordMethod = (*env)->GetMethodID(env, proxyInfoClass, "setPassword",
62
					"(Ljava/lang/String;)V"));
63
}
64
65
/*
66
 * Class:     org_eclipse_core_internal_net_UnixProxyProvider
67
 * Method:    getGConfProxyInfo
68
 * Signature: ([Ljava/lang/String);
69
 */
70
JNIEXPORT jobject JNICALL Java_org_eclipse_core_internal_net_UnixProxyProvider_getGConfProxyInfo(
71
		JNIEnv *env, jclass clazz, jstring protocol) {
72
73
	jboolean isCopy;
74
	const char *cprotocol;
75
76
	jobject proxyInfo= NULL;
77
78
	if (client == NULL) {
79
		gconfInit(env);
80
	}
81
82
	CHECK_NULL(proxyInfo = (*env)->NewObject(env, proxyInfoClass, proxyInfoConstructor, protocol));
83
84
	cprotocol = (*env)->GetStringUTFChars(env, protocol, &isCopy);
85
	if (cprotocol == NULL)
86
		return NULL;
87
88
	//printf("cprotocol: %s\n", cprotocol);
89
90
	if (strcasecmp(cprotocol, "http") == 0) {
91
		gboolean useProxy = gconf_client_get_bool(client,
92
				"/system/http_proxy/use_http_proxy", NULL);
93
		if (!useProxy) {
94
			proxyInfo = NULL;
95
			goto exit;
96
		}
97
98
		gchar *host = gconf_client_get_string(client,
99
				"/system/http_proxy/host", NULL);
100
		jobject jhost = (*env)->NewStringUTF(env, host);
101
		(*env)->CallVoidMethod(env, proxyInfo, hostMethod, jhost);
102
103
		gint port = gconf_client_get_int(client, "/system/http_proxy/port",
104
				NULL);
105
		(*env)->CallVoidMethod(env, proxyInfo, portMethod, port);
106
107
		gboolean reqAuth = gconf_client_get_bool(client,
108
				"/system/http_proxy/use_authentication", NULL);
109
		(*env)->CallVoidMethod(env, proxyInfo,
110
				requiresAuthenticationMethod, reqAuth);
111
		if (reqAuth) {
112
113
			gchar *user = gconf_client_get_string(client,
114
					"/system/http_proxy/authentication_user", NULL);
115
			jobject juser = (*env)->NewStringUTF(env, user);
116
			(*env)->CallVoidMethod(env, proxyInfo, userMethod, juser);
117
118
			gchar *password = gconf_client_get_string(client,
119
					"/system/http_proxy/authentication_password", NULL);
120
			jobject jpassword = (*env)->NewStringUTF(env, password);
121
			(*env)->CallVoidMethod(env, proxyInfo, passwordMethod,
122
					jpassword);
123
		}
124
		goto exit;
125
	}
126
127
	// Everything else applies only if the system proxy mode is manual
128
	gchar *mode = gconf_client_get_string(client, "/system/proxy/mode", NULL);
129
	if (strcasecmp(mode, "manual") != 0) {
130
		proxyInfo = NULL;
131
		goto exit;
132
	}
133
134
	char selector[100];
135
136
	if (strcasecmp(cprotocol, "https") == 0) {
137
		strcpy(selector, "/system/proxy/secure_");
138
	} else if (strcasecmp(cprotocol, "socks") == 0) {
139
		strcpy(selector, "/system/proxy/socks_");
140
	} else if (strcasecmp(cprotocol, "ftp") == 0) {
141
		strcpy(selector, "/system/proxy/ftp_");
142
	} else {
143
		proxyInfo = NULL;
144
		goto exit;
145
	}
146
147
	char useSelector[100];
148
	strcpy(useSelector, selector);
149
150
	gchar *host = gconf_client_get_string(client, strcat(useSelector, "host"),
151
			NULL);
152
	jobject jhost = (*env)->NewStringUTF(env, host);
153
	(*env)->CallVoidMethod(env, proxyInfo, hostMethod, jhost);
154
155
	strcpy(useSelector, selector);
156
	gint port = gconf_client_get_int(client, strcat(useSelector, "port"), NULL);
157
	(*env)->CallVoidMethod(env, proxyInfo, portMethod, port);
158
159
	exit: if (isCopy == JNI_TRUE)
160
		(*env)->ReleaseStringUTFChars(env, protocol, cprotocol);
161
	return proxyInfo;
162
}
163
164
typedef struct {
165
	jobjectArray npHostArray;
166
	JNIEnv *env;
167
	int index;
168
} ListProcContext;
169
170
// user_data is the ListProcContext
171
void listProc(gpointer data, gpointer user_data) {
172
	ListProcContext *lpc = user_data;
173
	jobject jnpHost = (*lpc->env)->NewStringUTF(lpc->env, (char *)data);
174
	(*lpc->env)->SetObjectArrayElement(lpc->env, lpc->npHostArray,
175
			lpc->index++, jnpHost);
176
}
177
178
/*
179
 * Class:     org_eclipse_core_internal_net_UnixProxyProvider
180
 * Method:    getGConfNonProxyHosts
181
 * Signature: ()[Ljava/lang/String;
182
 */
183
JNIEXPORT jobjectArray JNICALL Java_org_eclipse_core_internal_net_UnixProxyProvider_getGConfNonProxyHosts(
184
		JNIEnv *env, jclass clazz) {
185
186
	if (client == NULL) {
187
		gconfInit(env);
188
	}
189
190
	GSList *npHosts;
191
	int size;
192
193
	npHosts = gconf_client_get_list(client, "/system/http_proxy/ignore_hosts",
194
			GCONF_VALUE_STRING, NULL);
195
	size = g_slist_length(npHosts);
196
197
	// TODO - I'm not sure this is really valid, it's from the JVM implementation
198
	// of ProxySelector
199
	if (size == 0) {
200
		npHosts = gconf_client_get_list(client, "/system/proxy/no_proxy_for",
201
				GCONF_VALUE_STRING, NULL);
202
	}
203
	size = g_slist_length(npHosts);
204
205
	jobjectArray ret = (*env)->NewObjectArray(env, size, stringClass, NULL);
206
207
	ListProcContext lpc;
208
	lpc.env = env;
209
	lpc.npHostArray = ret;
210
	lpc.index = 0;
211
212
	g_slist_foreach(npHosts, listProc, &lpc);
213
	return ret;
214
}
215
216
/*
217
 * Class:     org_eclipse_core_internal_net_UnixProxyProvider
218
 * Method:    getKdeProxyInfo
219
 * Signature: ([Ljava/lang/String);
220
 */
221
JNIEXPORT jobject JNICALL Java_org_eclipse_core_internal_net_UnixProxyProvider_getKdeProxyInfo(
222
		JNIEnv *env, jclass clazz, jstring protocol) {
223
	//printf("getKdeProxyInfo - not implemented\n");
224
	return NULL;
225
}
226
227
/*
228
 * Class:     org_eclipse_core_internal_net_UnixProxyProvider
229
 * Method:    getKdeNonProxyHosts
230
 * Signature: ()Ljava/lang/String;
231
 */
232
JNIEXPORT jobject JNICALL Java_org_eclipse_core_internal_net_UnixProxyProvider_getKdeNonProxyHosts(
233
		JNIEnv *env, jclass clazz) {
234
	//printf("getKdeNonProxyHosts - not implemented\n");
235
	return NULL;
236
}
237
(-)src/org/eclipse/core/internal/net/UnixProxyProvider.java (+80 lines)
Added Link Here
1
package org.eclipse.core.internal.net;
2
3
import java.net.URI;
4
import java.net.URISyntaxException;
5
6
import org.eclipse.core.net.proxy.IProxyData;
7
8
public class UnixProxyProvider extends AbstractProxyProvider {
9
10
	static {
11
		System.loadLibrary("proxysupport"); //$NON-NLS-1$
12
	}
13
14
	public UnixProxyProvider() {
15
			Activator.logInfo("linuxProxyProvider initialized", null); //$NON-NLS-1$
16
	}
17
18
	public IProxyData[] getProxyData(URI uri) {
19
		String protocol = uri.getScheme();
20
21
		ProxyData pd = getSystemProxyInfo(protocol);
22
23
		if (pd != null) {
24
			IProxyData[] pds = new IProxyData[1];
25
			pds[0] = pd;
26
			return pds;
27
		}
28
29
		return new IProxyData[0];
30
	}
31
32
	protected String[] getNonProxiedHosts() {
33
		String[] npHosts = getGConfNonProxyHosts();
34
		if (npHosts != null && npHosts.length > 0)
35
			return npHosts;
36
		return getKdeNonProxyHosts();
37
	}
38
39
	// Returns null if something wrong or there is no proxy for the protocol
40
	protected ProxyData getSystemProxyInfo(String protocol) {
41
		ProxyData pd;
42
43
		// First try the environment variable which is a URL
44
		String sysHttp = System.getenv(protocol.toLowerCase() + "_proxy"); //$NON-NLS-1$
45
		if (sysHttp != null) {
46
			URI uri = null;
47
			try {
48
				uri = new URI(sysHttp);
49
			} catch (URISyntaxException e) {
50
				return null;
51
			}
52
			
53
			pd = new ProxyData(protocol);
54
			pd.setHost(uri.getHost());
55
			pd.setPort(uri.getPort());
56
			return pd;
57
		}
58
59
		// Then ask Gnome
60
		pd = getGConfProxyInfo(protocol);
61
		if (pd != null)
62
			return pd;
63
64
		// Then ask KDE
65
		pd = getKdeProxyInfo(protocol);
66
		if (pd != null)
67
			return pd;
68
69
		return null;
70
	}
71
72
	protected static native ProxyData getGConfProxyInfo(String protocol);
73
74
	protected static native String[] getGConfNonProxyHosts();
75
76
	protected static native ProxyData getKdeProxyInfo(String protocol);
77
78
	protected static native String[] getKdeNonProxyHosts();
79
80
}
(-)src/org/eclipse/core/internal/net/proxy/win32/util/ProxySelectorPACUtils.java (+136 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007, 2008 compeople AG and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 * 	compeople AG (Stefan Liebig) - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.core.internal.net.proxy.win32.util;
12
13
import java.util.ArrayList;
14
import java.util.Collections;
15
import java.util.HashMap;
16
import java.util.Iterator;
17
import java.util.List;
18
import java.util.Map;
19
import java.util.StringTokenizer;
20
21
import org.eclipse.core.internal.net.ProxyData;
22
import org.eclipse.core.net.proxy.IProxyData;
23
24
/**
25
 * 
26
 */
27
public final class ProxySelectorPACUtils {
28
29
30
	private static final Map PROXY_TYPE_MAP;
31
32
	/*
33
	 * @see "http://wp.netscape.com/eng/mozilla/2.0/relnotes/demo/proxy-live.html"
34
	 */
35
	private static final String PAC_PROXY_TYPE_DIRECT = "DIRECT"; //$NON-NLS-1$
36
	private static final String PAC_PROXY_TYPE_PROXY = "PROXY"; //$NON-NLS-1$
37
	private static final String PAC_PROXY_TYPE_SOCKS = "SOCKS"; //$NON-NLS-1$
38
39
	private static final String NO_PROXY = "DIRECT"; //$NON-NLS-1$
40
	
41
	static {
42
		// mapping of pacProgram proxy type names to java proxy types:
43
		final Map temp = new HashMap();
44
		temp.put(PAC_PROXY_TYPE_DIRECT, NO_PROXY);
45
		temp.put(PAC_PROXY_TYPE_PROXY, IProxyData.HTTP_PROXY_TYPE);
46
		temp.put(PAC_PROXY_TYPE_SOCKS, IProxyData.SOCKS_PROXY_TYPE);
47
		PROXY_TYPE_MAP = Collections.unmodifiableMap(temp);
48
	}
49
50
	private ProxySelectorPACUtils() {
51
		// utility
52
	}
53
54
	/**
55
	 * @param pacFindProxyForUrlResult
56
	 * @return a list of IProxyData objects
57
	 */
58
	public static List getProxies(String pacFindProxyForUrlResult) {
59
		if (StringUtils.isDeepEmpty(pacFindProxyForUrlResult)) {
60
			return Collections.EMPTY_LIST;
61
		}
62
63
		// final List<Proxy> result = new ArrayList<Proxy>();
64
		final List result = new ArrayList();
65
		final StringTokenizer scanner = new StringTokenizer(
66
				pacFindProxyForUrlResult, ";"); //$NON-NLS-1$
67
		while (scanner.hasMoreTokens()) {
68
			final String pacProxy = scanner.nextToken().trim();
69
			final IProxyData proxy = getProxy(pacProxy);
70
			if (proxy != null) {
71
				result.add(proxy);
72
			}
73
		}
74
75
		return result;
76
	}
77
78
	private static IProxyData getProxy(String pacProxy) {
79
80
		if (StringUtils.isEmpty(pacProxy)) {
81
			return null;
82
		}
83
84
		if (!startsWithProxyType(pacProxy)) {
85
			// Assume "PROXY" type!
86
			pacProxy = "PROXY " + pacProxy; //$NON-NLS-1$
87
		}
88
		StringTokenizer scanner = new StringTokenizer(pacProxy);
89
		String pacProxyType = scanner.nextToken();
90
		String proxyType = (String) PROXY_TYPE_MAP.get(pacProxyType);
91
		if (proxyType == null || proxyType.equals(NO_PROXY))
92
			return null;
93
94
		String pacHostnameAndPort = null;
95
		if (scanner.hasMoreTokens()) {
96
			pacHostnameAndPort = scanner.nextToken();
97
		}
98
		String hostname = getHostname(pacHostnameAndPort);
99
		if (hostname != null) {
100
			int port = getPort(pacHostnameAndPort);
101
			IProxyData proxy = new ProxyData(proxyType);
102
			proxy.setHost(hostname);
103
			proxy.setPort(port);
104
			return proxy;
105
		}
106
		return null;
107
	}
108
109
	private static boolean startsWithProxyType(String pacProxy) {
110
		Iterator iter = PROXY_TYPE_MAP.keySet().iterator();
111
		while (iter.hasNext()) {
112
			if (pacProxy.startsWith((String) iter.next())) {
113
				return true;
114
			}
115
		}
116
117
		return false;
118
	}
119
120
	static String getHostname(String pacHostnameAndPort) {
121
		if (pacHostnameAndPort != null) {
122
			return pacHostnameAndPort.substring(0, pacHostnameAndPort
123
					.indexOf(':'));
124
		}
125
		return null;
126
	}
127
128
	static int getPort(String pacHostnameAndPort) {
129
		if (pacHostnameAndPort != null && pacHostnameAndPort.indexOf(':') > -1) {
130
			return Integer.parseInt(pacHostnameAndPort
131
					.substring(pacHostnameAndPort.indexOf(':') + 1));
132
		}
133
		return 0;
134
	}
135
136
}
(-)natives/unix/Debug/objects.mk (+7 lines)
Added Link Here
1
################################################################################
2
# Automatically-generated file. Do not edit!
3
################################################################################
4
5
USER_OBJS :=
6
7
LIBS := -lgconf-2 -lORBit-2 -lgthread-2.0 -lrt -lgobject-2.0 -lglib-2.0
(-)src/org/eclipse/core/internal/net/proxy/win32/winhttp/WinHttpAutoProxyOptions.java (+63 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007, 2008 compeople AG and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 * 	compeople AG (Stefan Liebig) - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.core.internal.net.proxy.win32.winhttp;
12
13
/**
14
 * Wrapper for Win32 WINHTTP_AUTOPROXY_OPTIONS Structure
15
 */
16
public class WinHttpAutoProxyOptions {
17
18
	// Flags for WINHTTP_AUTOPROXY_OPTIONS::dwFlags
19
	public static final int WINHTTP_AUTOPROXY_AUTO_DETECT = 0x00000001;
20
	public static final int WINHTTP_AUTOPROXY_CONFIG_URL = 0x00000002;
21
	public static final int WINHTTP_AUTOPROXY_RUN_INPROCESS = 0x00010000;
22
	public static final int WINHTTP_AUTOPROXY_RUN_OUTPROCESS_ONLY = 0x00020000;
23
24
	// Flags for WINHTTP_AUTOPROXY_OPTIONS::dwAutoDetectFlags 
25
	public static final int WINHTTP_AUTO_DETECT_TYPE_DHCP = 0x00000001;
26
	public static final int WINHTTP_AUTO_DETECT_TYPE_DNS_A = 0x00000002;
27
28
	int flags;
29
	int autoDetectFlags;
30
	String autoConfigUrl;
31
	String reservedPointer;
32
	int reservedInt;
33
	boolean autoLogonIfChallenged;
34
35
	/**
36
	 * @param autoConfigUrl the autoConfigUrl to set
37
	 */
38
	public void setAutoConfigUrl( String autoConfigUrl ) {
39
		this.autoConfigUrl = autoConfigUrl;
40
	}
41
42
	/**
43
	 * @param autoDetectFlags the autoDetectFlags to set
44
	 */
45
	public void setAutoDetectFlags( int autoDetectFlags ) {
46
		this.autoDetectFlags = autoDetectFlags;
47
	}
48
49
	/**
50
	 * @param autoLogonIfChallenged the autoLogonIfChallenged to set
51
	 */
52
	public void setAutoLogonIfChallenged( boolean autoLogonIfChallenged ) {
53
		this.autoLogonIfChallenged = autoLogonIfChallenged;
54
	}
55
56
	/**
57
	 * @param flags the flags to set
58
	 */
59
	public void setFlags( int flags ) {
60
		this.flags = flags;
61
	}
62
63
}

Return to bug 180921