You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by sh...@apache.org on 2013/03/20 20:29:00 UTC

js commit: [CB-52] API: FileTransfer - Basic Authentication

Updated Branches:
  refs/heads/master f97bd694d -> 598bbdff9


[CB-52] API: FileTransfer - Basic Authentication

commit 00a7ca15142804d7b8f0c8d8ade0088ba34b3015
Author: Shazron Abdullah <sh...@apache.org>
Date:   Wed Mar 20 10:03:25 2013 -0700

    [CB-52] Fix whitespace issues

commit e72a998830a5e5dba5bd9694affca5544d8fad6c
Author: Shazron Abdullah <sh...@apache.org>
Date:   Wed Mar 20 09:57:48 2013 -0700

    [CB-52] API: FileTransfer Download - Basic Authentication

commit 18ad146a4ec3411bbf944bcdb99e067456ba6cb3
Author: Shazron Abdullah <sh...@apache.org>
Date:   Tue Mar 19 17:06:19 2013 -0700

    [CB-52] API: FileTransfer Upload - Basic Authentication


Project: http://git-wip-us.apache.org/repos/asf/cordova-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-js/commit/598bbdff
Tree: http://git-wip-us.apache.org/repos/asf/cordova-js/tree/598bbdff
Diff: http://git-wip-us.apache.org/repos/asf/cordova-js/diff/598bbdff

Branch: refs/heads/master
Commit: 598bbdff96f348b02e3931ed2011700e4addde25
Parents: f97bd69
Author: Shazron Abdullah <sh...@apache.org>
Authored: Wed Mar 20 12:28:43 2013 -0700
Committer: Shazron Abdullah <sh...@apache.org>
Committed: Wed Mar 20 12:28:43 2013 -0700

----------------------------------------------------------------------
 lib/common/plugin/FileTransfer.js |   55 ++++++++++++++++++++++++++++++++
 1 files changed, 55 insertions(+), 0 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-js/blob/598bbdff/lib/common/plugin/FileTransfer.js
----------------------------------------------------------------------
diff --git a/lib/common/plugin/FileTransfer.js b/lib/common/plugin/FileTransfer.js
index 778f955..2e4c980 100644
--- a/lib/common/plugin/FileTransfer.js
+++ b/lib/common/plugin/FileTransfer.js
@@ -32,6 +32,38 @@ function newProgressEvent(result) {
     return pe;
 }
 
+function getBasicAuthHeader(urlString) {
+    var header =  null;
+
+    if (window.btoa) {
+        // parse the url using the Location object
+        var url = document.createElement('a');
+        url.href = urlString;
+
+        var credentials = null;
+        var protocol = url.protocol + "//";
+        var origin = protocol + url.host;
+
+        // check whether there are the username:password credentials in the url
+        if (url.href.indexOf(origin) != 0) { // credentials found
+            var atIndex = url.href.indexOf("@");
+            credentials = url.href.substring(protocol.length, atIndex);
+        }
+
+        if (credentials) {
+            var authHeader = "Authorization";
+            var authHeaderValue = "Basic " + window.btoa(credentials);
+
+            header = {
+                name : authHeader,
+                value : authHeaderValue
+            };
+        }
+    }
+
+    return header;
+}
+
 var idCounter = 0;
 
 /**
@@ -62,6 +94,18 @@ FileTransfer.prototype.upload = function(filePath, server, successCallback, erro
     var params = null;
     var chunkedMode = true;
     var headers = null;
+
+    var basicAuthHeader = getBasicAuthHeader(server);
+    if (basicAuthHeader) {
+        if (!options) {
+            options = new FileUploadOptions();
+        }
+        if (!options.headers) {
+            options.headers = {};
+        }
+        options.headers[basicAuthHeader.name] = basicAuthHeader.value;
+    }
+
     if (options) {
         fileKey = options.fileKey;
         fileName = options.fileName;
@@ -109,6 +153,17 @@ FileTransfer.prototype.download = function(source, target, successCallback, erro
     argscheck.checkArgs('ssFF*', 'FileTransfer.download', arguments);
     var self = this;
 
+    var basicAuthHeader = getBasicAuthHeader(source);
+    if (basicAuthHeader) {
+        if (!options) {
+            options = {};
+        }
+        if (!options.headers) {
+            options.headers = {};
+        }
+        options.headers[basicAuthHeader.name] = basicAuthHeader.value;
+    }
+
     var headers = null;
     if (options) {
         headers = options.headers || null;