[76] | 1 | // XMLHttpRequest.js Copyright (C) 2010 Sergey Ilinsky (http://www.ilinsky.com) |
---|
| 2 | // |
---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 4 | // you may not use this file except in compliance with the License. |
---|
| 5 | // You may obtain a copy of the License at |
---|
| 6 | // |
---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 8 | // |
---|
| 9 | // Unless required by applicable law or agreed to in writing, software |
---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 12 | // See the License for the specific language governing permissions and |
---|
| 13 | // limitations under the License. |
---|
| 14 | |
---|
| 15 | /** |
---|
| 16 | * @requires OpenLayers/Request.js |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | (function () { |
---|
| 20 | |
---|
| 21 | // Save reference to earlier defined object implementation (if any) |
---|
| 22 | var oXMLHttpRequest = window.XMLHttpRequest; |
---|
| 23 | |
---|
| 24 | // Define on browser type |
---|
| 25 | var bGecko = !!window.controllers, |
---|
| 26 | bIE = window.document.all && !window.opera, |
---|
| 27 | bIE7 = bIE && window.navigator.userAgent.match(/MSIE ([\.0-9]+)/) && RegExp.$1 == 7; |
---|
| 28 | |
---|
| 29 | // Constructor |
---|
| 30 | function cXMLHttpRequest() { |
---|
| 31 | this._object = oXMLHttpRequest && !bIE7 ? new oXMLHttpRequest : new window.ActiveXObject("Microsoft.XMLHTTP"); |
---|
| 32 | this._listeners = []; |
---|
| 33 | }; |
---|
| 34 | |
---|
| 35 | // BUGFIX: Firefox with Firebug installed would break pages if not executed |
---|
| 36 | if (bGecko && oXMLHttpRequest.wrapped) |
---|
| 37 | cXMLHttpRequest.wrapped = oXMLHttpRequest.wrapped; |
---|
| 38 | |
---|
| 39 | // Constants |
---|
| 40 | cXMLHttpRequest.UNSENT = 0; |
---|
| 41 | cXMLHttpRequest.OPENED = 1; |
---|
| 42 | cXMLHttpRequest.HEADERS_RECEIVED = 2; |
---|
| 43 | cXMLHttpRequest.LOADING = 3; |
---|
| 44 | cXMLHttpRequest.DONE = 4; |
---|
| 45 | |
---|
| 46 | // Public Properties |
---|
| 47 | cXMLHttpRequest.prototype.readyState = cXMLHttpRequest.UNSENT; |
---|
| 48 | cXMLHttpRequest.prototype.responseText = ''; |
---|
| 49 | cXMLHttpRequest.prototype.responseXML = null; |
---|
| 50 | cXMLHttpRequest.prototype.status = 0; |
---|
| 51 | cXMLHttpRequest.prototype.statusText = ''; |
---|
| 52 | |
---|
| 53 | // Instance-level Events Handlers |
---|
| 54 | cXMLHttpRequest.prototype.onreadystatechange = null; |
---|
| 55 | |
---|
| 56 | // Class-level Events Handlers |
---|
| 57 | cXMLHttpRequest.onreadystatechange = null; |
---|
| 58 | cXMLHttpRequest.onopen = null; |
---|
| 59 | cXMLHttpRequest.onsend = null; |
---|
| 60 | cXMLHttpRequest.onabort = null; |
---|
| 61 | |
---|
| 62 | // Public Methods |
---|
| 63 | cXMLHttpRequest.prototype.open = function(sMethod, sUrl, bAsync, sUser, sPassword) { |
---|
| 64 | // Delete headers, required when object is reused |
---|
| 65 | delete this._headers; |
---|
| 66 | |
---|
| 67 | // When bAsync parameter value is omitted, use true as default |
---|
| 68 | if (arguments.length < 3) |
---|
| 69 | bAsync = true; |
---|
| 70 | |
---|
| 71 | // Save async parameter for fixing Gecko bug with missing readystatechange in synchronous requests |
---|
| 72 | this._async = bAsync; |
---|
| 73 | |
---|
| 74 | // Set the onreadystatechange handler |
---|
| 75 | var oRequest = this, |
---|
| 76 | nState = this.readyState, |
---|
| 77 | fOnUnload; |
---|
| 78 | |
---|
| 79 | // BUGFIX: IE - memory leak on page unload (inter-page leak) |
---|
| 80 | if (bIE && bAsync) { |
---|
| 81 | fOnUnload = function() { |
---|
| 82 | if (nState != cXMLHttpRequest.DONE) { |
---|
| 83 | fCleanTransport(oRequest); |
---|
| 84 | // Safe to abort here since onreadystatechange handler removed |
---|
| 85 | oRequest.abort(); |
---|
| 86 | } |
---|
| 87 | }; |
---|
| 88 | window.attachEvent("onunload", fOnUnload); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | // Add method sniffer |
---|
| 92 | if (cXMLHttpRequest.onopen) |
---|
| 93 | cXMLHttpRequest.onopen.apply(this, arguments); |
---|
| 94 | |
---|
| 95 | if (arguments.length > 4) |
---|
| 96 | this._object.open(sMethod, sUrl, bAsync, sUser, sPassword); |
---|
| 97 | else |
---|
| 98 | if (arguments.length > 3) |
---|
| 99 | this._object.open(sMethod, sUrl, bAsync, sUser); |
---|
| 100 | else |
---|
| 101 | this._object.open(sMethod, sUrl, bAsync); |
---|
| 102 | |
---|
| 103 | if (!bGecko && !bIE) { |
---|
| 104 | this.readyState = cXMLHttpRequest.OPENED; |
---|
| 105 | fReadyStateChange(this); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | this._object.onreadystatechange = function() { |
---|
| 109 | if (bGecko && !bAsync) |
---|
| 110 | return; |
---|
| 111 | |
---|
| 112 | // Synchronize state |
---|
| 113 | oRequest.readyState = oRequest._object.readyState; |
---|
| 114 | |
---|
| 115 | // |
---|
| 116 | fSynchronizeValues(oRequest); |
---|
| 117 | |
---|
| 118 | // BUGFIX: Firefox fires unnecessary DONE when aborting |
---|
| 119 | if (oRequest._aborted) { |
---|
| 120 | // Reset readyState to UNSENT |
---|
| 121 | oRequest.readyState = cXMLHttpRequest.UNSENT; |
---|
| 122 | |
---|
| 123 | // Return now |
---|
| 124 | return; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | if (oRequest.readyState == cXMLHttpRequest.DONE) { |
---|
| 128 | // |
---|
| 129 | fCleanTransport(oRequest); |
---|
| 130 | // Uncomment this block if you need a fix for IE cache |
---|
| 131 | /* |
---|
| 132 | // BUGFIX: IE - cache issue |
---|
| 133 | if (!oRequest._object.getResponseHeader("Date")) { |
---|
| 134 | // Save object to cache |
---|
| 135 | oRequest._cached = oRequest._object; |
---|
| 136 | |
---|
| 137 | // Instantiate a new transport object |
---|
| 138 | cXMLHttpRequest.call(oRequest); |
---|
| 139 | |
---|
| 140 | // Re-send request |
---|
| 141 | if (sUser) { |
---|
| 142 | if (sPassword) |
---|
| 143 | oRequest._object.open(sMethod, sUrl, bAsync, sUser, sPassword); |
---|
| 144 | else |
---|
| 145 | oRequest._object.open(sMethod, sUrl, bAsync, sUser); |
---|
| 146 | } |
---|
| 147 | else |
---|
| 148 | oRequest._object.open(sMethod, sUrl, bAsync); |
---|
| 149 | oRequest._object.setRequestHeader("If-Modified-Since", oRequest._cached.getResponseHeader("Last-Modified") || new window.Date(0)); |
---|
| 150 | // Copy headers set |
---|
| 151 | if (oRequest._headers) |
---|
| 152 | for (var sHeader in oRequest._headers) |
---|
| 153 | if (typeof oRequest._headers[sHeader] == "string") // Some frameworks prototype objects with functions |
---|
| 154 | oRequest._object.setRequestHeader(sHeader, oRequest._headers[sHeader]); |
---|
| 155 | |
---|
| 156 | oRequest._object.onreadystatechange = function() { |
---|
| 157 | // Synchronize state |
---|
| 158 | oRequest.readyState = oRequest._object.readyState; |
---|
| 159 | |
---|
| 160 | if (oRequest._aborted) { |
---|
| 161 | // |
---|
| 162 | oRequest.readyState = cXMLHttpRequest.UNSENT; |
---|
| 163 | |
---|
| 164 | // Return |
---|
| 165 | return; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | if (oRequest.readyState == cXMLHttpRequest.DONE) { |
---|
| 169 | // Clean Object |
---|
| 170 | fCleanTransport(oRequest); |
---|
| 171 | |
---|
| 172 | // get cached request |
---|
| 173 | if (oRequest.status == 304) |
---|
| 174 | oRequest._object = oRequest._cached; |
---|
| 175 | |
---|
| 176 | // |
---|
| 177 | delete oRequest._cached; |
---|
| 178 | |
---|
| 179 | // |
---|
| 180 | fSynchronizeValues(oRequest); |
---|
| 181 | |
---|
| 182 | // |
---|
| 183 | fReadyStateChange(oRequest); |
---|
| 184 | |
---|
| 185 | // BUGFIX: IE - memory leak in interrupted |
---|
| 186 | if (bIE && bAsync) |
---|
| 187 | window.detachEvent("onunload", fOnUnload); |
---|
| 188 | } |
---|
| 189 | }; |
---|
| 190 | oRequest._object.send(null); |
---|
| 191 | |
---|
| 192 | // Return now - wait until re-sent request is finished |
---|
| 193 | return; |
---|
| 194 | }; |
---|
| 195 | */ |
---|
| 196 | // BUGFIX: IE - memory leak in interrupted |
---|
| 197 | if (bIE && bAsync) |
---|
| 198 | window.detachEvent("onunload", fOnUnload); |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | // BUGFIX: Some browsers (Internet Explorer, Gecko) fire OPEN readystate twice |
---|
| 202 | if (nState != oRequest.readyState) |
---|
| 203 | fReadyStateChange(oRequest); |
---|
| 204 | |
---|
| 205 | nState = oRequest.readyState; |
---|
| 206 | } |
---|
| 207 | }; |
---|
| 208 | cXMLHttpRequest.prototype.send = function(vData) { |
---|
| 209 | // Add method sniffer |
---|
| 210 | if (cXMLHttpRequest.onsend) |
---|
| 211 | cXMLHttpRequest.onsend.apply(this, arguments); |
---|
| 212 | |
---|
| 213 | // BUGFIX: Safari - fails sending documents created/modified dynamically, so an explicit serialization required |
---|
| 214 | // BUGFIX: IE - rewrites any custom mime-type to "text/xml" in case an XMLNode is sent |
---|
| 215 | // BUGFIX: Gecko - fails sending Element (this is up to the implementation either to standard) |
---|
| 216 | if (vData && vData.nodeType) { |
---|
| 217 | vData = window.XMLSerializer ? new window.XMLSerializer().serializeToString(vData) : vData.xml; |
---|
| 218 | if (!this._headers["Content-Type"]) |
---|
| 219 | this._object.setRequestHeader("Content-Type", "application/xml"); |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | this._object.send(vData); |
---|
| 223 | |
---|
| 224 | // BUGFIX: Gecko - missing readystatechange calls in synchronous requests |
---|
| 225 | if (bGecko && !this._async) { |
---|
| 226 | this.readyState = cXMLHttpRequest.OPENED; |
---|
| 227 | |
---|
| 228 | // Synchronize state |
---|
| 229 | fSynchronizeValues(this); |
---|
| 230 | |
---|
| 231 | // Simulate missing states |
---|
| 232 | while (this.readyState < cXMLHttpRequest.DONE) { |
---|
| 233 | this.readyState++; |
---|
| 234 | fReadyStateChange(this); |
---|
| 235 | // Check if we are aborted |
---|
| 236 | if (this._aborted) |
---|
| 237 | return; |
---|
| 238 | } |
---|
| 239 | } |
---|
| 240 | }; |
---|
| 241 | cXMLHttpRequest.prototype.abort = function() { |
---|
| 242 | // Add method sniffer |
---|
| 243 | if (cXMLHttpRequest.onabort) |
---|
| 244 | cXMLHttpRequest.onabort.apply(this, arguments); |
---|
| 245 | |
---|
| 246 | // BUGFIX: Gecko - unnecessary DONE when aborting |
---|
| 247 | if (this.readyState > cXMLHttpRequest.UNSENT) |
---|
| 248 | this._aborted = true; |
---|
| 249 | |
---|
| 250 | this._object.abort(); |
---|
| 251 | |
---|
| 252 | // BUGFIX: IE - memory leak |
---|
| 253 | fCleanTransport(this); |
---|
| 254 | }; |
---|
| 255 | cXMLHttpRequest.prototype.getAllResponseHeaders = function() { |
---|
| 256 | return this._object.getAllResponseHeaders(); |
---|
| 257 | }; |
---|
| 258 | cXMLHttpRequest.prototype.getResponseHeader = function(sName) { |
---|
| 259 | return this._object.getResponseHeader(sName); |
---|
| 260 | }; |
---|
| 261 | cXMLHttpRequest.prototype.setRequestHeader = function(sName, sValue) { |
---|
| 262 | // BUGFIX: IE - cache issue |
---|
| 263 | if (!this._headers) |
---|
| 264 | this._headers = {}; |
---|
| 265 | this._headers[sName] = sValue; |
---|
| 266 | |
---|
| 267 | return this._object.setRequestHeader(sName, sValue); |
---|
| 268 | }; |
---|
| 269 | |
---|
| 270 | // EventTarget interface implementation |
---|
| 271 | cXMLHttpRequest.prototype.addEventListener = function(sName, fHandler, bUseCapture) { |
---|
| 272 | for (var nIndex = 0, oListener; oListener = this._listeners[nIndex]; nIndex++) |
---|
| 273 | if (oListener[0] == sName && oListener[1] == fHandler && oListener[2] == bUseCapture) |
---|
| 274 | return; |
---|
| 275 | // Add listener |
---|
| 276 | this._listeners.push([sName, fHandler, bUseCapture]); |
---|
| 277 | }; |
---|
| 278 | |
---|
| 279 | cXMLHttpRequest.prototype.removeEventListener = function(sName, fHandler, bUseCapture) { |
---|
| 280 | for (var nIndex = 0, oListener; oListener = this._listeners[nIndex]; nIndex++) |
---|
| 281 | if (oListener[0] == sName && oListener[1] == fHandler && oListener[2] == bUseCapture) |
---|
| 282 | break; |
---|
| 283 | // Remove listener |
---|
| 284 | if (oListener) |
---|
| 285 | this._listeners.splice(nIndex, 1); |
---|
| 286 | }; |
---|
| 287 | |
---|
| 288 | cXMLHttpRequest.prototype.dispatchEvent = function(oEvent) { |
---|
| 289 | var oEventPseudo = { |
---|
| 290 | 'type': oEvent.type, |
---|
| 291 | 'target': this, |
---|
| 292 | 'currentTarget':this, |
---|
| 293 | 'eventPhase': 2, |
---|
| 294 | 'bubbles': oEvent.bubbles, |
---|
| 295 | 'cancelable': oEvent.cancelable, |
---|
| 296 | 'timeStamp': oEvent.timeStamp, |
---|
| 297 | 'stopPropagation': function() {}, // There is no flow |
---|
| 298 | 'preventDefault': function() {}, // There is no default action |
---|
| 299 | 'initEvent': function() {} // Original event object should be initialized |
---|
| 300 | }; |
---|
| 301 | |
---|
| 302 | // Execute onreadystatechange |
---|
| 303 | if (oEventPseudo.type == "readystatechange" && this.onreadystatechange) |
---|
| 304 | (this.onreadystatechange.handleEvent || this.onreadystatechange).apply(this, [oEventPseudo]); |
---|
| 305 | |
---|
| 306 | // Execute listeners |
---|
| 307 | for (var nIndex = 0, oListener; oListener = this._listeners[nIndex]; nIndex++) |
---|
| 308 | if (oListener[0] == oEventPseudo.type && !oListener[2]) |
---|
| 309 | (oListener[1].handleEvent || oListener[1]).apply(this, [oEventPseudo]); |
---|
| 310 | }; |
---|
| 311 | |
---|
| 312 | // |
---|
| 313 | cXMLHttpRequest.prototype.toString = function() { |
---|
| 314 | return '[' + "object" + ' ' + "XMLHttpRequest" + ']'; |
---|
| 315 | }; |
---|
| 316 | |
---|
| 317 | cXMLHttpRequest.toString = function() { |
---|
| 318 | return '[' + "XMLHttpRequest" + ']'; |
---|
| 319 | }; |
---|
| 320 | |
---|
| 321 | // Helper function |
---|
| 322 | function fReadyStateChange(oRequest) { |
---|
| 323 | // Sniffing code |
---|
| 324 | if (cXMLHttpRequest.onreadystatechange) |
---|
| 325 | cXMLHttpRequest.onreadystatechange.apply(oRequest); |
---|
| 326 | |
---|
| 327 | // Fake event |
---|
| 328 | oRequest.dispatchEvent({ |
---|
| 329 | 'type': "readystatechange", |
---|
| 330 | 'bubbles': false, |
---|
| 331 | 'cancelable': false, |
---|
| 332 | 'timeStamp': new Date + 0 |
---|
| 333 | }); |
---|
| 334 | }; |
---|
| 335 | |
---|
| 336 | function fGetDocument(oRequest) { |
---|
| 337 | var oDocument = oRequest.responseXML, |
---|
| 338 | sResponse = oRequest.responseText; |
---|
| 339 | // Try parsing responseText |
---|
| 340 | if (bIE && sResponse && oDocument && !oDocument.documentElement && oRequest.getResponseHeader("Content-Type").match(/[^\/]+\/[^\+]+\+xml/)) { |
---|
| 341 | oDocument = new window.ActiveXObject("Microsoft.XMLDOM"); |
---|
| 342 | oDocument.async = false; |
---|
| 343 | oDocument.validateOnParse = false; |
---|
| 344 | oDocument.loadXML(sResponse); |
---|
| 345 | } |
---|
| 346 | // Check if there is no error in document |
---|
| 347 | if (oDocument) |
---|
| 348 | if ((bIE && oDocument.parseError != 0) || !oDocument.documentElement || (oDocument.documentElement && oDocument.documentElement.tagName == "parsererror")) |
---|
| 349 | return null; |
---|
| 350 | return oDocument; |
---|
| 351 | }; |
---|
| 352 | |
---|
| 353 | function fSynchronizeValues(oRequest) { |
---|
| 354 | try { oRequest.responseText = oRequest._object.responseText; } catch (e) {} |
---|
| 355 | try { oRequest.responseXML = fGetDocument(oRequest._object); } catch (e) {} |
---|
| 356 | try { oRequest.status = oRequest._object.status; } catch (e) {} |
---|
| 357 | try { oRequest.statusText = oRequest._object.statusText; } catch (e) {} |
---|
| 358 | }; |
---|
| 359 | |
---|
| 360 | function fCleanTransport(oRequest) { |
---|
| 361 | // BUGFIX: IE - memory leak (on-page leak) |
---|
| 362 | oRequest._object.onreadystatechange = new window.Function; |
---|
| 363 | }; |
---|
| 364 | |
---|
| 365 | // Internet Explorer 5.0 (missing apply) |
---|
| 366 | if (!window.Function.prototype.apply) { |
---|
| 367 | window.Function.prototype.apply = function(oRequest, oArguments) { |
---|
| 368 | if (!oArguments) |
---|
| 369 | oArguments = []; |
---|
| 370 | oRequest.__func = this; |
---|
| 371 | oRequest.__func(oArguments[0], oArguments[1], oArguments[2], oArguments[3], oArguments[4]); |
---|
| 372 | delete oRequest.__func; |
---|
| 373 | }; |
---|
| 374 | }; |
---|
| 375 | |
---|
| 376 | // Register new object with window |
---|
| 377 | /** |
---|
| 378 | * Class: OpenLayers.Request.XMLHttpRequest |
---|
| 379 | * Standard-compliant (W3C) cross-browser implementation of the |
---|
| 380 | * XMLHttpRequest object. From |
---|
| 381 | * http://code.google.com/p/xmlhttprequest/. |
---|
| 382 | */ |
---|
| 383 | OpenLayers.Request.XMLHttpRequest = cXMLHttpRequest; |
---|
| 384 | })(); |
---|