incubator-callback-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From macdo...@apache.org
Subject [1/25] android commit: CB-1411: Add trustAllHosts option to FileTransfer.download on Android
Date Tue, 18 Sep 2012 02:12:09 GMT
Updated Branches:
  refs/heads/master d859bb8e6 -> d181d89dd


CB-1411: Add trustAllHosts option to FileTransfer.download on Android


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/commit/d181d89d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/tree/d181d89d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/diff/d181d89d

Branch: refs/heads/master
Commit: d181d89dd2ec4007c749d6c10d04d46716a44be1
Parents: ac14b0d
Author: Simon MacDonald <simon.macdonald@gmail.com>
Authored: Mon Sep 17 22:08:48 2012 -0400
Committer: Simon MacDonald <simon.macdonald@gmail.com>
Committed: Mon Sep 17 22:09:52 2012 -0400

----------------------------------------------------------------------
 framework/src/org/apache/cordova/FileTransfer.java |   35 +++++++++++++-
 1 files changed, 32 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/d181d89d/framework/src/org/apache/cordova/FileTransfer.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/FileTransfer.java b/framework/src/org/apache/cordova/FileTransfer.java
index e1b6919..881caa5 100644
--- a/framework/src/org/apache/cordova/FileTransfer.java
+++ b/framework/src/org/apache/cordova/FileTransfer.java
@@ -85,7 +85,7 @@ public class FileTransfer extends Plugin {
         if (action.equals("upload")) {
             return upload(URLDecoder.decode(source), target, args);
         } else if (action.equals("download")) {
-            return download(source, target);
+            return download(source, target, args.optBoolean(2));
         } else {
             return new PluginResult(PluginResult.Status.INVALID_ACTION);
         }
@@ -459,7 +459,7 @@ public class FileTransfer extends Plugin {
      * @param target      	Full path of the file on the file system
      * @return JSONObject 	the downloaded file
      */
-    private PluginResult download(String source, String target) {
+    private PluginResult download(String source, String target, boolean trustEveryone) {
         Log.d(LOG_TAG, "download " + source + " to " +  target);
 
         HttpURLConnection connection = null;
@@ -473,7 +473,30 @@ public class FileTransfer extends Plugin {
             if (webView.isUrlWhiteListed(source))
             {
               URL url = new URL(source);
-              connection = (HttpURLConnection) url.openConnection();
+              boolean useHttps = url.getProtocol().toLowerCase().equals("https");
+              // Open a HTTP connection to the URL based on protocol
+              if (useHttps) {
+                  // Using standard HTTPS connection. Will not allow self signed certificate
+                  if (!trustEveryone) {
+                	  connection = (HttpsURLConnection) url.openConnection();
+                  }
+                  // Use our HTTPS connection that blindly trusts everyone.
+                  // This should only be used in debug environments
+                  else {
+                      // Setup the HTTPS connection class to trust everyone
+                      trustAllHosts();
+                      HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
+                      // Save the current hostnameVerifier
+                      defaultHostnameVerifier = https.getHostnameVerifier();
+                      // Setup the connection not to verify hostnames
+                      https.setHostnameVerifier(DO_NOT_VERIFY);
+                      connection = https;
+                  }
+              }
+              // Return a standard HTTP connection
+              else {
+            	  connection = (HttpURLConnection) url.openConnection();
+              }
               connection.setRequestMethod("GET");
 
               //Add cookie support
@@ -516,6 +539,12 @@ public class FileTransfer extends Plugin {
               FileUtils fileUtil = new FileUtils();
               JSONObject fileEntry = fileUtil.getEntry(file);
 
+              // Revert back to the proper verifier and socket factories
+              if (trustEveryone && url.getProtocol().toLowerCase().equals("https"))
{
+                  ((HttpsURLConnection) connection).setHostnameVerifier(defaultHostnameVerifier);
+                  HttpsURLConnection.setDefaultSSLSocketFactory(defaultSSLSocketFactory);
+              }
+
               return new PluginResult(PluginResult.Status.OK, fileEntry);
             }
             else


Mime
View raw message