diff options
author | Jesse Wilson <jessewilson@google.com> | 2009-09-18 18:06:43 -0700 |
---|---|---|
committer | Jesse Wilson <jessewilson@google.com> | 2009-09-18 18:16:16 -0700 |
commit | d0f80d445644bfc08b62339f01766b924e42dc4d (patch) | |
tree | 9899ca93cb995318bed07d10a7053c0f52d2cc1d | |
parent | c3a3b399a9d7707b7f3c4108e0e31a1b664a6f22 (diff) | |
download | frameworks_base-d0f80d445644bfc08b62339f01766b924e42dc4d.zip frameworks_base-d0f80d445644bfc08b62339f01766b924e42dc4d.tar.gz frameworks_base-d0f80d445644bfc08b62339f01766b924e42dc4d.tar.bz2 |
Setting the default HTTP user agent at runtime init.
I can't do this in HttpURLConnection directly, since that would
cause a forbidden dependency from Dalvik on Android.
-rw-r--r-- | core/java/com/android/internal/os/RuntimeInit.java | 37 | ||||
-rw-r--r-- | tests/CoreTests/android/core/URLTest.java | 42 |
2 files changed, 72 insertions, 7 deletions
diff --git a/core/java/com/android/internal/os/RuntimeInit.java b/core/java/com/android/internal/os/RuntimeInit.java index 4e6f9ca..c782c8c 100644 --- a/core/java/com/android/internal/os/RuntimeInit.java +++ b/core/java/com/android/internal/os/RuntimeInit.java @@ -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; @@ -111,6 +112,12 @@ public class RuntimeInit { 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 * F9/F10 at any time to capture traces. This has performance @@ -126,6 +133,36 @@ public class RuntimeInit { } /** + * 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 * the assumption that they will then cause the VM instance to exit. diff --git a/tests/CoreTests/android/core/URLTest.java b/tests/CoreTests/android/core/URLTest.java index 56f9f7b..5efcd5b 100644 --- a/tests/CoreTests/android/core/URLTest.java +++ b/tests/CoreTests/android/core/URLTest.java @@ -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(); |