Merge change 25879 into eclair
* changes: Setting the default HTTP user agent at runtime init.
This commit is contained in:
@@ -25,6 +25,7 @@ import android.os.Process;
|
||||
import android.os.RemoteException;
|
||||
import android.os.ServiceManager;
|
||||
import android.os.SystemProperties;
|
||||
import android.os.Build;
|
||||
import android.server.data.CrashData;
|
||||
import android.util.Config;
|
||||
import android.util.Log;
|
||||
@@ -110,6 +111,12 @@ public class RuntimeInit {
|
||||
LogManager.getLogManager().reset();
|
||||
new AndroidConfig();
|
||||
|
||||
/*
|
||||
* Sets the default HTTP User-Agent used by HttpURLConnection.
|
||||
*/
|
||||
String userAgent = getDefaultUserAgent();
|
||||
System.setProperty("http.agent", userAgent);
|
||||
|
||||
/*
|
||||
* If we're running in an emulator launched with "-trace", put the
|
||||
* VM into emulator trace profiling mode so that the user can hit
|
||||
@@ -125,6 +132,36 @@ public class RuntimeInit {
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an HTTP user agent of the form
|
||||
* "Dalvik/1.1.0 (Linux; U; Android Eclair Build/MASTER)".
|
||||
*/
|
||||
private static String getDefaultUserAgent() {
|
||||
StringBuilder result = new StringBuilder(64);
|
||||
result.append("Dalvik/");
|
||||
result.append(System.getProperty("java.vm.version")); // such as 1.1.0
|
||||
result.append(" (Linux; U; Android ");
|
||||
|
||||
String version = Build.VERSION.RELEASE; // "1.0" or "3.4b5"
|
||||
result.append(version.length() > 0 ? version : "1.0");
|
||||
|
||||
// add the model for the release build
|
||||
if ("REL".equals(Build.VERSION.CODENAME)) {
|
||||
String model = Build.MODEL;
|
||||
if (model.length() > 0) {
|
||||
result.append("; ");
|
||||
result.append(model);
|
||||
}
|
||||
}
|
||||
String id = Build.ID; // "MASTER" or "M4-rc20"
|
||||
if (id.length() > 0) {
|
||||
result.append(" Build/");
|
||||
result.append(id);
|
||||
}
|
||||
result.append(")");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes a static "main(argv[]) method on class "className".
|
||||
* Converts various failing exceptions into RuntimeExceptions, with
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package android.core;
|
||||
|
||||
import android.test.suitebuilder.annotation.Suppress;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
@@ -29,10 +30,9 @@ import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import android.test.suitebuilder.annotation.Suppress;
|
||||
|
||||
@Suppress
|
||||
public class URLTest extends TestCase {
|
||||
|
||||
private static void get(String u) throws IOException {
|
||||
@@ -63,10 +63,12 @@ public class URLTest extends TestCase {
|
||||
assertTrue(new String(data).indexOf("<html>") >= 0);
|
||||
}
|
||||
|
||||
@Suppress
|
||||
public void testGetHTTP() throws Exception {
|
||||
get("http://www.google.com");
|
||||
}
|
||||
|
||||
@Suppress
|
||||
public void testGetHTTPS() throws Exception {
|
||||
get("https://www.fortify.net/cgi/ssl_2.pl");
|
||||
}
|
||||
@@ -79,6 +81,7 @@ public class URLTest extends TestCase {
|
||||
private static class DummyServer implements Runnable {
|
||||
|
||||
private int keepAliveCount;
|
||||
private Map<String, String> headers = new HashMap<String, String>();
|
||||
|
||||
public DummyServer(int keepAliveCount) {
|
||||
this.keepAliveCount = keepAliveCount;
|
||||
@@ -93,9 +96,17 @@ public class URLTest extends TestCase {
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
|
||||
try {
|
||||
for (int i = 0; i < keepAliveCount; i++) {
|
||||
String header = reader.readLine();
|
||||
while (header != null && header.length() != 0) {
|
||||
header = reader.readLine();
|
||||
reader.readLine();
|
||||
headers.clear();
|
||||
while (true) {
|
||||
String header = reader.readLine();
|
||||
if (header.length() == 0) {
|
||||
break;
|
||||
}
|
||||
int colon = header.indexOf(":");
|
||||
String key = header.substring(0, colon);
|
||||
String value = header.substring(colon + 1).trim();
|
||||
headers.put(key, value);
|
||||
}
|
||||
|
||||
OutputStream output = socket.getOutputStream();
|
||||
@@ -142,6 +153,7 @@ public class URLTest extends TestCase {
|
||||
/**
|
||||
* Test case for HTTP keep-alive behavior.
|
||||
*/
|
||||
@Suppress
|
||||
public void testGetKeepAlive() throws Exception {
|
||||
new Thread(new DummyServer(3)).start();
|
||||
Thread.sleep(100);
|
||||
@@ -160,9 +172,24 @@ public class URLTest extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress
|
||||
public void testUserAgentHeader() throws Exception {
|
||||
DummyServer server = new DummyServer(1);
|
||||
new Thread(server).start();
|
||||
Thread.sleep(100);
|
||||
|
||||
// We expect the request to work three times, then it fails.
|
||||
request(new URL("http://localhost:8182"));
|
||||
|
||||
String userAgent = server.headers.get("User-Agent");
|
||||
assertTrue("Unexpected User-Agent: " + userAgent, userAgent.matches(
|
||||
"Dalvik/[\\d.]+ \\(Linux; U; Android \\w+(;.*)?( Build/\\w+)?\\)"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Regression for issue 1001814.
|
||||
*/
|
||||
@Suppress
|
||||
public void testHttpConnectionTimeout() throws Exception {
|
||||
int timeout = 5000;
|
||||
HttpURLConnection cn = null;
|
||||
@@ -190,7 +217,8 @@ public class URLTest extends TestCase {
|
||||
/**
|
||||
* Regression test for issue 1158780 where using '{' and '}' in an URL threw
|
||||
* an NPE. The RI accepts this URL and returns the status 404.
|
||||
*/
|
||||
*/
|
||||
@Suppress
|
||||
public void testMalformedUrl() throws Exception {
|
||||
URL url = new URL("http://www.google.com/cgi-bin/myscript?g={United+States}+Borders+Mexico+{Climate+change}+Marketing+{Automotive+industry}+News+Health+Internet");
|
||||
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
|
||||
|
||||
Reference in New Issue
Block a user