You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@cordova.apache.org by "ASF GitHub Bot (JIRA)" <ji...@apache.org> on 2017/11/22 00:26:00 UTC

[jira] [Commented] (CB-13017) Download with proxies

    [ https://issues.apache.org/jira/browse/CB-13017?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16261752#comment-16261752 ] 

ASF GitHub Bot commented on CB-13017:
-------------------------------------

maverickmishra closed pull request #184: CB-13017 (android) Added proxy option in download method
URL: https://github.com/apache/cordova-plugin-file-transfer/pull/184
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/src/android/FileTransfer.java b/src/android/FileTransfer.java
index de15981..2bcb8cb 100644
--- a/src/android/FileTransfer.java
+++ b/src/android/FileTransfer.java
@@ -31,8 +31,9 @@ Licensed to the Apache Software Foundation (ASF) under one
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
 import java.net.HttpURLConnection;
-import java.net.URLConnection;
+import java.net.*;
 import java.security.cert.CertificateException;
 import java.security.cert.X509Certificate;
 import java.util.HashMap;
@@ -736,12 +737,27 @@ private static String getArgument(JSONArray args, int position, String defaultSt
      */
     private void download(final String source, final String target, JSONArray args, CallbackContext callbackContext) throws JSONException {
         LOG.d(LOG_TAG, "download " + source + " to " +  target);
+        int port_ = 0;
+        String proxy_ip_ = "no_proxy";
 
-        final CordovaResourceApi resourceApi = webView.getResourceApi();
+        try {
+            port_ = Integer.parseInt(args.getString(3));
+        }
+        catch (Exception e){
+            port_ = 0;
+        }
+        final int port = port_;
+
+        if (args.getString(2) == null || args.getString(2) == "null")
+            proxy_ip_ = "no_proxy";
+        else
+            proxy_ip_ = args.getString(2);
+        final String proxy_ip = proxy_ip_;
 
-        final boolean trustEveryone = args.optBoolean(2);
-        final String objectId = args.getString(3);
-        final JSONObject headers = args.optJSONObject(4);
+        final CordovaResourceApi resourceApi = webView.getResourceApi();
+        final boolean trustEveryone = args.optBoolean(4);
+        final String objectId = args.getString(5);
+        final JSONObject headers = args.optJSONObject(6);
 
         final Uri sourceUri = resourceApi.remapUri(Uri.parse(source));
         int uriType = CordovaResourceApi.getUriType(sourceUri);
@@ -809,6 +825,11 @@ public void run() {
                 Uri targetUri = resourceApi.remapUri(
                         tmpTarget.getScheme() != null ? tmpTarget : Uri.fromFile(new File(target)));
                 HttpURLConnection connection = null;
+                Proxy proxy;
+                if (proxy_ip == "no_proxy")
+                    proxy = Proxy.NO_PROXY;
+                else
+                    proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy_ip, port));
                 HostnameVerifier oldHostnameVerifier = null;
                 SSLSocketFactory oldSocketFactory = null;
                 File file = null;
@@ -837,7 +858,10 @@ public void run() {
                     } else {
                         // connect to server
                         // Open a HTTP connection to the URL based on protocol
-                        connection = resourceApi.createHttpConnection(sourceUri);
+                        if (proxy_ip == "no_proxy")
+                            connection = resourceApi.createHttpConnection(sourceUri);
+                        else
+                            connection = (HttpURLConnection) new URL(source).openConnection(proxy);
                         if (useHttps && trustEveryone) {
                             // Setup the HTTPS connection class to trust everyone
                             HttpsURLConnection https = (HttpsURLConnection)connection;
diff --git a/www/FileTransfer.js b/www/FileTransfer.js
index 80cf91c..ea07aee 100644
--- a/www/FileTransfer.js
+++ b/www/FileTransfer.js
@@ -179,6 +179,8 @@ FileTransfer.prototype.download = function(source, target, successCallback, erro
     argscheck.checkArgs('ssFF*', 'FileTransfer.download', arguments);
     var self = this;
 
+    var proxy = null;
+    var port = null;
     var basicAuthHeader = getBasicAuthHeader(source);
     if (basicAuthHeader) {
         source = source.replace(getUrlCredentials(source) + '@', '');
@@ -188,6 +190,11 @@ FileTransfer.prototype.download = function(source, target, successCallback, erro
         options.headers[basicAuthHeader.name] = basicAuthHeader.value;
     }
 
+    if (options.proxy)
+        proxy = options.proxy;
+    if (options.port)
+        port = options.port;
+
     var headers = null;
     if (options) {
         headers = options.headers || null;
@@ -221,11 +228,10 @@ FileTransfer.prototype.download = function(source, target, successCallback, erro
     };
 
     var fail = errorCallback && function(e) {
-        var error = new FileTransferError(e.code, e.source, e.target, e.http_status, e.body, e.exception);
-        errorCallback(error);
-    };
-
-    exec(win, fail, 'FileTransfer', 'download', [source, target, trustAllHosts, this._id, headers]);
+            var error = new FileTransferError(e.code, e.source, e.target, e.http_status, e.body, e.exception);
+            errorCallback(error);
+        };
+    exec(win, fail, 'FileTransfer', 'download', [source, target, proxy, port, trustAllHosts, this._id, headers]);
 };
 
 /**


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


> Download with proxies
> ---------------------
>
>                 Key: CB-13017
>                 URL: https://issues.apache.org/jira/browse/CB-13017
>             Project: Apache Cordova
>          Issue Type: Improvement
>          Components: cordova-plugin-file-transfer (DEPRECATED)
>    Affects Versions: cordova@7.0.0
>            Reporter: Engin YĆ¼ksel
>            Priority: Minor
>              Labels: android
>
> This changes only affect android platform



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org