Source: lib/net/data_uri_plugin.js

  1. /**
  2. * @license
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. goog.provide('shaka.net.DataUriPlugin');
  18. goog.require('shaka.log');
  19. goog.require('shaka.net.NetworkingEngine');
  20. goog.require('shaka.util.Error');
  21. goog.require('shaka.util.StringUtils');
  22. goog.require('shaka.util.Uint8ArrayUtils');
  23. /**
  24. * @namespace
  25. * @summary A networking plugin to handle data URIs.
  26. * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs
  27. * @param {string} uri
  28. * @param {shakaExtern.Request} request
  29. * @return {!Promise.<shakaExtern.Response>}
  30. * @export
  31. */
  32. shaka.net.DataUriPlugin = function(uri, request) {
  33. return new Promise(function(resolve, reject) {
  34. var parsed = shaka.net.DataUriPlugin.parse(uri);
  35. /** @type {shakaExtern.Response} */
  36. var response = {
  37. uri: uri,
  38. data: parsed.data,
  39. headers: {
  40. 'content-type': parsed.contentType
  41. }
  42. };
  43. resolve(response);
  44. });
  45. };
  46. /**
  47. * @param {string} uri
  48. * @return {{data: ArrayBuffer, contentType: string}}
  49. */
  50. shaka.net.DataUriPlugin.parse = function(uri) {
  51. // Extract the scheme.
  52. var parts = uri.split(':');
  53. if (parts.length < 2 || parts[0] != 'data') {
  54. shaka.log.error('Bad data URI, failed to parse scheme');
  55. throw new shaka.util.Error(
  56. shaka.util.Error.Severity.CRITICAL,
  57. shaka.util.Error.Category.NETWORK,
  58. shaka.util.Error.Code.MALFORMED_DATA_URI,
  59. uri);
  60. }
  61. var path = parts.slice(1).join(':');
  62. // Extract the encoding and MIME type (required but can be empty).
  63. var infoAndData = path.split(',');
  64. if (infoAndData.length < 2) {
  65. shaka.log.error('Bad data URI, failed to extract encoding and MIME type');
  66. throw new shaka.util.Error(
  67. shaka.util.Error.Severity.CRITICAL,
  68. shaka.util.Error.Category.NETWORK,
  69. shaka.util.Error.Code.MALFORMED_DATA_URI,
  70. uri);
  71. }
  72. var info = infoAndData[0];
  73. var dataStr = window.decodeURIComponent(infoAndData.slice(1).join(','));
  74. // Extract the encoding (optional).
  75. var typeAndEncoding = info.split(';');
  76. var encoding = null;
  77. if (typeAndEncoding.length > 1)
  78. encoding = typeAndEncoding[1];
  79. // Convert the data.
  80. /** @type {ArrayBuffer} */
  81. var data;
  82. if (encoding == 'base64') {
  83. data = shaka.util.Uint8ArrayUtils.fromBase64(dataStr).buffer;
  84. } else if (encoding) {
  85. shaka.log.error('Bad data URI, unknown encoding');
  86. throw new shaka.util.Error(
  87. shaka.util.Error.Severity.CRITICAL,
  88. shaka.util.Error.Category.NETWORK,
  89. shaka.util.Error.Code.UNKNOWN_DATA_URI_ENCODING,
  90. uri);
  91. } else {
  92. data = shaka.util.StringUtils.toUTF8(dataStr);
  93. }
  94. return {data: data, contentType: typeAndEncoding[0]};
  95. };
  96. shaka.net.NetworkingEngine.registerScheme('data', shaka.net.DataUriPlugin);