Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var XMLHttpRequestInternal = XMLHttpRequest;
- var XMLHttpRequestInternalPrototype = XMLHttpRequest.prototype;
- var _Event = function Event(type, bubbles, cancelable, target) {
- this.type = type;
- this.bubbles = bubbles;
- this.cancelable = cancelable;
- this.target = target;
- this.currentTarget = target;
- };
- _Event.prototype = {
- stopPropagation: function () {},
- preventDefault: function () {
- this.defaultPrevented = true;
- }
- };
- /*
- Used to set the statusText property of an xhr object
- */
- var httpStatusCodes = {
- 100: "Continue",
- 101: "Switching Protocols",
- 200: "OK",
- 201: "Created",
- 202: "Accepted",
- 203: "Non-Authoritative Information",
- 204: "No Content",
- 205: "Reset Content",
- 206: "Partial Content",
- 300: "Multiple Choice",
- 301: "Moved Permanently",
- 302: "Found",
- 303: "See Other",
- 304: "Not Modified",
- 305: "Use Proxy",
- 307: "Temporary Redirect",
- 400: "Bad Request",
- 401: "Unauthorized",
- 402: "Payment Required",
- 403: "Forbidden",
- 404: "Not Found",
- 405: "Method Not Allowed",
- 406: "Not Acceptable",
- 407: "Proxy Authentication Required",
- 408: "Request Timeout",
- 409: "Conflict",
- 410: "Gone",
- 411: "Length Required",
- 412: "Precondition Failed",
- 413: "Request Entity Too Large",
- 414: "Request-URI Too Long",
- 415: "Unsupported Media Type",
- 416: "Requested Range Not Satisfiable",
- 417: "Expectation Failed",
- 422: "Unprocessable Entity",
- 500: "Internal Server Error",
- 501: "Not Implemented",
- 502: "Bad Gateway",
- 503: "Service Unavailable",
- 504: "Gateway Timeout",
- 505: "HTTP Version Not Supported"
- };
- /*
- Cross-browser XML parsing. Used to turn
- XML responses into Document objects
- Borrowed from JSpec
- */
- function parseXML(text) {
- var xmlDoc;
- if (typeof DOMParser != "undefined") {
- var parser = new DOMParser();
- xmlDoc = parser.parseFromString(text, "text/xml");
- } else {
- xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
- xmlDoc.async = "false";
- xmlDoc.loadXML(text);
- }
- return xmlDoc;
- }
- /*
- Without mocking, the native XMLHttpRequest object will throw
- an error when attempting to set these headers. We match this behavior.
- */
- var unsafeHeaders = {
- "Accept-Charset": true,
- "Accept-Encoding": true,
- "Connection": true,
- "Content-Length": true,
- "Cookie": true,
- "Cookie2": true,
- "Content-Transfer-Encoding": true,
- "Date": true,
- "Expect": true,
- "Host": true,
- "Keep-Alive": true,
- "Referer": true,
- "TE": true,
- "Trailer": true,
- "Transfer-Encoding": true,
- "Upgrade": true,
- "User-Agent": true,
- "Via": true
- };
- /*
- Adds an "event" onto the xhr object
- that just calls the same-named method. This is
- in case a library adds callbacks for these events.
- */
- function _addEventListener(eventName, xhr){
- xhr.addEventListener(eventName, function (event) {
- var listener = xhr["on" + eventName];
- if (listener && typeof listener == "function") {
- listener.call(event.target, event);
- }
- });
- }
- function EventedObject() {
- this._eventListeners = {};
- var events = ["loadstart", "progress", "load", "abort", "loadend"];
- for (var i = events.length - 1; i >= 0; i--) {
- _addEventListener(events[i], this);
- }
- };
- EventedObject.prototype = {
- /*
- Duplicates the behavior of native XMLHttpRequest's addEventListener function
- */
- addEventListener: function addEventListener(event, listener) {
- this._eventListeners[event] = this._eventListeners[event] || [];
- this._eventListeners[event].push(listener);
- },
- /*
- Duplicates the behavior of native XMLHttpRequest's removeEventListener function
- */
- removeEventListener: function removeEventListener(event, listener) {
- var listeners = this._eventListeners[event] || [];
- for (var i = 0, l = listeners.length; i < l; ++i) {
- if (listeners[i] == listener) {
- return listeners.splice(i, 1);
- }
- }
- },
- /*
- Duplicates the behavior of native XMLHttpRequest's dispatchEvent function
- */
- dispatchEvent: function dispatchEvent(event) {
- var type = event.type;
- var listeners = this._eventListeners[type] || [];
- for (var i = 0; i < listeners.length; i++) {
- if (typeof listeners[i] == "function") {
- listeners[i].call(this, event);
- } else {
- listeners[i].handleEvent(event);
- }
- }
- return !!event.defaultPrevented;
- },
- /*
- Triggers an `onprogress` event with the given parameters.
- */
- _progress: function _progress(lengthComputable, loaded, total) {
- var event = new _Event('progress');
- event.target = this;
- event.lengthComputable = lengthComputable;
- event.loaded = loaded;
- event.total = total;
- this.dispatchEvent(event);
- }
- }
- /*
- Constructor for a window.XMLHttpRequest
- */
- XMLHttpRequest = function() {
- EventedObject.call(this);
- this.readyState = XMLHttpRequest.UNSENT;
- this.requestHeaders = {};
- this.requestBody = null;
- this.status = 0;
- this.statusText = "";
- this.upload = new EventedObject();
- }
- XMLHttpRequest.prototype = new EventedObject();
- // These status codes are available on the native XMLHttpRequest
- // object, so we match that here in case a library is relying on them.
- XMLHttpRequest.UNSENT = 0;
- XMLHttpRequest.OPENED = 1;
- XMLHttpRequest.HEADERS_RECEIVED = 2;
- XMLHttpRequest.LOADING = 3;
- XMLHttpRequest.DONE = 4;
- var XMLHttpRequestProto = {
- UNSENT: 0,
- OPENED: 1,
- HEADERS_RECEIVED: 2,
- LOADING: 3,
- DONE: 4,
- async: true,
- withCredentials: false,
- timeout: 0,
- /*
- Duplicates the behavior of native XMLHttpRequest's open function
- */
- open: function open(method, url, async, username, password) {
- this.method = method;
- this.url = url;
- this.async = typeof async == "boolean" ? async : true;
- this.username = username;
- this.password = password;
- this.responseText = null;
- this.responseXML = null;
- this.requestHeaders = {};
- this.sendFlag = false;
- this._readyStateChange(XMLHttpRequest.OPENED);
- },
- /*
- Duplicates the behavior of native XMLHttpRequest's setRequestHeader function
- */
- setRequestHeader: function setRequestHeader(header, value) {
- verifyState(this);
- if (unsafeHeaders[header] || /^(Sec-|Proxy-)/.test(header)) {
- throw new Error("Refused to set unsafe header \"" + header + "\"");
- }
- if (this.requestHeaders[header]) {
- this.requestHeaders[header] += "," + value;
- } else {
- this.requestHeaders[header] = value;
- }
- },
- /*
- Duplicates the behavior of native XMLHttpRequest's send function
- */
- send: function send(data) {
- verifyState(this);
- if (!/^(get|head)$/i.test(this.method)) {
- var hasContentTypeHeader = false
- Object.keys(this.requestHeaders).forEach(function (key) {
- if (key.toLowerCase() === 'content-type') {
- hasContentTypeHeader = true;
- }
- });
- if (!hasContentTypeHeader && !(data || '').toString().match('FormData')) {
- this.requestHeaders["Content-Type"] = "text/plain;charset=UTF-8";
- }
- this.requestBody = data;
- }
- this.errorFlag = false;
- this.sendFlag = this.async;
- this._readyStateChange(XMLHttpRequest.OPENED);
- if (typeof this.onSend == "function") {
- this.onSend(this);
- }
- this.dispatchEvent(new _Event("loadstart", false, false, this));
- },
- onSend: function onSend() {
- let this_ref = this;
- var link = document.createElement("a");
- link.href = this.url;
- let url = link.protocol+"//"+link.host+link.pathname+link.search+link.hash;
- if((url.includes("vod.dori") == false && url.includes("video.dori") == false) || this.async == false) {
- let xhr = new XMLHttpRequestInternal();
- if(this.responseType) {
- xhr.responseType = this.responseType;
- }
- if(this.forceMimeType) {
- xhr.overrideMimeType(this.forceMimeType);
- }
- if(this.async) {
- xhr.onload = function(event) {
- if(xhr.readyState == 4) {
- this_ref.respond(xhr.status, xhr.getAllResponseHeaders(), xhr.response);
- }
- }
- }
- xhr.onerror = function(exception) {
- this_ref.dispatchEvent(exception);
- }
- xhr.onprogress = function(event) {
- this_ref.dispatchEvent(event);
- }
- xhr.ontimeout = function(event) {
- this_ref.dispatchEvent(event);
- }
- xhr.onabort = function(event) {
- this_ref.dispatchEvent(event);
- }
- xhr.open(this_ref.method, this_ref.url, this_ref.async, this_ref.user, this_ref.password);
- Object.keys(this.requestHeaders).forEach(function (key) {
- xhr.setRequestHeader(key, this.requestHeaders[key]);
- });
- if(this_ref.async) {
- xhr.timeout = this_ref.timeout;
- }
- xhr.send(this.requestBody);
- if(!this_ref.async) {
- this_ref.respond(xhr.status, xhr.getAllResponseHeaders(), xhr.response);
- }
- } else {
- if(!this_ref.async) {
- console.log("XHR attempting to do a synchronous request but webrtc doesn't support it");
- debugger;
- }
- url = url + "&good=1";
- let has_content_type = false;
- let headers = [];
- Object.keys(this.requestHeaders).forEach(function (key) {
- headers.push(key + ": " + this_ref.requestHeaders[key]);
- if(this_ref.requestHeaders[key].toLowerCase() == "content-type") {
- has_content_type = false;
- }
- });
- if(has_content_type == false) {
- if(typeof(this.requestBody) === "string") {
- headers.push("Content-Type: application/text");
- }
- }
- requester.request(this_ref.method, url, this.requestBody, headers).then(function(http) {
- this_ref.respond(http.status, http.getAllResponseHeaders(), http.responseText);
- }, function(exception) {
- this_ref.dispatchEvent(new _Event("error", false, false, this));
- this_ref.dispatchEvent(new _Event("loadend", false, false, this));
- });
- }
- },
- /*
- Duplicates the behavior of native XMLHttpRequest's abort function
- */
- abort: function abort() {
- this.aborted = true;
- this.responseText = null;
- this.errorFlag = true;
- this.requestHeaders = {};
- if (this.readyState > XMLHttpRequest.UNSENT && this.sendFlag) {
- this._readyStateChange(XMLHttpRequest.DONE);
- this.sendFlag = false;
- }
- this.readyState = XMLHttpRequest.UNSENT;
- this.dispatchEvent(new _Event("abort", false, false, this));
- if (typeof this.onerror === "function") {
- this.onerror();
- }
- },
- /*
- Duplicates the behavior of native XMLHttpRequest's getResponseHeader function
- */
- getResponseHeader: function getResponseHeader(header) {
- if (this.readyState < XMLHttpRequest.HEADERS_RECEIVED) {
- return null;
- }
- if (/^Set-Cookie2?$/i.test(header)) {
- return null;
- }
- header = header.toLowerCase();
- for (var h in this.responseHeaders) {
- if (h.toLowerCase() == header) {
- return this.responseHeaders[h];
- }
- }
- return null;
- },
- /*
- Duplicates the behavior of native XMLHttpRequest's getAllResponseHeaders function
- */
- getAllResponseHeaders: function getAllResponseHeaders() {
- if (this.readyState < XMLHttpRequest.HEADERS_RECEIVED) {
- return "";
- }
- var headers = "";
- for (var header in this.responseHeaders) {
- if (this.responseHeaders.hasOwnProperty(header) && !/^Set-Cookie2?$/i.test(header)) {
- headers += header + ": " + this.responseHeaders[header] + "\r\n";
- }
- }
- return headers;
- },
- /*
- Duplicates the behavior of native XMLHttpRequest's overrideMimeType function
- */
- overrideMimeType: function overrideMimeType(mimeType) {
- if (typeof mimeType === "string") {
- this.forceMimeType = mimeType.toLowerCase();
- }
- },
- /*
- Places a XMLHttpRequest object into the passed
- state.
- */
- _readyStateChange: function _readyStateChange(state) {
- this.readyState = state;
- if (typeof this.onreadystatechange == "function") {
- this.onreadystatechange(new _Event("readystatechange"));
- }
- this.dispatchEvent(new _Event("readystatechange"));
- if (this.readyState == XMLHttpRequest.DONE) {
- this.dispatchEvent(new _Event("load", false, false, this));
- this.dispatchEvent(new _Event("loadend", false, false, this));
- }
- },
- /*
- Sets theXMLHttpRequest object's response headers and
- places the object into readyState 2
- */
- _setResponseHeaders: function _setResponseHeaders(headers) {
- this.responseHeaders = {};
- for (var header in headers) {
- if (headers.hasOwnProperty(header)) {
- this.responseHeaders[header] = headers[header];
- }
- }
- if (this.forceMimeType) {
- this.responseHeaders['Content-Type'] = this.forceMimeType;
- }
- if (this.async) {
- this._readyStateChange(XMLHttpRequest.HEADERS_RECEIVED);
- } else {
- this.readyState = XMLHttpRequest.HEADERS_RECEIVED;
- }
- },
- /*
- Sets the XMLHttpRequest object's response body and
- if body text is XML, sets responseXML to parsed document
- object
- */
- _setResponseBody: function _setResponseBody(body) {
- verifyRequestSent(this);
- verifyHeadersReceived(this);
- verifyResponseBodyType(body);
- var chunkSize = this.chunkSize || 10;
- var index = 0;
- this.responseText = "";
- do {
- if (this.async) {
- this._readyStateChange(XMLHttpRequest.LOADING);
- }
- this.responseText += body.substring(index, index + chunkSize);
- index += chunkSize;
- } while (index < body.length);
- var type = this.getResponseHeader("Content-Type");
- if (this.responseText && (!type || /(text\/xml)|(application\/xml)|(\+xml)/.test(type))) {
- try {
- this.responseXML = parseXML(this.responseText);
- } catch (e) {
- // Unable to parse XML - no biggie
- }
- }
- if (this.async) {
- this._readyStateChange(XMLHttpRequest.DONE);
- } else {
- this.readyState = XMLHttpRequest.DONE;
- }
- },
- /*
- Forces a response on to the XMLHttpRequest object.
- This is the public API for faking responses. This function
- takes a number status, headers object, and string body:
- ```
- xhr.respond(404, {Content-Type: 'text/plain'}, "Sorry. This object was not found.")
- ```
- */
- respond: function respond(status, headers, body) {
- this._setResponseHeaders(headers || {});
- this.status = typeof status == "number" ? status : 200;
- this.statusText = httpStatusCodes[this.status];
- this._setResponseBody(body || "");
- }
- };
- for (var property in XMLHttpRequestProto) {
- XMLHttpRequest.prototype[property] = XMLHttpRequestProto[property];
- }
- function verifyState(xhr) {
- if (xhr.readyState !== XMLHttpRequest.OPENED) {
- throw new Error("INVALID_STATE_ERR");
- }
- if (xhr.sendFlag) {
- throw new Error("INVALID_STATE_ERR");
- }
- }
- function verifyRequestSent(xhr) {
- if (xhr.readyState == XMLHttpRequest.DONE) {
- throw new Error("Request done");
- }
- }
- function verifyHeadersReceived(xhr) {
- if (xhr.async && xhr.readyState != XMLHttpRequest.HEADERS_RECEIVED) {
- throw new Error("No headers received");
- }
- }
- function verifyResponseBodyType(body) {
- if (typeof body != "string") {
- var error = new Error("Attempted to respond to XMLHttpRequest with " +
- body + ", which is not a string.");
- error.name = "InvalidBodyException";
- throw error;
- }
- }
- for(let index = 0; index < 1000; index += 1) {
- let xmlhttp = new XMLHttpRequest(),
- method = 'GET',
- url = 'http://localhost:8000/index.html';
- xmlhttp.open(method, url, true);
- xmlhttp.onload = function () {
- console.log(xmlhttp.responseText);
- };
- xmlhttp.onerror = function () {
- console.log(this._url);
- };
- xmlhttp.send("test" + index);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement