TrustedServers.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import Uri from "../ThirdParty/Uri.js";
  2. import defined from "./defined.js";
  3. import DeveloperError from "./DeveloperError.js";
  4. /**
  5. * A singleton that contains all of the servers that are trusted. Credentials will be sent with
  6. * any requests to these servers.
  7. *
  8. * @namespace TrustedServers
  9. *
  10. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  11. */
  12. var TrustedServers = {};
  13. var _servers = {};
  14. /**
  15. * Adds a trusted server to the registry
  16. *
  17. * @param {String} host The host to be added.
  18. * @param {Number} port The port used to access the host.
  19. *
  20. * @example
  21. * // Add a trusted server
  22. * TrustedServers.add('my.server.com', 80);
  23. */
  24. TrustedServers.add = function (host, port) {
  25. //>>includeStart('debug', pragmas.debug);
  26. if (!defined(host)) {
  27. throw new DeveloperError("host is required.");
  28. }
  29. if (!defined(port) || port <= 0) {
  30. throw new DeveloperError("port is required to be greater than 0.");
  31. }
  32. //>>includeEnd('debug');
  33. var authority = host.toLowerCase() + ":" + port;
  34. if (!defined(_servers[authority])) {
  35. _servers[authority] = true;
  36. }
  37. };
  38. /**
  39. * Removes a trusted server from the registry
  40. *
  41. * @param {String} host The host to be removed.
  42. * @param {Number} port The port used to access the host.
  43. *
  44. * @example
  45. * // Remove a trusted server
  46. * TrustedServers.remove('my.server.com', 80);
  47. */
  48. TrustedServers.remove = function (host, port) {
  49. //>>includeStart('debug', pragmas.debug);
  50. if (!defined(host)) {
  51. throw new DeveloperError("host is required.");
  52. }
  53. if (!defined(port) || port <= 0) {
  54. throw new DeveloperError("port is required to be greater than 0.");
  55. }
  56. //>>includeEnd('debug');
  57. var authority = host.toLowerCase() + ":" + port;
  58. if (defined(_servers[authority])) {
  59. delete _servers[authority];
  60. }
  61. };
  62. function getAuthority(url) {
  63. var uri = new Uri(url);
  64. uri.normalize();
  65. // Removes username:password@ so we just have host[:port]
  66. var authority = uri.getAuthority();
  67. if (!defined(authority)) {
  68. return undefined; // Relative URL
  69. }
  70. if (authority.indexOf("@") !== -1) {
  71. var parts = authority.split("@");
  72. authority = parts[1];
  73. }
  74. // If the port is missing add one based on the scheme
  75. if (authority.indexOf(":") === -1) {
  76. var scheme = uri.getScheme();
  77. if (!defined(scheme)) {
  78. scheme = window.location.protocol;
  79. scheme = scheme.substring(0, scheme.length - 1);
  80. }
  81. if (scheme === "http") {
  82. authority += ":80";
  83. } else if (scheme === "https") {
  84. authority += ":443";
  85. } else {
  86. return undefined;
  87. }
  88. }
  89. return authority;
  90. }
  91. /**
  92. * Tests whether a server is trusted or not. The server must have been added with the port if it is included in the url.
  93. *
  94. * @param {String} url The url to be tested against the trusted list
  95. *
  96. * @returns {boolean} Returns true if url is trusted, false otherwise.
  97. *
  98. * @example
  99. * // Add server
  100. * TrustedServers.add('my.server.com', 81);
  101. *
  102. * // Check if server is trusted
  103. * if (TrustedServers.contains('https://my.server.com:81/path/to/file.png')) {
  104. * // my.server.com:81 is trusted
  105. * }
  106. * if (TrustedServers.contains('https://my.server.com/path/to/file.png')) {
  107. * // my.server.com isn't trusted
  108. * }
  109. */
  110. TrustedServers.contains = function (url) {
  111. //>>includeStart('debug', pragmas.debug);
  112. if (!defined(url)) {
  113. throw new DeveloperError("url is required.");
  114. }
  115. //>>includeEnd('debug');
  116. var authority = getAuthority(url);
  117. if (defined(authority) && defined(_servers[authority])) {
  118. return true;
  119. }
  120. return false;
  121. };
  122. /**
  123. * Clears the registry
  124. *
  125. * @example
  126. * // Remove a trusted server
  127. * TrustedServers.clear();
  128. */
  129. TrustedServers.clear = function () {
  130. _servers = {};
  131. };
  132. export default TrustedServers;