Skip to content

Commit 3fb33c7

Browse files
Romain GuyAndroid Code Review
authored andcommitted
Merge "Added method getPreferredHttpHost"
2 parents 9e7f41e + 2986f85 commit 3fb33c7

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed

core/java/android/net/Proxy.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,20 @@
1616

1717
package android.net;
1818

19+
import org.apache.harmony.luni.platform.INetworkSystem;
20+
import org.apache.harmony.luni.platform.Platform;
21+
import org.apache.http.HttpHost;
22+
1923
import android.content.ContentResolver;
2024
import android.content.Context;
2125
import android.os.SystemProperties;
2226
import android.provider.Settings;
2327
import android.util.Log;
2428

29+
import java.net.InetAddress;
30+
import java.net.URI;
31+
import java.net.UnknownHostException;
32+
2533
import junit.framework.Assert;
2634

2735
/**
@@ -36,6 +44,8 @@ final public class Proxy {
3644
static final public String PROXY_CHANGE_ACTION =
3745
"android.intent.action.PROXY_CHANGE";
3846

47+
static final private INetworkSystem NETIMPL = Platform.getNetworkSystem();
48+
3949
/**
4050
* Return the proxy host set by the user.
4151
* @param ctx A Context used to get the settings for the proxy host.
@@ -120,4 +130,74 @@ static final public int getDefaultPort() {
120130
}
121131
}
122132

133+
/**
134+
* Returns the preferred proxy to be used by clients. This is a wrapper
135+
* around {@link android.net.Proxy#getHost()}. Currently no proxy will
136+
* be returned for localhost or if the active network is Wi-Fi.
137+
*
138+
* @param context the context which will be passed to
139+
* {@link android.net.Proxy#getHost()}
140+
* @param url the target URL for the request
141+
* @note Calling this method requires permission
142+
* android.permission.ACCESS_NETWORK_STATE
143+
* @return The preferred proxy to be used by clients, or null if there
144+
* is no proxy.
145+
*
146+
* {@hide}
147+
*/
148+
static final public HttpHost getPreferredHttpHost(Context context,
149+
String url) {
150+
if (!isLocalHost(url) && !isNetworkWifi(context)) {
151+
final String proxyHost = Proxy.getHost(context);
152+
if (proxyHost != null) {
153+
return new HttpHost(proxyHost, Proxy.getPort(context), "http");
154+
}
155+
}
156+
157+
return null;
158+
}
159+
160+
static final private boolean isLocalHost(String url) {
161+
if (url == null) {
162+
return false;
163+
}
164+
165+
try {
166+
final URI uri = URI.create(url);
167+
final String host = uri.getHost();
168+
if (host != null) {
169+
if (host.equalsIgnoreCase("localhost")) {
170+
return true;
171+
}
172+
if (InetAddress.getByAddress(NETIMPL.ipStringToByteArray(host))
173+
.isLoopbackAddress()) {
174+
return true;
175+
}
176+
}
177+
} catch (UnknownHostException uex) {
178+
// Ignore (INetworkSystem.ipStringToByteArray)
179+
} catch (IllegalArgumentException iex) {
180+
// Ignore (URI.create)
181+
}
182+
183+
return false;
184+
}
185+
186+
static final private boolean isNetworkWifi(Context context) {
187+
if (context == null) {
188+
return false;
189+
}
190+
191+
final ConnectivityManager connectivity = (ConnectivityManager)
192+
context.getSystemService(Context.CONNECTIVITY_SERVICE);
193+
if (connectivity != null) {
194+
final NetworkInfo info = connectivity.getActiveNetworkInfo();
195+
if (info != null &&
196+
info.getType() == ConnectivityManager.TYPE_WIFI) {
197+
return true;
198+
}
199+
}
200+
201+
return false;
202+
}
123203
};

tests/CoreTests/android/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
2525
<uses-permission android:name="android.permission.WRITE_APN_SETTINGS" />
2626
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
27+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
2728

2829
<!-- location test permissions -->
2930
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (C) 2010 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package android.core;
18+
19+
import org.apache.http.HttpHost;
20+
21+
import android.content.Context;
22+
import android.net.Proxy;
23+
import android.test.AndroidTestCase;
24+
25+
/**
26+
* Proxy tests
27+
*/
28+
public class ProxyTest extends AndroidTestCase {
29+
private Context mContext;
30+
private HttpHost mHttpHost;
31+
32+
@Override
33+
protected void setUp() throws Exception {
34+
super.setUp();
35+
36+
mContext = getContext();
37+
mHttpHost = null;
38+
String proxyHost = Proxy.getHost(mContext);
39+
int proxyPort = Proxy.getPort(mContext);
40+
if (proxyHost != null) {
41+
mHttpHost = new HttpHost(proxyHost, proxyPort, "http");
42+
}
43+
}
44+
45+
@Override
46+
protected void tearDown() throws Exception {
47+
super.tearDown();
48+
}
49+
50+
/**
51+
* Bad url parameter should not cause any exception.
52+
*/
53+
public void testProxyGetPreferredHttpHost_UrlBad() throws Exception {
54+
assertEquals(mHttpHost, Proxy.getPreferredHttpHost(mContext, null));
55+
assertEquals(mHttpHost, Proxy.getPreferredHttpHost(mContext, ""));
56+
assertEquals(mHttpHost, Proxy.getPreferredHttpHost(mContext, "bad:"));
57+
assertEquals(mHttpHost, Proxy.getPreferredHttpHost(mContext, "bad"));
58+
assertEquals(mHttpHost, Proxy.getPreferredHttpHost(mContext, "bad:\\"));
59+
assertEquals(mHttpHost, Proxy.getPreferredHttpHost(mContext, "bad://#"));
60+
assertEquals(mHttpHost, Proxy.getPreferredHttpHost(mContext, "://#"));
61+
}
62+
63+
/**
64+
* Proxy (if available) should be returned when url parameter is not localhost.
65+
*/
66+
public void testProxyGetPreferredHttpHost_UrlNotlLocalhost() throws Exception {
67+
assertEquals(mHttpHost, Proxy.getPreferredHttpHost(mContext, "http://"));
68+
assertEquals(mHttpHost, Proxy.getPreferredHttpHost(mContext, "http://example.com"));
69+
assertEquals(mHttpHost, Proxy.getPreferredHttpHost(mContext, "http://example.com/"));
70+
assertEquals(mHttpHost, Proxy.getPreferredHttpHost(mContext, "http://192.168.0.1/"));
71+
assertEquals(mHttpHost, Proxy.getPreferredHttpHost(mContext, "file:///foo/bar"));
72+
assertEquals(mHttpHost, Proxy.getPreferredHttpHost(mContext, "rtsp://example.com"));
73+
assertEquals(mHttpHost, Proxy.getPreferredHttpHost(mContext, "rtsp://example.com/"));
74+
assertEquals(mHttpHost, Proxy.getPreferredHttpHost(mContext, "javascript:alert(1)"));
75+
}
76+
77+
/**
78+
* No proxy should be returned when url parameter is localhost.
79+
*/
80+
public void testProxyGetPreferredHttpHost_UrlLocalhost() throws Exception {
81+
assertNull(Proxy.getPreferredHttpHost(mContext, "http://localhost"));
82+
assertNull(Proxy.getPreferredHttpHost(mContext, "http://localhost/"));
83+
assertNull(Proxy.getPreferredHttpHost(mContext, "http://localhost/hej.html"));
84+
assertNull(Proxy.getPreferredHttpHost(mContext, "http://127.0.0.1"));
85+
assertNull(Proxy.getPreferredHttpHost(mContext, "http://127.0.0.1/"));
86+
assertNull(Proxy.getPreferredHttpHost(mContext, "http://127.0.0.1/hej.html"));
87+
assertNull(Proxy.getPreferredHttpHost(mContext, "http://127.0.0.1:80/"));
88+
assertNull(Proxy.getPreferredHttpHost(mContext, "http://127.0.0.1:8080/"));
89+
assertNull(Proxy.getPreferredHttpHost(mContext, "rtsp://127.0.0.1/"));
90+
assertNull(Proxy.getPreferredHttpHost(mContext, "rtsp://localhost/"));
91+
assertNull(Proxy.getPreferredHttpHost(mContext, "https://localhost/"));
92+
}
93+
}

0 commit comments

Comments
 (0)