am c3c6621a: Merge change 25879 into eclair
Merge commit 'c3c6621a2cf2f0d40d062dcad1c9f65485473841' into eclair-plus-aosp * commit 'c3c6621a2cf2f0d40d062dcad1c9f65485473841': 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.RemoteException;
|
||||||
import android.os.ServiceManager;
|
import android.os.ServiceManager;
|
||||||
import android.os.SystemProperties;
|
import android.os.SystemProperties;
|
||||||
|
import android.os.Build;
|
||||||
import android.server.data.CrashData;
|
import android.server.data.CrashData;
|
||||||
import android.util.Config;
|
import android.util.Config;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
@@ -110,6 +111,12 @@ public class RuntimeInit {
|
|||||||
LogManager.getLogManager().reset();
|
LogManager.getLogManager().reset();
|
||||||
new AndroidConfig();
|
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
|
* If we're running in an emulator launched with "-trace", put the
|
||||||
* VM into emulator trace profiling mode so that the user can hit
|
* VM into emulator trace profiling mode so that the user can hit
|
||||||
@@ -125,6 +132,36 @@ public class RuntimeInit {
|
|||||||
initialized = true;
|
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".
|
* Invokes a static "main(argv[]) method on class "className".
|
||||||
* Converts various failing exceptions into RuntimeExceptions, with
|
* Converts various failing exceptions into RuntimeExceptions, with
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package android.core;
|
package android.core;
|
||||||
|
|
||||||
|
import android.test.suitebuilder.annotation.Suppress;
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
@@ -29,10 +30,9 @@ import java.net.ServerSocket;
|
|||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import android.test.suitebuilder.annotation.Suppress;
|
|
||||||
|
|
||||||
@Suppress
|
|
||||||
public class URLTest extends TestCase {
|
public class URLTest extends TestCase {
|
||||||
|
|
||||||
private static void get(String u) throws IOException {
|
private static void get(String u) throws IOException {
|
||||||
@@ -63,10 +63,12 @@ public class URLTest extends TestCase {
|
|||||||
assertTrue(new String(data).indexOf("<html>") >= 0);
|
assertTrue(new String(data).indexOf("<html>") >= 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Suppress
|
||||||
public void testGetHTTP() throws Exception {
|
public void testGetHTTP() throws Exception {
|
||||||
get("http://www.google.com");
|
get("http://www.google.com");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Suppress
|
||||||
public void testGetHTTPS() throws Exception {
|
public void testGetHTTPS() throws Exception {
|
||||||
get("https://www.fortify.net/cgi/ssl_2.pl");
|
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 static class DummyServer implements Runnable {
|
||||||
|
|
||||||
private int keepAliveCount;
|
private int keepAliveCount;
|
||||||
|
private Map<String, String> headers = new HashMap<String, String>();
|
||||||
|
|
||||||
public DummyServer(int keepAliveCount) {
|
public DummyServer(int keepAliveCount) {
|
||||||
this.keepAliveCount = keepAliveCount;
|
this.keepAliveCount = keepAliveCount;
|
||||||
@@ -93,9 +96,17 @@ public class URLTest extends TestCase {
|
|||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
|
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
|
||||||
try {
|
try {
|
||||||
for (int i = 0; i < keepAliveCount; i++) {
|
for (int i = 0; i < keepAliveCount; i++) {
|
||||||
String header = reader.readLine();
|
reader.readLine();
|
||||||
while (header != null && header.length() != 0) {
|
headers.clear();
|
||||||
header = reader.readLine();
|
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();
|
OutputStream output = socket.getOutputStream();
|
||||||
@@ -142,6 +153,7 @@ public class URLTest extends TestCase {
|
|||||||
/**
|
/**
|
||||||
* Test case for HTTP keep-alive behavior.
|
* Test case for HTTP keep-alive behavior.
|
||||||
*/
|
*/
|
||||||
|
@Suppress
|
||||||
public void testGetKeepAlive() throws Exception {
|
public void testGetKeepAlive() throws Exception {
|
||||||
new Thread(new DummyServer(3)).start();
|
new Thread(new DummyServer(3)).start();
|
||||||
Thread.sleep(100);
|
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.
|
* Regression for issue 1001814.
|
||||||
*/
|
*/
|
||||||
|
@Suppress
|
||||||
public void testHttpConnectionTimeout() throws Exception {
|
public void testHttpConnectionTimeout() throws Exception {
|
||||||
int timeout = 5000;
|
int timeout = 5000;
|
||||||
HttpURLConnection cn = null;
|
HttpURLConnection cn = null;
|
||||||
@@ -190,7 +217,8 @@ public class URLTest extends TestCase {
|
|||||||
/**
|
/**
|
||||||
* Regression test for issue 1158780 where using '{' and '}' in an URL threw
|
* Regression test for issue 1158780 where using '{' and '}' in an URL threw
|
||||||
* an NPE. The RI accepts this URL and returns the status 404.
|
* an NPE. The RI accepts this URL and returns the status 404.
|
||||||
*/
|
*/
|
||||||
|
@Suppress
|
||||||
public void testMalformedUrl() throws Exception {
|
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");
|
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();
|
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
|
||||||
|
|||||||
Reference in New Issue
Block a user