Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the file scheme to joinUrls. #5989

Merged
merged 3 commits into from
Nov 28, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Change Log
### 1.40 - 2017-12-01

* Added ability to support touch event in Imagery Layers Split demo application. [#5948](https://github.com/AnalyticalGraphicsInc/cesium/pull/5948)
* Added `file:` scheme compatibility to `joinUrls`. [#5989](https://github.com/AnalyticalGraphicsInc/cesium/pull/5989)
* Fixed `Invalid asm.js: Invalid member of stdlib` console error by recompiling crunch.js with latest emscripten toolchain. [#5847](https://github.com/AnalyticalGraphicsInc/cesium/issues/5847)

### 1.39 - 2017-11-01
Expand Down
4 changes: 4 additions & 0 deletions Source/Core/joinUrls.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ define([
if (baseUri.path !== '' && baseUri.path !== '/') {
url = url.replace(/\/?$/, '/');
baseUri.path = baseUri.path.replace(/^\/?/g, '');

if (baseUri.authority === '') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly is this checking for? I'd like to better understand this change before we merge.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On line 79, a regex is used to add a slash in case a slash is missing from the end of the authority. For example:

http://host => http://host/

http://host/ => http://host/

But with the file protocol, authority can be blank for local files. So:

file:// => file:// -- incorrect, should be file:///

In this case, the regex on line 79 does not add a needed slash after an empty authority, because it sees a slash that came before the authority as satisfying the expression.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally, this test was if (url === 'file://'), but I think checking for the blank authority is potentially more generic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mramato Are you OK with this now?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, have been out on vacation. Can we just add a one line comment with your above explanation so future devs aren't confused. Once that's in, I'll merge.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

url += '/';
}
}
}

Expand Down
73 changes: 65 additions & 8 deletions Specs/Core/joinUrlsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ defineSuite([

var qualifiedUrl = "http://www.url.com";
var qualifiedUrlWithQueryString = "http://www.url.com" + "?" + queryString;
var qualifiedUrlWithPath = "http://www.url.com/some/path";
var qualifiedUrlWithPath = "http://www.url.com/some/qualified/path";

// Local files require three slashes on the front, to leave the hostname blank.
// Windows is leanient so long as a drive letter is present. Other systems are not.
var localFileUrlWithPath = "file:///mnt/local/path";
var driveUrlWithPath = "file:///c:/some/drive/path";
// Files can be read from network shares with only two slashes on the front.
var networkUrlWithPath = "file://networkShareHostname/some/remote/path";

var absolutePath = "/some/path";
var absolutePathWithQueryString = absolutePath + "?" + queryString;
Expand Down Expand Up @@ -56,7 +63,7 @@ defineSuite([
});

it('appends relative path correctly to qualified url', function() {
var result = joinUrls(qualifiedUrl, absolutePath);
var result = joinUrls(qualifiedUrl, relativePath);
expect(result).toEqual(expectedQualifiedUrl);
});

Expand Down Expand Up @@ -95,14 +102,9 @@ defineSuite([
expect(result).toEqual(qualifiedUrl);
});

it('appends qualfied url correctly to qualified url with path', function() {
var result = joinUrls(qualifiedUrl, qualifiedUrlWithPath);
expect(result).toEqual(expectedQualifiedUrl);
});

it('appends qualfied url with path correctly to qualified url', function() {
var result = joinUrls(qualifiedUrl, qualifiedUrlWithPath);
expect(result).toEqual(expectedQualifiedUrl);
expect(result).toEqual(qualifiedUrlWithPath);
});

it('appends qualfied url with path correctly to qualified url with path', function() {
Expand Down Expand Up @@ -168,4 +170,59 @@ defineSuite([
result = joinUrls(absolutePath, dataUri);
expect(result).toEqual(dataUri);
});

// File scheme tests
//
// NOTE: Following example set by 'appends absolute path correctly to qualified url with path',
// the so-called 'absolutePath' is expected to simply append to the existing path, not reset it.

it('appends absolute path correctly to local file with path', function() {
var result = joinUrls(localFileUrlWithPath, absolutePath);
expect(result).toEqual(localFileUrlWithPath + absolutePath);
});

it('appends relative path correctly to local file with path', function() {
var result = joinUrls(localFileUrlWithPath, relativePath);
expect(result).toEqual(localFileUrlWithPath + absolutePath);
});

it('appends absolute path correctly to drive letter with path', function() {
var result = joinUrls(driveUrlWithPath, absolutePath);
expect(result).toEqual(driveUrlWithPath + absolutePath);
});

it('appends relative path correctly to drive letter with path', function() {
var result = joinUrls(driveUrlWithPath, relativePath);
expect(result).toEqual(driveUrlWithPath + absolutePath);
});

it('appends absolute path correctly to network share with path', function() {
var result = joinUrls(networkUrlWithPath, absolutePath);
expect(result).toEqual(networkUrlWithPath + absolutePath);
});

it('appends relative path correctly to network share with path', function() {
var result = joinUrls(networkUrlWithPath, relativePath);
expect(result).toEqual(networkUrlWithPath + absolutePath);
});

it('works when the file scheme appears in the second path', function() {
var result = joinUrls(driveUrlWithPath, localFileUrlWithPath);
expect(result).toEqual(localFileUrlWithPath);
});

it('works when a drive letter appears in the second path', function() {
var result = joinUrls(localFileUrlWithPath, driveUrlWithPath);
expect(result).toEqual(driveUrlWithPath);
});

it('works when the first is a network file and the second is a local file', function() {
var result = joinUrls(networkUrlWithPath, localFileUrlWithPath);
expect(result).toEqual(localFileUrlWithPath);
});

it('works when the first is a local file and the second is a network file', function() {
var result = joinUrls(localFileUrlWithPath, networkUrlWithPath);
expect(result).toEqual(networkUrlWithPath);
});
});