couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gar...@apache.org
Subject [3/3] fauxton commit: updated refs/heads/master to 367d422
Date Mon, 20 Feb 2017 18:03:21 GMT
Improve URL encoding

This improves the way we encode usernames and passwords. So if no
username and password are required it should still work and if the user
has special characters in the url it should still work.


Project: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/commit/367d4226
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/tree/367d4226
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/diff/367d4226

Branch: refs/heads/master
Commit: 367d422683a79d19377a9a6d317aaa56b84b9a96
Parents: 8d28518
Author: Garren Smith <garren.smith@gmail.com>
Authored: Mon Feb 20 17:21:47 2017 +0200
Committer: Garren Smith <garren.smith@gmail.com>
Committed: Mon Feb 20 20:02:51 2017 +0200

----------------------------------------------------------------------
 app/addons/replication/__tests__/api.tests.js | 40 +++++++++---
 app/addons/replication/api.js                 | 74 +++++++++-------------
 2 files changed, 62 insertions(+), 52 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/blob/367d4226/app/addons/replication/__tests__/api.tests.js
----------------------------------------------------------------------
diff --git a/app/addons/replication/__tests__/api.tests.js b/app/addons/replication/__tests__/api.tests.js
index 73677cf..29fb17e 100644
--- a/app/addons/replication/__tests__/api.tests.js
+++ b/app/addons/replication/__tests__/api.tests.js
@@ -16,7 +16,9 @@ import {
   continuous,
   createTarget,
   addDocIdAndRev,
-  getDocUrl
+  getDocUrl,
+  encodeFullUrl,
+  decodeFullUrl
 } from '../api';
 import Constants from '../constants';
 
@@ -33,7 +35,7 @@ describe('Replication API', () => {
         remoteSource
       });
 
-      assert.deepEqual(source, 'http://remote-couchdb.com/my%2Fdb%2Fhere');
+      assert.deepEqual(source.url, 'http://remote-couchdb.com/my%2Fdb%2Fhere');
     });
 
     it('returns local source with auth info and encoded', () => {
@@ -44,7 +46,7 @@ describe('Replication API', () => {
         localSource,
         username: 'the-user',
         password: 'password'
-      });
+      }, {origin: 'http://dev:6767'});
 
       assert.deepEqual(source.headers, {Authorization:"Basic dGhlLXVzZXI6cGFzc3dvcmQ="});
       assert.ok(/my%2Fdb/.test(source.url));
@@ -59,7 +61,7 @@ describe('Replication API', () => {
       assert.deepEqual("http://remote-couchdb.com/my%2Fdb", getTarget({
         replicationTarget: Constants.REPLICATION_TARGET.NEW_REMOTE_DATABASE,
         remoteTarget: remoteTarget
-      }));
+      }).url);
     });
 
     it('returns existing local database', () => {
@@ -94,10 +96,10 @@ describe('Replication API', () => {
         localTarget: 'my-new/db',
         username: 'the-user',
         password: 'password'
-      });
+      }, {origin: 'http://dev:5555'});
 
-      assert.ok(/the-user:password@/.test(target));
-      assert.ok(/my-new%2Fdb/.test(target));
+      assert.deepEqual(target.headers, {Authorization:"Basic dGhlLXVzZXI6cGFzc3dvcmQ="});
+      assert.ok(/my-new%2Fdb/.test(target.url));
     });
 
     it("doesn't encode username and password if it is not supplied", () => {
@@ -117,7 +119,8 @@ describe('Replication API', () => {
         localTarget: 'my-new/db'
       }, location);
 
-      assert.deepEqual("http://dev:8000/my-new%2Fdb", target);
+      assert.deepEqual("http://dev:8000/my-new%2Fdb", target.url);
+      assert.deepEqual({}, target.headers);
     });
   });
 
@@ -185,4 +188,25 @@ describe('Replication API', () => {
     });
   });
 
+  describe("encodeFullUrl", () => {
+    it("encodes db correctly", () => {
+      const url = "http://dev:5984/boom/aaaa";
+      const encodedUrl = encodeFullUrl(url);
+
+      assert.deepEqual("http://dev:5984/boom%2Faaaa", encodedUrl);
+    });
+
+  });
+
+  describe("decodeFullUrl", () => {
+
+    it("encodes db correctly", () => {
+      const url = "http://dev:5984/boom%2Faaaa";
+      const encodedUrl = decodeFullUrl(url);
+
+      assert.deepEqual("http://dev:5984/boom/aaaa", encodedUrl);
+    });
+
+  });
+
 });

http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/blob/367d4226/app/addons/replication/api.js
----------------------------------------------------------------------
diff --git a/app/addons/replication/api.js b/app/addons/replication/api.js
index 1892956..327d35d 100644
--- a/app/addons/replication/api.js
+++ b/app/addons/replication/api.js
@@ -19,19 +19,12 @@ import _ from 'lodash';
 export const encodeFullUrl = (fullUrl) => {
   if (!fullUrl) {return '';}
   const url = new URL(fullUrl);
-  if (url.username && url.password) {
-    return `${url.protocol}//${url.username}:${url.password}@${url.hostname}/${encodeURIComponent(url.pathname.slice(1))}`;
-  }
   return `${url.origin}/${encodeURIComponent(url.pathname.slice(1))}`;
 };
 
 export const decodeFullUrl = (fullUrl) => {
   if (!fullUrl) {return '';}
   const url = new URL(fullUrl);
-  if (url.username && url.password) {
-    return `${url.protocol}//${url.username}:${url.password}@${url.hostname}/${decodeURIComponent(url.pathname.slice(1))}`;
-  }
-
   return `${url.origin}/${decodeURIComponent(url.pathname.slice(1))}`;
 };
 
@@ -48,15 +41,23 @@ export const getAuthHeaders = (username, password) => {
   };
 };
 
-export const getSource = ({replicationSource, localSource, remoteSource, username, password})
=> {
+export const getSource = ({
+  replicationSource,
+  localSource,
+  remoteSource,
+  username,
+  password
+},
+{origin} = window.location) => {
+  let url = remoteSource;
   if (replicationSource === Constants.REPLICATION_SOURCE.LOCAL) {
-    return {
-      headers: getAuthHeaders(username, password),
-      url: `${window.location.origin}/${encodeURIComponent(localSource)}`
-    };
-  } else {
-    return encodeFullUrl(remoteSource);
+    url = `${origin}/${localSource}`;
   }
+
+  return {
+    headers: getAuthHeaders(username, password),
+    url: encodeFullUrl(url)
+  };
 };
 
 export const getTarget = ({
@@ -67,40 +68,25 @@ export const getTarget = ({
   username,
   password
 },
-location = window.location //this allows us to mock out window.location for our tests
+{origin} = window.location //this allows us to mock out window.location for our tests
 ) => {
-  let target = encodeFullUrl(remoteTarget);
+
   const encodedLocalTarget = encodeURIComponent(localTarget);
-  const headers = getAuthHeaders(username, password);
-
-  const {
-    origin,
-    port,
-    protocol,
-    hostname
-  } = location;
-
-  if (replicationTarget === Constants.REPLICATION_TARGET.EXISTING_LOCAL_DATABASE) {
-    target = {
-      headers: headers,
-      url: `${origin}/${encodedLocalTarget}`
-    };
-  } else if (replicationTarget === Constants.REPLICATION_TARGET.NEW_LOCAL_DATABASE) {
-
-    // check to see if we really need to send headers here or can just do the ELSE clause
in all scenarioe
-    if (replicationSource === Constants.REPLICATION_SOURCE.LOCAL) {
-      target = {
-        headers: headers,
-        url: `${origin}/${encodedLocalTarget}`
-      };
-    } else {
-      target = `${protocol}//` +
-        ((username && password) ? `${username}:${password}@` : '') +
-        `${hostname}${port ? `:${port}` : ''}/${encodedLocalTarget}`;
-    }
+  let headers = getAuthHeaders(username, password);
+  let target = `${origin}/${encodedLocalTarget}`;
+
+  if (replicationTarget === Constants.REPLICATION_TARGET.NEW_REMOTE_DATABASE ||
+        replicationTarget === Constants.REPLICATION_TARGET.EXISTING_REMOTE_DATABASE) {
+
+    const targetUrl = new URL(remoteTarget);
+    target = encodeFullUrl(remoteTarget);
+    headers = getAuthHeaders(targetUrl.username, targetUrl.password);
   }
 
-  return target;
+  return {
+    headers: headers,
+    url: target
+  };
 };
 
 export const createTarget = (replicationTarget) => {


Mime
View raw message