Advertisement
Guest User

Untitled

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