xhrJsCode top-level property
Implementation
final String xhrJsCode = """
function XMLHttpRequest() {
this._send_native = XMLHttpRequestExtension_send_native;
this._httpMethod = null;
this._url = null;
this._requestHeaders = [];
this._responseHeaders = [];
this.response = null;
this.responseText = null;
this.responseXML = null;
this.responseType = "";
this.onreadystatechange = null;
this.onloadstart = null;
this.onprogress = null;
this.onabort = null;
this.onerror = null;
this.onload = null;
this.onloadend = null;
this.ontimeout = null;
this.readyState = 0;
this.status = 0;
this.statusText = "";
this.withCredentials = null;
};
// readystate enum
XMLHttpRequest.UNSENT = 0;
XMLHttpRequest.OPENED = 1;
XMLHttpRequest.HEADERS = 2;
XMLHttpRequest.LOADING = 3;
XMLHttpRequest.DONE = 4;
XMLHttpRequest.prototype.constructor = XMLHttpRequest;
XMLHttpRequest.prototype.open = function(httpMethod, url) {
this._httpMethod = httpMethod == null ? httpMethod : String(httpMethod);
this._url = url == null ? url : String(url);
// Headers of a previous response must not show on a reused request.
this._responseHeaders = [];
this.readyState = XMLHttpRequest.OPENED;
if (typeof this.onreadystatechange === "function") {
//console.log("Calling onreadystatechange(OPENED)...");
this.onreadystatechange();
}
};
XMLHttpRequest.prototype.send = function(data) {
this.readyState = XMLHttpRequest.LOADING;
if (typeof this.onreadystatechange === "function") {
//console.log("Calling onreadystatechange(LOADING)...");
this.onreadystatechange();
}
if (typeof this.onloadstart === "function") {
//console.log("Calling onloadstart()...");
this.onloadstart();
}
var that = this;
if (typeof ArrayBuffer !== "undefined" && data && (data instanceof ArrayBuffer || ArrayBuffer.isView(data))) {
// Bodies are passed to Dart as text; sending the bytes as their string
// form would silently upload the wrong data.
setTimeout(function() {
that._send_native_callback({ statusCode: 0, statusText: "" }, null, "XHR binary request bodies are not supported");
}, 0);
return;
}
// As in browsers, other bodies that are not strings are sent as String(body).
var body = (data === undefined || data === null || data === "") ? null : String(data);
this._send_native(this._httpMethod, this._url, this._requestHeaders, body, function(responseInfo, responseText, error) {
that._send_native_callback(responseInfo, responseText, error);
}, this);
};
XMLHttpRequest.prototype.abort = function() {
this.readyState = XMLHttpRequest.UNSENT;
// Note: this.onreadystatechange() is not supposed to be called according to the XHR specs
}
// responseInfo: {statusCode, statusText, responseHeaders}
XMLHttpRequest.prototype._send_native_callback = function(responseInfo, responseText, error) {
${_debugSendNativeCallback()}
if (this.readyState === XMLHttpRequest.UNSENT) {
console.log("XHR native callback ignored because the request has been aborted");
if (typeof this.onabort === "function") {
//console.log("Calling onabort()...");
this.onabort();
}
return;
}
if (this.readyState != XMLHttpRequest.LOADING) {
// Request was not expected
console.log("XHR native callback ignored because the current state is not LOADING");
return;
}
// Response info
// TODO: responseXML?
this.responseURL = this._url;
this.status = responseInfo.statusCode;
this.statusText = responseInfo.statusText;
// Stored as [lower-cased name, value] pairs; anything else is ignored.
this._responseHeaders = [];
var rawHeaders = responseInfo.responseHeaders;
if (Array.isArray(rawHeaders)) {
for (var h = 0; h < rawHeaders.length; h++) {
var pair = rawHeaders[h];
if (!Array.isArray(pair) || pair.length < 2) continue;
this._responseHeaders.push([String(pair[0]).toLowerCase(), String(pair[1])]);
}
}
this.readyState = XMLHttpRequest.DONE;
// Response
this.response = null;
this.responseText = null;
this.responseXML = null;
if (error) {
this.responseText = error;
} else {
this.responseText = responseText;
//console.log('RESPONSE TEXT: ' + responseText);
switch (this.responseType) {
case "":
case "text":
this.response = this.responseText;
break;
case "arraybuffer":
error = "XHR arraybuffer response is not supported!";
break;
case "document":
this.response = this.responseText;
this.responseXML = this.responseText;
break;
case "json":
try {
this.response = JSON.parse(responseText);
}
catch (e) {
error = "Could not parse JSON response: " + responseText;
}
break;
default:
error = "Unsupported responseType: " + responseInfo.responseType;
}
}
this.readyState = XMLHttpRequest.DONE;
if (typeof this.onreadystatechange === "function") {
//console.log("Calling onreadystatechange(DONE)...");
this.onreadystatechange();
}
if (error === "timeout") {
// Timeout
console.warn("Got XHR timeout");
if (typeof this.ontimeout === "function") {
//console.log("Calling ontimeout()...");
this.ontimeout();
}
} else if (error) {
// Error
console.warn("Got XHR error:", error);
if (typeof this.onerror === "function") {
//console.log("Calling onerror()...");
this.onerror();
}
} else {
// Success
//console.log("XHR success: response =", this.response);
if (typeof this.onload === "function") {
//console.log("Calling onload()...");
this.onload();
}
}
if (typeof this.onloadend === "function") {
//console.log("Calling onloadend()...");
this.onloadend();
}
};
XMLHttpRequest.prototype.setRequestHeader = function(header, value) {
this._requestHeaders.push([String(header), String(value)]);
};
// As in browsers: lower-cased names sorted by their upper-cased form, one
// "name: value" line per name (repeated names combined with ", "), each line
// ending in CRLF.
XMLHttpRequest.prototype.getAllResponseHeaders = function() {
var items = [];
var i;
for (i = 0; i < this._responseHeaders.length; i++) {
var keyValue = this._responseHeaders[i];
items.push({ name: keyValue[0], key: keyValue[0].toUpperCase(), value: keyValue[1], index: i });
}
// Ties go to the name first, so equal names always end up adjacent.
items.sort(function(a, b) {
if (a.key !== b.key) return a.key < b.key ? -1 : 1;
if (a.name !== b.name) return a.name < b.name ? -1 : 1;
return a.index - b.index;
});
var ret = "";
for (i = 0; i < items.length; i++) {
var value = items[i].value;
while (i + 1 < items.length && items[i + 1].name === items[i].name) {
i++;
value += ", " + items[i].value;
}
ret += items[i].name + ": " + value + "\\r\\n";
}
return ret;
};
// The name is matched case-insensitively. Returns null when the response
// has no such header (an empty header value is returned as "").
XMLHttpRequest.prototype.getResponseHeader = function(name) {
var wanted = String(name).toLowerCase();
var ret = null;
for (var i = 0; i < this._responseHeaders.length; i++) {
var keyValue = this._responseHeaders[i];
if (keyValue[0] !== wanted) continue;
ret = ret === null ? keyValue[1] : ret + ", " + keyValue[1];
}
return ret;
};
// XMLHttpRequest.prototype.overrideMimeType = function() {
// // TODO
// };
this.XMLHttpRequest = XMLHttpRequest;""";