From 067f1aa6a56636a015278a6c8ba9d80d446fe65b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Arranz?= Date: Thu, 17 Nov 2016 12:08:56 +0100 Subject: [PATCH] Support URLs using hashes. Fix #255 --- src/js_tests/wirecloud/ioSpec.js | 66 +++++++++++++++++++ .../platform/static/js/wirecloud/io.js | 14 ++-- 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/src/js_tests/wirecloud/ioSpec.js b/src/js_tests/wirecloud/ioSpec.js index 3e4015e130..6733f36180 100644 --- a/src/js_tests/wirecloud/ioSpec.js +++ b/src/js_tests/wirecloud/ioSpec.js @@ -197,6 +197,72 @@ })).toBe(original.toString()); }); + it("should maintain the hash part for proxied URLs", function () { + var original = "http://server:1234/path?q=1#id"; + var expected = "https://wirecloud.example.com/cdp/http/server:1234/path?q=1#id"; + + expect(Wirecloud.io.buildProxyURL(original)).toBe(expected); + }); + + it("should maintain the hash part for proxied URLs when providing parameters", function () { + var original = "http://server:1234/path#id"; + var expected = "https://wirecloud.example.com/cdp/http/server:1234/path?q=1#id"; + + expect(Wirecloud.io.buildProxyURL(original, { + method: 'GET', + parameters: { + q: 1 + } + })).toBe(expected); + }); + + it("should maintain the \"hash\" part when using blob URLs", function () { + // In fact, blob urls don't support hashes. This test checks + // buildProxyURL don't process data as a hash component + var original = "blob:d3958f5c-0777-0845-9dcf-2cb28783acaf#id"; + + expect(Wirecloud.io.buildProxyURL(original, { + method: 'GET' + })).toBe(original); + }); + + it("should maintain the \"hash\" part when using blob URLs and providing parameters", function () { + // In fact, blob urls don't support hashes. This test checks + // buildProxyURL don't process data as a hash component + var original = "blob:d3958f5c-0777-0845-9dcf-2cb28783acaf#id"; + + expect(Wirecloud.io.buildProxyURL(original, { + method: 'GET', + parameters: { + e: 1, + b: "c" + } + })).toBe(original); + }); + + it("should maintain the \"hash\" part when using data URLs", function () { + // In fact, data urls don't support hashes. This test checks + // buildProxyURL don't process data as a hash component + var original = "data:text/html,lots%20of%20text...

bottom?arg=val#id"; + + expect(Wirecloud.io.buildProxyURL(original, { + method: 'GET' + })).toBe(original); + }); + + it("should maintain the \"hash\" part when using data URLs and providing parameters", function () { + // In fact, data urls don't support hashes. This test checks + // buildProxyURL don't process data as a hash component + var original = "data:text/html,lots%20of%20text...

bottom?arg=val#id"; + + expect(Wirecloud.io.buildProxyURL(original, { + method: 'GET', + parameters: { + e: 1, + b: "c" + } + })).toBe(original); + }); }); describe("makeRequest(url, options)", function () { diff --git a/src/wirecloud/platform/static/js/wirecloud/io.js b/src/wirecloud/platform/static/js/wirecloud/io.js index 3650d76f95..2c8c1ed9f2 100644 --- a/src/wirecloud/platform/static/js/wirecloud/io.js +++ b/src/wirecloud/platform/static/js/wirecloud/io.js @@ -232,7 +232,7 @@ var io = {}; io.buildProxyURL = function buildProxyURL(url, options) { - var forceProxy, inmemoryurl; + var forceProxy, hash; options = utils.merge({ method: 'POST', @@ -248,9 +248,13 @@ } forceProxy = !!options.forceProxy; - inmemoryurl = ["blob:", "data:"].indexOf(url.protocol) !== -1; + if (["blob:", "data:"].indexOf(url.protocol) !== -1) { + return url.toString(); + } - if (!inmemoryurl && (forceProxy || (options.supportsAccessControl !== true && url.origin !== Wirecloud.location.domain))) { + hash = url.hash; + url.hash = ''; + if (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 { @@ -258,7 +262,7 @@ } // Add parameters - if (!inmemoryurl && options.parameters != null && (typeof options.parameters === 'string' || typeof options.parameters === 'object')) { + if (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); @@ -268,7 +272,7 @@ } } - return url; + return url + hash; }; io.makeRequest = function makeRequest(url, options) {