From 62ea37be2eb36b8195285f6c058ab889d7d08225 Mon Sep 17 00:00:00 2001 From: Daniel Silhavy Date: Fri, 16 Jun 2023 11:17:56 +0200 Subject: [PATCH] Fix/content steering host (#4213) * Add protocol to pathway cloning host url in case it is not defined. --- src/core/Utils.js | 13 +++++++++++-- src/dash/controllers/ContentSteeringController.js | 6 ++++-- test/unit/core.Utils.js | 15 +++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/core/Utils.js b/src/core/Utils.js index 59962fdd1b..5742c9bfd7 100644 --- a/src/core/Utils.js +++ b/src/core/Utils.js @@ -145,8 +145,8 @@ class Utils { /** * Compares both urls and returns a relative url (target relative to original) - * @param {string} original - * @param {string} target + * @param {string} originalUrl + * @param {string} targetUrl * @return {string|*} */ static getRelativeUrl(originalUrl, targetUrl) { @@ -187,6 +187,15 @@ class Utils { return {}; } } + + /** + * Checks for existence of "http" or "https" in a string + * @param string + * @returns {boolean} + */ + static stringHasProtocol(string) { + return (/(http(s?)):\/\//i.test(string)) + } } export default Utils; diff --git a/src/dash/controllers/ContentSteeringController.js b/src/dash/controllers/ContentSteeringController.js index 3957df0a51..da9972cc4b 100644 --- a/src/dash/controllers/ContentSteeringController.js +++ b/src/dash/controllers/ContentSteeringController.js @@ -36,10 +36,10 @@ import ContentSteeringRequest from '../vo/ContentSteeringRequest'; import ContentSteeringResponse from '../vo/ContentSteeringResponse'; import DashConstants from '../constants/DashConstants'; import MediaPlayerEvents from '../../streaming/MediaPlayerEvents'; -import Utils from '../../core/Utils'; import URLUtils from '../../streaming/utils/URLUtils'; import BaseURL from '../vo/BaseURL'; import MpdLocation from '../vo/MpdLocation'; +import Utils from '../../core/Utils.js'; const QUERY_PARAMETER_KEYS = { THROUGHPUT: '_DASH_throughput', @@ -486,9 +486,11 @@ function ContentSteeringController() { } if (reference) { const referenceUrl = new URL(reference.url); + let host = pathwayClone[DashConstants.CONTENT_STEERING_RESPONSE.URI_REPLACEMENT][DashConstants.CONTENT_STEERING_RESPONSE.HOST]; + host = Utils.stringHasProtocol(host) ? host : `${referenceUrl.protocol}//${host}`; const synthesizedElement = { - synthesizedUrl: `${pathwayClone[DashConstants.CONTENT_STEERING_RESPONSE.URI_REPLACEMENT][DashConstants.CONTENT_STEERING_RESPONSE.HOST]}${referenceUrl.pathname}`, + synthesizedUrl: `${host}${referenceUrl.pathname}`, serviceLocation: pathwayClone[DashConstants.CONTENT_STEERING_RESPONSE.ID], queryParams: pathwayClone[DashConstants.CONTENT_STEERING_RESPONSE.URI_REPLACEMENT][DashConstants.CONTENT_STEERING_RESPONSE.PARAMS], reference diff --git a/test/unit/core.Utils.js b/test/unit/core.Utils.js index dbe477a29b..7c63a631b4 100644 --- a/test/unit/core.Utils.js +++ b/test/unit/core.Utils.js @@ -94,4 +94,19 @@ describe('Utils', () => { expect(Utils.parseHttpHeaders(null)).to.be.empty; }) }) + + describe('stringHasProtocol', () => { + + it('Should return true for valid http url', () => { + expect(Utils.stringHasProtocol('http://dash.akamaized.net')).to.be.true; + }) + + it('Should return true for valid https url', () => { + expect(Utils.stringHasProtocol('https://dash.akamaized.net')).to.be.true; + }) + + it('Should return false if url has no protocol', () => { + expect(Utils.stringHasProtocol('dash.akamaized.net')).to.be.false; + }) + }) })