All files / dash.js/src/streaming/controllers ExtUrlQueryInfoController.js

90.12% Statements 73/81
83.6% Branches 51/61
100% Functions 14/14
90% Lines 72/80

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205                                                                                    5x     5x       136x   136x 136x   136x 136x 136x       136x 136x 14x   122x         136x 86x           136x 136x   136x   136x 28x     28x     108x   136x 35x   136x         136x 50x 50x   86x 86x       136x 50x     86x 11x   75x         15x         14x 14x   14x   14x 14x     14x   14x 28x     28x   28x 80x 80x   80x   28x   14x         16x 16x 2x   14x 10x   10x     10x 10x 10x       10x 10x 10x 10x 9x   4x 5x 4x 3x                           5x   5x       5x     1x    
/**
 * The copyright in this software is being made available under the BSD License,
 * included below. This software may be subject to other third party and contributor
 * rights, including patent rights, and no such rights are granted under this license.
 *
 * Copyright (c) 2013, Dash Industry Forum.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *  * Redistributions of source code must retain the above copyright notice, this
 *  list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice,
 *  this list of conditions and the following disclaimer in the documentation and/or
 *  other materials provided with the distribution.
 *  * Neither the name of Dash Industry Forum nor the names of its
 *  contributors may be used to endorse or promote products derived from this software
 *  without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY
 *  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 *  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 *  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 *  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 */
 
import FactoryMaker from '../../core/FactoryMaker.js';
import Utils from '../../core/Utils.js';
import DashConstants from '../../dash/constants/DashConstants.js';
import Constants from '../constants/Constants.js';
import {HTTPRequest} from '../vo/metrics/HTTPRequest.js';
import Debug from '../../core/Debug.js';
 
function ExtUrlQueryInfoController() {
    let instance,
        logger,
        mpdQueryStringInformation;
    const context = this.context;
 
    function setup() {
        logger = Debug(context).getInstance().getLogger(instance);
    }
 
    function _generateQueryParams(resultObject, manifestObject, mpdUrlQuery, parentLevelInfo, mpdElement) {
        const property = _getDescriptorTypeFromManifestObject(manifestObject, mpdElement);
 
        _generateInitialQueryString(property, parentLevelInfo.initialQueryString, resultObject, mpdUrlQuery);
        _generateFinalQueryString(property, resultObject, parentLevelInfo.finalQueryString);
 
        resultObject.sameOriginOnly = property?.ExtUrlQueryInfo?.sameOriginOnly;
        resultObject.queryParams = Utils.parseQueryParams(resultObject?.finalQueryString);
        resultObject.includeInRequests = _getIncludeInRequestFromProperty(property, parentLevelInfo.includeInRequests);
    }
 
    function _getDescriptorTypeFromManifestObject(manifestObject, mpdElement) {
        let properties = [];
        if (mpdElement === DashConstants.PERIOD) {
            properties = manifestObject[DashConstants.SUPPLEMENTAL_PROPERTY] || [];
        } else {
            properties = [
                ...(manifestObject[DashConstants.ESSENTIAL_PROPERTY] || []),
                ...(manifestObject[DashConstants.SUPPLEMENTAL_PROPERTY] || [])
            ];
        }
        return properties.find((prop) => (
            (prop.schemeIdUri === Constants.URL_QUERY_INFO_SCHEME && prop.UrlQueryInfo) ||
            (prop.schemeIdUri === Constants.EXT_URL_QUERY_INFO_SCHEME && prop.ExtUrlQueryInfo)
        ));
    }
 
    function _generateInitialQueryString(property, defaultInitialString, dst, mpdUrlQuery) {
        dst.initialQueryString = '';
        let initialQueryString = '';
 
        const queryInfo = property?.ExtUrlQueryInfo || property?.UrlQueryInfo;
 
        if (queryInfo && queryInfo.queryString) {
            Iif (defaultInitialString && defaultInitialString.length > 0) {
                initialQueryString = defaultInitialString + '&' + queryInfo.queryString;
            } else {
                initialQueryString = queryInfo.queryString;
            }
        } else {
            initialQueryString = defaultInitialString;
        }
        if (queryInfo?.useMPDUrlQuery === 'true' && mpdUrlQuery) {
            initialQueryString = initialQueryString ? initialQueryString + '&' + mpdUrlQuery : mpdUrlQuery;
        }
        dst.initialQueryString = initialQueryString;
    }
 
    // The logic for supporting templates with queryTemplate=$query:<param>$ is not in place yet, this only support queryTemplate="$querypart$".
    function _generateFinalQueryString(property, resultObject, parentQueryString) {
        if (!property) {
            resultObject.finalQueryString = parentQueryString;
            return;
        }
        const queryTemplate = property?.ExtUrlQueryInfo?.queryTemplate || property?.UrlQueryInfo?.queryTemplate || '';
        resultObject.finalQueryString = queryTemplate === DashConstants.QUERY_PART ? resultObject?.initialQueryString : '';
    }
 
    function _getIncludeInRequestFromProperty(property, parentIncludeInRequests) {
        if (!property) {
            return parentIncludeInRequests;
        }
 
        if (property.ExtUrlQueryInfo?.includeInRequests) {
            return property.ExtUrlQueryInfo.includeInRequests.split(' ');
        } else {
            return [DashConstants.SEGMENT_TYPE];
        }
    }
 
    function createFinalQueryStrings(manifest) {
        mpdQueryStringInformation = {
            origin: new URL(manifest.url).origin,
            period: []
        };
 
        const mpdUrlQuery = manifest.url.split('?')[1];
        const initialMpdObject = { initialQueryString: '', includeInRequests: [] };
 
        _generateQueryParams(mpdQueryStringInformation, manifest, mpdUrlQuery, initialMpdObject, DashConstants.MPD);
 
        manifest.Period.forEach((period) => {
            const periodObject = {
                adaptation: []
            };
            _generateQueryParams(periodObject, period, mpdUrlQuery, mpdQueryStringInformation, DashConstants.PERIOD);
 
            period.AdaptationSet.forEach((adaptationSet) => {
                const adaptationObject = {
                    representation: []
                };
                _generateQueryParams(adaptationObject, adaptationSet, mpdUrlQuery, periodObject, DashConstants.ADAPTATION_SET);
 
                adaptationSet.Representation.forEach((representation) => {
                    const representationObject = {};
                    _generateQueryParams(representationObject, representation, mpdUrlQuery, adaptationObject, DashConstants.REPRESENTATION);
 
                    adaptationObject.representation.push(representationObject);
                });
                periodObject.adaptation.push(adaptationObject);
            });
            mpdQueryStringInformation.period.push(periodObject);
        });
    }
 
    function getFinalQueryString(request) {
        try {
            if (!mpdQueryStringInformation) {
                return null;
            }
            if (request.type === HTTPRequest.MEDIA_SEGMENT_TYPE || request.type === HTTPRequest.INIT_SEGMENT_TYPE) {
                const representation = request.representation;
 
                Iif (!representation) {
                    return null;
                }
                const adaptation = representation.adaptation;
                const period = adaptation.period;
                const queryInfo = mpdQueryStringInformation
                    .period[period.index]
                    .adaptation[adaptation.index]
                    .representation[representation.index];
                const requestUrl = new URL(request.url);
                const canSendToOrigin = !queryInfo.sameOriginOnly || mpdQueryStringInformation.origin === requestUrl.origin;
                const inRequest = queryInfo.includeInRequests.includes(DashConstants.SEGMENT_TYPE);
                if (inRequest && canSendToOrigin) {
                    return queryInfo.queryParams;
                }
            } else if (request.type === HTTPRequest.MPD_TYPE) {
                const inRequest = [DashConstants.MPD_TYPE, DashConstants.MPD_PATCH_TYPE].some(r => mpdQueryStringInformation.includeInRequests.includes(r));
                if (inRequest) {
                    return mpdQueryStringInformation.queryParams;
                }
            } else Eif (request.type === HTTPRequest.CONTENT_STEERING_TYPE) {
                const inRequest = mpdQueryStringInformation.includeInRequests.includes(DashConstants.STEERING_TYPE);
                if (inRequest) {
                    return mpdQueryStringInformation.queryParams;
                }
            }
        } catch (e) {
            logger.error(e);
            return null
        }
    }
 
    setup();
 
    instance = {
        getFinalQueryString,
        createFinalQueryStrings
    };
    return instance;
}
 
ExtUrlQueryInfoController.__dashjs_factory_name = 'ExtUrlQueryInfoController';
export default FactoryMaker.getSingletonFactory(ExtUrlQueryInfoController);