Advertisement
Guest User

Untitled

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