181 lines
6.4 KiB
JavaScript
181 lines
6.4 KiB
JavaScript
|
|
// Copyright 2007 Sergey Ilinsky (http://www.ilinsky.com)
|
||
|
|
(function () {
|
||
|
|
var oXMLHttpRequest = window.XMLHttpRequest;
|
||
|
|
var bGecko = !!window.controllers,
|
||
|
|
bIE = window.document.all && !window.opera;
|
||
|
|
function cXMLHttpRequest() {
|
||
|
|
this._object = oXMLHttpRequest ? new oXMLHttpRequest : new window.ActiveXObject('Microsoft.XMLHTTP');
|
||
|
|
};
|
||
|
|
if (bGecko && oXMLHttpRequest.wrapped)
|
||
|
|
cXMLHttpRequest.wrapped = oXMLHttpRequest.wrapped;
|
||
|
|
cXMLHttpRequest.UNSENT = 0;
|
||
|
|
cXMLHttpRequest.OPENED = 1;
|
||
|
|
cXMLHttpRequest.HEADERS_RECEIVED = 2;
|
||
|
|
cXMLHttpRequest.LOADING = 3;
|
||
|
|
cXMLHttpRequest.DONE = 4;
|
||
|
|
cXMLHttpRequest.prototype.readyState = cXMLHttpRequest.UNSENT;
|
||
|
|
cXMLHttpRequest.prototype.responseText = "";
|
||
|
|
cXMLHttpRequest.prototype.responseXML = null;
|
||
|
|
cXMLHttpRequest.prototype.status = 0;
|
||
|
|
cXMLHttpRequest.prototype.statusText = "";
|
||
|
|
cXMLHttpRequest.prototype.onreadystatechange = null;
|
||
|
|
cXMLHttpRequest.onreadystatechange = null;
|
||
|
|
cXMLHttpRequest.onopen = null;
|
||
|
|
cXMLHttpRequest.onsend = null;
|
||
|
|
cXMLHttpRequest.onabort = null;
|
||
|
|
cXMLHttpRequest.prototype.open = function(sMethod, sUrl, bAsync, sUser, sPassword) {
|
||
|
|
this._async = bAsync;
|
||
|
|
var oRequest = this,
|
||
|
|
nState = this.readyState;
|
||
|
|
if (bIE) {
|
||
|
|
var fOnUnload = function() {
|
||
|
|
if (oRequest._object.readyState != cXMLHttpRequest.DONE)
|
||
|
|
fCleanTransport(oRequest);
|
||
|
|
};
|
||
|
|
if (bAsync)
|
||
|
|
window.attachEvent("onunload", fOnUnload);
|
||
|
|
}
|
||
|
|
this._object.onreadystatechange = function() {
|
||
|
|
if (bGecko && !bAsync)
|
||
|
|
return;
|
||
|
|
oRequest.readyState = oRequest._object.readyState;
|
||
|
|
fSynchronizeValues(oRequest);
|
||
|
|
if (oRequest._aborted) {
|
||
|
|
oRequest.readyState = cXMLHttpRequest.UNSENT;
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if (oRequest.readyState == cXMLHttpRequest.DONE) {
|
||
|
|
fCleanTransport(oRequest);
|
||
|
|
if (!oRequest._object.getResponseHeader("Date")) {
|
||
|
|
oRequest._cached = oRequest._object;
|
||
|
|
cXMLHttpRequest.call(oRequest);
|
||
|
|
oRequest._object.open(sMethod, sUrl, bAsync, sUser, sPassword);
|
||
|
|
oRequest._object.setRequestHeader("If-Modified-Since", oRequest._cached.getResponseHeader("Last-Modified") || new window.Date(0));
|
||
|
|
if (oRequest._headers)
|
||
|
|
for (var sHeader in oRequest._headers)
|
||
|
|
if (typeof oRequest._headers[sHeader] == "string")
|
||
|
|
oRequest._object.setRequestHeader(sHeader, oRequest._headers[sHeader]);
|
||
|
|
oRequest._object.onreadystatechange = function() {
|
||
|
|
oRequest.readyState = oRequest._object.readyState;
|
||
|
|
if (oRequest._aborted) {
|
||
|
|
oRequest.readyState = cXMLHttpRequest.UNSENT;
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if (oRequest.readyState == cXMLHttpRequest.DONE) {
|
||
|
|
fCleanTransport(oRequest);
|
||
|
|
if (oRequest.status == 304)
|
||
|
|
oRequest._object = oRequest._cached;
|
||
|
|
delete oRequest._cached;
|
||
|
|
fSynchronizeValues(oRequest);
|
||
|
|
fReadyStateChange(oRequest);
|
||
|
|
if (bIE && bAsync)
|
||
|
|
window.detachEvent("onunload", fOnUnload);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
oRequest._object.send(null);
|
||
|
|
return;
|
||
|
|
};
|
||
|
|
if (bIE && bAsync)
|
||
|
|
window.detachEvent("onunload", fOnUnload);
|
||
|
|
}
|
||
|
|
if (nState != oRequest.readyState)
|
||
|
|
fReadyStateChange(oRequest);
|
||
|
|
|
||
|
|
nState = oRequest.readyState;
|
||
|
|
};
|
||
|
|
if (cXMLHttpRequest.onopen)
|
||
|
|
cXMLHttpRequest.onopen.apply(this, arguments);
|
||
|
|
this._object.open(sMethod, sUrl, bAsync, sUser, sPassword);
|
||
|
|
if (!bAsync && bGecko) {
|
||
|
|
this.readyState = cXMLHttpRequest.OPENED;
|
||
|
|
fReadyStateChange(this);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
cXMLHttpRequest.prototype.send = function(vData) {
|
||
|
|
|
||
|
|
if (cXMLHttpRequest.onsend)
|
||
|
|
cXMLHttpRequest.onsend.apply(this, arguments);
|
||
|
|
if (vData && vData.nodeType) {
|
||
|
|
vData = window.XMLSerializer ? new window.XMLSerializer().serializeToString(vData) : vData.xml;
|
||
|
|
if (!this._headers["Content-Type"])
|
||
|
|
|
||
|
|
this._object.setRequestHeader("Content-Type", "application/xml");
|
||
|
|
//this._object.setRequestHeader("Content-Type", "text/plain");
|
||
|
|
}
|
||
|
|
this._object.send(vData);
|
||
|
|
if (bGecko && !this._async) {
|
||
|
|
this.readyState = cXMLHttpRequest.OPENED;
|
||
|
|
fSynchronizeValues(this);
|
||
|
|
while (this.readyState < cXMLHttpRequest.DONE) {
|
||
|
|
this.readyState++;
|
||
|
|
fReadyStateChange(this);
|
||
|
|
if (this._aborted)
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
};
|
||
|
|
cXMLHttpRequest.prototype.abort = function() {
|
||
|
|
if (cXMLHttpRequest.onabort)
|
||
|
|
cXMLHttpRequest.onabort.apply(this, arguments);
|
||
|
|
if (this.readyState > cXMLHttpRequest.UNSENT)
|
||
|
|
this._aborted = true;
|
||
|
|
this._object.abort();
|
||
|
|
fCleanTransport(this);
|
||
|
|
};
|
||
|
|
cXMLHttpRequest.prototype.getAllResponseHeaders = function() {
|
||
|
|
return this._object.getAllResponseHeaders();
|
||
|
|
};
|
||
|
|
cXMLHttpRequest.prototype.getResponseHeader = function(sName) {
|
||
|
|
return this._object.getResponseHeader(sName);
|
||
|
|
};
|
||
|
|
cXMLHttpRequest.prototype.setRequestHeader = function(sName, sValue) {
|
||
|
|
if (!this._headers)
|
||
|
|
this._headers = {};
|
||
|
|
this._headers[sName] = sValue;
|
||
|
|
return this._object.setRequestHeader(sName, sValue);
|
||
|
|
};
|
||
|
|
cXMLHttpRequest.prototype.toString = function() {
|
||
|
|
return '[' + "object" + ' ' + "XMLHttpRequest" + ']';
|
||
|
|
};
|
||
|
|
cXMLHttpRequest.toString = function() {
|
||
|
|
return '[' + "XMLHttpRequest" + ']';
|
||
|
|
};
|
||
|
|
function fReadyStateChange(oRequest) {
|
||
|
|
if (oRequest.onreadystatechange)
|
||
|
|
oRequest.onreadystatechange.apply(oRequest);
|
||
|
|
if (cXMLHttpRequest.onreadystatechange)
|
||
|
|
cXMLHttpRequest.onreadystatechange.apply(oRequest);
|
||
|
|
};
|
||
|
|
function fGetDocument(oRequest) {
|
||
|
|
var oDocument = oRequest.responseXML;
|
||
|
|
if (bIE && oDocument && !oDocument.documentElement && oRequest.getResponseHeader("Content-Type").match(/[^\/]+\/[^\+]+\+xml/)) {
|
||
|
|
oDocument = new ActiveXObject('Microsoft.XMLDOM');
|
||
|
|
oDocument.loadXML(oRequest.responseText);
|
||
|
|
}
|
||
|
|
if (oDocument)
|
||
|
|
if ((bIE && oDocument.parseError != 0) || (oDocument.documentElement && oDocument.documentElement.tagName == "parsererror"))
|
||
|
|
return null;
|
||
|
|
return oDocument;
|
||
|
|
};
|
||
|
|
function fSynchronizeValues(oRequest) {
|
||
|
|
try { oRequest.responseText = oRequest._object.responseText; } catch (e) {}
|
||
|
|
try { oRequest.responseXML = fGetDocument(oRequest._object); } catch (e) {}
|
||
|
|
try { oRequest.status = oRequest._object.status; } catch (e) {}
|
||
|
|
try { oRequest.statusText = oRequest._object.statusText; } catch (e) {}
|
||
|
|
};
|
||
|
|
|
||
|
|
function fCleanTransport(oRequest) {
|
||
|
|
oRequest._object.onreadystatechange = new window.Function;
|
||
|
|
delete oRequest._headers;
|
||
|
|
};
|
||
|
|
if (!window.Function.prototype.apply) {
|
||
|
|
window.Function.prototype.apply = function(oRequest, oArguments) {
|
||
|
|
if (!oArguments)
|
||
|
|
oArguments = [];
|
||
|
|
oRequest.__func = this;
|
||
|
|
oRequest.__func(oArguments[0], oArguments[1], oArguments[2], oArguments[3], oArguments[4]);
|
||
|
|
delete oRequest.__func;
|
||
|
|
};
|
||
|
|
};
|
||
|
|
window.XMLHttpRequest = cXMLHttpRequest;
|
||
|
|
})();
|