Skip to content

Commit

Permalink
Fix #521 - properly support path-absolute BaseURLs
Browse files Browse the repository at this point in the history
  • Loading branch information
davemevans committed Jul 6, 2016
1 parent 04ba56c commit c7efeb6
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/streaming/controllers/BaseURLController.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,12 @@ function BaseURLController() {

if (b) {
if (!urlUtils.isRelative(b.url)) {
p.url = b.url;
p.serviceLocation = b.serviceLocation;
if (urlUtils.isPathAbsolute(b.url)) {
p.url = urlUtils.parseOrigin(p.url) + b.url;
} else {
p.url = b.url;
p.serviceLocation = b.serviceLocation;
}
} else {
p.url += b.url;
}
Expand Down
32 changes: 32 additions & 0 deletions src/streaming/utils/URLUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function URLUtils() {

const absUrl = /^(?:(?:[a-z]+:)?\/)?\//i;
const httpUrlRegex = /^https?:\/\//i;
const originRegex = /^(https?:\/\/[^\/]+)\/?/i;

/**
* Returns a string that contains the Base URL of a URL, if determinable.
Expand All @@ -65,6 +66,24 @@ function URLUtils() {
return base;
}

/**
* Returns a string that contains the scheme and origin of a URL,
* if determinable.
* @param {string} url - full url
* @return {string}
* @memberof module:URLUtils
* @instance
*/
function parseOrigin(url) {
const matches = url.match(originRegex);

if (matches) {
return matches[1];
}

return '';
}

/**
* Determines whether the url is relative.
* @return {bool}
Expand All @@ -77,6 +96,17 @@ function URLUtils() {
}


/**
* Determines whether the url is path-absolute.
* @return {bool}
* @param {string} url
* @memberof module:URLUtils
* @instance
*/
function isPathAbsolute(url) {
return absUrl.test(url) && url.charAt(0) === '/';
}

/**
* Determines whether the url is an HTTP-URL as defined in ISO/IEC
* 23009-1:2014 3.1.15. ie URL with a fixed scheme of http or https
Expand All @@ -91,7 +121,9 @@ function URLUtils() {

instance = {
parseBaseUrl: parseBaseUrl,
parseOrigin: parseOrigin,
isRelative: isRelative,
isPathAbsolute: isPathAbsolute,
isHTTPURL: isHTTPURL
};

Expand Down
59 changes: 57 additions & 2 deletions test/streaming.utils.URLUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ describe('URLUtils', function () {

const result = urlUtils.isHTTPURL(ftpUrl);

// Assert
expect(result).to.be.false; // jshint ignore:line
});
});
Expand All @@ -51,6 +50,32 @@ describe('URLUtils', function () {
});
});

describe('isPathAbsolute', () => {
it('should return true for a path-absolute url', () => {
const pathAbsoluteUrl = '/path/to/some/file';

const result = urlUtils.isPathAbsolute(pathAbsoluteUrl);

expect(result).to.be.true; // jshint ignore:line
});

it('should return false for a relative url', () => {
const relativeUrl = 'path/to/some/file';

const result = urlUtils.isPathAbsolute(relativeUrl);

expect(result).to.be.false; // jshint ignore:line
});

it('should return false for an absolute url', () => {
const absoluteUrl = 'https://www.example.com';

const result = urlUtils.isPathAbsolute(absoluteUrl);

expect(result).to.be.false; // jshint ignore:line
});
});

describe('parseBaseUrl', () => {
it('should return the base url of a valid url', () => {
const baseUrl = 'http://www.example.com/';
Expand Down Expand Up @@ -82,11 +107,41 @@ describe('URLUtils', function () {
});

it('should return an empty string if argument is not a url', () => {
const arg = 'skjdlkasdhflkhasdlkfhl'
const arg = 'skjdlkasdhflkhasdlkfhl';

const result = urlUtils.parseBaseUrl(arg);

expect(result).to.be.empty; // jshint ignore:line
});
});

describe('parseOrigin', () => {
it('should return the scheme and origin url of a valid url', () => {
const schemeAndOrigin = 'http://www.example.com';
const pathAbsolute = '/MPDs/index.html';
const url = schemeAndOrigin + pathAbsolute;

const result = urlUtils.parseOrigin(url);

expect(result).to.equal(schemeAndOrigin); // jshint ignore:line
});

it('should return the scheme and origin url if no relative portion', () => {
const baseUrl = 'http://www.example.com';
const slash = '/';
const url = baseUrl + slash;

const result = urlUtils.parseOrigin(url);

expect(result).to.equal(baseUrl); // jshint ignore:line
});

it('should return an empty string if argument is not a url', () => {
const arg = 'skjdlkasdhflkhasdlkfhl';

const result = urlUtils.parseOrigin(arg);

expect(result).to.be.empty; // jshint ignore:line
});
});
});

0 comments on commit c7efeb6

Please sign in to comment.