2013-03-17 15:28:49 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/utils/dom/http.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: utils
|
|
|
|
|
|
|
|
Browser HTTP support
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*
|
|
|
|
A quick and dirty HTTP function; to be refactored later. Options are:
|
|
|
|
url: URL to retrieve
|
2018-08-23 12:13:49 +00:00
|
|
|
headers: hashmap of headers to send
|
2013-03-17 15:28:49 +00:00
|
|
|
type: GET, PUT, POST etc
|
2019-04-15 20:07:23 +00:00
|
|
|
callback: function invoked with (err,data,xhr)
|
2017-03-17 13:41:17 +00:00
|
|
|
returnProp: string name of the property to return as first argument of callback
|
2013-03-17 15:28:49 +00:00
|
|
|
*/
|
|
|
|
exports.httpRequest = function(options) {
|
|
|
|
var type = options.type || "GET",
|
2020-03-30 14:24:05 +00:00
|
|
|
url = options.url,
|
2013-03-17 15:28:49 +00:00
|
|
|
headers = options.headers || {accept: "application/json"},
|
2020-08-17 17:44:36 +00:00
|
|
|
hasHeader = function(targetHeader) {
|
|
|
|
targetHeader = targetHeader.toLowerCase();
|
|
|
|
var result = false;
|
|
|
|
$tw.utils.each(headers,function(header,headerTitle,object) {
|
|
|
|
if(headerTitle.toLowerCase() === targetHeader) {
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
},
|
2021-08-17 08:56:52 +00:00
|
|
|
getHeader = function(targetHeader) {
|
|
|
|
return headers[targetHeader] || headers[targetHeader.toLowerCase()];
|
|
|
|
},
|
|
|
|
isSimpleRequest = function(type,headers) {
|
|
|
|
if(["GET","HEAD","POST"].indexOf(type) === -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for(var header in headers) {
|
|
|
|
if(["accept","accept-language","content-language","content-type"].indexOf(header.toLowerCase()) === -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(hasHeader("Content-Type") && ["application/x-www-form-urlencoded","multipart/form-data","text/plain"].indexOf(getHeader["Content-Type"]) === -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
2017-03-17 13:41:17 +00:00
|
|
|
returnProp = options.returnProp || "responseText",
|
2013-03-17 15:28:49 +00:00
|
|
|
request = new XMLHttpRequest(),
|
|
|
|
data = "",
|
|
|
|
f,results;
|
|
|
|
// Massage the data hashmap into a string
|
|
|
|
if(options.data) {
|
|
|
|
if(typeof options.data === "string") { // Already a string
|
|
|
|
data = options.data;
|
|
|
|
} else { // A hashmap of strings
|
|
|
|
results = [];
|
|
|
|
$tw.utils.each(options.data,function(dataItem,dataItemTitle) {
|
|
|
|
results.push(dataItemTitle + "=" + encodeURIComponent(dataItem));
|
|
|
|
});
|
2020-03-30 14:24:05 +00:00
|
|
|
if(type === "GET" || type === "HEAD") {
|
|
|
|
url += "?" + results.join("&");
|
|
|
|
} else {
|
|
|
|
data = results.join("&");
|
|
|
|
}
|
2013-03-17 15:28:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Set up the state change handler
|
|
|
|
request.onreadystatechange = function() {
|
|
|
|
if(this.readyState === 4) {
|
2014-09-24 14:19:23 +00:00
|
|
|
if(this.status === 200 || this.status === 201 || this.status === 204) {
|
2013-03-17 15:28:49 +00:00
|
|
|
// Success!
|
2017-03-17 13:41:17 +00:00
|
|
|
options.callback(null,this[returnProp],this);
|
2013-03-17 15:28:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Something went wrong
|
2019-04-15 20:07:23 +00:00
|
|
|
options.callback($tw.language.getString("Error/XMLHttpRequest") + ": " + this.status,null,this);
|
2013-03-17 15:28:49 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
// Make the request
|
2020-03-30 14:24:05 +00:00
|
|
|
request.open(type,url,true);
|
2013-03-17 15:28:49 +00:00
|
|
|
if(headers) {
|
|
|
|
$tw.utils.each(headers,function(header,headerTitle,object) {
|
|
|
|
request.setRequestHeader(headerTitle,header);
|
|
|
|
});
|
|
|
|
}
|
2020-08-17 17:44:36 +00:00
|
|
|
if(data && !hasHeader("Content-Type")) {
|
|
|
|
request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
|
2013-03-17 15:28:49 +00:00
|
|
|
}
|
2021-08-17 08:56:52 +00:00
|
|
|
if(!hasHeader("X-Requested-With") && !isSimpleRequest(type,headers)) {
|
2018-07-18 15:54:43 +00:00
|
|
|
request.setRequestHeader("X-Requested-With","TiddlyWiki");
|
|
|
|
}
|
2015-08-29 15:33:04 +00:00
|
|
|
try {
|
|
|
|
request.send(data);
|
|
|
|
} catch(e) {
|
2019-04-15 20:07:23 +00:00
|
|
|
options.callback(e,null,this);
|
2015-08-29 15:33:04 +00:00
|
|
|
}
|
2013-03-17 15:28:49 +00:00
|
|
|
return request;
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|