Guest User

Untitled

a guest
Feb 19th, 2018
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.30 KB | None | 0 0
  1.  
  2. var XMLHttpRequestInternal = XMLHttpRequest;
  3. XMLHttpRequest = function() {
  4. this.onreadystatechange = function() { };
  5. this.readyState = 0;
  6. this.response = null;
  7. this.responseText = null;
  8. this.responseType = "";
  9. this.responseURL = null;
  10. this.responseXML = null;
  11. this.status = 0;
  12. this.statusText = "";
  13. this.timeout = 0;
  14. this._loadcallbacks = [];
  15. this._errorcallbacks = [];
  16. this._progresscallbacks = [];
  17. this._loadstartcallbacks = [];
  18. this._loadendcallbacks = [];
  19. this._timeoutcallbacks = [];
  20. this._abortcallbacks = [];
  21. this._headers = [];
  22. this.onload = function(e) {
  23.  
  24. }
  25.  
  26. this.onerror = function(e) {
  27.  
  28. }
  29.  
  30. this.onprogress = function(e) {
  31.  
  32. }
  33.  
  34. this.onloadstart = function(e) {
  35.  
  36. }
  37.  
  38. this.onloadend = function(e) {
  39.  
  40. }
  41.  
  42. this.ontimeout = function(e) {
  43.  
  44. }
  45.  
  46. this.onabort = function(e) {
  47.  
  48. }
  49.  
  50. this.abort = function() {
  51. if(this._aborted == false) {
  52. this._aborted = true;
  53. }
  54. };
  55.  
  56. this.addEventListener = function(event, callback) {
  57. if(event === "load") {
  58. this._loadcallbacks.push(callback);
  59. } else if(event == "error") {
  60. this._errorcallbacks.push(callback);
  61. } else if(event == "progress") {
  62. this._progresscallbacks.push(callback);
  63. } else if(event == "loadstart") {
  64. this._loadstartcallbacks.push(callback);
  65. } else if(event == "loadend") {
  66. this._loadendcallbacks.push(callback);
  67. } else if(event == "timeout") {
  68. this._timeoutcallbacks.push(callback);
  69. } else if(event == "abort") {
  70. this._abortcallbacks.push(callback);
  71. } else {
  72. console.log("Unknown event");
  73. debugger;
  74. }
  75. };
  76.  
  77. this.getAllResponseHeaders = function() {
  78. return null;
  79. };
  80.  
  81. this.setRequestHeader = function(header, value) {
  82. this._headers.push([header, value]);
  83. }
  84.  
  85. this.open = function (method, url, async, user, password) {
  86. this._method = method;
  87. this._url = url;
  88.  
  89. if(async != null) {
  90. this._async = async;
  91. } else {
  92. this._async = true;
  93. }
  94.  
  95. this._user = user;
  96. this._password = password;
  97. };
  98.  
  99. this.overrideMimeType = function(mime_type) {
  100. this._overrideMime = mime_type;
  101. };
  102.  
  103. this.send = function(post_data) {
  104. if(!this._url) {
  105. return;
  106. }
  107.  
  108. let this_ref = this;
  109. if((this._url.includes("vod.dori") == false && this._url.includes("video.dori") == false) || this._async == false) {
  110. let xhr = new XMLHttpRequestInternal();
  111. xhr.responseType = this.responseType;
  112.  
  113. if(this._overrideMime) {
  114. xhr.overrideMimeType(this._overrideMime);
  115. }
  116.  
  117. this.abort = function() {
  118. xhr.abort();
  119. }
  120.  
  121. this_ref.getAllResponseHeaders = function() {
  122. return xhr.getAllResponseHeaders();
  123. }
  124.  
  125. this_ref.getResponseHeader = function(name) {
  126. return xhr.getResponseHeader(name);
  127. }
  128.  
  129. var event = { target: this_ref };
  130. for(callback of this_ref._loadstartcallbacks) {
  131. callback.call(this_ref);
  132. }
  133. this_ref.onloadstart(event);
  134.  
  135. xhr.onload = function(event) {
  136. if(xhr.readyState == 4) {
  137. this_ref.readyState = 1;
  138. this_ref.onreadystatechange.call(this_ref, event);
  139. this_ref.readyState = 2;
  140. this_ref.onreadystatechange.call(this_ref, event);
  141.  
  142. this_ref.status = xhr.status;
  143. this_ref.statusText = xhr.statusText;
  144.  
  145. this_ref.readyState = 3;
  146. this_ref.onreadystatechange.call(this_ref, event);
  147.  
  148. this_ref.response = xhr.response;
  149. this_ref.responseText = xhr.responseText;
  150. this_ref.responseXML = xhr.responseXML;
  151. this_ref.responseURL = xhr.responseURL;
  152.  
  153. this_ref.readyState = 4;
  154. this_ref.onreadystatechange.call(this_ref, event);
  155.  
  156. this_ref.onload(event);
  157. for(callback of this_ref._loadcallbacks) {
  158. callback.call(this_ref, event);
  159. }
  160.  
  161. this_ref.onloadend(event);
  162. for(callback of this_ref._loadendcallbacks) {
  163. callback.call(this_ref, event);
  164. }
  165. }
  166. else {
  167. this_ref.readyState = xhr.readyState;
  168. this_ref.onreadystatechange.call(this_ref, event);
  169. }
  170. }
  171.  
  172. xhr.onerror = function(exception) {
  173. this_ref.onerror(exception);
  174. for(callback of this_ref._errorcallbacks) {
  175. callback.call(this_ref, exception);
  176. }
  177.  
  178. this_ref.onloadend(exception);
  179. for(callback of this_ref._loadendcallbacks) {
  180. callback.call(this_ref, exception);
  181. }
  182. }
  183.  
  184. xhr.onprogress = function(event) {
  185. this_ref.onprogress(event);
  186. for(callback of this_ref._progresscallbacks) {
  187. callback.call(this_ref, event);
  188. }
  189. }
  190.  
  191. xhr.ontimeout = function(event) {
  192. this_ref.ontimeout(event);
  193. for(callback of this_ref._timeoutcallbacks) {
  194. callback.call(this_ref, event);
  195. }
  196. }
  197.  
  198. xhr.onabort = function(event) {
  199. this_ref.onabort(event);
  200. for(callback of this_ref._abortcallbacks) {
  201. callback.call(this_ref, event);
  202. }
  203. }
  204.  
  205. xhr.open(this_ref._method, this_ref._url, this_ref._async, this_ref._user, this_ref._password);
  206.  
  207. let num_headers = this_ref._headers.length;
  208. for(let index = 0; index < num_headers; index += 1) {
  209. xhr.setRequestHeader(this._headers[index][0], this._headers[index][1]);
  210. }
  211.  
  212. if(this_ref._async) {
  213. xhr.timeout = this_ref.timeout;
  214. }
  215.  
  216. xhr.send(post_data);
  217.  
  218. if(!this_ref._async) {
  219. this_ref.status = xhr.status;
  220. this_ref.statusText = xhr.statusText;
  221. this_ref.response = xhr.response;
  222. this_ref.responseText = xhr.responseText;
  223. this_ref.responseXML = xhr.responseXML;
  224. this_ref.responseURL = xhr.responseURL;
  225. }
  226.  
  227. } else {
  228.  
  229. var link = document.createElement("a");
  230. link.href = this._url;
  231.  
  232. let url = link.protocol+"//"+link.host+link.pathname+link.search+link.hash;
  233.  
  234. if(!this_ref._async) {
  235. console.log("XHR attempting to do a synchronous request but webrtc doesn't support it");
  236. debugger;
  237. }
  238.  
  239. let has_content_type = false;
  240.  
  241. let headers = [];
  242. let num_headers = this_ref._headers.length;
  243. for(let index = 0; index < num_headers; index += 1) {
  244. headers.push(this._headers[index][0] + ": ", this._headers[index][1]);
  245. if(this._headers[index][0].toLowerCase() == "content-type") {
  246. has_content_type = false;
  247. }
  248. }
  249.  
  250. if(has_content_type == false) {
  251. if(typeof(post_data) === "string") {
  252. headers.push("Content-Type: application/text");
  253. }
  254. }
  255.  
  256. var event = { target: this_ref, currentTarget: this_ref, type: "loadstart" };
  257. for(callback of this_ref._loadstartcallbacks) {
  258. callback.call(this_ref);
  259. }
  260. this_ref.onloadstart(event);
  261.  
  262. requester.request(this_ref._method, url, post_data, headers).then(function(http) {
  263.  
  264. this_ref.readyState = 1;
  265. this_ref.onreadystatechange.call(this_ref, event);
  266. this_ref.readyState = 2;
  267. this_ref.onreadystatechange.call(this_ref, event);
  268.  
  269. this_ref.status = http.status;
  270. this_ref.statusText = http.statusText;
  271.  
  272. this_ref.readyState = 3;
  273. this_ref.onreadystatechange.call(this_ref, event);
  274.  
  275. let is_xml = false;
  276. if(this_ref._overrideMime) {
  277. if(this_ref._overrideMime === "application/xml" || this_ref._overrideMime == "text/xml") {
  278. is_xml = true;
  279. }
  280. } else {
  281. let headers = http.getAllResponseHeaders().toLowerCase();
  282.  
  283. if(headers.includes("content-type: application/xml") || headers.includes("content-type: text/xml")) {
  284. is_xml = true;
  285. }
  286. }
  287.  
  288. if(is_xml) {
  289. parser = new DOMParser();
  290. this_ref.responseXML = parser.parseFromString(this_ref.responseText, "text/xml");
  291. }
  292.  
  293. this_ref.getAllResponseHeaders = function() { http.getAllResponseHeaders(); };
  294. this_ref.getResponseHeader = function(get_header)
  295. {
  296. let header_str = http.getAllResponseHeaders().toLowerCase();
  297. let headers = header_str.split("\n");
  298.  
  299. let test_header = get_header.toLowerCase();
  300.  
  301. for(header of headers) {
  302. let data = header.split(":");
  303. if(data[0] == test_header) {
  304. return data[1].trim();
  305. }
  306. }
  307.  
  308. return null;
  309. };
  310.  
  311. this_ref.response = http.response;
  312. this_ref.responseText = http.responseText;
  313. this_ref.responseURL = this_ref._url;
  314.  
  315. this_ref.readyState = 4;
  316. this_ref.onreadystatechange.call(this_ref, event);
  317.  
  318. event.type = "progress";
  319. event.lengthComputable = true;
  320. event.loaded = this_ref.response.size;
  321. event.total = this_ref.response.size;
  322. this_ref.onprogress(event);
  323. for(callback of this_ref._loadcallbacks) {
  324. callback.call(this_ref, event);
  325. }
  326.  
  327. event.type = "load";
  328. this_ref.onload(event);
  329. for(callback of this_ref._loadcallbacks) {
  330. callback.call(this_ref, event);
  331. }
  332.  
  333. event.type = "loadend";
  334. this_ref.onloadend(event);
  335. for(callback of this_ref._loadendcallbacks) {
  336. callback.call(this_ref, event);
  337. }
  338. }, function(exception) {
  339.  
  340. this_ref.onerror(exception);
  341. for(callback of this_ref._errorcallbacks) {
  342. callback.call(this_ref, exception);
  343. }
  344.  
  345. this_ref.onloadend(exception);
  346. for(callback of this_ref._loadendcallbacks) {
  347. callback.call(this_ref, exception);
  348. }
  349. });
  350. }
  351. };
  352. };
  353.  
  354. XMLHttpRequest.prototype = {
  355. 'UNSENT': 0,
  356. 'OPENED': 1,
  357. 'HEADERS_RECIEVED': 2,
  358. 'LOADING': 3,
  359. 'DONE': 4,
  360. };
Add Comment
Please, Sign In to add comment