Files
shaka-player/lib/net/data_uri_plugin.js
T
Joey Parrish c934f426d8 Add new error type
This error type will be used for all internal errors, and will rely
on numeric error codes which can be easily checked by the application.

This also changes PSSH parsing to throw on errors instead of retaining
partial data.

Issue #201
b/25306826

Change-Id: I19d23d99d4ee72cb31fe5f233bac57a3a9cfc283
2015-12-04 10:26:08 -08:00

87 lines
2.8 KiB
JavaScript

/**
* @license
* Copyright 2015 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
goog.provide('shaka.net.DataUriPlugin');
goog.require('shaka.net.NetworkingEngine');
goog.require('shaka.util.Error');
goog.require('shaka.util.StringUtils');
goog.require('shaka.util.Uint8ArrayUtils');
/**
* A plugin that makes data uri requests. This plugin is auto-registered
* with the networking engine.
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs
* @param {string} uri
* @param {shaka.net.NetworkingEngine.Request} request
* @return {!Promise.<shaka.net.NetworkingEngine.Response>}
*/
shaka.net.DataUriPlugin = function(uri, request) {
return new Promise(function(resolve, reject) {
// Extract the scheme.
var parts = uri.split(':');
if (parts.length < 2 || parts[0] != 'data') {
shaka.log.error('Bad data URI, failed to parse scheme');
reject(new shaka.util.Error(
shaka.util.Error.Category.NETWORK,
shaka.util.Error.Code.MALFORMED_DATA_URI,
uri));
}
var path = parts.slice(1).join(':');
// Extract the encoding and MIME type (required but can be empty).
var infoAndData = path.split(',');
if (infoAndData.length < 2) {
shaka.log.error('Bad data URI, failed to extract encoding and MIME type');
reject(new shaka.util.Error(
shaka.util.Error.Category.NETWORK,
shaka.util.Error.Code.MALFORMED_DATA_URI,
uri));
}
var info = infoAndData[0];
var data = infoAndData.slice(1).join(',');
// Extract the encoding (optional).
var typeAndEncoding = info.split(';');
var encoding = null;
if (typeAndEncoding.length > 1)
encoding = typeAndEncoding[1];
// Convert the data.
data = window.decodeURIComponent(data);
if (encoding == 'base64') {
data = shaka.util.StringUtils.fromBase64(data);
} else if (encoding) {
shaka.log.error('Bad data URI, unknown encoding');
reject(new shaka.util.Error(
shaka.util.Error.Category.NETWORK,
shaka.util.Error.Code.UNKNOWN_DATA_URI_ENCODING,
uri));
}
data = shaka.util.Uint8ArrayUtils.fromString(data).buffer;
var response = {data: data, headers: {}};
resolve(response);
});
};
shaka.net.NetworkingEngine.registerScheme('data', shaka.net.DataUriPlugin);