welsonjs/lib/http.js

372 lines
11 KiB
JavaScript
Raw Normal View History

2020-06-28 14:22:57 +00:00
////////////////////////////////////////////////////////////////////////
// HTTP API
////////////////////////////////////////////////////////////////////////
2020-12-07 08:33:12 +00:00
var FILE = require("lib/file");
2020-06-28 14:22:57 +00:00
2020-11-10 09:13:41 +00:00
var HTTPObject = function() {
this.interface = null;
2020-11-25 06:10:02 +00:00
this.contentType = "application/x-www-form-urlencoded";
2020-11-10 09:13:41 +00:00
this.requestBody = "";
this.responseBody = null;
2020-11-26 08:13:13 +00:00
this.method = "GET";
2020-11-10 09:13:41 +00:00
this.headers = {};
2020-11-25 06:10:02 +00:00
this.parameters = {};
2020-11-26 08:13:13 +00:00
this.dataType = null;
2020-11-12 06:39:50 +00:00
this.userAgent = "WelsonJS/0.1.4-dev (https://github.com/gnh1201/welsonjs)";
2020-11-26 08:13:13 +00:00
this.isAsync = false;
2020-12-07 03:48:37 +00:00
2020-11-10 09:13:41 +00:00
this.create = function() {
this.interface = CreateObject([
2020-11-12 03:16:31 +00:00
"Microsoft.XMLHTTP",
2020-11-10 09:13:41 +00:00
"WinHttp.WinHttpRequest.5.1",
2020-11-12 06:52:45 +00:00
"Msxml3.XMLHTTP",
2020-11-10 09:13:41 +00:00
"Msxml2.XMLHTTP",
"Msxml2.XMLHTTP.7.0",
"Msxml2.XMLHTTP.6.0",
"Msxml2.XMLHTTP.5.O",
"Msxml2.XMLHTTP.4.O",
"Msxml2.XMLHTTP.3.O",
2020-11-12 06:52:45 +00:00
"Msxml2.XMLHTTP.2.6",
2020-11-12 06:53:57 +00:00
"Msxml2.ServerXMLHTTP",
2020-11-12 06:54:42 +00:00
"Msxml2.ServerXMLHTTP.6.0",
"Msxml2.ServerXMLHTTP.5.0",
2020-11-12 06:53:57 +00:00
"Msxml2.ServerXMLHTTP.4.0",
"Msxml2.ServerXMLHTTP.3.0"
2020-12-07 03:48:37 +00:00
]);
2020-11-10 09:13:41 +00:00
return this;
2020-11-26 08:13:13 +00:00
};
this.jqEnabled = function() {
return (typeof(window) !== "undefined" && typeof(window.jQuery) !== "undefined");
};
this.jqAjax = function(options) {
return (this.jqEnabled() ? window.jQuery.ajax(options) : null);
};
2020-06-28 14:22:57 +00:00
2020-11-10 09:13:41 +00:00
this.isJSONRequest = function() {
return (this.contentType === "application/json");
};
this.isJSONResponse = function() {
2020-12-07 08:33:12 +00:00
try {
if (this.dataType === "json") {
return true;
} else {
var headers = this.getHeaders();
for (var key in headers) {
var _k = key.toLowerCase();
var _v = headers[key].toLowerCase();
if (_k === "content-type" && _v === "application/json") {
this.dataType = "json";
return true;
}
2020-11-10 09:13:41 +00:00
}
}
2020-12-07 08:33:12 +00:00
} catch (e) {
console.error("HTTPObject.isJSONResponse() -> ", e.message);
2020-11-10 09:13:41 +00:00
}
2020-06-28 14:22:57 +00:00
2020-11-10 09:13:41 +00:00
return false;
};
2020-06-28 14:22:57 +00:00
2020-11-10 09:13:41 +00:00
this.setMethod = function(method) {
this.method = method;
};
2020-06-28 14:22:57 +00:00
2020-11-10 09:13:41 +00:00
this.setDataType = function(type) {
this.dataType = type;
return this;
};
2020-06-28 14:22:57 +00:00
2020-11-10 09:13:41 +00:00
this.setContentType = function(type) {
this.contentType = type;
return this;
};
2020-06-28 14:22:57 +00:00
2020-11-10 09:13:41 +00:00
this.setRequestBody = function(data) {
this.requestBody = data;
return this;
2020-06-28 14:22:57 +00:00
}
2020-11-10 09:13:41 +00:00
this.setResponseBody = function(data) {
this.responseBody = data;
return this;
2020-11-05 05:09:20 +00:00
}
2020-06-28 14:22:57 +00:00
2020-11-10 09:13:41 +00:00
this.setHeader = function(key, value) {
2020-11-12 03:16:31 +00:00
this.headers[key] = value;
2020-11-10 09:13:41 +00:00
return this;
};
this.setHeaders = function(headers) {
2020-12-07 08:33:12 +00:00
try {
var headers = (typeof(headers) !== "undefined") ? headers : {};
for (var key in headers) {
var value = headers[key];
this.setHeader(key, value);
if (key.toLowerCase() == "content-type") {
this.contentType = value.toLowerCase();
}
2020-11-10 09:13:41 +00:00
}
2020-12-07 08:33:12 +00:00
} catch (e) {
console.error("HTTPObject.setHeaders() -> ", e.message);
2020-11-10 09:13:41 +00:00
}
2020-06-28 14:22:57 +00:00
2020-11-10 09:13:41 +00:00
return this;
};
2020-06-28 14:22:57 +00:00
2020-11-10 09:13:41 +00:00
this.getHeader = function(key) {
2020-11-25 02:46:20 +00:00
try {
return this.interface.getResponseHeader(key);
} catch (e) {
2020-11-25 06:10:02 +00:00
console.error("HTTPObject.getHeader() -> ", e.message);
2020-11-25 02:46:20 +00:00
}
2020-11-10 09:13:41 +00:00
};
2020-06-28 14:22:57 +00:00
2020-11-10 09:13:41 +00:00
this.getHeaders = function() {
2020-11-25 02:46:20 +00:00
try {
var raw = this.interface.getAllResponseHeaders();
return raw.split(/[\r\n]+/).filter(function(s) {
return s.trim().length > 0;
}).map(function(s) {
return s.trim().split(": ");
}).reduce(function(acc, c) {
acc[c[0].trim()] = c[1].trim();
return acc;
}, {});
} catch (e) {
2020-11-25 06:10:02 +00:00
console.error("HTTPObject.getHeaders() -> ", e.message);
2020-11-25 02:46:20 +00:00
}
2020-11-10 09:13:41 +00:00
};
2020-11-05 05:32:27 +00:00
2020-11-25 06:10:02 +00:00
this.setParameter = function(key, value) {
this.parameters[key] = value;
return this;
};
this.getParameter = function(key) {
return this.parameters[key];
};
this.setParameters = function(params) {
2020-12-07 08:33:12 +00:00
try {
var params = (typeof(params) !== "undefined") ? params : {};
for (var key in params) {
var value = params[key];
this.setParameter(key, value);
}
} catch (e) {
console.error("HTTPObject.setParameters() -> ", e.message);
2020-11-25 06:10:02 +00:00
}
return this;
};
this.getParameters = function() {
return this.parameters;
};
2020-11-12 03:16:31 +00:00
this.setBearerAuth = function(token) {
this.setHeader("Authorization", "Bearer " + token);
return this;
};
this.setUseCache = function(flag) {
if (flag === false) {
this.setHeaders({
//"Pragma": "no-cache",
//"Cache-Control": "no-cache",
"If-Modified-Since": "Sat, 1 Jan 2000 00:00:00 GMT"
});
}
return this;
};
2020-11-26 08:13:13 +00:00
this.setUseAsync = function(flag) {
this.isAsync = flag;
return this;
}
2020-11-12 06:39:50 +00:00
this.setUserAgent = function(agent) {
this.userAgent = agent;
this.setHeader("User-Agent", this.userAgent);
};
2020-11-25 06:10:02 +00:00
this.serialize = function() {
if (this.isJSONRequest() && typeof(this.requestBody) === "object") {
return JSON.stringify(this.requestBody);
} else if (typeof(requestBody) === "object") {
return this.serializeURL(this.requestBody);
} else {
return this.requestBody;
}
};
this.serializeURL = function(obj) {
var str = [];
for (var p in obj)
if (obj.hasOwnProperty(p)) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
return str.join("&");
};
this.serializeParameters = function(url) {
if (Object.keys(this.parameters).length > 0) {
if (url.indexOf('?') > -1) {
return url + '&' + this.serializeURL(this.parameters);
} else {
return url + '?' + this.serializeURL(this.parameters);
}
} else {
return url;
}
};
2020-11-26 08:13:13 +00:00
this.open = function(method, url) {
2020-11-10 09:13:41 +00:00
this.setMethod(method.toUpperCase());
2020-06-28 14:22:57 +00:00
2020-11-25 02:46:20 +00:00
try {
switch (this.method) {
case "POST":
2020-11-26 08:13:13 +00:00
this.interface.open(method, this.serializeParameters(url), this.isAsync);
2020-11-25 02:46:20 +00:00
break;
2020-06-28 14:22:57 +00:00
2020-11-25 02:46:20 +00:00
case "GET":
2020-11-26 08:13:13 +00:00
this.interface.open(method, this.serializeParameters(url), this.isAsync);
2020-11-25 02:46:20 +00:00
break;
2020-06-28 14:22:57 +00:00
2020-11-25 02:46:20 +00:00
case "PATCH":
break;
2020-06-28 14:22:57 +00:00
2020-11-25 02:46:20 +00:00
default:
console.error("Not supported HTTP method: " + method);
}
} catch (e) {
2020-11-25 06:10:02 +00:00
console.error("HTTPObject.open() -> ", e.message);
2020-11-10 09:13:41 +00:00
}
2020-06-28 14:22:57 +00:00
2020-11-10 09:13:41 +00:00
return this;
};
2020-06-28 14:22:57 +00:00
2020-11-10 09:13:41 +00:00
this.send = function(callback) {
this.setHeader("Content-Type", this.contentType);
2020-06-28 14:22:57 +00:00
2020-11-25 02:46:20 +00:00
try {
for (var key in this.headers) {
this.interface.setRequestHeader(key, this.headers[key]);
}
2020-12-07 03:48:37 +00:00
2020-11-25 06:10:02 +00:00
switch (this.method) {
case "GET":
this.interface.send();
break;
default:
this.interface.send(this.serialize(this.requestBody));
2020-11-25 02:46:20 +00:00
}
2020-11-10 09:13:41 +00:00
2020-12-07 08:33:12 +00:00
// Waiting a response
2020-12-07 03:48:37 +00:00
while (this.interface.readyState < 4) {
2020-12-07 08:33:12 +00:00
sleep(100);
2020-12-07 03:48:37 +00:00
}
2020-11-25 02:46:20 +00:00
if (this.isJSONResponse()) {
2020-12-07 08:33:12 +00:00
if (typeof(WScript) !== "undefined") {
JSON = {};
FILE.includeFile("app/assets/js/json2.js");
}
2020-11-25 02:46:20 +00:00
this.setResponseBody(JSON.parse(this.interface.responseText));
} else {
this.setResponseBody(this.interface.responseText);
}
2020-11-10 09:13:41 +00:00
2020-11-25 02:46:20 +00:00
if (typeof(callback) === "function") {
2020-12-07 08:33:12 +00:00
try {
callback(this.responseBody);
} catch (e) {
console.log("callback of HTTPObject.send() -> ", e.message);
}
2020-11-25 02:46:20 +00:00
}
} catch (e) {
2020-11-25 06:10:02 +00:00
console.error("HTTPObject.send() -> ", e.message);
2020-11-10 09:13:41 +00:00
}
2020-06-28 14:22:57 +00:00
2020-11-10 09:13:41 +00:00
return this;
};
this.post = function(url, callback) {
2020-11-26 08:13:13 +00:00
try {
return this.open("POST", url).send(callback);
} catch (e) {
console.error("HTTPObject.post() -> ", e.message);
}
2020-11-10 09:13:41 +00:00
};
this.get = function(url, callback) {
2020-11-26 08:13:13 +00:00
try {
2020-11-27 06:58:43 +00:00
if (this.jqEnabled()) {
return this.jqAjax({
type: "GET",
headers: this.headers,
2020-11-27 07:11:59 +00:00
url: this.serializeParameters(url),
2020-11-27 06:58:43 +00:00
data: this.requestBody,
contentType: this.contentType,
success: callback,
async: this.isAsync,
error: function(request, status, error) {
2020-11-27 07:14:44 +00:00
console.error("code: ", request.status, ", message: " + request.responseText + ", error: " + error);
2020-11-27 06:58:43 +00:00
}
});
} else {
return this.open("GET", url).send(callback);
}
2020-11-26 08:13:13 +00:00
} catch (e) {
console.error("HTTPObject.get() -> ", e.message);
}
2020-11-10 09:13:41 +00:00
};
2020-11-12 04:36:16 +00:00
this.patch = function(url, callback) {
2020-11-25 02:46:20 +00:00
try {
2020-11-26 08:13:13 +00:00
if (this.jqEnabled()) {
return this.jqAjax({
type: "PATCH",
headers: this.headers,
2020-11-27 07:11:59 +00:00
url: this.serializeParameters(url),
2020-11-26 08:13:13 +00:00
data: this.requestBody,
contentType: this.contentType,
success: callback,
2020-11-27 06:58:43 +00:00
async: this.isAsync,
error: function(request, status, error) {
2020-11-27 07:14:44 +00:00
console.error("code: ", request.status, ", message: " + request.responseText + ", error: " + error);
2020-11-27 06:58:43 +00:00
}
2020-11-26 08:13:13 +00:00
});
2020-11-12 04:36:16 +00:00
} else {
2020-11-26 08:13:13 +00:00
throw Error("PATCH does not supported on GUI mode");
2020-11-12 04:36:16 +00:00
}
2020-11-25 02:46:20 +00:00
} catch (e) {
2020-11-25 06:10:02 +00:00
console.error("HTTPObject.patch() -> ", e.message);
2020-11-25 02:46:20 +00:00
}
2020-11-12 04:36:16 +00:00
};
2020-11-10 09:13:41 +00:00
this.create();
2020-11-12 06:39:50 +00:00
this.setUserAgent(this.userAgent);
2020-06-28 14:22:57 +00:00
};
2020-11-10 09:13:41 +00:00
exports.create = function() {
return (new HTTPObject());
2020-06-28 14:22:57 +00:00
};
2020-11-10 09:13:41 +00:00
2020-11-25 06:10:02 +00:00
exports.post = function(url, data, headers, params) {
return (new HTTPObject()).setHeaders(headers).setRequestBody(data).setParameters(params).post(url).responseBody;
};
2020-11-12 05:02:25 +00:00
exports.get = function(url, data, headers) {
2020-11-25 06:10:02 +00:00
return (new HTTPObject()).setHeaders(headers).setParameters(data).get(url).responseBody;
};
2020-11-12 05:02:25 +00:00
2020-11-10 09:13:41 +00:00
exports.VERSIONINFO = "HTTP Lib (http.js) version 0.2";
exports.global = global;
exports.require = global.require;