PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/Ip/Internal/Core/assets/js/easyXDM/cors/index.html

https://gitlab.com/x33n/ImpressPages
HTML | 214 lines | 193 code | 21 blank | 0 comment | 0 complexity | 434ef438945549c0d7c409af673d2a1a MD5 | raw file
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>easyXDM cross-domain XHMLHttpRequest provider</title>
  5. <script type="text/javascript" src="../easyXDM.debug.js">
  6. // This should be changed so that it points to the minified version before use in production.
  7. </script>
  8. <script type="text/javascript">
  9. // Update to point to your copy
  10. easyXDM.DomHelper.requiresJSON("../json2.js");
  11. </script>
  12. <script type="text/javascript">
  13. /*
  14. * This is a CORS (Cross-Origin Resource Sharing) and AJAX enabled endpoint for easyXDM.
  15. * The ACL code is adapted from pmxdr (http://github.com/eligrey/pmxdr/) by Eli Grey (http://eligrey.com/)
  16. *
  17. */
  18. // From http://peter.michaux.ca/articles/feature-detection-state-of-the-art-browser-scripting
  19. function isHostMethod(object, property){
  20. var t = typeof object[property];
  21. return t == 'function' ||
  22. (!!(t == 'object' && object[property])) ||
  23. t == 'unknown';
  24. }
  25. /**
  26. * Creates a cross-browser XMLHttpRequest object
  27. * @return {XMLHttpRequest} A XMLHttpRequest object.
  28. */
  29. var getXhr = (function(){
  30. if (isHostMethod(window, "XMLHttpRequest")) {
  31. return function(){
  32. return new XMLHttpRequest();
  33. };
  34. }
  35. else {
  36. var item = (function(){
  37. var list = ["Microsoft", "Msxml2", "Msxml3"], i = list.length;
  38. while (i--) {
  39. try {
  40. item = list[i] + ".XMLHTTP";
  41. var obj = new ActiveXObject(item);
  42. return item;
  43. }
  44. catch (e) {
  45. }
  46. }
  47. }());
  48. return function(){
  49. return new ActiveXObject(item);
  50. };
  51. }
  52. }());
  53. // this file is by default set up to use Access Control - this means that it will use the headers set by the server to decide whether or not to allow the call to return
  54. var useAccessControl = true;
  55. // always trusted origins, can be exact strings or regular expressions
  56. var alwaysTrustedOrigins = [(/\.?easyxdm\.net/), (/xdm1/)];
  57. // instantiate a new easyXDM object which will handle the request
  58. var remote = new easyXDM.Rpc({
  59. local: "../name.html",
  60. swf: "../easyxdm.swf"
  61. }, {
  62. local: {
  63. // define the exposed method
  64. request: function(config, success, error){
  65. // apply default values if not set
  66. easyXDM.apply(config, {
  67. method: "POST",
  68. headers: {
  69. "Content-Type": "application/x-www-form-urlencoded",
  70. "X-Requested-With": "XMLHttpRequest"
  71. },
  72. success: Function.prototype,
  73. error: function(msg){
  74. throw new Error(msg);
  75. },
  76. data: {},
  77. timeout: 10 * 1000
  78. }, true);
  79. // set the CORS request header
  80. // only if there is no XHR2 features
  81. if (!window.XMLHttpRequest || !('withCredentials' in (new XMLHttpRequest))) {
  82. config.headers.Origin = remote.origin;
  83. }
  84. var isPOST = config.method == "POST";
  85. // convert the data into a format we can send to the server
  86. var pairs = [];
  87. for (var key in config.data) {
  88. if (config.data.hasOwnProperty(key)) {
  89. pairs.push(encodeURIComponent(key) + "=" + encodeURIComponent(config.data[key]));
  90. }
  91. }
  92. var data = pairs.join("&");
  93. // create the XMLHttpRequest object
  94. var req = getXhr();
  95. var url = !isPOST && data
  96. ? config.url + (~config.url.indexOf('?') ? '&' : '?') + data
  97. : config.url;
  98. req.open(config.method, url, true);
  99. // apply the request headers
  100. for (var prop in config.headers) {
  101. if (config.headers.hasOwnProperty(prop) && config.headers[prop]) {
  102. req.setRequestHeader(prop, config.headers[prop]);
  103. }
  104. }
  105. // set a timeout
  106. var timeout;
  107. timeout = setTimeout(function(){
  108. // reset the handler
  109. req.onreadystatechange = Function.prototype;
  110. req.abort();
  111. req = null;
  112. error({
  113. message: "timeout after " + config.timeout + " second",
  114. status: 0,
  115. data: null,
  116. toString: function(){
  117. return this.message + " Status: " + this.status;
  118. }
  119. }, null);
  120. }, config.timeout);
  121. // check if this origin should always be trusted
  122. var alwaysTrusted = false, i = alwaysTrustedOrigins.length;
  123. while (i-- && !alwaysTrusted) {
  124. if (alwaysTrustedOrigins[i] instanceof RegExp) {
  125. alwaysTrusted = alwaysTrustedOrigins[i].test(remote.origin);
  126. }
  127. else if (typeof alwaysTrustedOrigins[i] == "string") {
  128. alwaysTrusted = (remote.origin === alwaysTrustedOrigins[i]);
  129. }
  130. }
  131. // define the onreadystate handler
  132. req.onreadystatechange = function(){
  133. if (req.readyState == 4) {
  134. clearTimeout(timeout);
  135. // parse the response headers
  136. var rawHeaders = req.getAllResponseHeaders(), headers = {}, headers_lowercase = {}, reHeader = /([\w-_]+):\s+(.*)$/gm, m;
  137. while ((m = reHeader.exec(rawHeaders))) {
  138. headers_lowercase[m[1].toLowerCase()] = headers[m[1]] = m[2];
  139. }
  140. if (req.status < 200 || req.status >= 300) {
  141. if (useAccessControl) {
  142. error("INVALID_STATUS_CODE");
  143. }
  144. else {
  145. error("INVALID_STATUS_CODE", {
  146. status: req.status,
  147. data: req.responseText
  148. });
  149. }
  150. }
  151. else {
  152. var errorMessage;
  153. if (useAccessControl) {
  154. // normalize the valuse access controls
  155. var aclAllowedOrigin = (headers_lowercase["access-control-allow-origin"] || "").replace(/\s/g, "");
  156. var aclAllowedMethods = (headers_lowercase["access-control-allow-methods"] || "").replace(/\s/g, "");
  157. // determine if origin is trusted
  158. if (alwaysTrusted || aclAllowedOrigin == "*" || aclAllowedOrigin.indexOf(remote.origin) != -1) {
  159. // determine if the request method was allowed
  160. if (aclAllowedMethods && aclAllowedMethods != "*" && aclAllowedMethods.indexOf(config.method) == -1) {
  161. errorMessage = "DISALLOWED_REQUEST_METHOD";
  162. }
  163. }
  164. else {
  165. errorMessage = "DISALLOWED_ORIGIN";
  166. }
  167. }
  168. if (errorMessage) {
  169. error(errorMessage);
  170. }
  171. else {
  172. success({
  173. data: req.responseText,
  174. status: req.status,
  175. headers: headers
  176. });
  177. }
  178. }
  179. // reset the handler
  180. req.onreadystatechange = Function.prototype;
  181. req = null;
  182. }
  183. };
  184. // issue the request
  185. req.send(isPOST ? data : "");
  186. }
  187. }
  188. });
  189. </script>
  190. </head>
  191. <body>
  192. </body>
  193. </html>