Update http.js

This commit is contained in:
Namhyeon Go 2022-01-13 16:21:20 +09:00 committed by GitHub
parent 66ad0e69af
commit 8f6859ceae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,8 +2,17 @@
// HTTP API
////////////////////////////////////////////////////////////////////////
var FILE = require("lib/file");
var SHELL = require("lib/shell");
var HTTPObject = function() {
// UUID v4
function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
var HTTPObject = function(engine) {
this.interface = null;
this.contentType = "application/x-www-form-urlencoded";
this.requestBody = "";
@ -14,25 +23,33 @@ var HTTPObject = function() {
this.dataType = null;
this.userAgent = "WelsonJS/0.1.4-dev (https://github.com/gnh1201/welsonjs)";
this.isAsync = false;
this.engine = (typeof(engine) !== "undefined" ? engine : "MSXML");
this.cookie = null;
this.states = [];
this.create = function() {
this.interface = CreateObject([
"Microsoft.XMLHTTP",
"WinHttp.WinHttpRequest.5.1",
"Msxml3.XMLHTTP",
"Msxml2.XMLHTTP",
"Msxml2.XMLHTTP.7.0",
"Msxml2.XMLHTTP.6.0",
"Msxml2.XMLHTTP.5.O",
"Msxml2.XMLHTTP.4.O",
"Msxml2.XMLHTTP.3.O",
"Msxml2.XMLHTTP.2.6",
"Msxml2.ServerXMLHTTP",
"Msxml2.ServerXMLHTTP.6.0",
"Msxml2.ServerXMLHTTP.5.0",
"Msxml2.ServerXMLHTTP.4.0",
"Msxml2.ServerXMLHTTP.3.0"
]);
if (this.engine == "MSXML") {
this.interface = CreateObject([
"Microsoft.XMLHTTP",
"WinHttp.WinHttpRequest.5.1",
"Msxml3.XMLHTTP",
"Msxml2.XMLHTTP",
"Msxml2.XMLHTTP.7.0",
"Msxml2.XMLHTTP.6.0",
"Msxml2.XMLHTTP.5.O",
"Msxml2.XMLHTTP.4.O",
"Msxml2.XMLHTTP.3.O",
"Msxml2.XMLHTTP.2.6",
"Msxml2.ServerXMLHTTP",
"Msxml2.ServerXMLHTTP.6.0",
"Msxml2.ServerXMLHTTP.5.0",
"Msxml2.ServerXMLHTTP.4.0",
"Msxml2.ServerXMLHTTP.3.0"
]);
} else if (this.engine == "CURL") {
this.interface = SHELL.create();
}
return this;
};
@ -52,7 +69,7 @@ var HTTPObject = function() {
try {
if (this.dataType === "json") {
return true;
} else {
} else if (this.engine == "MSXML") {
var headers = this.getHeaders();
for (var key in headers) {
var _k = key.toLowerCase();
@ -70,8 +87,18 @@ var HTTPObject = function() {
return false;
};
this.setEngine = function(engine) {
if (typeof(engine) == "string") {
this.engine = engine.toUpperCase();
} else {
this.engine = engine;
}
return this;
};
this.setMethod = function(method) {
this.method = method;
return this;
};
this.setDataType = function(type) {
@ -98,15 +125,26 @@ var HTTPObject = function() {
this.headers[key] = value;
return this;
};
this.setCookie = function(cookie) {
this.cookie = cookie;
};
this.setHeaders = function(headers) {
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();
switch (key.toUpperCase()) {
case "CONTENT-TYPE":
this.setContentType(value.toLowerCase());
break;
case "COOKIE":
this.setCookie(value);
break;
default:
this.setHeader(key, value);
}
}
} catch (e) {
@ -191,6 +229,7 @@ var HTTPObject = function() {
this.setUserAgent = function(agent) {
this.userAgent = agent;
this.setHeader("User-Agent", this.userAgent);
return this;
};
this.serialize = function() {
@ -218,6 +257,12 @@ var HTTPObject = function() {
this.serializeParameters = function(url) {
console.log(Object.keys(this.parameters).join(","));
// UUID v4
while (url.indexOf("{uuidv4}") > -1) {
url = url.replace("{uuidv4}", uuidv4());
}
// Bind parameters
if (Object.keys(this.parameters).length > 0) {
// Type 2
var parameters = {};
@ -245,23 +290,27 @@ var HTTPObject = function() {
};
this.open = function(method, url) {
this.setMethod(method.toUpperCase());
var url = this.serializeParameters(url);
this.setMethod(method.toUpperCase()); // set method
this.pushState(null, null, url); // push state
try {
switch (this.method) {
case "POST":
this.interface.open(method, this.serializeParameters(url), this.isAsync);
break;
if (this.engine == "MSXML") {
switch (this.method) {
case "POST":
this.interface.open(method, url, this.isAsync);
break;
case "GET":
this.interface.open(method, this.serializeParameters(url), this.isAsync);
break;
case "GET":
this.interface.open(method, url, this.isAsync);
break;
case "PATCH":
break;
default:
console.error("Not supported HTTP method: " + method);
default:
console.error("Not supported HTTP method: " + method);
}
} else {
console.log("Selected engine: " + this.engine);
}
} catch (e) {
console.error("HTTPObject.open() -> ", e.message);
@ -271,37 +320,75 @@ var HTTPObject = function() {
};
this.send = function(callback) {
this.setHeader("Content-Type", this.contentType);
var responseText = null;
if (this.contentType != null) {
this.setHeader("Content-Type", this.contentType);
}
try {
for (var key in this.headers) {
this.interface.setRequestHeader(key, this.headers[key]);
}
switch (this.method) {
case "GET":
this.interface.send();
break;
if (this.engine == "MSXML") {
for (var key in this.headers) {
this.interface.setRequestHeader(key, this.headers[key]);
}
switch (this.method) {
case "GET":
this.interface.send();
break;
default:
this.interface.send(this.serialize(this.requestBody));
default:
this.interface.send(this.serialize(this.requestBody));
}
// Waiting a response
while (this.interface.readyState < 4) sleep(100);
// Get response text
responseText = this.interface.responseText;
} else if (this.engine == "CURL") {
if (this.states.length > 0) {
// Make CURL context
var state = this.states[this.states.length - 1];
var cmd = ["bin\\curl", "-X", this.method];
if (this.headers.length > 0) {
for (var key in this.headers) {
var value = this.headers[key];
if (value != null) {
cmd.push("-H");
cmd.push(key + ": " + value);
}
}
}
if (this.cookie != null) {
cmd.push("-b");
cmd.push(this.cookie);
}
if (this.userAgent != null) {
cmd.push("-A");
cmd.push(this.userAgent);
}
cmd.push(state.url);
// Get response text
responseText = this.interface.exec(cmd);
}
}
// Waiting a response
while (this.interface.readyState < 4) {
sleep(100);
}
console.log("ResponseText: " + this.interface.responseText);
console.log("ResponseText: " + responseText);
if (this.isJSONResponse()) {
if (typeof(WScript) !== "undefined") {
JSON = {};
FILE.includeFile("app/assets/js/json2.js");
}
this.setResponseBody(JSON.parse(this.interface.responseText));
this.setResponseBody(JSON.parse(responseText));
} else {
this.setResponseBody(this.interface.responseText);
this.setResponseBody(responseText);
}
if (typeof(callback) === "function") {
@ -371,13 +458,25 @@ var HTTPObject = function() {
console.error("HTTPObject.patch() -> ", e.message);
}
};
this.pushState = function(state, title, url) {
this.states.push({
"state": state,
"title": title,
"url": url
});
};
this.popState = function() {
return this.states.pop();
};
this.create();
this.setUserAgent(this.userAgent);
};
exports.create = function() {
return (new HTTPObject());
exports.create = function(engine) {
return (new HTTPObject(engine));
};
exports.post = function(url, data, headers, params) {
@ -388,6 +487,6 @@ exports.get = function(url, data, headers) {
return (new HTTPObject()).setHeaders(headers).setParameters(data).setUseCache(false).get(url).responseBody;
};
exports.VERSIONINFO = "HTTP Lib (http.js) version 0.2";
exports.VERSIONINFO = "HTTP Lib (http.js) version 0.3";
exports.global = global;
exports.require = global.require;