Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.27 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) {
  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. this_ref.onload(event);
  156. for(callback of this_ref._loadcallbacks) {
  157. callback.call(this_ref, event);
  158. }
  159.  
  160. this_ref.onloadend(event);
  161. for(callback of this_ref._loadendcallbacks) {
  162. callback.call(this_ref, event);
  163. }
  164. }
  165. else {
  166. this_ref.readyState = xhr.readyState;
  167. this_ref.onreadystatechange.call(this_ref, event);
  168. }
  169. }
  170.  
  171. xhr.onerror = function(exception) {
  172. this_ref.onerror(exception);
  173. for(callback of this_ref._errorcallbacks) {
  174. callback.call(this_ref, exception);
  175. }
  176.  
  177. this_ref.onloadend(exception);
  178. for(callback of this_ref._loadendcallbacks) {
  179. callback.call(this_ref, exception);
  180. }
  181. }
  182.  
  183. xhr.onprogress = function(event) {
  184. this_ref.onprogress(event);
  185. for(callback of this_ref._progresscallbacks) {
  186. callback.call(this_ref, event);
  187. }
  188. }
  189.  
  190. xhr.ontimeout = function(event) {
  191. this_ref.ontimeout(event);
  192. for(callback of this_ref._timeoutcallbacks) {
  193. callback.call(this_ref, event);
  194. }
  195. }
  196.  
  197. xhr.onabort = function(event) {
  198. this_ref.onabort(event);
  199. for(callback of this_ref._abortcallbacks) {
  200. callback.call(this_ref, event);
  201. }
  202. }
  203.  
  204. xhr.open(this_ref._method, this_ref._url, this_ref._async, this_ref._user, this_ref._password);
  205.  
  206. let num_headers = this_ref._headers.length;
  207. for(let index = 0; index < num_headers; index += 1) {
  208. xhr.setRequestHeader(this._headers[index][0], this._headers[index][1]);
  209. }
  210.  
  211. if(this_ref._async) {
  212. xhr.timeout = this_ref.timeout;
  213. }
  214.  
  215. xhr.send(post_data);
  216.  
  217. if(!this_ref._async) {
  218. this_ref.status = xhr.status;
  219. this_ref.statusText = xhr.statusText;
  220. this_ref.response = xhr.response;
  221. this_ref.responseText = xhr.responseText;
  222. this_ref.responseXML = xhr.responseXML;
  223. this_ref.responseURL = xhr.responseURL;
  224. }
  225.  
  226. } else {
  227.  
  228. var link = document.createElement("a");
  229. link.href = this._url;
  230.  
  231. let url = link.protocol+"//"+link.host+link.pathname+link.search+link.hash;
  232.  
  233. if(!this_ref._async) {
  234. console.log("XHR attempting to do a synchronous request but webrtc doesn't support it");
  235. debugger;
  236. }
  237.  
  238. let has_content_type = false;
  239.  
  240. let headers = [];
  241. let num_headers = this_ref._headers.length;
  242. for(let index = 0; index < num_headers; index += 1) {
  243. headers.push(this._headers[index][0] + ": ", this._headers[index][1]);
  244. if(this._headers[index][0].toLowerCase() == "content-type") {
  245. has_content_type = false;
  246. }
  247. }
  248.  
  249. if(has_content_type == false) {
  250. if(typeof(post_data) === "string") {
  251. headers.push("Content-Type: application/text");
  252. }
  253. }
  254.  
  255. var event = { target: this_ref, currentTarget: this_ref, type: "loadstart" };
  256. for(callback of this_ref._loadstartcallbacks) {
  257. callback.call(this_ref);
  258. }
  259. this_ref.onloadstart(event);
  260.  
  261. requester.request(this_ref._method, url, post_data, headers).then(function(http) {
  262.  
  263. this_ref.readyState = 1;
  264. this_ref.onreadystatechange.call(this_ref, event);
  265. this_ref.readyState = 2;
  266. this_ref.onreadystatechange.call(this_ref, event);
  267.  
  268. this_ref.status = http.status;
  269. this_ref.statusText = http.statusText;
  270.  
  271. this_ref.readyState = 3;
  272. this_ref.onreadystatechange.call(this_ref, event);
  273.  
  274. let is_xml = false;
  275. if(this_ref._overrideMime) {
  276. if(this_ref._overrideMime === "application/xml" || this_ref._overrideMime == "text/xml") {
  277. is_xml = true;
  278. }
  279. } else {
  280. let headers = http.getAllResponseHeaders().toLowerCase();
  281.  
  282. if(headers.includes("content-type: application/xml") || headers.includes("content-type: text/xml")) {
  283. is_xml = true;
  284. }
  285. }
  286.  
  287. if(is_xml) {
  288. parser = new DOMParser();
  289. this_ref.responseXML = parser.parseFromString(this_ref.responseText, "text/xml");
  290. }
  291.  
  292. this_ref.getAllResponseHeaders = function() { http.getAllResponseHeaders(); };
  293. this_ref.getResponseHeader = function(get_header)
  294. {
  295. let header_str = http.getAllResponseHeaders().toLowerCase();
  296. let headers = header_str.split("\n");
  297.  
  298. let test_header = get_header.toLowerCase();
  299.  
  300. for(header of headers) {
  301. let data = header.split(":");
  302. if(data[0] == test_header) {
  303. return data[1].trim();
  304. }
  305. }
  306.  
  307. return null;
  308. };
  309.  
  310. this_ref.response = http.response;
  311. this_ref.responseText = http.responseText;
  312. this_ref.responseURL = this_ref._url;
  313.  
  314. this_ref.readyState = 4;
  315. this_ref.onreadystatechange.call(this_ref, event);
  316.  
  317. event.type = "progress";
  318. event.lengthComputable = true;
  319. event.loaded = this_ref.response.size;
  320. event.total = this_ref.response.size;
  321. this_ref.onprogress(event);
  322. for(callback of this_ref._loadcallbacks) {
  323. callback.call(this_ref, event);
  324. }
  325.  
  326. event.type = "load";
  327. this_ref.onload(event);
  328. for(callback of this_ref._loadcallbacks) {
  329. callback.call(this_ref, event);
  330. }
  331.  
  332. event.type = "loadend";
  333. this_ref.onloadend(event);
  334. for(callback of this_ref._loadendcallbacks) {
  335. callback.call(this_ref, event);
  336. }
  337. }, function(exception) {
  338.  
  339. this_ref.onerror(exception);
  340. for(callback of this_ref._errorcallbacks) {
  341. callback.call(this_ref, exception);
  342. }
  343.  
  344. this_ref.onloadend(exception);
  345. for(callback of this_ref._loadendcallbacks) {
  346. callback.call(this_ref, exception);
  347. }
  348. });
  349. }
  350. };
  351. };
  352.  
  353. XMLHttpRequest.prototype = {
  354. 'UNSENT': 0,
  355. 'OPENED': 1,
  356. 'HEADERS_RECIEVED': 2,
  357. 'LOADING': 3,
  358. 'DONE': 4,
  359. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement