Change http facade behavior in SL4A.
b/22948456

Does not throw exception when response code is not 200 any more.
httpPing now returns various information instead of just response code.

Change-Id: I299c9895369975d4688e97586d78b38d55bc856e
diff --git a/Common/src/com/googlecode/android_scripting/facade/wifi/HttpFacade.java b/Common/src/com/googlecode/android_scripting/facade/wifi/HttpFacade.java
index 752bcba..5ab0023 100644
--- a/Common/src/com/googlecode/android_scripting/facade/wifi/HttpFacade.java
+++ b/Common/src/com/googlecode/android_scripting/facade/wifi/HttpFacade.java
@@ -18,8 +18,6 @@
 import java.nio.charset.StandardCharsets;
 import java.util.HashMap;
 
-import android.app.Service;
-
 import com.googlecode.android_scripting.Log;
 import com.googlecode.android_scripting.facade.FacadeManager;
 import com.googlecode.android_scripting.jsonrpc.RpcReceiver;
@@ -32,7 +30,6 @@
  */
 public class HttpFacade extends RpcReceiver {
 
-    private final Service mService;
     private ServerSocket mServerSocket = null;
     private int mServerTimeout = -1;
     private HashMap<Integer, Socket> mSockets = null;
@@ -40,7 +37,6 @@
 
     public HttpFacade(FacadeManager manager) throws IOException {
         super(manager);
-        mService = manager.getService();
         mSockets = new HashMap<Integer, Socket>();
     }
 
@@ -80,7 +76,7 @@
 
     /**
      * Send an http request and get the response.
-     *
+     * 
      * @param url The url to send request to.
      * @return The HttpURLConnection object.
      * @throws IOException When request failed to go through with response code 200.
@@ -90,6 +86,7 @@
         HttpURLConnection urlConnection = null;
         try {
             urlConnection = (HttpURLConnection) targetURL.openConnection();
+            urlConnection.connect();
         } catch (IOException e) {
             Log.e("Failed to open a connection to " + url);
             Log.e(e.toString());
@@ -99,14 +96,6 @@
                 urlConnection.disconnect();
             }
         }
-        int code = urlConnection.getResponseCode();
-        if (code != HttpURLConnection.HTTP_OK) {
-            Log.d("Http request did not return 200.");
-            Log.d("Response code: " + code);
-            String respMsg = urlConnection.getResponseMessage();
-            Log.d("Response message: " + respMsg);
-            throw new IOException("HTTP request did not return 200. " + respMsg);
-        }
         return urlConnection;
     }
 
@@ -126,7 +115,6 @@
     @Rpc(description = "Download a file from specified url.")
     public void httpDownloadFile(String url) throws IOException {
         HttpURLConnection urlConnection = httpRequest(url);
-
         String filename = null;
         String contentDisposition = urlConnection.getHeaderField("Content-Disposition");
         // Try to figure out the name of the file being downloaded.
@@ -152,26 +140,16 @@
     }
 
     @Rpc(description = "Make an http request and return the response message.")
-    public String httpPing(@RpcParameter(name = "url") String url) throws IOException {
+    public HttpURLConnection httpPing(@RpcParameter(name = "url") String url) throws IOException {
         try {
             HttpURLConnection urlConnection = null;
             urlConnection = httpRequest(url);
-            String resp = urlConnection.getResponseMessage();
-            Log.d("Fetched " + resp);
-            return resp;
+            return urlConnection;
         } catch (UnknownHostException e) {
             return null;
         }
     }
 
-    @Rpc(description = "Make an http request and only return the length of the response content.")
-    public Integer httpRequestLength(@RpcParameter(name = "url") String url) throws IOException {
-        HttpURLConnection urlConnection = httpRequest(url);
-        int respSize = urlConnection.getContentLength();
-        Log.d("Fetched: " + respSize);
-        return respSize;
-    }
-
     @Rpc(description = "Make an http request and return the response content as a string.")
     public String httpRequestString(@RpcParameter(name = "url") String url) throws IOException {
         HttpURLConnection urlConnection = httpRequest(url);
diff --git a/Common/src/com/googlecode/android_scripting/jsonrpc/JsonBuilder.java b/Common/src/com/googlecode/android_scripting/jsonrpc/JsonBuilder.java
index 60874c4..94b3941 100644
--- a/Common/src/com/googlecode/android_scripting/jsonrpc/JsonBuilder.java
+++ b/Common/src/com/googlecode/android_scripting/jsonrpc/JsonBuilder.java
@@ -16,12 +16,14 @@
 
 package com.googlecode.android_scripting.jsonrpc;
 
+import java.io.IOException;
+import java.net.HttpURLConnection;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
+import java.net.URL;
 import java.security.PrivateKey;
 import java.security.cert.CertificateEncodingException;
 import java.security.cert.X509Certificate;
-import java.util.Arrays;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
@@ -203,12 +205,18 @@
         if (data instanceof NetworkInfo) {
             return buildNetworkInfo((NetworkInfo) data);
         }
+        if (data instanceof HttpURLConnection) {
+            return buildHttpURLConnection((HttpURLConnection) data);
+        }
         if (data instanceof InetSocketAddress) {
             return buildInetSocketAddress((InetSocketAddress) data);
         }
         if (data instanceof InetAddress) {
             return buildInetAddress((InetAddress) data);
         }
+        if (data instanceof URL) {
+            return buildURL((URL) data);
+        }
         if (data instanceof Point) {
             return buildPoint((Point) data);
         }
@@ -541,7 +549,11 @@
             throws JSONException {
         JSONObject result = new JSONObject();
         for (Entry<String, ?> entry : map.entrySet()) {
-            result.put(entry.getKey(), build(entry.getValue()));
+            String key = entry.getKey();
+            if (key == null) {
+                key = "";
+            }
+            result.put(key, build(entry.getValue()));
         }
         return result;
     }
@@ -754,6 +766,26 @@
         return result;
     }
 
+    private static Object buildHttpURLConnection(HttpURLConnection data)
+            throws JSONException {
+        JSONObject con = new JSONObject();
+        try {
+            con.put("ResponseCode", data.getResponseCode());
+            con.put("ResponseMessage", data.getResponseMessage());
+        } catch (IOException e) {
+            e.printStackTrace();
+            return con;
+        }
+        con.put("ContentLength", data.getContentLength());
+        con.put("ContentEncoding", data.getContentEncoding());
+        con.put("ContentType", data.getContentType());
+        con.put("Date", data.getDate());
+        con.put("ReadTimeout", data.getReadTimeout());
+        con.put("HeaderFields", buildJsonMap(data.getHeaderFields()));
+        con.put("URL", buildURL(data.getURL()));
+        return con;
+    }
+
     private static Object buildNetwork(Network data) throws JSONException {
         JSONObject nw = new JSONObject();
         nw.put("netId", data.netId);
@@ -775,6 +807,16 @@
         return info;
     }
 
+    private static Object buildURL(URL data) throws JSONException {
+        JSONObject url = new JSONObject();
+        url.put("Authority", data.getAuthority());
+        url.put("Host", data.getHost());
+        url.put("Path", data.getPath());
+        url.put("Port", data.getPort());
+        url.put("Protocol", data.getProtocol());
+        return url;
+    }
+
     private static JSONObject buildPhoneAccount(PhoneAccount data)
             throws JSONException {
         JSONObject acct = new JSONObject();