Skip to content

Commit

Permalink
Add support for data and blob urls. Fix #254
Browse files Browse the repository at this point in the history
  • Loading branch information
aarranz committed Nov 17, 2016
1 parent 5d6021e commit a2407e7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
56 changes: 56 additions & 0 deletions src/js_tests/wirecloud/ioSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,38 @@
})).toBe(expected);
});

it("should do nothing when using data urls and the forceProxy option", function () {
var original = "data:text/html,lots%20of%20text...<p><a%20name%3D\"bottom\">bottom</a>?arg=val";

expect(Wirecloud.io.buildProxyURL(original, {
forceProxy: true
})).toBe(original);
});

it("should do nothing when using data urls and the forceProxy option (using URL instances)", function () {
var original = new URL("data:text/html,lots%20of%20text...<p><a%20name%3D\"bottom\">bottom</a>?arg=val");

expect(Wirecloud.io.buildProxyURL(original, {
forceProxy: true
})).toBe(original.toString());
});

it("should do nothing when using blob urls and the forceProxy option", function () {
var original = "blob:d3958f5c-0777-0845-9dcf-2cb28783acaf";

expect(Wirecloud.io.buildProxyURL(original, {
forceProxy: true
})).toBe(original);
});

it("should do nothing when using blob urls and the forceProxy option (using URL instances)", function () {
var original = new URL("blob:d3958f5c-0777-0845-9dcf-2cb28783acaf");

expect(Wirecloud.io.buildProxyURL(original, {
forceProxy: true
})).toBe(original.toString());
});

it("should support the parameters option (using URL instances)", function () {
var original = new URL("http://server:1234/path");
var expected = "https://wirecloud.example.com/cdp/http/server:1234/path?e=1&b=c";
Expand Down Expand Up @@ -141,6 +173,30 @@
})).toBe(expected);
});

it("should ignore the parameters option when using data URL instances", function () {
var original = new URL("data:text/html,lots of text...<p><a name%3D\"bottom\">bottom</a>?arg=val");

expect(Wirecloud.io.buildProxyURL(original, {
method: 'GET',
parameters: {
e: 1,
b: "c"
}
})).toBe(original.toString());
});

it("should ignore the parameters option when using blob URL instances", function () {
var original = new URL("blob:d3958f5c-0777-0845-9dcf-2cb28783acaf");

expect(Wirecloud.io.buildProxyURL(original, {
method: 'GET',
parameters: {
e: 1,
b: "c"
}
})).toBe(original.toString());
});

});

describe("makeRequest(url, options)", function () {
Expand Down
11 changes: 6 additions & 5 deletions src/wirecloud/platform/static/js/wirecloud/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@
var io = {};

io.buildProxyURL = function buildProxyURL(url, options) {
var forceProxy;
var forceProxy, inmemoryurl;

options = utils.merge({
method: 'POST',
Expand All @@ -243,21 +243,22 @@
postBody: null
}, options);

forceProxy = !!options.forceProxy;

if (!(url instanceof URL)) {
url = new URL(url, Wirecloud.location.domain + Wirecloud.URLs.ROOT_URL);
}

if (forceProxy || (options.supportsAccessControl !== true && url.origin !== Wirecloud.location.domain)) {
forceProxy = !!options.forceProxy;
inmemoryurl = ["blob:", "data:"].indexOf(url.protocol) !== -1;

if (!inmemoryurl && (forceProxy || (options.supportsAccessControl !== true && url.origin !== Wirecloud.location.domain))) {
url = Wirecloud.location.domain +
Wirecloud.URLs.PROXY.evaluate({protocol: url.protocol.slice(0, -1), domain: url.host, path: url.pathname}) + url.search;
} else {
url = url.toString();
}

// Add parameters
if (options.parameters != null && (typeof options.parameters === 'string' || typeof options.parameters === 'object')) {
if (!inmemoryurl && options.parameters != null && (typeof options.parameters === 'string' || typeof options.parameters === 'object')) {
if (['PUT', 'POST'].indexOf(options.method) === -1 || options.postBody != null) {
if (url.indexOf('?') !== -1) {
url += '&' + toQueryString(options.parameters);
Expand Down

0 comments on commit a2407e7

Please sign in to comment.